2016-09-02 13:31:28 +02:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
2017-03-21 14:23:00 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
|
|
|
const expect = chai.expect
|
|
|
|
chai.use(dirtyChai)
|
2019-04-18 01:35:06 +08:00
|
|
|
|
|
|
|
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
|
2016-09-02 13:31:28 +02:00
|
|
|
|
|
|
|
module.exports = (common) => {
|
|
|
|
describe('dial', () => {
|
|
|
|
let addrs
|
|
|
|
let transport
|
2019-04-18 01:35:06 +08:00
|
|
|
let connector
|
2016-09-02 13:31:28 +02:00
|
|
|
let listener
|
|
|
|
|
2019-04-18 01:35:06 +08:00
|
|
|
before(async () => {
|
|
|
|
({ addrs, transport, connector } = await common.setup())
|
2016-09-02 13:31:28 +02:00
|
|
|
})
|
|
|
|
|
2019-04-18 01:35:06 +08:00
|
|
|
after(() => common.teardown && common.teardown())
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
listener = transport.createListener((conn) => pipe(conn, conn))
|
|
|
|
return listener.listen(addrs[0])
|
2016-09-02 13:31:28 +02:00
|
|
|
})
|
|
|
|
|
2019-04-18 01:35:06 +08:00
|
|
|
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')
|
2016-09-02 13:31:28 +02:00
|
|
|
})
|
|
|
|
|
2019-04-18 01:35:06 +08:00
|
|
|
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')
|
2016-09-02 13:31:28 +02:00
|
|
|
})
|
|
|
|
|
2019-04-18 01:35:06 +08:00
|
|
|
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)
|
2019-04-18 18:44:17 +08:00
|
|
|
expect(err.type).to.eql(AbortError.type)
|
2019-04-18 01:35:06 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
expect.fail('Did not throw error with code ' + AbortError.code)
|
2016-09-02 13:31:28 +02:00
|
|
|
})
|
|
|
|
|
2019-04-18 01:35:06 +08:00
|
|
|
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)
|
2019-04-18 18:44:17 +08:00
|
|
|
expect(err.type).to.eql(AbortError.type)
|
2019-04-18 01:35:06 +08:00
|
|
|
return
|
|
|
|
} finally {
|
|
|
|
connector.restore()
|
|
|
|
}
|
|
|
|
expect.fail('Did not throw error with code ' + AbortError.code)
|
2016-09-02 13:31:28 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|