-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.spec.js
515 lines (431 loc) · 16.2 KB
/
index.spec.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const sinon = require('sinon')
const isNode = require('detect-node')
const parallel = require('async/parallel')
const series = require('async/series')
const { Key } = require('interface-datastore')
const { Record } = require('libp2p-record')
const DatastorePubsub = require('../src')
const { connect, waitFor, waitForPeerToSubscribe, spawnDaemon, stopDaemon } = require('./utils')
// Always returning the expected values
// Valid record and select the new one
const smoothValidator = {
validate: (data, peerId, callback) => {
callback(null, true)
},
select: (receivedRecod, currentRecord, callback) => {
callback(null, 0)
}
}
describe('datastore-pubsub', function () {
this.timeout(60 * 1000)
if (!isNode) return
let ipfsdA = null
let ipfsdB = null
let ipfsdAId = null
let ipfsdBId = null
// spawn daemon
before(function (done) {
parallel([
(cb) => spawnDaemon(cb),
(cb) => spawnDaemon(cb)
], (err, daemons) => {
expect(err).to.not.exist()
ipfsdA = daemons[0]
ipfsdB = daemons[1]
parallel([
(cb) => ipfsdA.api.id(cb),
(cb) => ipfsdB.api.id(cb)
], (err, ids) => {
expect(err).to.not.exist()
ipfsdAId = ids[0]
ipfsdBId = ids[1]
connect(ipfsdA, ipfsdAId, ipfsdB, ipfsdBId, done)
})
})
})
let pubsubA = null
let datastoreA = null
let peerIdA = null
let privKeyA = null
let pubKeyA = null
let datastoreB = null
let peerIdB = null
let pubsubB = null
// create DatastorePubsub instances
before(function (done) {
pubsubA = ipfsdA.api._libp2pNode.pubsub
datastoreA = ipfsdA.api._repo.datastore
peerIdA = ipfsdA.api._peerInfo.id
privKeyA = peerIdA.privKey
pubKeyA = peerIdA.pubKey
pubsubB = ipfsdB.api._libp2pNode.pubsub
datastoreB = ipfsdB.api._repo.datastore
peerIdB = ipfsdB.api._peerInfo.id
done()
})
const value = 'value'
let testCounter = 0
let keyRef = null
let key = null
let record = null
let serializedRecord = null
// prepare Record
beforeEach(function (done) {
keyRef = `key${testCounter}`
key = (new Key(keyRef)).toBuffer()
record = new Record(key, Buffer.from(value), peerIdA)
// sign and serialize record
record.serializeSigned(privKeyA, (err, serialized) => {
expect(err).to.not.exist()
serializedRecord = serialized
done()
})
})
afterEach(function (done) {
++testCounter
done()
})
after(function (done) {
parallel([
(cb) => stopDaemon(ipfsdA, cb),
(cb) => stopDaemon(ipfsdB, cb)
], done)
})
it('should subscribe the topic, but receive error as no entry is stored locally', function (done) {
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
pubsubA.ls((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.not.include(`/${keyRef}`) // not subscribed key reference yet
dsPubsubA.get(key, (err) => {
expect(err).to.exist() // not locally stored record
pubsubA.ls((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.include(`/${keyRef}`) // subscribed key reference
done()
})
})
})
})
it('should put correctly to daemon A and daemon B should not receive it without subscribing', function (done) {
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, smoothValidator)
pubsubB.ls((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.not.include(`/${keyRef}`) // not subscribed
dsPubsubA.put(key, serializedRecord, (err) => {
expect(err).to.not.exist()
dsPubsubB.get(key, (err, res) => {
expect(err).to.exist() // did not receive record
expect(res).to.not.exist()
done()
})
})
})
})
it('should put correctly to daemon A and daemon B should receive it as it tried to get it first and subscribed it', function (done) {
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, smoothValidator)
const topic = `/${keyRef}`
let receivedMessage = false
function messageHandler () {
receivedMessage = true
}
pubsubB.ls((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.not.include(topic) // not subscribed
dsPubsubB.get(key, (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist() // not value available, but subscribed now
series([
(cb) => waitForPeerToSubscribe(topic, ipfsdBId, ipfsdA, cb),
// subscribe in order to understand when the message arrive to the node
(cb) => pubsubB.subscribe(topic, messageHandler, cb),
(cb) => dsPubsubA.put(key, serializedRecord, cb),
// wait until message arrives
(cb) => waitFor(() => receivedMessage === true, cb),
// get from datastore
(cb) => dsPubsubB.get(key, cb)
], (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res[4]).to.exist()
const receivedRecord = Record.deserialize(res[4])
expect(receivedRecord.value.toString()).to.equal(value)
receivedRecord.verifySignature(pubKeyA, (err) => {
expect(err).to.not.exist()
done()
})
})
})
})
})
it('should fail to create the DatastorePubsub if no validator is provided', function (done) {
let dsPubsubB
try {
dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB) // no validator
} catch (err) {
expect(err).to.exist()
}
expect(dsPubsubB).to.equal(undefined)
done()
})
it('should fail to create the DatastorePubsub if no validate function is provided', function (done) {
const customValidator = {
validate: undefined,
select: (receivedRecod, currentRecord, callback) => {
callback(null, 0)
}
}
let dsPubsubB
try {
dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, customValidator)
} catch (err) {
expect(err).to.exist()
}
expect(dsPubsubB).to.equal(undefined)
done()
})
it('should fail to create the DatastorePubsub if no select function is provided', function (done) {
const customValidator = {
validate: (data, peerId, callback) => {
callback(null, true)
},
select: undefined
}
let dsPubsubB
try {
dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, customValidator)
} catch (err) {
expect(err).to.exist()
}
expect(dsPubsubB).to.equal(undefined)
done()
})
it('should fail if it fails to validate the record', function (done) {
const customValidator = {
validate: (data, peerId, callback) => {
callback(null, false) // return false validation
},
select: (receivedRecod, currentRecord, callback) => {
callback(null, 0)
}
}
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, customValidator)
const topic = `/${keyRef}`
let receivedMessage = false
function messageHandler () {
receivedMessage = true
}
dsPubsubB.get(key, (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist() // not value available, but subscribed now
series([
(cb) => waitForPeerToSubscribe(topic, ipfsdBId, ipfsdA, cb),
// subscribe in order to understand when the message arrive to the node
(cb) => pubsubB.subscribe(topic, messageHandler, cb),
(cb) => dsPubsubA.put(key, serializedRecord, cb),
// wait until message arrives
(cb) => waitFor(() => receivedMessage === true, cb),
// get from datastore
(cb) => dsPubsubB.get(key, cb)
], (err, res) => {
// No record received, in spite of message received
expect(err).to.exist() // message was discarded as a result of failing the validation
expect(res[4]).to.not.exist()
done()
})
})
})
it('should get the second record if the selector selects it as the newest one', function (done) {
const customValidator = {
validate: (data, peerId, callback) => {
callback(null, true)
},
select: (receivedRecod, currentRecord, callback) => {
callback(null, 1) // current record is the newer
}
}
const newValue = 'new value'
const record = new Record(key, Buffer.from(newValue), peerIdA)
let newSerializedRecord
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, customValidator)
const topic = `/${keyRef}`
let receivedMessage = false
function messageHandler () {
receivedMessage = true
}
record.serializeSigned(privKeyA, (err, serialized) => {
expect(err).to.not.exist()
newSerializedRecord = serialized
dsPubsubB.get(key, (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist() // not value available, but subscribed now
series([
(cb) => waitForPeerToSubscribe(topic, ipfsdBId, ipfsdA, cb),
// subscribe in order to understand when the message arrive to the node
(cb) => pubsubB.subscribe(topic, messageHandler, cb),
(cb) => dsPubsubA.put(key, serializedRecord, cb),
// wait until message arrives
(cb) => waitFor(() => receivedMessage === true, cb),
(cb) => dsPubsubA.put(key, newSerializedRecord, cb), // put new serializedRecord
// wait until message arrives
(cb) => waitFor(() => receivedMessage === true, cb),
// get from datastore
(cb) => dsPubsubB.get(key, cb)
], (err, res) => {
expect(err).to.not.exist() // message was discarded as a result of no validator available
expect(res[6]).to.exist()
const receivedRecord = Record.deserialize(res[6])
expect(receivedRecord.value.toString()).to.not.equal(newValue) // not equal to the last value
done()
})
})
})
})
it('should get the new record if the selector selects it as the newest one', function (done) {
const customValidator = {
validate: (data, peerId, callback) => {
callback(null, true)
},
select: (receivedRecod, currentRecord, callback) => {
callback(null, 0) // received record is the newer
}
}
const newValue = 'new value'
const record = new Record(key, Buffer.from(newValue), peerIdA)
let newSerializedRecord
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, customValidator)
const topic = `/${keyRef}`
let receivedMessage = false
function messageHandler () {
receivedMessage = true
}
record.serializeSigned(privKeyA, (err, serialized) => {
expect(err).to.not.exist()
newSerializedRecord = serialized
dsPubsubB.get(key, (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist() // not value available, but it is subscribed now
series([
(cb) => waitForPeerToSubscribe(topic, ipfsdBId, ipfsdA, cb),
// subscribe in order to understand when the message arrive to the node
(cb) => pubsubB.subscribe(topic, messageHandler, cb),
(cb) => dsPubsubA.put(key, serializedRecord, cb),
// wait until message arrives
(cb) => waitFor(() => receivedMessage === true, cb),
(cb) => dsPubsubA.put(key, newSerializedRecord, cb), // put new serializedRecord
// wait until message arrives
(cb) => waitFor(() => receivedMessage === true, cb),
// get from datastore
(cb) => dsPubsubB.get(key, cb)
], (err, res) => {
expect(err).to.not.exist() // message was discarded as a result of no validator available
expect(res[6]).to.exist()
const receivedRecord = Record.deserialize(res[6])
expect(receivedRecord.value.toString()).to.equal(newValue) // equal to the last value
done()
})
})
})
})
it('should subscribe the topic and after a message being received, discarde it using the subscriptionKeyFn', function (done) {
const subscriptionKeyFn = (topic, callback) => {
expect(topic).to.equal(key.toString())
callback(new Error('DISCARD MESSAGE'))
}
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, smoothValidator, subscriptionKeyFn)
const topic = `/${keyRef}`
let receivedMessage = false
function messageHandler () {
receivedMessage = true
}
pubsubB.ls((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.not.include(topic) // not subscribed
dsPubsubB.get(key, (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist() // not value available, but subscribed now
series([
(cb) => waitForPeerToSubscribe(topic, ipfsdBId, ipfsdA, cb),
// subscribe in order to understand when the message arrive to the node
(cb) => pubsubB.subscribe(topic, messageHandler, cb),
(cb) => dsPubsubA.put(key, serializedRecord, cb),
// wait until message arrives
(cb) => waitFor(() => receivedMessage === true, cb),
// get from datastore
(cb) => dsPubsubB.get(key, cb)
], (err) => {
expect(err).to.exist() // As message was discarded, it was not stored in the datastore
done()
})
})
})
})
it('should subscribe the topic and after a message being received, change its key using subscriptionKeyFn', function (done) {
const subscriptionKeyFn = (topic, callback) => {
expect(topic).to.equal(key.toString())
callback(null, `${topic}new`)
}
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, smoothValidator, subscriptionKeyFn)
const topic = `/${keyRef}`
const keyNew = Buffer.from(`${key.toString()}new`)
let receivedMessage = false
function messageHandler () {
receivedMessage = true
}
pubsubB.ls((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.not.include(topic) // not subscribed
dsPubsubB.get(key, (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist() // not value available, but subscribed now
series([
(cb) => waitForPeerToSubscribe(topic, ipfsdBId, ipfsdA, cb),
// subscribe in order to understand when the message arrive to the node
(cb) => pubsubB.subscribe(topic, messageHandler, cb),
(cb) => dsPubsubA.put(key, serializedRecord, cb),
// wait until message arrives
(cb) => waitFor(() => receivedMessage === true, cb),
// get from datastore
(cb) => dsPubsubB.get(keyNew, cb)
], (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res[4]).to.exist()
const receivedRecord = Record.deserialize(res[4])
expect(receivedRecord.value.toString()).to.equal(value)
done()
})
})
})
})
it('should subscribe a topic only once', function (done) {
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
sinon.spy(pubsubA, 'subscribe')
dsPubsubA.get(key, (err) => {
expect(err).to.exist() // not locally stored record
dsPubsubA.get(key, (err) => {
expect(err).to.exist() // not locally stored record
expect(pubsubA.subscribe.calledOnce).to.equal(true)
done()
})
})
})
})