Skip to content

Commit 858b308

Browse files
committed
add tests
1 parent 1159037 commit 858b308

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

test/basic.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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/bitlove-intro.torrent')
7+
var parsedTorrent = parseTorrent(torrent)
8+
9+
var peerId = new Buffer('01234567890123456789')
10+
var port = 6881
11+
12+
test('client.start()', function (t) {
13+
t.plan(4)
14+
15+
var client = new Client(peerId, port, parsedTorrent)
16+
17+
client.on('error', function (err) {
18+
t.fail(err.message)
19+
})
20+
21+
client.once('update', function (data) {
22+
t.equal(data.announce, 'http://t.bitlove.org/announce')
23+
t.equal(typeof data.complete, 'number')
24+
t.equal(typeof data.incomplete, 'number')
25+
})
26+
27+
client.once('peer', function (addr) {
28+
t.pass('there is at least one peer') // TODO: this shouldn't rely on an external server!
29+
client.stop()
30+
})
31+
32+
client.start()
33+
})
34+
35+
test('client.stop()', function (t) {
36+
t.plan(3)
37+
38+
var client = new Client(peerId, port, parsedTorrent)
39+
40+
client.on('error', function (err) {
41+
t.fail(err.message)
42+
})
43+
44+
client.start()
45+
46+
setTimeout(function () {
47+
client.stop()
48+
49+
client.once('update', function (data) {
50+
// receive one final update after calling stop
51+
t.equal(data.announce, 'http://t.bitlove.org/announce')
52+
t.equal(typeof data.complete, 'number')
53+
t.equal(typeof data.incomplete, 'number')
54+
})
55+
}, 1000)
56+
})
57+
58+
test('client.update()', function (t) {
59+
t.plan(3)
60+
61+
var client = new Client(peerId, port, parsedTorrent, { interval: 5000 })
62+
63+
client.on('error', function (err) {
64+
t.fail(err.message)
65+
})
66+
67+
client.start()
68+
69+
client.once('update', function () {
70+
71+
client.once('update', function (data) {
72+
// received an update!
73+
t.equal(data.announce, 'http://t.bitlove.org/announce')
74+
t.equal(typeof data.complete, 'number')
75+
t.equal(typeof data.incomplete, 'number')
76+
client.stop()
77+
})
78+
79+
})
80+
})
81+
82+
// TODO: add test where tracker doesn't support compact
597 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)