Skip to content

Commit

Permalink
add callback to pubsub.unsubscribe and test (libp2p#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Dec 20, 2018
1 parent 26de739 commit 473eaf4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = (node) => {
subscribe(callback)
},

unsubscribe: (topic, handler) => {
unsubscribe: (topic, handler, callback) => {
if (!node.isStarted() && !floodSub.started) {
throw new Error(NOT_STARTED_YET)
}
Expand All @@ -43,6 +43,8 @@ module.exports = (node) => {
if (floodSub.listenerCount(topic) === 0) {
floodSub.unsubscribe(topic)
}

setImmediate(() => callback())
},

publish: (topic, data, callback) => {
Expand Down
20 changes: 12 additions & 8 deletions test/pubsub.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,25 @@ function stopTwo (nodes, callback) {
// 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) => {
it('start two nodes and send one message, then unsubscribe', (done) => {
const data = Buffer.from('test')
const handler = (msg, nodes, cb) => {
expect(msg.data).to.eql(data)
cb(null, nodes)
}
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) => {
nodes[0].pubsub.subscribe('pubsub', (msg, nodes, cb) => handler, (err) => {
expect(err).to.not.exist()
setTimeout(() => nodes[1].pubsub.publish('pubsub', data, (err) => {
expect(err).to.not.exist()
}), 500)
setTimeout(() => nodes[0].pubsub.unsubscribe('pubsub', handler, (err) => {
expect(err).to.not.exist()
console.log("\tunsubscribed!")
done()
}), 600)
}
)
},
Expand Down

0 comments on commit 473eaf4

Please sign in to comment.