|
| 1 | +var Client = require('../').Client |
| 2 | +var fs = require('fs') |
| 3 | +var parseTorrent = require('parse-torrent') |
| 4 | +var test = require('tape') |
| 5 | + |
| 6 | +var torrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') |
| 7 | +var parsedTorrent = parseTorrent(torrent) |
| 8 | + |
| 9 | +// remove all tracker servers except a single UDP one, for now |
| 10 | +parsedTorrent.announce = [ 'udp://tracker.openbittorrent.com:80' ] |
| 11 | + |
| 12 | +var peerId = new Buffer('01234567890123456789') |
| 13 | +var port = 6881 |
| 14 | + |
| 15 | +test('udp: client.start()', function (t) { |
| 16 | + t.plan(4) |
| 17 | + |
| 18 | + var client = new Client(peerId, port, parsedTorrent) |
| 19 | + |
| 20 | + client.on('error', function (err) { |
| 21 | + t.fail(err.message) |
| 22 | + }) |
| 23 | + |
| 24 | + client.once('update', function (data) { |
| 25 | + t.equal(data.announce, 'udp://tracker.openbittorrent.com:80') |
| 26 | + t.equal(typeof data.complete, 'number') |
| 27 | + t.equal(typeof data.incomplete, 'number') |
| 28 | + }) |
| 29 | + |
| 30 | + client.once('peer', function (addr) { |
| 31 | + t.pass('there is at least one peer') // TODO: this shouldn't rely on an external server! |
| 32 | + client.stop() |
| 33 | + }) |
| 34 | + |
| 35 | + client.start() |
| 36 | +}) |
| 37 | + |
| 38 | +test('udp: client.stop()', function (t) { |
| 39 | + t.plan(3) |
| 40 | + |
| 41 | + var client = new Client(peerId, port, parsedTorrent) |
| 42 | + |
| 43 | + client.on('error', function (err) { |
| 44 | + t.fail(err.message) |
| 45 | + }) |
| 46 | + |
| 47 | + client.start() |
| 48 | + |
| 49 | + setTimeout(function () { |
| 50 | + client.stop() |
| 51 | + |
| 52 | + client.once('update', function (data) { |
| 53 | + // receive one final update after calling stop |
| 54 | + t.equal(data.announce, 'udp://tracker.openbittorrent.com:80') |
| 55 | + t.equal(typeof data.complete, 'number') |
| 56 | + t.equal(typeof data.incomplete, 'number') |
| 57 | + }) |
| 58 | + |
| 59 | + }, 1000) |
| 60 | +}) |
| 61 | + |
| 62 | +test('udp: client.update()', function (t) { |
| 63 | + t.plan(3) |
| 64 | + |
| 65 | + var client = new Client(peerId, port, parsedTorrent, { interval: 5000 }) |
| 66 | + |
| 67 | + client.on('error', function (err) { |
| 68 | + t.fail(err.message) |
| 69 | + }) |
| 70 | + |
| 71 | + client.start() |
| 72 | + |
| 73 | + client.once('update', function () { |
| 74 | + |
| 75 | + client.once('update', function (data) { |
| 76 | + // received an update! |
| 77 | + t.equal(data.announce, 'udp://tracker.openbittorrent.com:80') |
| 78 | + t.equal(typeof data.complete, 'number') |
| 79 | + t.equal(typeof data.incomplete, 'number') |
| 80 | + client.stop() |
| 81 | + }) |
| 82 | + |
| 83 | + }) |
| 84 | +}) |
0 commit comments