@@ -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 ( )
@@ -169,7 +172,7 @@ describe('datastore-pubsub', function () {
169
172
}
170
173
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
171
174
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
172
- const topic = `/${ keyRef } `
175
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
173
176
let receivedMessage = false
174
177
175
178
function messageHandler ( ) {
@@ -181,9 +184,9 @@ describe('datastore-pubsub', function () {
181
184
expect ( res ) . to . not . exist ( ) // no value available, but subscribed now
182
185
183
186
series ( [
184
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
187
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
185
188
// subscribe in order to understand when the message arrive to the node
186
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
189
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
187
190
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
188
191
// wait until message arrives
189
192
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -200,7 +203,7 @@ describe('datastore-pubsub', function () {
200
203
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 ) {
201
204
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
202
205
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator )
203
- const topic = `/${ keyRef } `
206
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
204
207
let receivedMessage = false
205
208
206
209
function messageHandler ( ) {
@@ -210,16 +213,16 @@ describe('datastore-pubsub', function () {
210
213
pubsubB . ls ( ( err , res ) => {
211
214
expect ( err ) . to . not . exist ( )
212
215
expect ( res ) . to . exist ( )
213
- expect ( res ) . to . not . include ( topic ) // not subscribed
216
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
214
217
215
218
dsPubsubB . get ( key , ( err , res ) => {
216
219
expect ( err ) . to . exist ( )
217
220
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
218
221
219
222
series ( [
220
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
223
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
221
224
// subscribe in order to understand when the message arrive to the node
222
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
225
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
223
226
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
224
227
// wait until message arrives
225
228
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -300,7 +303,7 @@ describe('datastore-pubsub', function () {
300
303
}
301
304
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
302
305
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
303
- const topic = `/${ keyRef } `
306
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
304
307
let receivedMessage = false
305
308
306
309
function messageHandler ( ) {
@@ -312,9 +315,9 @@ describe('datastore-pubsub', function () {
312
315
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
313
316
314
317
series ( [
315
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
318
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
316
319
// subscribe in order to understand when the message arrive to the node
317
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
320
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
318
321
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
319
322
// wait until message arrives
320
323
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -345,7 +348,7 @@ describe('datastore-pubsub', function () {
345
348
346
349
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
347
350
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
348
- const topic = `/${ keyRef } `
351
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
349
352
let receivedMessage = false
350
353
351
354
function messageHandler ( ) {
@@ -357,9 +360,9 @@ describe('datastore-pubsub', function () {
357
360
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
358
361
359
362
series ( [
360
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
363
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
361
364
// subscribe in order to understand when the message arrive to the node
362
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
365
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
363
366
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
364
367
// wait until message arrives
365
368
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -396,7 +399,7 @@ describe('datastore-pubsub', function () {
396
399
397
400
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
398
401
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
399
- const topic = `/${ keyRef } `
402
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
400
403
let receivedMessage = false
401
404
402
405
function messageHandler ( ) {
@@ -408,9 +411,9 @@ describe('datastore-pubsub', function () {
408
411
expect ( res ) . to . not . exist ( ) // not value available, but it is subscribed now
409
412
410
413
series ( [
411
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
414
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
412
415
// subscribe in order to understand when the message arrive to the node
413
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
416
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
414
417
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
415
418
// wait until message arrives
416
419
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -431,14 +434,14 @@ describe('datastore-pubsub', function () {
431
434
} )
432
435
} )
433
436
434
- it ( 'should subscribe the topic and after a message being received, discarde it using the subscriptionKeyFn' , function ( done ) {
437
+ it ( 'should subscribe the topic and after a message being received, discard it using the subscriptionKeyFn' , function ( done ) {
435
438
const subscriptionKeyFn = ( topic , callback ) => {
436
- expect ( topic ) . to . equal ( key . toString ( ) )
439
+ expect ( topic ) . to . equal ( `/ ${ keyRef } ` )
437
440
callback ( new Error ( 'DISCARD MESSAGE' ) )
438
441
}
439
442
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
440
443
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator , subscriptionKeyFn )
441
- const topic = `/${ keyRef } `
444
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
442
445
let receivedMessage = false
443
446
444
447
function messageHandler ( ) {
@@ -448,16 +451,16 @@ describe('datastore-pubsub', function () {
448
451
pubsubB . ls ( ( err , res ) => {
449
452
expect ( err ) . to . not . exist ( )
450
453
expect ( res ) . to . exist ( )
451
- expect ( res ) . to . not . include ( topic ) // not subscribed
454
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
452
455
453
456
dsPubsubB . get ( key , ( err , res ) => {
454
457
expect ( err ) . to . exist ( )
455
458
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
456
459
457
460
series ( [
458
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
461
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
459
462
// subscribe in order to understand when the message arrive to the node
460
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
463
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
461
464
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
462
465
// wait until message arrives
463
466
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -478,7 +481,7 @@ describe('datastore-pubsub', function () {
478
481
}
479
482
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
480
483
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator , subscriptionKeyFn )
481
- const topic = `/${ keyRef } `
484
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
482
485
const keyNew = Buffer . from ( `${ key . toString ( ) } new` )
483
486
let receivedMessage = false
484
487
@@ -489,16 +492,16 @@ describe('datastore-pubsub', function () {
489
492
pubsubB . ls ( ( err , res ) => {
490
493
expect ( err ) . to . not . exist ( )
491
494
expect ( res ) . to . exist ( )
492
- expect ( res ) . to . not . include ( topic ) // not subscribed
495
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
493
496
494
497
dsPubsubB . get ( key , ( err , res ) => {
495
498
expect ( err ) . to . exist ( )
496
499
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
497
500
498
501
series ( [
499
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
502
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
500
503
// subscribe in order to understand when the message arrive to the node
501
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
504
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
502
505
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
503
506
// wait until message arrives
504
507
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
0 commit comments