From 62be68979b055d5c181b020e52b3abd08b4dc126 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 23 May 2014 21:17:31 -0700 Subject: [PATCH] ALL TESTS WORK OFFLINE Fixes #18 --- test/udp-client.js | 63 +++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/test/udp-client.js b/test/udp-client.js index e6e6de8..47d55bf 100644 --- a/test/udp-client.js +++ b/test/udp-client.js @@ -1,54 +1,77 @@ var Client = require('../').Client var fs = require('fs') var parseTorrent = require('parse-torrent') +var portfinder = require('portfinder') +var Server = require('../').Server var test = require('tape') var torrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') var parsedTorrent = parseTorrent(torrent) -// remove all tracker servers except a single UDP one, for now -var announceUrl = 'udp://tracker.publicbt.com:80' -parsedTorrent.announce = [ announceUrl ] - var peerId = new Buffer('01234567890123456789') var port = 6881 test('udp: client.start/update/stop()', function (t) { - t.plan(10) + t.plan(12) - var client = new Client(peerId, port, parsedTorrent) + var server = new Server({ udp: 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! + var announceUrl + portfinder.getPort(function (err, port) { + t.error(err, 'found free port') + + // remove all tracker servers except a single UDP one, for now + announceUrl = 'http://127.0.0.1:' + port + '/announce' + parsedTorrent.announce = [ announceUrl ] + + server.listen(port) + + var client = new Client(peerId, port, 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.once('peer', function (addr) { + t.pass('there is at least one peer') // TODO: this shouldn't rely on an external server! 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.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 close') + }) + }) + + client.stop() }) - client.stop() + client.update() }) - client.update() + client.start() }) - - client.start() })