@@ -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,19 +116,20 @@ 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
126
128
127
129
pubsubA . ls ( ( err , res ) => {
128
130
expect ( err ) . to . not . exist ( )
129
131
expect ( res ) . to . exist ( )
130
- expect ( res ) . to . include ( `/ ${ keyRef } ` ) // subscribed key reference
132
+ expect ( res ) . to . include ( subsTopic ) // subscribed key reference
131
133
done ( )
132
134
} )
133
135
} )
@@ -137,11 +139,12 @@ describe('datastore-pubsub', function () {
137
139
it ( 'should put correctly to daemon A and daemon B should not receive it without subscribing' , function ( done ) {
138
140
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
139
141
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator )
142
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
140
143
141
144
pubsubB . ls ( ( err , res ) => {
142
145
expect ( err ) . to . not . exist ( )
143
146
expect ( res ) . to . exist ( )
144
- expect ( res ) . to . not . include ( `/ ${ keyRef } ` ) // not subscribed
147
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
145
148
146
149
dsPubsubA . put ( key , serializedRecord , ( err ) => {
147
150
expect ( err ) . to . not . exist ( )
@@ -157,7 +160,7 @@ describe('datastore-pubsub', function () {
157
160
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 ) {
158
161
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
159
162
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator )
160
- const topic = `/${ keyRef } `
163
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
161
164
let receivedMessage = false
162
165
163
166
function messageHandler ( ) {
@@ -167,16 +170,16 @@ describe('datastore-pubsub', function () {
167
170
pubsubB . ls ( ( err , res ) => {
168
171
expect ( err ) . to . not . exist ( )
169
172
expect ( res ) . to . exist ( )
170
- expect ( res ) . to . not . include ( topic ) // not subscribed
173
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
171
174
172
175
dsPubsubB . get ( key , ( err , res ) => {
173
176
expect ( err ) . to . exist ( )
174
177
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
175
178
176
179
series ( [
177
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
180
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
178
181
// subscribe in order to understand when the message arrive to the node
179
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
182
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
180
183
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
181
184
// wait until message arrives
182
185
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -257,7 +260,7 @@ describe('datastore-pubsub', function () {
257
260
}
258
261
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
259
262
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
260
- const topic = `/${ keyRef } `
263
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
261
264
let receivedMessage = false
262
265
263
266
function messageHandler ( ) {
@@ -269,9 +272,9 @@ describe('datastore-pubsub', function () {
269
272
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
270
273
271
274
series ( [
272
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
275
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
273
276
// subscribe in order to understand when the message arrive to the node
274
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
277
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
275
278
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
276
279
// wait until message arrives
277
280
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -302,7 +305,7 @@ describe('datastore-pubsub', function () {
302
305
303
306
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
304
307
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
305
- const topic = `/${ keyRef } `
308
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
306
309
let receivedMessage = false
307
310
308
311
function messageHandler ( ) {
@@ -314,9 +317,9 @@ describe('datastore-pubsub', function () {
314
317
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
315
318
316
319
series ( [
317
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
320
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
318
321
// subscribe in order to understand when the message arrive to the node
319
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
322
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
320
323
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
321
324
// wait until message arrives
322
325
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -353,7 +356,7 @@ describe('datastore-pubsub', function () {
353
356
354
357
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
355
358
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , customValidator )
356
- const topic = `/${ keyRef } `
359
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
357
360
let receivedMessage = false
358
361
359
362
function messageHandler ( ) {
@@ -365,9 +368,9 @@ describe('datastore-pubsub', function () {
365
368
expect ( res ) . to . not . exist ( ) // not value available, but it is subscribed now
366
369
367
370
series ( [
368
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
371
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
369
372
// subscribe in order to understand when the message arrive to the node
370
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
373
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
371
374
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
372
375
// wait until message arrives
373
376
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -388,14 +391,14 @@ describe('datastore-pubsub', function () {
388
391
} )
389
392
} )
390
393
391
- it ( 'should subscribe the topic and after a message being received, discarde it using the subscriptionKeyFn' , function ( done ) {
394
+ it ( 'should subscribe the topic and after a message being received, discard it using the subscriptionKeyFn' , function ( done ) {
392
395
const subscriptionKeyFn = ( topic , callback ) => {
393
- expect ( topic ) . to . equal ( key . toString ( ) )
396
+ expect ( topic ) . to . equal ( `/ ${ keyRef } ` )
394
397
callback ( new Error ( 'DISCARD MESSAGE' ) )
395
398
}
396
399
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
397
400
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator , subscriptionKeyFn )
398
- const topic = `/${ keyRef } `
401
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
399
402
let receivedMessage = false
400
403
401
404
function messageHandler ( ) {
@@ -405,16 +408,16 @@ describe('datastore-pubsub', function () {
405
408
pubsubB . ls ( ( err , res ) => {
406
409
expect ( err ) . to . not . exist ( )
407
410
expect ( res ) . to . exist ( )
408
- expect ( res ) . to . not . include ( topic ) // not subscribed
411
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
409
412
410
413
dsPubsubB . get ( key , ( err , res ) => {
411
414
expect ( err ) . to . exist ( )
412
415
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
413
416
414
417
series ( [
415
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
418
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
416
419
// subscribe in order to understand when the message arrive to the node
417
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
420
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
418
421
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
419
422
// wait until message arrives
420
423
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
@@ -435,7 +438,7 @@ describe('datastore-pubsub', function () {
435
438
}
436
439
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
437
440
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator , subscriptionKeyFn )
438
- const topic = `/${ keyRef } `
441
+ const subsTopic = keyToTopic ( `/${ keyRef } ` )
439
442
const keyNew = Buffer . from ( `${ key . toString ( ) } new` )
440
443
let receivedMessage = false
441
444
@@ -446,16 +449,16 @@ describe('datastore-pubsub', function () {
446
449
pubsubB . ls ( ( err , res ) => {
447
450
expect ( err ) . to . not . exist ( )
448
451
expect ( res ) . to . exist ( )
449
- expect ( res ) . to . not . include ( topic ) // not subscribed
452
+ expect ( res ) . to . not . include ( subsTopic ) // not subscribed
450
453
451
454
dsPubsubB . get ( key , ( err , res ) => {
452
455
expect ( err ) . to . exist ( )
453
456
expect ( res ) . to . not . exist ( ) // not value available, but subscribed now
454
457
455
458
series ( [
456
- ( cb ) => waitForPeerToSubscribe ( topic , ipfsdBId , ipfsdA , cb ) ,
459
+ ( cb ) => waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA , cb ) ,
457
460
// subscribe in order to understand when the message arrive to the node
458
- ( cb ) => pubsubB . subscribe ( topic , messageHandler , cb ) ,
461
+ ( cb ) => pubsubB . subscribe ( subsTopic , messageHandler , cb ) ,
459
462
( cb ) => dsPubsubA . put ( key , serializedRecord , cb ) ,
460
463
// wait until message arrives
461
464
( cb ) => waitFor ( ( ) => receivedMessage === true , cb ) ,
0 commit comments