diff options
Diffstat (limited to 'test/download-tracker-torrent.js')
-rw-r--r-- | test/download-tracker-torrent.js | 89 |
1 files changed, 42 insertions, 47 deletions
diff --git a/test/download-tracker-torrent.js b/test/download-tracker-torrent.js index fdbf559..1c31236 100644 --- a/test/download-tracker-torrent.js +++ b/test/download-tracker-torrent.js @@ -1,16 +1,11 @@ -var auto = require('run-auto') +var common = require('./common') +var extend = require('xtend') var fs = require('fs') -var parseTorrent = require('parse-torrent') -var path = require('path') +var series = require('run-series') var test = require('tape') var TrackerServer = require('bittorrent-tracker/server') var WebTorrent = require('../') -var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') -var leavesFile = fs.readFileSync(leavesPath) -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - test('Download using UDP tracker (via .torrent file)', function (t) { torrentDownloadTest(t, 'udp') }) @@ -23,39 +18,39 @@ function torrentDownloadTest (t, serverType) { t.plan(9) var trackerStartCount = 0 + var parsedTorrent = extend(common.leaves.parsedTorrent) - auto({ - tracker: function (cb) { - var tracker = new TrackerServer( - serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } - ) - - tracker.on('error', function (err) { t.fail(err) }) - tracker.on('warning', function (err) { t.fail(err) }) + var tracker = new TrackerServer( + serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } + ) - tracker.on('start', function () { - trackerStartCount += 1 - }) + tracker.on('error', function (err) { t.fail(err) }) + tracker.on('warning', function (err) { t.fail(err) }) - tracker.listen(function () { - var port = tracker[serverType].address().port - var announceUrl = serverType === 'http' - ? 'http://127.0.0.1:' + port + '/announce' - : 'udp://127.0.0.1:' + port + tracker.on('start', function () { + trackerStartCount += 1 + }) - // Overwrite announce with our local tracker - leavesParsed.announce = [ announceUrl ] + var client1, client2 - cb(null, tracker) - }) + series([ + function (cb) { + tracker.listen(cb) }, - client1: ['tracker', function (cb) { - var client1 = new WebTorrent({ dht: false }) + function (cb) { + client1 = new WebTorrent({ dht: false }) client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - client1.add(leavesParsed) + var port = tracker[serverType].address().port + + var announceUrl = serverType === 'http' + ? 'http://127.0.0.1:' + port + '/announce' + : 'udp://127.0.0.1:' + port + + // Overwrite announce with our local tracker + parsedTorrent.announce = [ announceUrl ] client1.on('torrent', function (torrent) { // torrent metadata has been fetched -- sanity check it @@ -67,24 +62,24 @@ function torrentDownloadTest (t, serverType) { t.deepEqual(torrent.files.map(function (file) { return file.name }), names) - torrent.load(fs.createReadStream(leavesPath), function (err) { - cb(err, client1) - }) + torrent.load(fs.createReadStream(common.leaves.contentPath), cb) }) - }], - client2: ['client1', function (cb) { - var client2 = new WebTorrent({ dht: false }) + client1.add(parsedTorrent) + }, + + function (cb) { + client2 = new WebTorrent({ dht: false }) client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) - client2.add(leavesParsed) + client2.add(parsedTorrent) client2.on('torrent', function (torrent) { torrent.files.forEach(function (file) { file.getBuffer(function (err, buf) { if (err) throw err - t.deepEqual(buf, leavesFile, 'downloaded correct content') + t.deepEqual(buf, common.leaves.content, 'downloaded correct content') gotBuffer = true maybeDone() }) @@ -99,23 +94,23 @@ function torrentDownloadTest (t, serverType) { var gotBuffer = false var torrentDone = false function maybeDone () { - if (gotBuffer && torrentDone) cb(null, client2) + if (gotBuffer && torrentDone) cb(null) } }) - }] + } - }, function (err, r) { + ], function (err) { t.error(err) t.equal(trackerStartCount, 2) - r.tracker.close(function () { + tracker.close(function () { t.pass('tracker closed') }) - r.client1.destroy(function () { - t.pass('client1 destroyed') + client1.destroy(function (err) { + t.error(err, 'client1 destroyed') }) - r.client2.destroy(function () { - t.pass('client2 destroyed') + client2.destroy(function (err) { + t.error(err, 'client2 destroyed') }) }) } |