forked from webtorrent/bittorrent-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-ws-socket-pool.js
56 lines (47 loc) · 1.6 KB
/
client-ws-socket-pool.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
const Client = require('../')
const common = require('./common')
const fixtures = require('webtorrent-fixtures')
const test = require('tape')
const peerId = Buffer.from('01234567890123456789')
const port = 6681
test('ensure client.destroy() callback is called with re-used websockets in socketPool', function (t) {
t.plan(4)
common.createServer(t, 'ws', function (server, announceUrl) {
const client1 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId,
port,
wrtc: {}
})
common.mockWebsocketTracker(client1)
client1.on('error', function (err) { t.error(err) })
client1.on('warning', function (err) { t.error(err) })
client1.start()
client1.once('update', function () {
t.pass('got client1 update')
// second ws client using same announce url will re-use the same websocket
const client2 = new Client({
infoHash: fixtures.alice.parsedTorrent.infoHash, // different info hash
announce: announceUrl,
peerId,
port,
wrtc: {}
})
common.mockWebsocketTracker(client2)
client2.on('error', function (err) { t.error(err) })
client2.on('warning', function (err) { t.error(err) })
client2.start()
client2.once('update', function () {
t.pass('got client2 update')
client1.destroy(function (err) {
t.error(err, 'got client1 destroy callback')
client2.destroy(function (err) {
t.error(err, 'got client2 destroy callback')
server.close()
})
})
})
})
})
})