Skip to content

Commit f6d0201

Browse files
committed
add failing udp server test
1 parent d1aca7c commit f6d0201

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

test/udp-server.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
var Client = require('../').Client
2+
var portfinder = require('portfinder')
3+
var Server = require('../').Server
4+
var test = require('tape')
5+
6+
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
7+
var peerId = '01234567890123456789'
8+
var peerId2 = '12345678901234567890'
9+
var torrentLength = 50000
10+
11+
test('server', function (t) {
12+
t.plan(26)
13+
14+
var server = new Server({ http: false })
15+
16+
server.on('error', function (err) {
17+
t.fail(err.message)
18+
})
19+
20+
server.on('complete', function () {})
21+
server.on('update', function () {})
22+
server.on('stop', function () {})
23+
24+
server.on('listening', function () {
25+
t.pass('server listening')
26+
})
27+
28+
portfinder.getPort(function (err, port) {
29+
t.error(err, 'found free port')
30+
server.listen(port)
31+
32+
var announceUrl = 'udp://127.0.0.1:' + port + '/announce'
33+
34+
var client = new Client(peerId, 6881, {
35+
infoHash: infoHash,
36+
length: torrentLength,
37+
announce: [ announceUrl ]
38+
})
39+
40+
client.start()
41+
42+
server.once('start', function () {
43+
t.pass('got start message from client1')
44+
})
45+
46+
client.once('update', function (data) {
47+
t.equal(data.announce, announceUrl)
48+
t.equal(data.complete, 0)
49+
t.equal(data.incomplete, 1)
50+
51+
t.equal(Object.keys(server.torrents).length, 1)
52+
t.equal(server.torrents[infoHash].complete, 0)
53+
t.equal(server.torrents[infoHash].incomplete, 1)
54+
t.equal(Object.keys(server.torrents[infoHash].peers).length, 1)
55+
t.deepEqual(server.torrents[infoHash].peers['127.0.0.1:6881'], {
56+
ip: '127.0.0.1',
57+
port: 6881,
58+
peerId: peerId
59+
})
60+
61+
client.complete()
62+
63+
client.once('update', function (data) {
64+
t.equal(data.announce, announceUrl)
65+
t.equal(data.complete, 1)
66+
t.equal(data.incomplete, 0)
67+
68+
client.scrape()
69+
70+
client.once('scrape', function (data) {
71+
t.equal(data.announce, announceUrl)
72+
t.equal(typeof data.complete, 'number')
73+
t.equal(typeof data.incomplete, 'number')
74+
t.equal(typeof data.downloaded, 'number')
75+
76+
var client2 = new Client(peerId2, 6882, {
77+
infoHash: infoHash,
78+
length: torrentLength,
79+
announce: [ announceUrl ]
80+
})
81+
82+
client2.start()
83+
84+
server.once('start', function () {
85+
t.pass('got start message from client2')
86+
})
87+
88+
client2.once('peer', function (addr) {
89+
t.equal(addr, '127.0.0.1:6881')
90+
91+
client2.stop()
92+
client2.once('update', function (data) {
93+
t.equal(data.announce, announceUrl)
94+
t.equal(data.complete, 1)
95+
t.equal(data.incomplete, 0)
96+
97+
client.stop()
98+
client.once('update', function (data) {
99+
t.equal(data.announce, announceUrl)
100+
t.equal(data.complete, 0)
101+
t.equal(data.incomplete, 0)
102+
103+
server.close()
104+
})
105+
})
106+
})
107+
})
108+
})
109+
})
110+
})
111+
})

0 commit comments

Comments
 (0)