Merge pull request #176 from feross/once

Add test: no "update" events after destroy()
This commit is contained in:
Feross Aboukhadijeh 2016-09-21 22:36:49 +02:00 committed by GitHub
commit f21911a509
2 changed files with 54 additions and 3 deletions

View File

@ -169,12 +169,12 @@ WebSocketTracker.prototype._openSocket = function () {
} else {
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
self.socket.consumers = 1
self.socket.on('connect', self._onSocketConnectBound)
self.socket.once('connect', self._onSocketConnectBound)
}
self.socket.on('data', self._onSocketDataBound)
self.socket.on('close', self._onSocketCloseBound)
self.socket.on('error', self._onSocketErrorBound)
self.socket.once('close', self._onSocketCloseBound)
self.socket.once('error', self._onSocketErrorBound)
}
WebSocketTracker.prototype._onSocketConnect = function () {

51
test/destroy.js Normal file
View File

@ -0,0 +1,51 @@
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
var test = require('tape')
var peerId = Buffer.from('01234567890123456789')
var port = 6881
function testNoEventsAfterDestroy (t, serverType) {
t.plan(1)
common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId,
port: port,
wrtc: {}
})
if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })
client.once('update', function () {
t.fail('no "update" event should fire, since client is destroyed')
})
// announce, then immediately destroy
client.update()
client.destroy()
setTimeout(function () {
t.pass('wait to see if any events are fired')
server.close()
}, 1000)
})
}
test('http: no "update" events after destroy()', function (t) {
testNoEventsAfterDestroy(t, 'http')
})
test('udp: no "update" events after destroy()', function (t) {
testNoEventsAfterDestroy(t, 'udp')
})
test('ws: no "update" events after destroy()', function (t) {
testNoEventsAfterDestroy(t, 'ws')
})