|
1 | 1 | var Client = require('../').Client |
2 | 2 | var fs = require('fs') |
3 | 3 | var parseTorrent = require('parse-torrent') |
| 4 | +var portfinder = require('portfinder') |
| 5 | +var Server = require('../').Server |
4 | 6 | var test = require('tape') |
5 | 7 |
|
6 | 8 | // TODO: add test where tracker doesn't support compact |
7 | 9 |
|
8 | 10 | var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent') |
9 | 11 | var parsedTorrent = parseTorrent(torrent) |
10 | 12 | var peerId = new Buffer('01234567890123456789') |
11 | | -var announceUrl = 'http://t.bitlove.org/announce' // TODO: shouldn't rely on an external server! |
| 13 | +var announceUrl = '' |
12 | 14 | var port = 6881 |
13 | 15 |
|
14 | | -test('torrent: client.start()', function (t) { |
15 | | - t.plan(4) |
16 | | - |
17 | | - var client = new Client(peerId, port, parsedTorrent) |
| 16 | +function createServer (cb) { |
| 17 | + var server = new Server({ udp: false }) |
18 | 18 |
|
19 | | - client.on('error', function (err) { |
20 | | - t.error(err) |
| 19 | + server.on('error', function (err) { |
| 20 | + t.fail(err.message) |
21 | 21 | }) |
22 | 22 |
|
23 | | - client.once('update', function (data) { |
24 | | - t.equal(data.announce, announceUrl) |
25 | | - t.equal(typeof data.complete, 'number') |
26 | | - t.equal(typeof data.incomplete, 'number') |
| 23 | + server.on('warning', function (err) { |
| 24 | + t.fail(err.message) |
27 | 25 | }) |
28 | 26 |
|
29 | | - client.once('peer', function (addr) { |
30 | | - t.pass('there is at least one peer') |
31 | | - client.stop() |
32 | | - }) |
| 27 | + portfinder.getPort(function (err, port) { |
| 28 | + if (err) return cb(err) |
33 | 29 |
|
34 | | - client.start() |
35 | | -}) |
| 30 | + announceUrl = 'http://127.0.0.1:' + port + '/announce' |
| 31 | + parsedTorrent.announce = [ announceUrl ] |
36 | 32 |
|
37 | | -test('torrent: client.stop()', function (t) { |
38 | | - t.plan(4) |
| 33 | + server.listen(port) |
| 34 | + cb(null, server) |
| 35 | + }) |
| 36 | +} |
39 | 37 |
|
40 | | - var client = new Client(peerId, port, parsedTorrent) |
| 38 | +test('torrent: client.start()', function (t) { |
| 39 | + t.plan(6) |
41 | 40 |
|
42 | | - client.on('error', function (err) { |
| 41 | + createServer(function (err, server) { |
43 | 42 | t.error(err) |
44 | | - }) |
45 | 43 |
|
46 | | - client.start() |
| 44 | + var client = new Client(peerId, port, parsedTorrent) |
47 | 45 |
|
48 | | - setTimeout(function () { |
49 | | - client.stop() |
| 46 | + client.on('error', function (err) { |
| 47 | + t.fail(err) |
| 48 | + }) |
50 | 49 |
|
51 | 50 | client.once('update', function (data) { |
52 | | - // receive one final update after calling stop |
53 | 51 | t.equal(data.announce, announceUrl) |
54 | 52 | t.equal(typeof data.complete, 'number') |
55 | 53 | t.equal(typeof data.incomplete, 'number') |
56 | 54 | }) |
57 | 55 |
|
58 | | - client.once('peer', function () { |
59 | | - t.pass('should get more peers on stop()') |
| 56 | + client.once('peer', function (addr) { |
| 57 | + t.pass('there is at least one peer') |
| 58 | + client.stop() |
| 59 | + |
| 60 | + client.once('update', function () { |
| 61 | + server.close(function () { |
| 62 | + t.pass('server close') |
| 63 | + }) |
| 64 | + }) |
60 | 65 | }) |
61 | | - }, 1000) |
62 | | -}) |
63 | 66 |
|
64 | | -test('torrent: client.update()', function (t) { |
65 | | - t.plan(3) |
| 67 | + client.start() |
| 68 | + }) |
| 69 | +}) |
66 | 70 |
|
67 | | - var client = new Client(peerId, port, parsedTorrent, { interval: 5000 }) |
| 71 | +test('torrent: client.stop()', function (t) { |
| 72 | + t.plan(5) |
68 | 73 |
|
69 | | - client.on('error', function (err) { |
| 74 | + createServer(function (err, server) { |
70 | 75 | t.error(err) |
71 | | - }) |
| 76 | + var client = new Client(peerId, port, parsedTorrent) |
72 | 77 |
|
73 | | - client.start() |
| 78 | + client.on('error', function (err) { |
| 79 | + t.fail(err) |
| 80 | + }) |
74 | 81 |
|
75 | | - client.once('update', function () { |
| 82 | + client.start() |
76 | 83 |
|
77 | | - client.once('update', function (data) { |
78 | | - // received an update! |
79 | | - t.equal(data.announce, announceUrl) |
80 | | - t.equal(typeof data.complete, 'number') |
81 | | - t.equal(typeof data.incomplete, 'number') |
| 84 | + setTimeout(function () { |
82 | 85 | client.stop() |
| 86 | + |
| 87 | + client.once('update', function (data) { |
| 88 | + // receive one final update after calling stop |
| 89 | + t.equal(data.announce, announceUrl) |
| 90 | + t.equal(typeof data.complete, 'number') |
| 91 | + t.equal(typeof data.incomplete, 'number') |
| 92 | + |
| 93 | + server.close(function () { |
| 94 | + t.pass('server close') |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + }, 1000) |
| 99 | + }) |
| 100 | +}) |
| 101 | + |
| 102 | +test('torrent: client.update()', function (t) { |
| 103 | + t.plan(5) |
| 104 | + |
| 105 | + createServer(function (err, server) { |
| 106 | + t.error(err) |
| 107 | + var client = new Client(peerId, port, parsedTorrent, { interval: 5000 }) |
| 108 | + |
| 109 | + client.on('error', function (err) { |
| 110 | + t.fail(err) |
83 | 111 | }) |
84 | 112 |
|
| 113 | + client.start() |
| 114 | + |
| 115 | + client.once('update', function () { |
| 116 | + |
| 117 | + client.once('update', function (data) { |
| 118 | + // received an update! |
| 119 | + t.equal(data.announce, announceUrl) |
| 120 | + t.equal(typeof data.complete, 'number') |
| 121 | + t.equal(typeof data.incomplete, 'number') |
| 122 | + client.stop() |
| 123 | + |
| 124 | + client.once('update', function () { |
| 125 | + server.close(function () { |
| 126 | + t.pass('server close') |
| 127 | + }) |
| 128 | + }) |
| 129 | + }) |
| 130 | + }) |
85 | 131 | }) |
86 | 132 | }) |
87 | 133 |
|
88 | 134 | test('torrent: client.scrape()', function (t) { |
89 | | - t.plan(4) |
90 | | - |
91 | | - var client = new Client(peerId, port, parsedTorrent) |
| 135 | + t.plan(6) |
92 | 136 |
|
93 | | - client.on('error', function (err) { |
| 137 | + createServer(function (err, server) { |
94 | 138 | t.error(err) |
95 | | - }) |
| 139 | + var client = new Client(peerId, port, parsedTorrent) |
96 | 140 |
|
97 | | - client.once('scrape', function (data) { |
98 | | - t.equal(data.announce, announceUrl) |
99 | | - t.equal(typeof data.complete, 'number') |
100 | | - t.equal(typeof data.incomplete, 'number') |
101 | | - t.equal(typeof data.downloaded, 'number') |
102 | | - }) |
| 141 | + client.on('error', function (err) { |
| 142 | + t.fail(err) |
| 143 | + }) |
| 144 | + |
| 145 | + client.once('scrape', function (data) { |
| 146 | + t.equal(data.announce, announceUrl) |
| 147 | + t.equal(typeof data.complete, 'number') |
| 148 | + t.equal(typeof data.incomplete, 'number') |
| 149 | + t.equal(typeof data.downloaded, 'number') |
103 | 150 |
|
104 | | - client.scrape() |
| 151 | + server.close(function () { |
| 152 | + t.pass('server close') |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + client.scrape() |
| 157 | + }) |
105 | 158 | }) |
0 commit comments