mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-06-14 15:21:42 +00:00
feat: abortable dials
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
This commit is contained in:
44
test/node.js
44
test/node.js
@ -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', () => {
|
||||
|
Reference in New Issue
Block a user