-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.js
107 lines (91 loc) · 3.46 KB
/
tests.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
const {createMany} = require('hyperspace/test/helpers/create')
const tape = require('tape')
const {PubSub} = require('./').debug()
const sodium = require('sodium-universal')
tape('basic', async t => {
t.plan(1)
const {clients, servers, cleanup} = await createMany(2)
try {
await Promise.all(servers.map(s => s.ready()))
await Promise.all(clients.map(c => c.ready()))
const pubsub1 = new PubSub(clients[0].network, {application: 'test', onError})
const pubsub2 = new PubSub(clients[1].network, {application: 'test', onError})
await Promise.all([
pubsub1.join('topic', {announce: true, lookup: true, flush: true}),
pubsub2.join('topic', {announce: false, lookup: true, flush: true}),
new Promise((resolve) => {
let waiting = true
clients[0].network.once('peer-open', () => {
waiting = false
resolve()
})
setTimeout(point, 1000)
function point() {
process.stdout.write('.')
if(waiting) setTimeout(point, 1000)
}
})
])
let success = false
pubsub2.sub('topic', msg => {
cleanup()
success = true
t.same(msg.toString('utf-8'), 'hello world')
}, false)
await new Promise(resolve => setTimeout(resolve, 1000))
pubsub1.pub('topic', Buffer.from('hello world', 'utf-8'))
await new Promise((resolve,reject) => setTimeout(() => success ? resolve() : reject(), 3000))
} catch (err) {
onError(err)
}
function onError(err) {
cleanup()
t.fail(err)
throw err
}
})
tape('private messages', async t => {
t.plan(1)
const {clients, servers, cleanup} = await createMany(2)
try {
await Promise.all(servers.map(s => s.ready()))
await Promise.all(clients.map(c => c.ready()))
const pubsub1 = new PubSub(clients[0].network, {application: 'test', onError})
const pubsub2 = new PubSub(clients[1].network, {application: 'test', onError})
const publicKey = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES)
const secretKey = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES)
sodium.crypto_box_keypair(publicKey, secretKey)
await Promise.all([
pubsub1.joinPublicKey(publicKey),
pubsub2.joinPublicKey(publicKey),
new Promise((resolve) => {
let waiting = true
clients[0].network.once('peer-open', () => {
waiting = false
resolve()
})
setTimeout(point, 1000)
function point() {
process.stdout.write('.')
if(waiting) setTimeout(point, 1000)
}
})
])
let success = false
pubsub1.subPrivateMsg(publicKey, secretKey, (msg) => {
cleanup()
success = true
t.same(msg.toString('utf-8'), 'hello')
})
await new Promise(resolve => setTimeout(resolve, 1000))
pubsub2.pubPrivateMsg(publicKey, Buffer.from('hello', 'utf-8'))
await new Promise((resolve,reject) => setTimeout(() => success ? resolve() : reject(), 3000))
} catch (err) {
onError(err)
}
function onError(err) {
t.fail(err)
cleanup()
throw err
}
})