Skip to content

Commit fe751f8

Browse files
authored
chore: update dependencies and tests (#19)
1 parent e01664e commit fe751f8

File tree

4 files changed

+145
-123
lines changed

4 files changed

+145
-123
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@
3434
"dependencies": {
3535
"debug": "^4.1.1",
3636
"err-code": "^2.0.0",
37-
"interface-datastore": "~0.7.0",
37+
"interface-datastore": "~0.8.0",
3838
"multibase": "~0.6.0"
3939
},
4040
"devDependencies": {
41-
"aegir": "^20.0.0",
41+
"aegir": "^20.5.0",
4242
"chai": "^4.2.0",
43-
"delay": "^4.3.0",
4443
"detect-node": "^2.0.4",
4544
"dirty-chai": "^2.0.1",
46-
"ipfs": "~0.37.0",
47-
"ipfs-http-client": "^33.0.0",
48-
"ipfsd-ctl": "^0.47.2",
45+
"it-pair": "^1.0.0",
46+
"libp2p-gossipsub": "0.2.0",
4947
"libp2p-record": "~0.7.0",
50-
"promisify-es6": "^1.0.3",
48+
"p-wait-for": "^3.1.0",
49+
"peer-id": "^0.13.6",
50+
"peer-info": "^0.16.2",
5151
"sinon": "^7.4.1"
5252
},
5353
"contributors": [

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class DatastorePubsub {
9292
}
9393

9494
const stringifiedTopic = keyToTopic(key)
95-
const subscriptions = await this._pubsub.ls()
95+
const subscriptions = await this._pubsub.getTopics()
9696

9797
// If already subscribed, just try to get it
9898
if (subscriptions && Array.isArray(subscriptions) && subscriptions.indexOf(stringifiedTopic) > -1) {

test/index.spec.js

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ const sinon = require('sinon')
99
const errcode = require('err-code')
1010
const isNode = require('detect-node')
1111

12-
const { Key } = require('interface-datastore')
13-
const { Record } = require('libp2p-record')
14-
1512
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')
1625
const { keyToTopic, topicToKey } = require('../src/utils')
17-
const { connect, waitFor, waitForPeerToSubscribe, spawnDaemon, stopDaemon } = require('./utils')
18-
const promisify = require('promisify-es6')
1926

2027
// Always returning the expected values
2128
// Valid record and select the new one
@@ -33,46 +40,37 @@ describe('datastore-pubsub', function () {
3340

3441
if (!isNode) return
3542

36-
let ipfsdA = null
37-
let ipfsdB = null
38-
let ipfsdAId = null
39-
let ipfsdBId = null
4043
let pubsubA = null
4144
let datastoreA = null
4245
let peerIdA = null
46+
const registrarRecordA = {}
4347

48+
let pubsubB = null
4449
let datastoreB = null
4550
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 = {}
5252

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
5461

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
7370
})
74-
}
75-
peerIdB = ipfsdB.api._peerInfo.id
71+
72+
datastoreA = new MemoryDatastore()
73+
datastoreB = new MemoryDatastore()
7674
})
7775

