Skip to content

Commit 2c4b567

Browse files
committedJun 13, 2021
chore: restructure pubsub tests
1 parent 2a6a635 commit 2c4b567

File tree

7 files changed

+105
-468
lines changed

7 files changed

+105
-468
lines changed
 

‎examples/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"fs-extra": "^8.1.0",
1313
"libp2p-pubsub-peer-discovery": "^4.0.0",
1414
"libp2p-relay-server": "^0.2.0",
15+
"libp2p-gossipsub": "^0.8.0",
1516
"p-defer": "^3.0.0",
1617
"which": "^2.0.1"
1718
},

‎examples/pubsub/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ We've seen many interesting use cases appear with this, here are some highlights
88
- [IPFS PubSub (using libp2p-floodsub) for IoT](https://www.youtube.com/watch?v=qLpM5pBDGiE).
99
- [Real Time distributed Applications](https://www.youtube.com/watch?v=vQrbxyDPSXg)
1010

11+
## 0. Set up the example
12+
13+
Before moving into the examples, you should run `npm install` on the top level `js-libp2p` folder, in order to install all the dependencies needed for this example. In addition, you will need to install the example related dependencies by doing `cd examples && npm install`. Once the install finishes, you should move into the example folder with `cd pubsub`.
14+
1115
## 1. Setting up a simple PubSub network on top of libp2p
1216

1317
For this example, we will use MulticastDNS for automatic Peer Discovery. This example is based the previous examples found in [Discovery Mechanisms](../discovery-mechanisms). You can find the complete version at [1.js](./1.js).

‎test/pubsub/configuration.node.js renamed to ‎test/configuration/pubsub.spec.js

+48-18
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
const { expect } = require('aegir/utils/chai')
55
const mergeOptions = require('merge-options')
6-
const { Multiaddr } = require('multiaddr')
6+
const pDefer = require('p-defer')
7+
const delay = require('delay')
78

89
const { create } = require('../../src')
9-
const { baseOptions, subsystemOptions } = require('./utils')
10+
const { baseOptions, pubsubSubsystemOptions } = require('./utils')
1011
const peerUtils = require('../utils/creators/peer')
1112

12-
const listenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/0')
13-
1413
describe('Pubsub subsystem is configurable', () => {
1514
let libp2p
1615

@@ -24,18 +23,15 @@ describe('Pubsub subsystem is configurable', () => {
2423
})
2524

2625
it('should exist if the module is provided', async () => {
27-
libp2p = await create(subsystemOptions)
26+
libp2p = await create(pubsubSubsystemOptions)
2827
expect(libp2p.pubsub).to.exist()
2928
})
3029

3130
it('should start and stop by default once libp2p starts', async () => {
3231
const [peerId] = await peerUtils.createPeerId()
3332

34-
const customOptions = mergeOptions(subsystemOptions, {
35-
peerId,
36-
addresses: {
37-
listen: [listenAddr]
38-
}
33+
const customOptions = mergeOptions(pubsubSubsystemOptions, {
34+
peerId
3935
})
4036

4137
libp2p = await create(customOptions)
@@ -51,11 +47,8 @@ describe('Pubsub subsystem is configurable', () => {
5147
it('should not start if disabled once libp2p starts', async () => {
5248
const [peerId] = await peerUtils.createPeerId()
5349

54-
const customOptions = mergeOptions(subsystemOptions, {
50+
const customOptions = mergeOptions(pubsubSubsystemOptions, {
5551
peerId,
56-
addresses: {
57-
listen: [listenAddr]
58-
},
5952
config: {
6053
pubsub: {
6154
enabled: false
@@ -73,11 +66,8 @@ describe('Pubsub subsystem is configurable', () => {
7366
it('should allow a manual start', async () => {
7467
const [peerId] = await peerUtils.createPeerId()
7568

76-
const customOptions = mergeOptions(subsystemOptions, {
69+
const customOptions = mergeOptions(pubsubSubsystemOptions, {
7770
peerId,
78-
addresses: {
79-
listen: [listenAddr]
80-
},
8171
config: {
8272
pubsub: {
8373
enabled: false
@@ -93,3 +83,43 @@ describe('Pubsub subsystem is configurable', () => {
9383
expect(libp2p.pubsub.started).to.equal(true)
9484
})
9585
})
86+
87+
describe('Pubsub subscription handlers adapter', () => {
88+
let libp2p
89+
90+
beforeEach(async () => {
91+
const [peerId] = await peerUtils.createPeerId()
92+
93+
libp2p = await create(mergeOptions(pubsubSubsystemOptions, {
94+
peerId
95+
}))
96+
97+
await libp2p.start()
98+
})
99+
100+
it('extends pubsub with subscribe handler', async () => {
101+
let countMessages = 0
102+
const topic = 'topic'
103+
const defer = pDefer()
104+
105+
const handler = () => {
106+
countMessages++
107+
if (countMessages > 1) {
108+
throw new Error('only one message should be received')
109+
}
110+
111+
defer.resolve()
112+
}
113+
114+
await libp2p.pubsub.subscribe(topic, handler)
115+
116+
libp2p.pubsub.emit(topic, 'useless-data')
117+
await defer.promise
118+
119+
await libp2p.pubsub.unsubscribe(topic, handler)
120+
libp2p.pubsub.emit(topic, 'useless-data')
121+
122+
// wait to guarantee that the handler is not called twice
123+
await delay(100)
124+
})
125+
})

‎test/configuration/utils.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict'
2+
3+
const Pubsub = require('libp2p-interfaces/src/pubsub')
4+
const { NOISE: Crypto } = require('libp2p-noise')
5+
const Muxer = require('libp2p-mplex')
6+
const Transport = require('libp2p-websockets')
7+
const filters = require('libp2p-websockets/src/filters')
8+
const transportKey = Transport.prototype[Symbol.toStringTag]
9+
10+
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
11+
const relayAddr = MULTIADDRS_WEBSOCKETS[0]
12+
13+
const mergeOptions = require('merge-options')
14+
15+
const baseOptions = {
16+
modules: {
17+
transport: [Transport],
18+
streamMuxer: [Muxer],
19+
connEncryption: [Crypto]
20+
}
21+
}
22+
23+
module.exports.baseOptions = baseOptions
24+
25+
class MockPubsub extends Pubsub {
26+
constructor (libp2p, options = {}) {
27+
super({
28+
debugName: 'mock-pubsub',
29+
multicodecs: '/mock-pubsub',
30+
libp2p,
31+
...options
32+
})
33+
}
34+
}
35+
36+
const pubsubSubsystemOptions = mergeOptions(baseOptions, {
37+
modules: {
38+
pubsub: MockPubsub
39+
},
40+
addresses: {
41+
listen: [`${relayAddr}/p2p-circuit`]
42+
},
43+
config: {
44+
transport: {
45+
[transportKey]: {
46+
filter: filters.all
47+
}
48+
}
49+
}
50+
})
51+
52+
module.exports.pubsubSubsystemOptions = pubsubSubsystemOptions

‎test/pubsub/implementations.node.js

-95
This file was deleted.

‎test/pubsub/operation.node.js

-326
This file was deleted.

‎test/pubsub/utils.js

-29
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.