-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
test.js
78 lines (67 loc) · 2.02 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
var krpc = require('./')
var tape = require('tape')
tape('query + reply', function (t) {
var server = krpc()
server.on('query', function (query, peer) {
t.same(query.q.toString(), 'echo')
t.same(query.a.hello, 42)
server.response(peer, query, { hello: 42 })
})
server.bind(0, function () {
var id = Buffer.from('aaaabbbbccccddddeeeeaaaabbbbccccddddeeee', 'hex')
var client = krpc({
nodes: ['localhost:' + server.address().port]
})
client.closest(id, { q: 'echo', a: { hello: 42 } }, onreply, function (err, n) {
server.destroy()
client.destroy()
t.error(err)
t.same(n, 1)
t.end()
})
function onreply (message, node) {
t.same(node.address, '127.0.0.1')
t.same(node.port, server.address().port)
t.same(message.r.hello, 42)
}
})
})
tape('query + closest', function (t) {
var server = krpc()
var other = krpc()
var visitedOther = false
other.on('query', function (query, peer) {
visitedOther = true
t.same(query.q.toString(), 'echo')
t.same(query.a.hello, 42)
server.response(peer, query, { hello: 42 })
})
server.on('query', function (query, peer) {
t.same(query.q.toString(), 'echo')
t.same(query.a.hello, 42)
server.response(peer, query, { hello: 42 }, [{ host: '127.0.0.1', port: other.address().port, id: other.id }])
})
other.bind(0, function () {
server.bind(0, function () {
var replies = 2
var id = Buffer.from('aaaabbbbccccddddeeeeaaaabbbbccccddddeeee', 'hex')
var client = krpc({
nodes: ['localhost:' + server.address().port, 'localhost:' + other.address().port]
})
client.closest(id, { q: 'echo', a: { hello: 42 } }, onreply, function (err, n) {
server.destroy()
client.destroy()
other.destroy()
t.error(err)
t.same(replies, 0)
t.same(n, 2)
t.ok(visitedOther)
t.end()
})
function onreply (message, node) {
replies--
t.same(message.r.hello, 42)
}
})
})
})