diff --git a/test/client-large-torrent.js b/test/client-large-torrent.js index 8333eee..1139d10 100644 --- a/test/client-large-torrent.js +++ b/test/client-large-torrent.js @@ -41,14 +41,19 @@ test('large torrent: client.start()', function (t) { t.equal(typeof data.incomplete, 'number') }) + client.start() + client.once('peer', function (addr) { t.pass('there is at least one peer') // TODO: this shouldn't rely on an external server! - client.stop() - server.close(function () { - t.pass('server close') - }) - }) - client.start() + client.stop() + + client.once('update', function () { + server.close(function () { + t.pass('server close') + }) + }) + + }) }) }) diff --git a/test/client-magnet.js b/test/client-magnet.js index e5bcd83..2280957 100644 --- a/test/client-magnet.js +++ b/test/client-magnet.js @@ -1,50 +1,73 @@ var Client = require('../').Client var fs = require('fs') var magnet = require('magnet-uri') +var portfinder = require('portfinder') +var Server = require('../').Server var test = require('tape') var uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80' var parsedTorrent = magnet(uri) var peerId = new Buffer('01234567890123456789') -var announceUrl = 'udp://tracker.openbittorrent.com:80' // TODO: shouldn't rely on an external server! -var port = 6881 test('magnet + udp: client.start/update/stop()', function (t) { - t.plan(10) + t.plan(12) - var client = new Client(peerId, port, parsedTorrent) + var server = new Server({ http: false }) - client.on('error', function (err) { - t.error(err) + server.on('error', function (err) { + t.fail(err.message) }) - client.once('update', function (data) { - t.equal(data.announce, announceUrl) - t.equal(typeof data.complete, 'number') - t.equal(typeof data.incomplete, 'number') + server.on('warning', function (err) { + t.fail(err.message) }) - client.once('peer', function (addr) { - t.pass('there is at least one peer') // TODO: this shouldn't rely on an external server! + portfinder.getPort(function (err, port) { + t.error(err, 'found free port') + server.listen(port) + var announceUrl = 'udp://127.0.0.1:' + port // TODO: shouldn't rely on an external server! + + // remove all tracker servers except a single UDP one, for now + parsedTorrent.announce = [ announceUrl ] + + var client = new Client(peerId, 6881, parsedTorrent) + + client.on('error', function (err) { + t.error(err) + }) client.once('update', function (data) { - // receive one final update after calling stop t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') + }) + + client.start() + + client.once('peer', function (addr) { + t.pass('there is at least one peer') // TODO: this shouldn't rely on an external server! + + client.update() client.once('update', function (data) { - // received an update! + // receive one final update after calling stop t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') + + client.stop() + + client.once('update', function (data) { + // received an update! + t.equal(data.announce, announceUrl) + t.equal(typeof data.complete, 'number') + t.equal(typeof data.incomplete, 'number') + + server.close(function () { + t.pass('server closed') + }) + }) }) - - client.stop() }) - - client.update() }) - - client.start() })