mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-13 17:21:21 +00:00
test: test pubsub on and off
This commit is contained in:
@ -11,16 +11,16 @@ module.exports = (node) => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
subscribe: (topic, options, handler, callback) => {
|
subscribe: (topic, options, handler, callback) => {
|
||||||
if (!node.isStarted() && !floodSub.started) {
|
|
||||||
return setImmediate(() => callback(new Error(NOT_STARTED_YET)))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
callback = handler
|
callback = handler
|
||||||
handler = options
|
handler = options
|
||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!node.isStarted() && !floodSub.started) {
|
||||||
|
return setImmediate(() => callback(new Error(NOT_STARTED_YET)))
|
||||||
|
}
|
||||||
|
|
||||||
function subscribe (cb) {
|
function subscribe (cb) {
|
||||||
if (floodSub.listenerCount(topic) === 0) {
|
if (floodSub.listenerCount(topic) === 0) {
|
||||||
floodSub.subscribe(topic)
|
floodSub.subscribe(topic)
|
||||||
|
@ -7,46 +7,81 @@ const chai = require('chai')
|
|||||||
chai.use(require('dirty-chai'))
|
chai.use(require('dirty-chai'))
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
const parallel = require('async/parallel')
|
const parallel = require('async/parallel')
|
||||||
|
const waterfall = require('async/waterfall')
|
||||||
const _times = require('lodash.times')
|
const _times = require('lodash.times')
|
||||||
const utils = require('./utils/node')
|
const utils = require('./utils/node')
|
||||||
const createNode = utils.createNode
|
const createNode = utils.createNode
|
||||||
|
|
||||||
describe('.pubsub', () => {
|
function startTwo (callback) {
|
||||||
let nodeA
|
const tasks = _times(2, () => (cb) => {
|
||||||
let nodeB
|
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||||
|
mdns: false
|
||||||
before(function (done) {
|
}, (err, node) => {
|
||||||
this.timeout(5 * 1000)
|
|
||||||
|
|
||||||
const tasks = _times(2, () => (cb) => {
|
|
||||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
|
||||||
mdns: false,
|
|
||||||
dht: true
|
|
||||||
}, (err, node) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
node.start((err) => cb(err, node))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
parallel(tasks, (err, nodes) => {
|
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
nodeA = nodes[0]
|
node.start((err) => cb(err, node))
|
||||||
nodeB = nodes[1]
|
|
||||||
|
|
||||||
nodeA.dial(nodeB.peerInfo, done)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
after((done) => {
|
parallel(tasks, (err, nodes) => {
|
||||||
parallel([
|
expect(err).to.not.exist()
|
||||||
(cb) => nodeA.stop(cb),
|
|
||||||
(cb) => nodeB.stop(cb)
|
|
||||||
], done)
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('.pubsub on (default)', () => {
|
nodes[0].dial(nodes[1].peerInfo, (err) => callback(err, nodes))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopTwo (nodes, callback) {
|
||||||
|
parallel([
|
||||||
|
(cb) => nodes[0].stop(cb),
|
||||||
|
(cb) => nodes[1].stop(cb)
|
||||||
|
], 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) => {
|
||||||
|
it('start two nodes and send one message', (done) => {
|
||||||
|
waterfall([
|
||||||
|
(cb) => startTwo(cb),
|
||||||
|
(nodes, cb) => {
|
||||||
|
const data = Buffer.from('test')
|
||||||
|
nodes[0].pubsub.subscribe('pubsub',
|
||||||
|
(msg) => {
|
||||||
|
expect(msg.data).to.eql(data)
|
||||||
|
cb(null, nodes)
|
||||||
|
},
|
||||||
|
(err) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
setTimeout(() => nodes[1].pubsub.publish('pubsub', data, (err) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
}), 500)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
},
|
||||||
|
(nodes, cb) => stopTwo(nodes, cb)
|
||||||
|
], done)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('.pubsub off', () => {
|
describe('.pubsub off', () => {
|
||||||
|
it('fail to use pubsub if disabled', (done) => {
|
||||||
|
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||||
|
mdns: false,
|
||||||
|
pubsub: false
|
||||||
|
}, (err, node) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
|
node.pubsub.subscribe('news',
|
||||||
|
(msg) => {},
|
||||||
|
(err) => {
|
||||||
|
expect(err).to.exist()
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user