7876
const value = 'value'
@@ -97,16 +95,16 @@ describe('datastore-pubsub', function () {
9795

9896
after(() => {
9997
return Promise.all([
100-
stopDaemon(ipfsdA),
101-
stopDaemon(ipfsdB)
98+
pubsubA.stop(),
99+
pubsubB.stop()
102100
])
103101
})
104102

105103
it('should subscribe the topic, but receive error as no entry is stored locally', async () => {
106104
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
107105
const subsTopic = keyToTopic(`/${keyRef}`)
108106

109-
let subscribers = await pubsubA.ls()
107+
let subscribers = await pubsubA.getTopics()
110108

111109
expect(subscribers).to.exist()
112110
expect(subscribers).to.not.include(subsTopic) // not subscribed key reference yet
@@ -119,18 +117,18 @@ describe('datastore-pubsub', function () {
119117

120118
expect(res).to.not.exist()
121119

122-
subscribers = await pubsubA.ls()
120+
subscribers = await pubsubA.getTopics()
123121

124122
expect(subscribers).to.exist()
125123
expect(subscribers).to.include(subsTopic) // subscribed key reference
126124
})
127125

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 () => {
129127
const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
130128
const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, smoothValidator)
131129
const subsTopic = keyToTopic(`/${keyRef}`)
132130

133-
const res = await pubsubB.ls()
131+
const res = await pubsubB.getTopics()
134132
expect(res).to.exist()
135133
expect(res).to.not.include(subsTopic) // not subscribed
136134

@@ -172,7 +170,7 @@ describe('datastore-pubsub', function () {
172170
expect(err.code).to.equal('ERR_NOT_FOUND')
173171
})
174172

175-
await waitForPeerToSubscribe(subsTopic, ipfsdBId, ipfsdA)
173+
await waitForPeerToSubscribe(subsTopic, peerIdB, pubsubA)
176174

177175
// subscribe in order to understand when the message arrive to the node
178176
await pubsubB.subscribe(subsTopic, messageHandler)
@@ -198,7 +196,7 @@ describe('datastore-pubsub', function () {
198196
receivedMessage = true
199197
}
200198

201-
const res = await pubsubB.ls()
199+
const res = await pubsubB.getTopics()
202200
expect(res).to.exist()
203201
expect(res).to.not.include(subsTopic) // not subscribed
204202

@@ -209,7 +207,7 @@ describe('datastore-pubsub', function () {
209207
expect(err.code).to.equal('ERR_NOT_FOUND')
210208
})
211209

212-
await waitForPeerToSubscribe(subsTopic, ipfsdBId, ipfsdA)
210+
await waitForPeerToSubscribe(subsTopic, peerIdB, pubsubA)
213211

214212
// subscribe in order to understand when the message arrive to the node
215213
await pubsubB.subscribe(subsTopic, messageHandler)
@@ -273,7 +271,7 @@ describe('datastore-pubsub', function () {
273271
expect(dsPubsubB).to.equal(undefined)
274272
})
275273

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 () => {
277275
const customValidator = {
278276
validate: () => {
279277
return false // return false validation
@@ -298,7 +296,7 @@ describe('datastore-pubsub', function () {
298296
expect(err.code).to.equal('ERR_NOT_FOUND')
299297
})
300298

301-
await waitForPeerToSubscribe(subsTopic, ipfsdBId, ipfsdA)
299+
await waitForPeerToSubscribe(subsTopic, peerIdB, pubsubA)
302300

303301
// subscribe in order to understand when the message arrive to the node
304302
await pubsubB.subscribe(subsTopic, messageHandler)
@@ -347,7 +345,7 @@ describe('datastore-pubsub', function () {
347345
expect(err.code).to.equal('ERR_NOT_FOUND')
348346
})
349347

350-
await waitForPeerToSubscribe(subsTopic, ipfsdBId, ipfsdA)
348+
await waitForPeerToSubscribe(subsTopic, peerIdB, pubsubA)
351349

352350
// subscribe in order to understand when the message arrive to the node
353351
await pubsubB.subscribe(subsTopic, messageHandler)
@@ -397,7 +395,7 @@ describe('datastore-pubsub', function () {
397395
expect(err.code).to.equal('ERR_NOT_FOUND')
398396
})
399397

400-
await waitForPeerToSubscribe(subsTopic, ipfsdBId, ipfsdA)
398+
await waitForPeerToSubscribe(subsTopic, peerIdB, pubsubA)
401399

402400
// subscribe in order to understand when the message arrive to the node
403401
await pubsubB.subscribe(subsTopic, messageHandler)
@@ -439,7 +437,7 @@ describe('datastore-pubsub', function () {
439437
receivedMessage = true
440438
}
441439

442-
const res = await pubsubB.ls()
440+
const res = await pubsubB.getTopics()
443441
expect(res).to.not.include(subsTopic) // not subscribed
444442

445443
// causes pubsub b to become subscribed to the topic
@@ -449,7 +447,7 @@ describe('datastore-pubsub', function () {
449447
expect(err.code).to.equal('ERR_NOT_FOUND')
450448
})
451449

452-
await waitForPeerToSubscribe(subsTopic, ipfsdBId, ipfsdA)
450+
await waitForPeerToSubscribe(subsTopic, peerIdB, pubsubA)
453451

454452
// subscribe in order to understand when the message arrive to the node
455453
await pubsubB.subscribe(subsTopic, messageHandler)
@@ -483,7 +481,7 @@ describe('datastore-pubsub', function () {
483481
receivedMessage = true
484482
}
485483

486-
const res = await pubsubB.ls()
484+
const res = await pubsubB.getTopics()
487485
expect(res).to.not.include(subsTopic) // not subscribed
488486

489487
// causes pubsub b to become subscribed to the topic
@@ -493,7 +491,7 @@ describe('datastore-pubsub', function () {
493491
expect(err.code).to.equal('ERR_NOT_FOUND')
494492
})
495493

496-
await waitForPeerToSubscribe(subsTopic, ipfsdBId, ipfsdA)
494+
await waitForPeerToSubscribe(subsTopic, peerIdB, pubsubA)
497495

498496
// subscribe in order to understand when the message arrive to the node
499497
await pubsubB.subscribe(subsTopic, messageHandler)

0 commit comments

Comments
 (0)