From 2aea063b8c8d597ce2e782c9268b09d8f1b8d64b Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 23 May 2014 21:14:19 -0700 Subject: [PATCH] make client tests work offline for #18 --- test/client.js | 181 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 117 insertions(+), 64 deletions(-) diff --git a/test/client.js b/test/client.js index 5215e2d..6ff41c2 100644 --- a/test/client.js +++ b/test/client.js @@ -1,6 +1,8 @@ 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') // TODO: add test where tracker doesn't support compact @@ -8,98 +10,149 @@ var test = require('tape') var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent') var parsedTorrent = parseTorrent(torrent) var peerId = new Buffer('01234567890123456789') -var announceUrl = 'http://t.bitlove.org/announce' // TODO: shouldn't rely on an external server! +var announceUrl = '' var port = 6881 +function createServer (cb) { + var server = new Server({ udp: false }) + + server.on('error', function (err) { + t.fail(err.message) + }) + + server.on('warning', function (err) { + t.fail(err.message) + }) + + portfinder.getPort(function (err, port) { + if (err) return cb(err) + + announceUrl = 'http://127.0.0.1:' + port + '/announce' + parsedTorrent.announce = [ announceUrl ] + + server.listen(port) + cb(null, server) + }) +} + test('torrent: client.start()', function (t) { - t.plan(4) + t.plan(6) - var client = new Client(peerId, port, parsedTorrent) - - client.on('error', function (err) { + createServer(function (err, server) { t.error(err) - }) - client.once('update', function (data) { - t.equal(data.announce, announceUrl) - t.equal(typeof data.complete, 'number') - t.equal(typeof data.incomplete, 'number') - }) + var client = new Client(peerId, port, parsedTorrent) - client.once('peer', function (addr) { - t.pass('there is at least one peer') - client.stop() - }) + client.on('error', function (err) { + t.fail(err) + }) - client.start() + client.once('update', function (data) { + 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') + client.stop() + + client.once('update', function () { + server.close(function () { + t.pass('server close') + }) + }) + }) + + client.start() + }) }) test('torrent: client.stop()', function (t) { - t.plan(4) + t.plan(5) - var client = new Client(peerId, port, parsedTorrent) - - client.on('error', function (err) { + createServer(function (err, server) { t.error(err) + var client = new Client(peerId, port, parsedTorrent) + + client.on('error', function (err) { + t.fail(err) + }) + + client.start() + + setTimeout(function () { + client.stop() + + 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') + + server.close(function () { + t.pass('server close') + }) + }) + + }, 1000) }) - - client.start() - - setTimeout(function () { - client.stop() - - 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 () { - t.pass('should get more peers on stop()') - }) - }, 1000) }) test('torrent: client.update()', function (t) { - t.plan(3) + t.plan(5) - var client = new Client(peerId, port, parsedTorrent, { interval: 5000 }) - - client.on('error', function (err) { + createServer(function (err, server) { t.error(err) - }) + var client = new Client(peerId, port, parsedTorrent, { interval: 5000 }) - client.start() - - client.once('update', function () { - - 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') - client.stop() + client.on('error', function (err) { + t.fail(err) }) + client.start() + + client.once('update', function () { + + 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') + client.stop() + + client.once('update', function () { + server.close(function () { + t.pass('server close') + }) + }) + }) + }) }) }) test('torrent: client.scrape()', function (t) { - t.plan(4) + t.plan(6) - var client = new Client(peerId, port, parsedTorrent) - - client.on('error', function (err) { + createServer(function (err, server) { t.error(err) - }) + var client = new Client(peerId, port, parsedTorrent) - client.once('scrape', function (data) { - t.equal(data.announce, announceUrl) - t.equal(typeof data.complete, 'number') - t.equal(typeof data.incomplete, 'number') - t.equal(typeof data.downloaded, 'number') - }) + client.on('error', function (err) { + t.fail(err) + }) - client.scrape() + client.once('scrape', function (data) { + t.equal(data.announce, announceUrl) + t.equal(typeof data.complete, 'number') + t.equal(typeof data.incomplete, 'number') + t.equal(typeof data.downloaded, 'number') + + server.close(function () { + t.pass('server close') + }) + }) + + client.scrape() + }) })