diff options
-rw-r--r-- | index.js | 3 | ||||
-rw-r--r-- | test/basic.js | 23 |
2 files changed, 23 insertions, 3 deletions
@@ -207,6 +207,9 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { createTorrent(input, opts, function (err, torrentBuf) { if (err) return self.emit('error', err) + // if client was destroyed asyncronously, bail early (or `add` will throw) + if (self.destroyed) return + self.add(torrentBuf, opts, function (torrent) { var tasks = [function (cb) { torrent.storage.load(streams, cb) diff --git a/test/basic.js b/test/basic.js index 1e3db44..241594e 100644 --- a/test/basic.js +++ b/test/basic.js @@ -84,15 +84,32 @@ test('client.seed (Buffer, Blob)', function (t) { } }) -test('throw if add or seed after destroy', function (t) { +test('after client.destroy(), throw on client.add() or client.seed()', function (t) { + t.plan(3) + var client = new WebTorrent({ dht: false, tracker: false }) - client.destroy() + client.destroy(function () { + t.pass('client destroyed') + }) t.throws(function () { client.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) }) t.throws(function () { client.seed(new Buffer('sup')) }) - t.end() }) +test('after client.destroy(), no "torrent" event should be emitted', function (t) { + t.plan(1) + + var client = new WebTorrent({ dht: false, tracker: false }) + client.add(leaves, function () { + t.fail('unexpected "torrent" event') + }) + client.seed(leavesBook, function () { + t.fail('unexpected "torrent" event') + }) + client.destroy(function () { + t.pass('client destroyed') + }) +}) |