bittorrent-tracker/test/client-ws-socket-pool.js

57 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

import Client from '../index.js'
import common from './common.js'
import fixtures from 'webtorrent-fixtures'
import test from 'tape'
2016-04-01 04:48:21 +00:00
2020-10-29 04:57:47 +00:00
const peerId = Buffer.from('01234567890123456789')
const port = 6681
2016-04-01 04:48:21 +00:00
2021-06-15 01:54:41 +00:00
test('ensure client.destroy() callback is called with re-used websockets in socketPool', t => {
2016-04-01 04:48:21 +00:00
t.plan(4)
2021-06-15 01:54:41 +00:00
common.createServer(t, 'ws', (server, announceUrl) => {
2020-10-29 04:57:47 +00:00
const client1 = new Client({
2016-04-01 04:48:21 +00:00
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
2020-04-25 09:34:40 +00:00
peerId,
port,
2016-04-01 04:48:21 +00:00
wrtc: {}
})
common.mockWebsocketTracker(client1)
2021-06-15 01:54:41 +00:00
client1.on('error', err => { t.error(err) })
client1.on('warning', err => { t.error(err) })
2016-04-01 04:48:21 +00:00
client1.start()
2021-06-15 01:54:41 +00:00
client1.once('update', () => {
2016-04-01 04:48:21 +00:00
t.pass('got client1 update')
// second ws client using same announce url will re-use the same websocket
2020-10-29 04:57:47 +00:00
const client2 = new Client({
2016-04-01 04:48:21 +00:00
infoHash: fixtures.alice.parsedTorrent.infoHash, // different info hash
announce: announceUrl,
2020-04-25 09:34:40 +00:00
peerId,
port,
2016-04-01 04:48:21 +00:00
wrtc: {}
})
common.mockWebsocketTracker(client2)
2021-06-15 01:54:41 +00:00
client2.on('error', err => { t.error(err) })
client2.on('warning', err => { t.error(err) })
2016-04-01 04:48:21 +00:00
client2.start()
2021-06-15 01:54:41 +00:00
client2.once('update', () => {
2016-04-01 04:48:21 +00:00
t.pass('got client2 update')
2021-06-15 01:54:41 +00:00
client1.destroy(err => {
2016-04-01 04:48:21 +00:00
t.error(err, 'got client1 destroy callback')
2021-06-15 01:54:41 +00:00
client2.destroy(err => {
2016-04-01 04:48:21 +00:00
t.error(err, 'got client2 destroy callback')
server.close()
})
})
})
})
})
})