feat: abortable dials

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
This commit is contained in:
Alan Shaw
2019-04-18 09:52:49 +01:00
parent 24d0a6132a
commit 16a6b55960
3 changed files with 92 additions and 4 deletions

View File

@ -10,6 +10,7 @@ const multiaddr = require('multiaddr')
const goodbye = require('it-goodbye')
const { collect, consume } = require('streaming-iterables')
const pipe = require('it-pipe')
const AbortController = require('abort-controller')
const WS = require('../src')
@ -207,6 +208,49 @@ describe('dial', () => {
expect(result).to.be.eql([Buffer.from('hey')])
})
it('should be abortable after connect', async () => {
const controller = new AbortController()
const conn = await ws.dial(ma, { signal: controller.signal })
const s = goodbye({
source: {
[Symbol.asyncIterator] () {
return this
},
next () {
return new Promise(resolve => {
setTimeout(() => resolve(Math.random()), 1000)
})
}
},
sink: consume
})
setTimeout(() => controller.abort(), 500)
try {
await pipe(s, conn, s)
} catch (err) {
expect(err.type).to.equal('aborted')
return
}
throw new Error('connection was not aborted')
})
it('should be abortable before connect', async () => {
const controller = new AbortController()
controller.abort() // Abort before connect
try {
await ws.dial(ma, { signal: controller.signal })
} catch (err) {
expect(err.type).to.equal('aborted')
return
}
throw new Error('connection was not aborted')
})
})
describe('ip6', () => {