@@ -15,6 +15,7 @@ const { Key } = require('interface-datastore')
15
15
const { Record } = require ( 'libp2p-record' )
16
16
17
17
const DatastorePubsub = require ( '../src' )
18
+ const { keyToTopic } = require ( '../src/utils' )
18
19
const { connect, waitFor, waitForPeerToSubscribe, spawnDaemon, stopDaemon } = require ( './utils' )
19
20
20
21
// Always returning the expected values
@@ -115,11 +116,12 @@ describe('datastore-pubsub', function () {
115
116
116
117
it ( 'should subscribe the topic, but receive error as no entry is stored locally' , function ( done ) {
117
118
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
119
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
118
120
119
121
pubsubA . ls ( ( err , res ) => {
120
122
expect ( err ) . to . not . exist ( )
121
123
expect ( res ) . to . exist ( )
122
- expect ( res ) . to . not . include ( `/ ${ keyRef } ` ) // not subscribed key reference yet
124
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed key reference yet
123
125
124
126
dsPubsubA . get ( key , ( err ) => {
125
127
expect ( err ) . to . exist ( ) // not locally stored record
@@ -128,7 +130,7 @@ describe('datastore-pubsub', function () {
128
130
pubsubA . ls ( ( err , res ) => {
129
131
expect ( err ) . to . not . exist ( )
130
132
expect ( res ) . to . exist ( )
131
- expect ( res ) . to . include ( `/ ${ keyRef } ` ) // subscribed key reference
133
+ expect ( res ) . to . include ( subsTopic ) // subscribed key reference
132
134
done ( )
133
135
} )
134
136
} )
@@ -138,11 +140,12 @@ describe('datastore-pubsub', function () {
138
140
it ( 'should put correctly to daemon A and daemon B should not receive it without subscribing' , function ( done ) {
139
141
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
140
142
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator )
143
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
141
144
142
145
pubsubB . ls ( ( err , res ) => {
143
146
expect ( err ) . to . not . exist ( )
144
147
expect ( res ) . to . exist ( )
145
- expect ( res ) . to . not . include ( `/ ${ keyRef } ` ) // not subscribed
148
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
146
149
147
150
dsPubsubA . put ( key , serializedRecord , ( err ) => {
148
151
expect ( err ) . to . not . exist ( )
@@ -201,7 +204,7 @@ describe('datastore-pubsub', function () {
201
204
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 ) {
202
205
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
203
206
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator )
204
- const topic = `/${ keyRef } `
207
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
205
208
let receivedMessage = false
206
209
207
210
function messageHandler ( ) {
@@ -211,16 +214,16 @@ describe('datastore-pubsub', function () {
211
214
pubsubB . ls ( ( err , res ) => {
212
215
expect ( err ) . to . not . exist ( )
213
216
expect ( res ) . to . exist ( )
214
- expect ( res ) . to . not . include ( topic ) // not subscribed
217
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
215
218
216
219
dsPubsubB . get ( key , ( err , res ) => {
217
220
expect ( err ) . to . exist ( )
218
221
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
219
222
220
223
series ( [
221
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
224
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
222
225
// subscribe in order to understand when the message arrive to the node
223
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
226
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
224
227
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
225
228
// wait until message arrives
226
229
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -301,7 +304,7 @@ describe('datastore-pubsub', function () {
301
304
}
302
305
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
303
306
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
304
- const topic = `/${ keyRef } `
307
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
305
308
let receivedMessage = false
306
309
307
310
function messageHandler ( ) {
@@ -313,9 +316,9 @@ describe('datastore-pubsub', function () {
313
316
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
314
317
315
318
series ( [
316
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
319
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
317
320
// subscribe in order to understand when the message arrive to the node
318
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
321
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
319
322
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
320
323
// wait until message arrives
321
324
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -346,7 +349,7 @@ describe('datastore-pubsub', function () {
346
349
347
350
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
348
351
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
349
- const topic = `/${ keyRef } `
352
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
350
353
let receivedMessage = false
351
354
352
355
function messageHandler ( ) {
@@ -358,9 +361,9 @@ describe('datastore-pubsub', function () {
358
361
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
359
362
360
363
series ( [
361
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
364
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
362
365
// subscribe in order to understand when the message arrive to the node
363
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
366
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
364
367
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
365
368
// wait until message arrives
366
369
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -397,7 +400,7 @@ describe('datastore-pubsub', function () {
397
400
398
401
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
399
402
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
400
- const topic = `/${ keyRef } `
403
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
401
404
let receivedMessage = false
402
405
403
406
function messageHandler ( ) {
@@ -409,9 +412,9 @@ describe('datastore-pubsub', function () {
409
412
expect ( res ) . to . not . exist ( ) // not value available, but it is subscribed now
410
413
411
414
series ( [
412
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
415
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
413
416
// subscribe in order to understand when the message arrive to the node
414
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
417
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
415
418
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
416
419
// wait until message arrives
417
420
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -432,14 +435,14 @@ describe('datastore-pubsub', function () {
432
435
} )
433
436
} )
434
437
435
- it ( 'should subscribe the topic and after a message being received, discarde it using the subscriptionKeyFn' , function ( done ) {
438
+ it ( 'should subscribe the topic and after a message being received, discard it using the subscriptionKeyFn' , function ( done ) {
436
439
const subscriptionKeyFn = ( topic , callback ) => {
437
- expect ( topic ) . to . equal ( key . toString ( ) )
440
+ expect ( topic ) . to . equal ( `/ ${ keyRef } ` )
438
441
callback ( new Error ( 'DISCARD MESSAGE' ) )
439
442
}
440
443
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
441
444
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator , subscriptionKeyFn )
442
- const topic = `/${ keyRef } `
445
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
443
446
let receivedMessage = false
444
447
445
448
function messageHandler ( ) {
@@ -449,16 +452,16 @@ describe('datastore-pubsub', function () {
449
452
pubsubB . ls ( ( err , res ) => {
450
453
expect ( err ) . to . not . exist ( )
451
454
expect ( res ) . to . exist ( )
452
- expect ( res ) . to . not . include ( topic ) // not subscribed
455
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
453
456
454
457
dsPubsubB . get ( key , ( err , res ) => {
455
458
expect ( err ) . to . exist ( )
456
459
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
457
460
458
461
series ( [
459
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
462
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
460
463
// subscribe in order to understand when the message arrive to the node
461
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
464
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
462
465
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
463
466
// wait until message arrives
464
467
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -479,7 +482,7 @@ describe('datastore-pubsub', function () {
479
482
}
480
483
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
481
484
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator , subscriptionKeyFn )
482
- const topic = `/${ keyRef } `
485
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
483
486
const keyNew = Buffer . from ( `${ key . toString ( ) } new` )
484
487
let receivedMessage = false
485
488
@@ -490,16 +493,16 @@ describe('datastore-pubsub', function () {
490
493
pubsubB . ls ( ( err , res ) => {
491
494
expect ( err ) . to . not . exist ( )
492
495
expect ( res ) . to . exist ( )
493
- expect ( res ) . to . not . include ( topic ) // not subscribed
496
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
494
497
495
498
dsPubsubB . get ( key , ( err , res ) => {
496
499
expect ( err ) . to . exist ( )
497
500
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
498
501
499
502
series ( [
500
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
503
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
501
504
// subscribe in order to understand when the message arrive to the node
502
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
505
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
503
506
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
504
507
// wait until message arrives
505
508
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
0 commit comments