This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdial-test.js
90 lines (73 loc) · 2.35 KB
/
dial-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const goodbye = require('it-goodbye')
const { collect } = require('streaming-iterables')
const pipe = require('it-pipe')
const AbortController = require('abort-controller')
const AbortError = require('./errors').AbortError
module.exports = (common) => {
describe('dial', () => {
let addrs
let transport
let connector
let listener
before(async () => {
({ addrs, transport, connector } = await common.setup())
})
after(() => common.teardown && common.teardown())
beforeEach(() => {
listener = transport.createListener((conn) => pipe(conn, conn))
return listener.listen(addrs[0])
})
afterEach(() => listener.close())
it('simple', async () => {
const conn = await transport.dial(addrs[0])
const s = goodbye({ source: ['hey'], sink: collect })
const result = await pipe(s, conn, s)
expect(result.length).to.equal(1)
expect(result[0].toString()).to.equal('hey')
})
it('to non existent listener', async () => {
try {
await transport.dial(addrs[1])
} catch (_) {
// Success: expected an error to be throw
return
}
expect.fail('Did not throw error attempting to connect to non-existent listener')
})
it('cancel before dialing', async () => {
const controller = new AbortController()
controller.abort()
const socket = transport.dial(addrs[0], { signal: controller.signal })
try {
await socket
} catch (err) {
expect(err.code).to.eql(AbortError.code)
return
}
expect.fail('Did not throw error with code ' + AbortError.code)
})
it('cancel while dialing', async () => {
// Add a delay to connect() so that we can cancel while the dial is in
// progress
connector.delay(100)
const controller = new AbortController()
const socket = transport.dial(addrs[0], { signal: controller.signal })
setTimeout(() => controller.abort(), 50)
try {
await socket
} catch (err) {
expect(err.code).to.eql(AbortError.code)
return
} finally {
connector.restore()
}
expect.fail('Did not throw error with code ' + AbortError.code)
})
})
}