Skip to content

Commit f285c9d

Browse files
committed
client.stop() no longer calls client.destroy()
You must call client.destroy() manually to clean up resources
1 parent ee91dff commit f285c9d

File tree

6 files changed

+56
-40
lines changed

6 files changed

+56
-40
lines changed

client.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ Client.prototype.stop = function (opts) {
153153
opts = self._defaultAnnounceOpts(opts)
154154
opts.event = 'stopped'
155155
self._announce(opts)
156-
157-
self.destroy()
158156
}
159157

160158
/**

test/client-large-torrent.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ test('large torrent: client.start()', function (t) {
4747
client.stop()
4848

4949
client.once('update', function () {
50-
server.close(function () {
51-
t.pass('server close')
52-
})
50+
t.pass('got response to stop')
51+
server.close()
52+
client.destroy()
5353
})
5454
})
5555
})

test/client-magnet.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var parsedTorrent = magnet(uri)
88
var peerId = new Buffer('01234567890123456789')
99

1010
test('magnet + udp: client.start/update/stop()', function (t) {
11-
t.plan(11)
11+
t.plan(10)
1212

1313
var server = new Server({ http: false })
1414

@@ -60,9 +60,8 @@ test('magnet + udp: client.start/update/stop()', function (t) {
6060
t.equal(typeof data.complete, 'number')
6161
t.equal(typeof data.incomplete, 'number')
6262

63-
server.close(function () {
64-
t.pass('server closed')
65-
})
63+
server.close()
64+
client.destroy()
6665
})
6766
})
6867
})

test/client.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ function testClientStart (t, serverType) {
3636
client.stop()
3737

3838
client.once('update', function () {
39-
server.close(function () {
40-
t.pass('server close')
41-
})
39+
t.pass('got response to stop')
40+
server.close()
41+
client.destroy()
4242
})
4343
})
4444

@@ -55,7 +55,7 @@ test('udp: client.start()', function (t) {
5555
})
5656

5757
function testClientStop (t, serverType) {
58-
t.plan(4)
58+
t.plan(3)
5959
common.createServer(t, serverType, function (server, announceUrl) {
6060
parsedTorrent.announce = [ announceUrl ]
6161
var client = new Client(peerId1, port, parsedTorrent)
@@ -79,9 +79,8 @@ function testClientStop (t, serverType) {
7979
t.equal(typeof data.complete, 'number')
8080
t.equal(typeof data.incomplete, 'number')
8181

82-
server.close(function () {
83-
t.pass('server close')
84-
})
82+
server.close()
83+
client.destroy()
8584
})
8685
}, 1000)
8786
})
@@ -121,9 +120,9 @@ function testClientUpdate (t, serverType) {
121120
client.stop()
122121

123122
client.once('update', function () {
124-
server.close(function () {
125-
t.pass('server close')
126-
})
123+
t.pass('got response to stop')
124+
server.close()
125+
client.destroy()
127126
})
128127
})
129128
})
@@ -139,7 +138,7 @@ test('udp: client.update()', function (t) {
139138
})
140139

141140
function testClientScrape (t, serverType) {
142-
t.plan(5)
141+
t.plan(4)
143142
common.createServer(t, serverType, function (server, announceUrl) {
144143
parsedTorrent.announce = [ announceUrl ]
145144
var client = new Client(peerId1, port, parsedTorrent)
@@ -158,9 +157,8 @@ function testClientScrape (t, serverType) {
158157
t.equal(typeof data.incomplete, 'number')
159158
t.equal(typeof data.downloaded, 'number')
160159

161-
server.close(function () {
162-
t.pass('server close')
163-
})
160+
server.close()
161+
client.destroy()
164162
})
165163

166164
client.scrape()
@@ -176,7 +174,7 @@ test('udp: client.scrape()', function (t) {
176174
})
177175

178176
function testClientAnnounceWithNumWant (t, serverType) {
179-
t.plan(1)
177+
t.plan(4)
180178
common.createServer(t, serverType, function (server, announceUrl) {
181179
parsedTorrent.announce = [ announceUrl ]
182180
var client1 = new Client(peerId1, port, parsedTorrent)
@@ -200,10 +198,30 @@ function testClientAnnounceWithNumWant (t, serverType) {
200198
client3.on('peer', function () {
201199
t.pass('got one peer (this should only fire once)')
202200

201+
var num = 3
202+
function tryCloseServer () {
203+
num -= 1
204+
if (num === 0) server.close()
205+
}
206+
203207
client1.stop()
208+
client1.once('update', function () {
209+
t.pass('got response to stop (client1)')
210+
client1.destroy()
211+
tryCloseServer()
212+
})
204213
client2.stop()
214+
client2.once('update', function () {
215+
t.pass('got response to stop (client2)')
216+
client2.destroy()
217+
tryCloseServer()
218+
})
205219
client3.stop()
206-
server.close()
220+
client3.once('update', function () {
221+
t.pass('got response to stop (client3)')
222+
client3.destroy()
223+
tryCloseServer()
224+
})
207225
})
208226
})
209227
})

test/scrape.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function testSingle (t, serverType) {
3939
t.equal(typeof data.complete, 'number')
4040
t.equal(typeof data.incomplete, 'number')
4141
t.equal(typeof data.downloaded, 'number')
42+
client.destroy()
4243
server.close(function () {
4344
t.end()
4445
})
@@ -147,7 +148,7 @@ test('server: multiple info_hash scrape (manual http request)', function (t) {
147148
})
148149
})
149150

150-
test('server: all info_hash scrape', function (t) {
151+
test('server: all info_hash scrape (manual http request)', function (t) {
151152
var server = new Server({ udp: false })
152153
server.on('error', function (err) {
153154
t.error(err)
@@ -185,7 +186,7 @@ test('server: all info_hash scrape', function (t) {
185186
t.equal(typeof data.files[binaryBitlove].incomplete, 'number')
186187
t.equal(typeof data.files[binaryBitlove].downloaded, 'number')
187188

188-
client.stop()
189+
client.destroy()
189190
server.close(function () {
190191
t.end()
191192
})

test/server.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var peerId2 = new Buffer('12345678901234567890')
88
var torrentLength = 50000
99

1010
function serverTest (t, serverType, serverFamily) {
11-
t.plan(26)
11+
t.plan(25)
1212

1313
var opts = serverType === 'http' ? { udp: false } : { http: false }
1414
var server = new Server(opts)
@@ -32,19 +32,19 @@ function serverTest (t, serverType, serverFamily) {
3232
var port = server[serverType].address().port
3333
var announceUrl = serverType + '://' + serverAddr + ':' + port + '/announce'
3434

35-
var client = new Client(peerId, 6881, {
35+
var client1 = new Client(peerId, 6881, {
3636
infoHash: infoHash,
3737
length: torrentLength,
3838
announce: [ announceUrl ]
3939
})
4040

41-
client.start()
41+
client1.start()
4242

4343
server.once('start', function () {
4444
t.pass('got start message from client1')
4545
})
4646

47-
client.once('update', function (data) {
47+
client1.once('update', function (data) {
4848
t.equal(data.announce, announceUrl)
4949
t.equal(data.complete, 0)
5050
t.equal(data.incomplete, 1)
@@ -61,16 +61,16 @@ function serverTest (t, serverType, serverFamily) {
6161
socket: undefined
6262
})
6363

64-
client.complete()
64+
client1.complete()
6565

66-
client.once('update', function (data) {
66+
client1.once('update', function (data) {
6767
t.equal(data.announce, announceUrl)
6868
t.equal(data.complete, 1)
6969
t.equal(data.incomplete, 0)
7070

71-
client.scrape()
71+
client1.scrape()
7272

73-
client.once('scrape', function (data) {
73+
client1.once('scrape', function (data) {
7474
t.equal(data.announce, announceUrl)
7575
t.equal(typeof data.complete, 'number')
7676
t.equal(typeof data.incomplete, 'number')
@@ -96,16 +96,16 @@ function serverTest (t, serverType, serverFamily) {
9696
t.equal(data.announce, announceUrl)
9797
t.equal(data.complete, 1)
9898
t.equal(data.incomplete, 0)
99+
client2.destroy()
99100

100-
client.stop()
101-
client.once('update', function (data) {
101+
client1.stop()
102+
client1.once('update', function (data) {
102103
t.equal(data.announce, announceUrl)
103104
t.equal(data.complete, 0)
104105
t.equal(data.incomplete, 0)
105106

106-
server.close(function () {
107-
t.pass('server closed')
108-
})
107+
client1.destroy()
108+
server.close()
109109
})
110110
})
111111
})

0 commit comments

Comments
 (0)