-
Notifications
You must be signed in to change notification settings - Fork 11
/
direct-channel.test.js
296 lines (250 loc) · 8.38 KB
/
direct-channel.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'use strict'
const path = require('path')
const rmrf = require('rimraf')
const assert = require('assert')
const pMapSeries = require('p-map-series')
const {
connectPeers,
startIpfs,
stopIpfs,
getIpfsPeerId,
testAPIs,
waitForPeers,
} = require('orbit-db-test-utils')
const Channel = require('../src/direct-channel')
const getPeerID = require('../src/get-peer-id')
const PROTOCOL = require('../src/protocol')
// IPFS instances used in these tests
const ipfsPaths = [
'./tmp/peer1/ipfs',
'./tmp/peer2/ipfs',
'./tmp/peer3/ipfs',
]
Object.keys(testAPIs).forEach(API => {
describe(`DirectChannel ${API}`, function() {
this.timeout(5000)
let instances = []
let ipfsd1, ipfsd2, ipfsd3, ipfs1, ipfs2, ipfs3
let id1, id2, id3
let expectedPeerIDs = []
before(async () => {
ipfsd1 = await startIpfs(API)
ipfsd2 = await startIpfs(API)
ipfsd3 = await startIpfs(API)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
ipfs3 = ipfsd3.api
id1 = await getIpfsPeerId(ipfs1)
id2 = await getIpfsPeerId(ipfs2)
id3 = await getIpfsPeerId(ipfs3)
await connectPeers(ipfs1, ipfs2)
await connectPeers(ipfs1, ipfs3)
await connectPeers(ipfs2, ipfs3)
// Note, we only create channels between peer1 and peer2 in these test,
// peer3 is used for "external actor" tests
expectedPeerIDs = Array.from([id1, id2]).sort()
})
after(async () => {
await stopIpfs(ipfsd1)
await stopIpfs(ipfsd2)
await stopIpfs(ipfsd3)
rmrf.sync('./tmp/') // remove test data directory
})
describe('create a channel', function() {
it('has two participants', async () => {
const c = await Channel.open(ipfs1, id2)
assert.deepEqual(c.peers, expectedPeerIDs)
c.close()
})
it('has correct ID', async () => {
const expectedID = path.join('/', PROTOCOL, expectedPeerIDs.join('/'))
const c = await Channel.open(ipfs1, id2)
assert.deepEqual(c.id, expectedID)
c.close()
})
it('has two peers', async () => {
const c1 = await Channel.open(ipfs1, id2)
const c2 = await Channel.open(ipfs2, id1)
assert.deepEqual(c1.peers, expectedPeerIDs)
assert.deepEqual(c2.peers, expectedPeerIDs)
assert.equal(c1.id, path.join('/', PROTOCOL, expectedPeerIDs.join('/')))
assert.equal(c2.id, path.join('/', PROTOCOL, expectedPeerIDs.join('/')))
c1.close()
c2.close()
})
it('can be created with one line', async () => {
const c = await Channel.open(ipfs1, id2)
const topics = await ipfs1.pubsub.ls()
const channelID = topics.find(e => e === c.id)
assert.equal(channelID, c.id)
c.close()
})
})
describe('properties', function() {
let c
beforeEach(async () => {
c = await Channel.open(ipfs1, id2)
})
afterEach(() => {
if (c) {
c.close()
}
})
it('has an id', async () => {
assert.equal(c.id, path.join('/', PROTOCOL, expectedPeerIDs.join('/')))
})
it('has two peers', async () => {
assert.equal(c.peers.length, 2)
assert.deepEqual(c.peers, expectedPeerIDs)
})
it('has an event emitter for \'message\' event', async () => {
let err
try {
c.on('message', () => {})
} catch (e) {
err = e
}
assert.equal(err, null)
})
})
describe('messaging', function() {
it('sends and receives messages', async () => {
const c1 = await Channel.open(ipfs1, id2)
const c2 = await Channel.open(ipfs2, id1)
await c1.connect()
await c2.connect()
return new Promise(async (resolve, reject) => {
c1.on('error', reject)
c2.on('error', reject)
c2.on('message', async (m) => {
assert.notEqual(m, null)
assert.equal(m.from, id1)
assert.equal(Buffer.from(m.data).toString(), Buffer.from('hello1'))
assert.equal(m.topicIDs.length, 1)
assert.equal(m.topicIDs[0], c1.id)
assert.equal(m.topicIDs[0], c2.id)
await c2.send(Buffer.from('hello2'))
})
c1.on('message', (m) => {
assert.equal(m.from, id2)
assert.equal(Buffer.from(m.data).toString(), Buffer.from('hello2'))
assert.equal(m.topicIDs.length, 1)
assert.equal(m.topicIDs[0], c1.id)
assert.equal(m.topicIDs[0], c2.id)
c1.close()
c2.close()
setTimeout(() => resolve(), 500)
})
await c1.send('hello1')
})
})
})
describe('connect', function() {
it('connects the peers', async () => {
let c1, c2
c1 = await Channel.open(ipfs1, id2)
c2 = await Channel.open(ipfs2, id1)
let peers = await ipfs1.pubsub.peers(c1.id)
// assert.deepEqual(peers, [])
await c1.connect()
peers = await ipfs1.pubsub.peers(c1.id)
assert.deepEqual(peers, [id2])
c1.close()
c2.close()
})
})
describe('disconnecting', function() {
it('closes a channel', async () => {
const c1 = await Channel.open(ipfs1, id2)
const c2 = await Channel.open(ipfs2, id1)
await c1.connect()
await c2.connect()
return new Promise(async (resolve, reject) => {
assert.equal(c1._closed, false)
assert.equal(c1._isClosed(), false)
c1.close()
const topics1 = await ipfs1.pubsub.ls()
assert.deepEqual(topics1, [])
assert.equal(c1._closed, true)
assert.equal(c1._isClosed(), true)
assert.equal(c2._closed, false)
assert.equal(c2._isClosed(), false)
c2.close()
const topics2 = await ipfs2.pubsub.ls()
assert.deepEqual(topics1, [])
assert.equal(c2._closed, true)
assert.equal(c2._isClosed(), true)
setTimeout(async () => {
const peers1 = await ipfs1.pubsub.peers(c1.id)
const peers2 = await ipfs2.pubsub.peers(c1.id)
assert.deepEqual(peers1, [])
assert.deepEqual(peers2, [])
resolve()
}, 200)
})
})
it('removes event listener upon closing the channel', async () => {
const c1 = await Channel.open(ipfs1, id2)
const c2 = await Channel.open(ipfs2, id1)
c1.on('message', () => {})
c2.on('message', () => {})
await c1.connect()
await c2.connect()
assert.equal(c1.listenerCount('message'), 1)
assert.equal(c2.listenerCount('message'), 1)
c1.close()
c2.close()
assert.equal(c1.listenerCount('message'), 0)
assert.equal(c2.listenerCount('message'), 0)
})
})
describe('errors', function() {
it('throws an error if pubsub is not supported by given IPFS instance', async () => {
let c, err
try {
c = await Channel.open({}, id2)
} catch (e) {
err = e
}
assert.equal(err, 'Error: This IPFS node does not support pubsub.')
})
it('throws an error if receiver ID was not given', async () => {
let c, err
try {
c = await Channel.open(ipfs1)
} catch (e) {
err = e
}
assert.equal(err, 'Error: Receiver ID was undefined')
})
})
describe('non-participant peers can\'t send messages', function() {
it('doesn\'t receive unwanted messages', async () => {
const c1 = await Channel.open(ipfs1, id2)
const c2 = await Channel.open(ipfs2, id1)
await c1.connect()
await c2.connect()
c1.on('message', (m) => {
assert.equal(m.from, id2)
assert.equal(m.data.toString(), 'hello1')
assert.equal(m.topicIDs.length, 1)
assert.equal(m.topicIDs[0], c1.id)
assert.equal(m.topicIDs[0], c2.id)
})
await ipfs3.pubsub.subscribe(c1.id, () => {})
await waitForPeers(ipfs1, [id3], c1.id, c1._isClosed.bind(c1))
await ipfs3.pubsub.publish(c1.id, Buffer.from('OMG!'))
return new Promise((resolve, reject) => {
setTimeout(() => {
c2.send('hello1')
setTimeout(() => {
c1.close()
c2.close()
resolve()
}, 1000)
}, 1000)
})
})
})
})
})