mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-21 21:11:34 +00:00
feat: integrate gossipsub by default (#365)
BREAKING CHANGE: new configuration for deciding the implementation of pubsub to be used. In this context, the experimental flags were also removed. See the README for the latest usage.
This commit is contained in:
@ -11,23 +11,31 @@ const parallel = require('async/parallel')
|
||||
const series = require('async/series')
|
||||
const _times = require('lodash.times')
|
||||
|
||||
const Floodsub = require('libp2p-floodsub')
|
||||
const mergeOptions = require('merge-options')
|
||||
|
||||
const { codes } = require('../src/errors')
|
||||
const createNode = require('./utils/create-node')
|
||||
|
||||
function startTwo (callback) {
|
||||
function startTwo (options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
const tasks = _times(2, () => (cb) => {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', mergeOptions({
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: true
|
||||
pubsub: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
}, options), (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
node.start((err) => cb(err, node))
|
||||
})
|
||||
@ -47,22 +55,17 @@ function stopTwo (nodes, callback) {
|
||||
], callback)
|
||||
}
|
||||
|
||||
// There is a vast test suite on PubSub through js-ipfs
|
||||
// https://github.com/ipfs/interface-ipfs-core/blob/master/js/src/pubsub.js
|
||||
// and libp2p-floodsub itself
|
||||
// https://github.com/libp2p/js-libp2p-floodsub/tree/master/test
|
||||
// TODO: consider if all or some of those should come here
|
||||
describe('.pubsub', () => {
|
||||
describe('.pubsub on (default)', (done) => {
|
||||
describe('.pubsub on (default)', () => {
|
||||
it('start two nodes and send one message, then unsubscribe', (done) => {
|
||||
// Check the final series error, and the publish handler
|
||||
expect(2).checks(done)
|
||||
|
||||
let nodes
|
||||
const data = Buffer.from('test')
|
||||
const data = 'test'
|
||||
const handler = (msg) => {
|
||||
// verify the data is correct and mark the expect
|
||||
expect(msg.data).to.eql(data).mark()
|
||||
expect(msg.data.toString()).to.eql(data).mark()
|
||||
}
|
||||
|
||||
series([
|
||||
@ -77,10 +80,6 @@ describe('.pubsub', () => {
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// publish on the second
|
||||
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
|
||||
// ls subscripts
|
||||
(cb) => nodes[1].pubsub.ls(cb),
|
||||
// get subscribed peers
|
||||
(cb) => nodes[1].pubsub.peers('pubsub', cb),
|
||||
// Wait a moment before unsubscribing
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// unsubscribe on the first
|
||||
@ -115,6 +114,123 @@ describe('.pubsub', () => {
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// publish on the second
|
||||
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
|
||||
// ls subscripts
|
||||
(cb) => nodes[1].pubsub.ls(cb),
|
||||
// get subscribed peers
|
||||
(cb) => nodes[1].pubsub.peers('pubsub', cb),
|
||||
// Wait a moment before unsubscribing
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// unsubscribe from all
|
||||
(cb) => nodes[0].pubsub.unsubscribe('pubsub', null, cb),
|
||||
// Verify unsubscribed
|
||||
(cb) => {
|
||||
nodes[0].pubsub.ls((err, topics) => {
|
||||
expect(topics.length).to.eql(0).mark()
|
||||
cb(err)
|
||||
})
|
||||
},
|
||||
// Stop both nodes
|
||||
(cb) => stopTwo(nodes, cb)
|
||||
], (err) => {
|
||||
// Verify there was no error, and mark the expect
|
||||
expect(err).to.not.exist().mark()
|
||||
})
|
||||
})
|
||||
it('publish should fail if data is not a buffer nor a string', (done) => {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
pubsub: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
node.start((err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
node.pubsub.publish('pubsub', 10, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.equal('ERR_DATA_IS_NOT_VALID')
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.pubsub on using floodsub', () => {
|
||||
it('start two nodes and send one message, then unsubscribe', (done) => {
|
||||
// Check the final series error, and the publish handler
|
||||
expect(2).checks(done)
|
||||
|
||||
let nodes
|
||||
const data = Buffer.from('test')
|
||||
const handler = (msg) => {
|
||||
// verify the data is correct and mark the expect
|
||||
expect(msg.data).to.eql(data).mark()
|
||||
}
|
||||
|
||||
series([
|
||||
// Start the nodes
|
||||
(cb) => startTwo({
|
||||
modules: {
|
||||
pubsub: Floodsub
|
||||
}
|
||||
}, (err, _nodes) => {
|
||||
nodes = _nodes
|
||||
cb(err)
|
||||
}),
|
||||
// subscribe on the first
|
||||
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb),
|
||||
// Wait a moment before publishing
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// publish on the second
|
||||
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
|
||||
// Wait a moment before unsubscribing
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// unsubscribe on the first
|
||||
(cb) => nodes[0].pubsub.unsubscribe('pubsub', handler, cb),
|
||||
// Stop both nodes
|
||||
(cb) => stopTwo(nodes, cb)
|
||||
], (err) => {
|
||||
// Verify there was no error, and mark the expect
|
||||
expect(err).to.not.exist().mark()
|
||||
})
|
||||
})
|
||||
it('start two nodes and send one message, then unsubscribe without handler', (done) => {
|
||||
// Check the final series error, and the publish handler
|
||||
expect(3).checks(done)
|
||||
|
||||
let nodes
|
||||
const data = Buffer.from('test')
|
||||
const handler = (msg) => {
|
||||
// verify the data is correct and mark the expect
|
||||
expect(msg.data).to.eql(data).mark()
|
||||
}
|
||||
|
||||
series([
|
||||
// Start the nodes
|
||||
(cb) => startTwo({
|
||||
modules: {
|
||||
pubsub: Floodsub
|
||||
}
|
||||
}, (err, _nodes) => {
|
||||
nodes = _nodes
|
||||
cb(err)
|
||||
}),
|
||||
// subscribe on the first
|
||||
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb),
|
||||
// Wait a moment before publishing
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// publish on the second
|
||||
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
|
||||
// Wait a moment before unsubscribing
|
||||
(cb) => setTimeout(cb, 500),
|
||||
// unsubscribe from all
|
||||
@ -141,9 +257,12 @@ describe('.pubsub', () => {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: true
|
||||
pubsub: {
|
||||
enabled: true
|
||||
}
|
||||
},
|
||||
modules: {
|
||||
pubsub: Floodsub
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
@ -151,9 +270,9 @@ describe('.pubsub', () => {
|
||||
node.start((err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
node.pubsub.publish('pubsub', 'datastr', (err) => {
|
||||
node.pubsub.publish('pubsub', 10, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.equal('ERR_DATA_IS_NOT_A_BUFFER')
|
||||
expect(err.code).to.equal('ERR_DATA_IS_NOT_VALID')
|
||||
|
||||
done()
|
||||
})
|
||||
@ -170,9 +289,6 @@ describe('.pubsub', () => {
|
||||
mdns: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: false
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
@ -194,8 +310,8 @@ describe('.pubsub', () => {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
pubsub: true
|
||||
pubsub: {
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
|
Reference in New Issue
Block a user