From fe1ffc0af6b496cc97a4cf4914d3e09b870d6137 Mon Sep 17 00:00:00 2001 From: Patrick Mooney Date: Wed, 12 Sep 2018 12:31:01 +0100 Subject: [PATCH] ensure the torrent event is emitted (#189) * ensure the torrent event is emitted * fix tracker test * remove v4 node as no longer compatible --- .travis.yml | 1 - index.js | 1 + test/meta-exchange.js | 43 +++++++++++++++++++++++++++++++++++++++++++ test/tracker.js | 16 ++++++++++------ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 test/meta-exchange.js diff --git a/.travis.yml b/.travis.yml index 4526aa6..2060f4d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,4 +6,3 @@ node_js: - "node" - "8" - "6" - - "4" diff --git a/index.js b/index.js index e1e8fce..0080afa 100644 --- a/index.js +++ b/index.js @@ -557,6 +557,7 @@ var torrentStream = function (link, opts, cb) { rechokeIntervalId = setInterval(onrechoke, RECHOKE_INTERVAL) process.nextTick(function () { + engine.emit('torrent', torrent) engine.emit('ready') refresh() }) diff --git a/test/meta-exchange.js b/test/meta-exchange.js new file mode 100644 index 0000000..01721da --- /dev/null +++ b/test/meta-exchange.js @@ -0,0 +1,43 @@ +var test = require('tap').test +var torrents = require('../') +var fs = require('fs') +var path = require('path') + +var fixture = torrents(fs.readFileSync(path.join(__dirname, 'data', 'star.torrent')), { + dht: false, + tracker: false +}) +fixture.listen(10000) + +var engine = function () { + var e = torrents('magnet:?xt=urn:btih:ef330b39f4801d25b4245212e75a38634bfc856e', { + dht: false, + tracker: false + }) + + e.connect('127.0.0.1:10000') + return e +} + +test('fixture should be ready', function (t) { + t.plan(1) + fixture.on('ready', t.ok.bind(t, true, 'should be ready')) +}) + +test('torrent should be emitted', function (t) { + t.plan(1) + var e = engine() + e.on('torrent', function (torrent) { + t.equal(torrent.infoHash, 'ef330b39f4801d25b4245212e75a38634bfc856e') + e.destroy(t.ok.bind(t, true, 'should be destroyed')) + }) +}) + +test('remove fixture and all content', function (t) { + t.plan(1) + fixture.destroy(function () { + fixture.remove(function () { + t.ok(!fs.existsSync(fixture.path)) + }) + }) +}) diff --git a/test/tracker.js b/test/tracker.js index e84bf17..2dc6bec 100644 --- a/test/tracker.js +++ b/test/tracker.js @@ -87,21 +87,25 @@ test('peer should connect to the swarm using magnet link and trackers', function test('peer should connect to an alternate tracker', function (t) { t.plan(5) + var engine = null var server = new tracker.Server() server.once('listening', function () { t.ok(true, 'tracker should be listening') + + engine = torrents(torrent, { dht: false, trackers: ['http://127.0.0.1:54321/announce'] }) + engine.once('ready', function () { + t.ok(true, 'should be ready') + }) }) server.once('start', function (addr) { t.equal(addr, '127.0.0.1:6881') - }) - server.listen(54321) - var engine = torrents(torrent, { dht: false, trackers: ['http://127.0.0.1:54321/announce'] }) - engine.once('ready', function () { - t.ok(true, 'should be ready') - engine.destroy(t.ok.bind(t, true, 'should be destroyed')) + engine.destroy(function () { + engine.remove(t.ok.bind(t, true, 'should be destroyed')) + }) server.close(t.ok.bind(t, true, 'tracker should be closed')) }) + server.listen(54321) }) test('cleanup', function (t) {