-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
122 lines (104 loc) · 2.9 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var tape = require('tape')
var rpc = require('./')
var dgram = require('dgram')
tape('query + response', function (t) {
var server = rpc()
var queried = false
server.on('query', function (query, peer) {
queried = true
t.same(peer.address, '127.0.0.1')
t.same(query.q.toString(), 'hello_world')
t.same(query.a, { hej: 10 })
server.response(peer, query, { hello: 42 })
})
server.bind(0, function () {
var port = server.address().port
var client = rpc()
t.same(client.inflight, 0)
client.query({ host: '127.0.0.1', port: port }, { q: 'hello_world', a: { hej: 10 } }, function (err, res) {
t.same(client.inflight, 0)
server.destroy()
client.destroy()
t.error(err)
t.ok(queried)
t.same(res.r, { hello: 42 })
t.end()
})
t.same(client.inflight, 1)
})
})
tape('parallel query', function (t) {
var server = rpc()
server.on('query', function (query, peer) {
server.response(peer, query, { echo: query.a })
})
server.bind(0, function () {
var port = server.address().port
var client = rpc()
var peer = { host: '127.0.0.1', port: port }
client.query(peer, { q: 'echo', a: 1 }, function (_, res) {
t.same(res.r, { echo: 1 })
done()
})
client.query(peer, { q: 'echo', a: 2 }, function (_, res) {
t.same(res.r, { echo: 2 })
done()
})
t.same(client.inflight, 2)
function done () {
if (client.inflight) return
client.destroy()
server.destroy()
t.end()
}
})
})
tape('query + error', function (t) {
var server = rpc()
server.on('query', function (query, peer) {
server.error(peer, query, 'oh no')
})
server.bind(0, function () {
var port = server.address().port
var client = rpc()
client.query({ host: '127.0.0.1', port: port }, { q: 'hello_world', a: { hej: 10 } }, function (err) {
client.destroy()
server.destroy()
t.ok(err)
t.same(err.message, 'oh no')
t.end()
})
})
})
tape('timeout', function (t) {
var socket = rpc({ timeout: 100 })
socket.query({ host: 'example.com', port: 12345 }, { q: 'timeout' }, function (err) {
socket.destroy()
t.ok(err)
t.same(err.message, 'Query timed out')
t.end()
})
})
tape('do not crash on empty string', function (t) {
if (/^v0\.10\./.test(process.version)) {
// Sending a zero length udp message does not work on Node 0.10
t.pass('skipping test on Node 0.10')
t.end()
return
}
var server = rpc()
var socket = dgram.createSocket('udp4')
server.on('query', function (query, peer) {
t.fail('should not get a query')
})
server.on('warning', function (err) {
t.ok(err instanceof Error, 'got expected warning')
server.destroy()
socket.close()
t.end()
})
server.bind(0, function () {
var port = server.address().port
socket.send('' /* invalid bencoded data */, 0, 0, port, '127.0.0.1')
})
})