forked from dat-ecosystem-archive/discovery-channel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
56 lines (44 loc) · 1.07 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
var test = require('tape')
var DC = require('./index.js')
var crypto = require('crypto')
test('list', function (t) {
var channel = DC({dht: false, dns: false})
var id = crypto.randomBytes(32)
channel.join(id)
t.same(channel.list(), [{id: id, port: 0}])
channel.leave(id)
channel.join(id, 8080)
t.same(channel.list(), [{id: id, port: 8080}])
channel.leave(id)
t.same(channel.list(), [{id: id, port: 8080}])
channel.leave(id, 8080)
t.same(channel.list(), [])
channel.destroy()
t.end()
})
test('find each other', function (t) {
var id = crypto.randomBytes(32)
var pending = 2
t.plan(2)
var channel1 = DC()
var channel2 = DC()
channel1.join(id, 1337)
channel2.join(id, 7331)
channel1.on('peer', function (hash, peer) {
if (peer.port === 7331) {
t.pass('found second channel')
done()
}
})
channel2.on('peer', function (hash, peer) {
if (peer.port === 1337) {
t.pass('found first channel')
done()
}
})
function done () {
if (--pending) return
channel1.destroy()
channel2.destroy()
}
})