@@ -9,13 +9,20 @@ const sinon = require('sinon')
9
9
const errcode = require ( 'err-code' )
10
10
const isNode = require ( 'detect-node' )
11
11
12
- const { Key } = require ( 'interface-datastore' )
13
- const { Record } = require ( 'libp2p-record' )
14
-
15
12
const DatastorePubsub = require ( '../src' )
13
+
14
+ const {
15
+ Key,
16
+ MemoryDatastore
17
+ } = require ( 'interface-datastore' )
18
+ const {
19
+ createPubsubNode,
20
+ connectPubsubNodes,
21
+ waitFor,
22
+ waitForPeerToSubscribe
23
+ } = require ( './utils' )
24
+ const { Record } = require ( 'libp2p-record' )
16
25
const { keyToTopic, topicToKey } = require ( '../src/utils' )
17
- const { connect, waitFor, waitForPeerToSubscribe, spawnDaemon, stopDaemon } = require ( './utils' )
18
- const promisify = require ( 'promisify-es6' )
19
26
20
27
// Always returning the expected values
21
28
// Valid record and select the new one
@@ -33,46 +40,37 @@ describe('datastore-pubsub', function () {
33
40
34
41
if ( ! isNode ) return
35
42
36
- let ipfsdA = null
37
- let ipfsdB = null
38
- let ipfsdAId = null
39
- let ipfsdBId = null
40
43
let pubsubA = null
41
44
let datastoreA = null
42
45
let peerIdA = null
46
+ const registrarRecordA = { }
43
47
48
+ let pubsubB = null
44
49
let datastoreB = null
45
50
let peerIdB = null
46
- let pubsubB = null
47
-
48
- // spawn daemon and create DatastorePubsub instances
49
- before ( async function ( ) {
50
- [ ipfsdA , ipfsdB ] = await Promise . all ( [ spawnDaemon ( ) , spawnDaemon ( ) ] ) ;
51
- [ ipfsdAId , ipfsdBId ] = await Promise . all ( [ ipfsdA . api . id ( ) , ipfsdB . api . id ( ) ] )
51
+ const registrarRecordB = { }
52
52
53
- await connect ( ipfsdA , ipfsdAId , ipfsdB , ipfsdBId )
53
+ // Mount pubsub protocol and create datastore instances
54
+ before ( async ( ) => {
55
+ [ pubsubA , pubsubB ] = await Promise . all ( [
56
+ createPubsubNode ( registrarRecordA ) ,
57
+ createPubsubNode ( registrarRecordB )
58
+ ] )
59
+ peerIdA = pubsubA . peerInfo . id
60
+ peerIdB = pubsubB . peerInfo . id
54
61
55
- pubsubA = ipfsdA . api . pubsub
56
- datastoreA = {
57
- get : promisify ( ipfsdA . api . _repo . datastore . get , {
58
- context : ipfsdA . api . _repo . datastore
59
- } ) ,
60
- put : promisify ( ipfsdA . api . _repo . datastore . put , {
61
- context : ipfsdA . api . _repo . datastore
62
- } )
63
- }
64
- peerIdA = ipfsdA . api . _peerInfo . id
65
-
66
- pubsubB = ipfsdB . api . pubsub
67
- datastoreB = {
68
- get : promisify ( ipfsdB . api . _repo . datastore . get , {
69
- context : ipfsdB . api . _repo . datastore
70
- } ) ,
71
- put : promisify ( ipfsdB . api . _repo . datastore . put , {
72
- context : ipfsdB . api . _repo . datastore
62
+ await connectPubsubNodes (
63
+ {
64
+ router : pubsubA ,
65
+ registrar : registrarRecordA
66
+ } ,
67
+ {
68
+ router : pubsubB ,
69
+ registrar : registrarRecordB
73
70
} )
74
- }
75
- peerIdB = ipfsdB . api . _peerInfo . id
71
+
72
+ datastoreA = new MemoryDatastore ( )
73
+ datastoreB = new MemoryDatastore ( )
76
74
} )
77
75
78
76
const value = 'value'
@@ -97,16 +95,16 @@ describe('datastore-pubsub', function () {
97
95
98
96
after ( ( ) => {
99
97
return Promise . all ( [
100
- stopDaemon ( ipfsdA ) ,
101
- stopDaemon ( ipfsdB )
98
+ pubsubA . stop ( ) ,
99
+ pubsubB . stop ( )
102
100
] )
103
101
} )
104
102
105
103
it ( 'should subscribe the topic, but receive error as no entry is stored locally' , async ( ) => {
106
104
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
107
105
const subsTopic = keyToTopic ( `/${ keyRef } ` )
108
106
109
- let subscribers = await pubsubA . ls ( )
107
+ let subscribers = await pubsubA . getTopics ( )
110
108
111
109
expect ( subscribers ) . to . exist ( )
112
110
expect ( subscribers ) . to . not . include ( subsTopic ) // not subscribed key reference yet
@@ -119,18 +117,18 @@ describe('datastore-pubsub', function () {
119
117
120
118
expect ( res ) . to . not . exist ( )
121
119
122
- subscribers = await pubsubA . ls ( )
120
+ subscribers = await pubsubA . getTopics ( )
123
121
124
122
expect ( subscribers ) . to . exist ( )
125
123
expect ( subscribers ) . to . include ( subsTopic ) // subscribed key reference
126
124
} )
127
125
128
- it ( 'should put correctly to daemon A and daemon B should not receive it without subscribing' , async ( ) => {
126
+ it ( 'should put correctly to node A and node B should not receive it without subscribing' , async ( ) => {
129
127
const dsPubsubA = new DatastorePubsub ( pubsubA , datastoreA , peerIdA , smoothValidator )
130
128
const dsPubsubB = new DatastorePubsub ( pubsubB , datastoreB , peerIdB , smoothValidator )
131
129
const subsTopic = keyToTopic ( `/${ keyRef } ` )
132
130
133
- const res = await pubsubB . ls ( )
131
+ const res = await pubsubB . getTopics ( )
134
132
expect ( res ) . to . exist ( )
135
133
expect ( res ) . to . not . include ( subsTopic ) // not subscribed
136
134
@@ -172,7 +170,7 @@ describe('datastore-pubsub', function () {
172
170
expect ( err . code ) . to . equal ( 'ERR_NOT_FOUND' )
173
171
} )
174
172
175
- await waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA )
173
+ await waitForPeerToSubscribe ( subsTopic , peerIdB , pubsubA )
176
174
177
175
// subscribe in order to understand when the message arrive to the node
178
176
await pubsubB . subscribe ( subsTopic , messageHandler )
@@ -198,7 +196,7 @@ describe('datastore-pubsub', function () {
198
196
receivedMessage = true
199
197
}
200
198
201
- const res = await pubsubB . ls ( )
199
+ const res = await pubsubB . getTopics ( )
202
200
expect ( res ) . to . exist ( )
203
201
expect ( res ) . to . not . include ( subsTopic ) // not subscribed
204
202
@@ -209,7 +207,7 @@ describe('datastore-pubsub', function () {
209
207
expect ( err . code ) . to . equal ( 'ERR_NOT_FOUND' )
210
208
} )
211
209
212
- await waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA )
210
+ await waitForPeerToSubscribe ( subsTopic , peerIdB , pubsubA )
213
211
214
212
// subscribe in order to understand when the message arrive to the node
215
213
await pubsubB . subscribe ( subsTopic , messageHandler )
@@ -273,7 +271,7 @@ describe('datastore-pubsub', function () {
273
271
expect ( dsPubsubB ) . to . equal ( undefined )
274
272
} )
275
273
276
- it ( 'should fail if it fails to validate the record' , async ( ) => {
274
+ it ( 'should fail if it fails getTopics to validate the record' , async ( ) => {
277
275
const customValidator = {
278
276
validate : ( ) => {
279
277
return false // return false validation
@@ -298,7 +296,7 @@ describe('datastore-pubsub', function () {
298
296
expect ( err . code ) . to . equal ( 'ERR_NOT_FOUND' )
299
297
} )
300
298
301
- await waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA )
299
+ await waitForPeerToSubscribe ( subsTopic , peerIdB , pubsubA )
302
300
303
301
// subscribe in order to understand when the message arrive to the node
304
302
await pubsubB . subscribe ( subsTopic , messageHandler )
@@ -347,7 +345,7 @@ describe('datastore-pubsub', function () {
347
345
expect ( err . code ) . to . equal ( 'ERR_NOT_FOUND' )
348
346
} )
349
347
350
- await waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA )
348
+ await waitForPeerToSubscribe ( subsTopic , peerIdB , pubsubA )
351
349
352
350
// subscribe in order to understand when the message arrive to the node
353
351
await pubsubB . subscribe ( subsTopic , messageHandler )
@@ -397,7 +395,7 @@ describe('datastore-pubsub', function () {
397
395
expect ( err . code ) . to . equal ( 'ERR_NOT_FOUND' )
398
396
} )
399
397
400
- await waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA )
398
+ await waitForPeerToSubscribe ( subsTopic , peerIdB , pubsubA )
401
399
402
400
// subscribe in order to understand when the message arrive to the node
403
401
await pubsubB . subscribe ( subsTopic , messageHandler )
@@ -439,7 +437,7 @@ describe('datastore-pubsub', function () {
439
437
receivedMessage = true
440
438
}
441
439
442
- const res = await pubsubB . ls ( )
440
+ const res = await pubsubB . getTopics ( )
443
441
expect ( res ) . to . not . include ( subsTopic ) // not subscribed
444
442
445
443
// causes pubsub b to become subscribed to the topic
@@ -449,7 +447,7 @@ describe('datastore-pubsub', function () {
449
447
expect ( err . code ) . to . equal ( 'ERR_NOT_FOUND' )
450
448
} )
451
449
452
- await waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA )
450
+ await waitForPeerToSubscribe ( subsTopic , peerIdB , pubsubA )
453
451
454
452
// subscribe in order to understand when the message arrive to the node
455
453
await pubsubB . subscribe ( subsTopic , messageHandler )
@@ -483,7 +481,7 @@ describe('datastore-pubsub', function () {
483
481
receivedMessage = true
484
482
}
485
483
486
- const res = await pubsubB . ls ( )
484
+ const res = await pubsubB . getTopics ( )
487
485
expect ( res ) . to . not . include ( subsTopic ) // not subscribed
488
486
489
487
// causes pubsub b to become subscribed to the topic
@@ -493,7 +491,7 @@ describe('datastore-pubsub', function () {
493
491
expect ( err . code ) . to . equal ( 'ERR_NOT_FOUND' )
494
492
} )
495
493
496
- await waitForPeerToSubscribe ( subsTopic , ipfsdBId , ipfsdA )
494
+ await waitForPeerToSubscribe ( subsTopic , peerIdB , pubsubA )
497
495
498
496
// subscribe in order to understand when the message arrive to the node
499
497
await pubsubB . subscribe ( subsTopic , messageHandler )
0 commit comments