diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2015-08-24 01:25:38 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2015-08-24 01:25:38 +0300 |
commit | 931a10c0fdb86ad926d586558db5102b13c7065d (patch) | |
tree | 1875f375e1c6fd401bf41fa2d940acb3676df63f /test | |
parent | 1fc60bb9d5fc3d73b6035b8ef48cdea64bba4289 (diff) |
improve tests to pass when verifying torrent data
Diffstat (limited to 'test')
-rw-r--r-- | test/blocklist-dht.js | 28 | ||||
-rw-r--r-- | test/blocklist-tracker.js | 2 | ||||
-rw-r--r-- | test/download-dht-magnet.js | 14 | ||||
-rw-r--r-- | test/download-dht-torrent.js | 14 | ||||
-rw-r--r-- | test/download-tracker-magnet.js | 13 | ||||
-rw-r--r-- | test/download-tracker-torrent.js | 14 | ||||
-rw-r--r-- | test/download-webseed-magnet.js | 11 | ||||
-rw-r--r-- | test/download-webseed-torrent.js | 11 |
8 files changed, 79 insertions, 28 deletions
diff --git a/test/blocklist-dht.js b/test/blocklist-dht.js index 3e8710b..6d4c1c9 100644 --- a/test/blocklist-dht.js +++ b/test/blocklist-dht.js @@ -13,7 +13,7 @@ var leavesParsed = parseTorrent(leavesTorrent) leavesParsed.announce = [] test('blocklist blocks peers discovered via DHT', function (t) { - t.plan(7) + t.plan(8) var dhtServer = new DHT({ bootstrap: false }) @@ -38,13 +38,6 @@ test('blocklist blocks peers discovered via DHT', function (t) { var torrent1 = client1.add(leavesParsed) - client1.on('torrent', function () { - torrent1.on('dhtAnnounce', function () { - t.pass('client1 announced to dht') - cb(null, client1) - }) - }) - torrent1.on('peer', function () { t.fail('client1 should not find any peers') }) @@ -52,6 +45,25 @@ test('blocklist blocks peers discovered via DHT', function (t) { torrent1.on('blockedPeer', function () { t.fail('client1 should not block any peers') }) + + torrent1.on('ready', function () { + t.pass('torrent1 ready') + torrentReady = true + maybeDone() + }) + + torrent1.on('dhtAnnounce', function () { + t.pass('client1 announced to dht') + announced = true + maybeDone() + }) + + var torrentReady = false + var announced = false + function maybeDone () { + if (torrentReady && announced) cb(null, client1) + } + }], client2: ['client1', function (cb, r) { diff --git a/test/blocklist-tracker.js b/test/blocklist-tracker.js index e34b16a..cac256e 100644 --- a/test/blocklist-tracker.js +++ b/test/blocklist-tracker.js @@ -13,7 +13,7 @@ test('blocklist blocks peers discovered via tracker', function (t) { auto({ tracker: function (cb) { - var tracker = new TrackerServer({ udp: false }) + var tracker = new TrackerServer({ udp: false, ws: false }) tracker.listen(function () { var port = tracker.http.address().port diff --git a/test/download-dht-magnet.js b/test/download-dht-magnet.js index b1c6a7a..34656e2 100644 --- a/test/download-dht-magnet.js +++ b/test/download-dht-magnet.js @@ -43,18 +43,20 @@ test('Download using DHT (via magnet uri)', function (t) { if (announced && loaded) cb(null, client1) } - client1.add(leavesParsed, function (torrent) { + var torrent = client1.add(leavesParsed) + + torrent.on('dhtAnnounce', function () { + announced = true + maybeDone() + }) + + torrent.on('ready', function () { // torrent metadata has been fetched -- sanity check it t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') var names = [ 'Leaves of Grass by Walt Whitman.epub' ] t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - torrent.on('dhtAnnounce', function () { - announced = true - maybeDone() - }) - torrent.load(fs.createReadStream(leavesPath), function (err) { t.error(err) loaded = true diff --git a/test/download-dht-torrent.js b/test/download-dht-torrent.js index bc51a71..408b060 100644 --- a/test/download-dht-torrent.js +++ b/test/download-dht-torrent.js @@ -36,7 +36,7 @@ test('Download using DHT (via .torrent file)', function (t) { client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - client1.add(leavesParsed) + var torrent = client1.add(leavesParsed) var announced = false var loaded = false @@ -44,23 +44,23 @@ test('Download using DHT (via .torrent file)', function (t) { if ((announced && loaded) || err) cb(err, client1) } - client1.on('torrent', function (torrent) { + torrent.on('ready', function () { // torrent metadata has been fetched -- sanity check it t.equal(torrent.name, 'Leaves of Grass by Walt Whitman.epub') var names = [ 'Leaves of Grass by Walt Whitman.epub' ] t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - torrent.on('dhtAnnounce', function () { - announced = true - maybeDone(null) - }) - torrent.load(fs.createReadStream(leavesPath), function (err) { loaded = true maybeDone(err) }) }) + + torrent.on('dhtAnnounce', function () { + announced = true + maybeDone(null) + }) }], client2: ['client1', function (cb, r) { diff --git a/test/download-tracker-magnet.js b/test/download-tracker-magnet.js index 43ed393..0aac1fb 100644 --- a/test/download-tracker-magnet.js +++ b/test/download-tracker-magnet.js @@ -27,7 +27,7 @@ function magnetDownloadTest (t, serverType) { auto({ tracker: function (cb) { var tracker = new TrackerServer( - serverType === 'udp' ? { http: false } : { udp: false } + serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } ) tracker.on('error', function (err) { t.fail(err) }) @@ -84,13 +84,22 @@ function magnetDownloadTest (t, serverType) { file.getBuffer(function (err, buf) { if (err) throw err t.deepEqual(buf, leavesFile, 'downloaded correct content') + gotBuffer = true + maybeDone() }) }) torrent.once('done', function () { t.pass('client2 downloaded torrent from client1') - cb(null, client2) + torrentDone = true + maybeDone() }) + + var gotBuffer = false + var torrentDone = false + function maybeDone () { + if (gotBuffer && torrentDone) cb(null, client2) + } }) }] diff --git a/test/download-tracker-torrent.js b/test/download-tracker-torrent.js index c724f0a..1fbe54f 100644 --- a/test/download-tracker-torrent.js +++ b/test/download-tracker-torrent.js @@ -26,7 +26,7 @@ function torrentDownloadTest (t, serverType) { auto({ tracker: function (cb) { var tracker = new TrackerServer( - serverType === 'udp' ? { http: false } : { udp: false } + serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } ) tracker.on('error', function (err) { t.fail(err) }) @@ -84,14 +84,24 @@ function torrentDownloadTest (t, serverType) { file.getBuffer(function (err, buf) { if (err) throw err t.deepEqual(buf, leavesFile, 'downloaded correct content') + gotBuffer = true + maybeDone() }) }) torrent.once('done', function () { t.pass('client2 downloaded torrent from client1') - cb(null, client2) + torrentDone = true + maybeDone() }) + + var gotBuffer = false + var torrentDone = false + function maybeDone () { + if (gotBuffer && torrentDone) cb(null, client2) + } }) + }] }, function (err, r) { diff --git a/test/download-webseed-magnet.js b/test/download-webseed-magnet.js index 214a92b..97ec256 100644 --- a/test/download-webseed-magnet.js +++ b/test/download-webseed-magnet.js @@ -81,13 +81,22 @@ test('Download using webseed (via magnet uri)', function (t) { file.getBuffer(function (err, buf) { t.error(err) t.deepEqual(buf, leavesFile, 'downloaded correct content') + gotBuffer = true + maybeDone() }) }) torrent.once('done', function () { t.pass('client2 downloaded torrent from client1') - cb(null, client2) + torrentDone = true + maybeDone() }) + + var gotBuffer = false + var torrentDone = false + function maybeDone () { + if (gotBuffer && torrentDone) cb(null, client2) + } }) client2.add(magnetUri) diff --git a/test/download-webseed-torrent.js b/test/download-webseed-torrent.js index 4e5d279..e9f8bd1 100644 --- a/test/download-webseed-torrent.js +++ b/test/download-webseed-torrent.js @@ -48,13 +48,22 @@ test('Download using webseed (via .torrent file)', function (t) { file.getBuffer(function (err, buf) { t.error(err) t.deepEqual(buf, leavesFile, 'downloaded correct content') + gotBuffer = true + maybeDone() }) }) torrent.once('done', function () { t.pass('client downloaded torrent from webseed') - cb(null, client) + torrentDone = true + maybeDone() }) + + var gotBuffer = false + var torrentDone = false + function maybeDone () { + if (gotBuffer && torrentDone) cb(null, client) + } }) client.add(leavesParsed) |