From 8237791da838da354b55255bd77769852dc1fab9 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 30 Apr 2018 20:48:12 +0100 Subject: [PATCH 1/8] spec: pubsub.unsubscribe needs to be async --- SPEC/PUBSUB.md | 3 +- js/src/pubsub.js | 85 +++++++++++++++++++++++------------------------- 2 files changed, 43 insertions(+), 45 deletions(-) diff --git a/SPEC/PUBSUB.md b/SPEC/PUBSUB.md index d65ada8f..06993c06 100644 --- a/SPEC/PUBSUB.md +++ b/SPEC/PUBSUB.md @@ -39,10 +39,11 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - `ipfs.pubsub.unsubscribe(topic, handler)` +##### `JavaScript` - `ipfs.pubsub.unsubscribe(topic, handler, callback)` - `topic: string` - The topic to unsubscribe from - `handler: (msg) => ()` - The handler to remove. +- `callback: (Error) => ()` (Optional) Called once the unsubscribe is done. This works like `EventEmitter.removeListener`, as that only the `handler` passed to a `subscribe` call before is removed from listening. The underlying subscription will only be canceled once all listeners for a topic have been removed. diff --git a/js/src/pubsub.js b/js/src/pubsub.js index 1479b74d..d47fd616 100644 --- a/js/src/pubsub.js +++ b/js/src/pubsub.js @@ -7,6 +7,7 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) const series = require('async/series') +const each = require('async/each') const waterfall = require('async/waterfall') const parallel = require('async/parallel') const whilst = require('async/whilst') @@ -137,12 +138,12 @@ module.exports = (common) => { expect(msg).to.have.property('topicIDs').eql([topic]) expect(msg).to.have.property('from', ipfs1.peerId.id) - ipfs1.pubsub.unsubscribe(topic, handler) - - ipfs1.pubsub.ls((err, topics) => { - expect(err).to.not.exist() - expect(topics).to.be.empty() - check() + ipfs1.pubsub.unsubscribe(topic, handler, () => { + ipfs1.pubsub.ls((err, topics) => { + expect(err).to.not.exist() + expect(topics).to.be.empty() + check() + }) }) } @@ -163,12 +164,12 @@ module.exports = (common) => { expect(msg).to.have.property('topicIDs').eql([topic]) expect(msg).to.have.property('from', ipfs1.peerId.id) - ipfs1.pubsub.unsubscribe(topic, handler) - - ipfs1.pubsub.ls((err, topics) => { - expect(err).to.not.exist() - expect(topics).to.be.empty() - check() + ipfs1.pubsub.unsubscribe(topic, handler, () => { + ipfs1.pubsub.ls((err, topics) => { + expect(err).to.not.exist() + expect(topics).to.be.empty() + check() + }) }) } @@ -189,12 +190,12 @@ module.exports = (common) => { expect(msg).to.have.property('topicIDs').eql([topic]) expect(msg).to.have.property('from', ipfs1.peerId.id) - ipfs1.pubsub.unsubscribe(topic, handler) - - ipfs1.pubsub.ls((err, topics) => { - expect(err).to.not.exist() - expect(topics).to.be.empty() - check() + ipfs1.pubsub.unsubscribe(topic, handler, () => { + ipfs1.pubsub.ls((err, topics) => { + expect(err).to.not.exist() + expect(topics).to.be.empty() + check() + }) }) } @@ -211,14 +212,10 @@ module.exports = (common) => { const handler1 = (msg) => { expect(msg.data.toString()).to.eql('hello') - ipfs1.pubsub.unsubscribe(topic, handler1) - series([ + (cb) => ipfs1.pubsub.unsubscribe(topic, handler1, cb) (cb) => ipfs1.pubsub.ls(cb), - (cb) => { - ipfs1.pubsub.unsubscribe(topic, handler2) - cb() - }, + (cb) => ipfs1.pubsub.unsubscribe(topic, handler2, cb), (cb) => ipfs1.pubsub.ls(cb) ], (err, res) => { expect(err).to.not.exist() @@ -251,8 +248,7 @@ module.exports = (common) => { const handler = (msg) => { expect(msg.data.toString()).to.eql('hi') - ipfs1.pubsub.unsubscribe(topic, handler) - check() + ipfs1.pubsub.unsubscribe(topic, handler, check) } ipfs1.pubsub.subscribe(topic, { @@ -389,8 +385,7 @@ module.exports = (common) => { expect(err).to.not.exist() expect(topics).to.be.eql([topic]) - ipfs1.pubsub.unsubscribe(topic, sub1) - done() + ipfs1.pubsub.unsubscribe(topic, sub1, done) }) }) }) @@ -414,11 +409,8 @@ module.exports = (common) => { ipfs1.pubsub.ls((err, list) => { expect(err).to.not.exist() - expect( - list.sort() - ).to.be.eql( - topics.map((t) => t.name).sort() - ) + expect(list.sort()) + .to.eql(topics.map((t) => t.name).sort()) topics.forEach((t) => { ipfs1.pubsub.unsubscribe(t.name, t.handler) @@ -439,9 +431,11 @@ module.exports = (common) => { topic = getTopic() }) - afterEach(() => { - ipfs1.pubsub.unsubscribe(topic, sub1) - ipfs2.pubsub.unsubscribe(topic, sub2) + afterEach((done) => { + parallel([ + (cb) => ipfs1.pubsub.unsubscribe(topic, sub1, cb) + (cb) => ipfs2.pubsub.unsubscribe(topic, sub2, cb) + ], done) }) it('receive messages from different node', (done) => { @@ -673,14 +667,17 @@ module.exports = (common) => { }, (err) => { expect(err).to.not.exist() - handlers.forEach((handler) => { - ipfs1.pubsub.unsubscribe(someTopic, handler) - }) - - ipfs1.pubsub.ls((err, topics) => { - expect(err).to.not.exist() - expect(topics).to.eql([]) - done() + each( + handlers, + (handler, cb) => ipfs1.pubsub.unsubscribe(someTopic, handler, cb) + (err) => { + expect(err).to.not.exist() + ipfs1.pubsub.ls((err, topics) => { + expect(err).to.not.exist() + expect(topics).to.eql([]) + done() + }) + }) }) } ) From ab865a51b016413e536ccd90e7da006eee2d3b6e Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 30 Apr 2018 20:55:45 +0100 Subject: [PATCH 2/8] spec: update the spec, remove the madness of handling two functions as last args (promisify makes it a pain) --- SPEC/PUBSUB.md | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/SPEC/PUBSUB.md b/SPEC/PUBSUB.md index 06993c06..3cc09d35 100644 --- a/SPEC/PUBSUB.md +++ b/SPEC/PUBSUB.md @@ -7,13 +7,13 @@ pubsub API ##### `Go` **WIP** -##### `JavaScript` - ipfs.pubsub.subscribe(topic, options, handler, callback) +##### `JavaScript` - ipfs.pubsub.subscribe(options, callback) -- `topic: string` -- `options: Object` - (Optional), might contain the following properties: - - `discover`: type: Boolean - Will use the DHT to find other peers. -- `handler: (msg) => ()` - Event handler which will be called with a message object everytime one is received. The `msg` has the format `{from: string, seqno: Buffer, data: Buffer, topicIDs: Array}`. -- `callback: (Error) => ()` (Optional) Called once the subscription is established. +- `options: Object`: Object containing the following properties: + - `topic: string` + - `discover: Boolean` - Will use the DHT to find other peers. + - `handler: (msg) => ()` - Event handler which will be called with a message object everytime one is received. The `msg` has the format `{from: string, seqno: Buffer, data: Buffer, topicIDs: Array}`. +- `callback: (Error) => ()` Called once the subscription is established. If no `callback` is passed, a [promise][] is returned. @@ -28,7 +28,10 @@ const receiveMsg = (msg) => { console.log(msg.data.toString()) } -ipfs.pubsub.subscribe(topic, receiveMsg) +ipfs.pubsub.subscribe({ + topic: topic, + handler: receiveMsg +}) ``` A great source of [examples][] can be found in the tests for this API. @@ -39,12 +42,15 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - `ipfs.pubsub.unsubscribe(topic, handler, callback)` +##### `JavaScript` - `ipfs.pubsub.unsubscribe(options, callback)` -- `topic: string` - The topic to unsubscribe from -- `handler: (msg) => ()` - The handler to remove. +- `options: Object`: Object containing the following properties: + - `topic: string` - The topic to unsubscribe from + - `handler: (msg) => ()` - The handler to remove. - `callback: (Error) => ()` (Optional) Called once the unsubscribe is done. +If no `callback` is passed, a [promise][] is returned. + This works like `EventEmitter.removeListener`, as that only the `handler` passed to a `subscribe` call before is removed from listening. The underlying subscription will only be canceled once all listeners for a topic have been removed. **Example:** @@ -56,12 +62,18 @@ const receiveMsg = (msg) => { console.log(msg.toString()) } -ipfs.pubsub.subscribe(topic, receiveMsg) - -setTimeout(() => { - // unsubscribe a second later - ipfs.pubsub.unsubscribe(topic, receiveMsg) -}, 1000) +ipfs.pubsub.subscribe({ + topic: topic, + handler: receiveMsg +}, () => { + setTimeout(() => { + // unsubscribe a second later + ipfs.pubsub.unsubscribe({ + topic: topic, + handler: receiveMsg + }) + }, 1000) +}) ``` A great source of [examples][] can be found in the tests for this API. From 4c13425a03a206b0c0684a65b836420c067c4cda Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 9 May 2018 11:01:10 +0100 Subject: [PATCH 3/8] docs: updates pubsub API docs with agreed method signatures License: MIT Signed-off-by: Alan Shaw --- SPEC/PUBSUB.md | 92 ++++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/SPEC/PUBSUB.md b/SPEC/PUBSUB.md index 3cc09d35..f40a1592 100644 --- a/SPEC/PUBSUB.md +++ b/SPEC/PUBSUB.md @@ -1,19 +1,27 @@ pubsub API ========== +* [pubsub.subscribe](#pubsubsubscribe) +* [pubsub.unsubscribe](#pubsubunsubscribe) +* [pubsub.publish](#pubsubpublish) +* [pubsub.ls](#pubsubls) +* [pubsub.peers](#pubsubpeers) + +--- + #### `pubsub.subscribe` > Subscribe to a pubsub topic. ##### `Go` **WIP** -##### `JavaScript` - ipfs.pubsub.subscribe(options, callback) +##### `JavaScript` - ipfs.pubsub.subscribe(topic, handler, [options], [callback]) -- `options: Object`: Object containing the following properties: - - `topic: string` +- `topic: String` +- `handler: (msg) => {}` - Event handler which will be called with a message object everytime one is received. The `msg` has the format `{from: String, seqno: Buffer, data: Buffer, topicIDs: Array}`. +- `options: Object` - (Optional) Object containing the following properties: - `discover: Boolean` - Will use the DHT to find other peers. - - `handler: (msg) => ()` - Event handler which will be called with a message object everytime one is received. The `msg` has the format `{from: string, seqno: Buffer, data: Buffer, topicIDs: Array}`. -- `callback: (Error) => ()` Called once the subscription is established. +- `callback: (Error) => {}` - (Optional) Called once the subscription is established. If no `callback` is passed, a [promise][] is returned. @@ -23,14 +31,13 @@ If no `callback` is passed, a [promise][] is returned. ```JavaScript const topic = 'fruit-of-the-day' +const receiveMsg = (msg) => console.log(msg.data.toString()) -const receiveMsg = (msg) => { - console.log(msg.data.toString()) -} - -ipfs.pubsub.subscribe({ - topic: topic, - handler: receiveMsg +ipfs.pubsub.subscribe(topic, receiveMsg, (err) => { + if (err) { + return console.error(`failed to subscribe to ${topic}`, err) + } + console.log(`subscribed to ${topic}`) }) ``` @@ -42,12 +49,11 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - `ipfs.pubsub.unsubscribe(options, callback)` +##### `JavaScript` - `ipfs.pubsub.unsubscribe(topic, handler, [callback])` -- `options: Object`: Object containing the following properties: - - `topic: string` - The topic to unsubscribe from - - `handler: (msg) => ()` - The handler to remove. -- `callback: (Error) => ()` (Optional) Called once the unsubscribe is done. +- `topic: String` - The topic to unsubscribe from +- `handler: (msg) => {}` - The handler to remove. +- `callback: (Error) => {}` (Optional) Called once the unsubscribe is done. If no `callback` is passed, a [promise][] is returned. @@ -57,20 +63,23 @@ This works like `EventEmitter.removeListener`, as that only the `handler` passed ```JavaScript const topic = 'fruit-of-the-day' +const receiveMsg = (msg) => console.log(msg.toString()) -const receiveMsg = (msg) => { - console.log(msg.toString()) -} +ipfs.pubsub.subscribe(topic, receiveMsg, (err) => { + if (err) { + return console.error(`failed to subscribe to ${topic}`, err) + } + + console.log(`subscribed to ${topic}`) -ipfs.pubsub.subscribe({ - topic: topic, - handler: receiveMsg -}, () => { + // unsubscribe a second later setTimeout(() => { - // unsubscribe a second later - ipfs.pubsub.unsubscribe({ - topic: topic, - handler: receiveMsg + ipfs.pubsub.unsubscribe(topic, receiveMsg, (err) => { + if (err) { + return console.error(`failed to unsubscribe from ${topic}`, err) + } + + console.log(`unsubscribed from ${topic}`) }) }, 1000) }) @@ -84,11 +93,11 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - ipfs.pubsub.publish(topic, data, callback) +##### `JavaScript` - ipfs.pubsub.publish(topic, data, [callback]) -- `topic: string` -- `data: buffer` - The actual message to send -- `callback: (Error) => ()` - Calls back with an error or nothing if the publish was successfull. +- `topic: String` +- `data: Buffer` - The message to send +- `callback: (Error) => {}` - (Optional) Calls back with an error or nothing if the publish was successful. If no `callback` is passed, a promise is returned. @@ -100,9 +109,10 @@ const msg = new Buffer('banana') ipfs.pubsub.publish(topic, msg, (err) => { if (err) { - throw err + return console.error(`failed to publish to ${topic}`, err) } // msg was broadcasted + console.log(`published to ${topic}`) }) ``` @@ -114,9 +124,9 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - ipfs.pubsub.ls(callback) +##### `JavaScript` - ipfs.pubsub.ls([callback]) -- `callback: (Error, Array>) => ()` - Calls back with an error or a list of topicCIDs that this peer is subscribed to. +- `callback: (Error, Array) => {}` - (Optional) Calls back with an error or a list of topicIDs that this peer is subscribed to. If no `callback` is passed, a promise is returned. @@ -125,7 +135,7 @@ If no `callback` is passed, a promise is returned. ```JavaScript ipfs.pubsub.ls((err, topics) => { if (err) { - throw err + return console.error('failed to get list of subscription topics', err) } console.log(topics) }) @@ -139,19 +149,21 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - ipfs.pubsub.peers(topic, callback) +##### `JavaScript` - ipfs.pubsub.peers(topic, [callback]) -- `topic: string` -- `callback: (Error, Array>) => ()` - Calls back with an error or a list of peer ids subscribed to the `topic`. +- `topic: String` +- `callback: (Error, Array) => {}` - (Optional) Calls back with an error or a list of peer IDs subscribed to the `topic`. If no `callback` is passed, a promise is returned. **Example:** ```JavaScript +const topic = 'fruit-of-the-day' + ipfs.pubsub.peers(topic, (err, peerIds) => { if (err) { - throw err + return console.error(`failed to get peers subscribed to ${topic}`, err) } console.log(peerIds) }) From c43f8bc4ca11a309c016c86f8cb86b7ab18ae139 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 9 May 2018 12:36:10 +0100 Subject: [PATCH 4/8] fix: pubsub subscribe call with options License: MIT Signed-off-by: Alan Shaw --- js/src/pubsub.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/js/src/pubsub.js b/js/src/pubsub.js index d47fd616..116d0c10 100644 --- a/js/src/pubsub.js +++ b/js/src/pubsub.js @@ -138,7 +138,9 @@ module.exports = (common) => { expect(msg).to.have.property('topicIDs').eql([topic]) expect(msg).to.have.property('from', ipfs1.peerId.id) - ipfs1.pubsub.unsubscribe(topic, handler, () => { + ipfs1.pubsub.unsubscribe(topic, handler, (err) => { + expect(err).to.not.exist() + ipfs1.pubsub.ls((err, topics) => { expect(err).to.not.exist() expect(topics).to.be.empty() @@ -164,7 +166,9 @@ module.exports = (common) => { expect(msg).to.have.property('topicIDs').eql([topic]) expect(msg).to.have.property('from', ipfs1.peerId.id) - ipfs1.pubsub.unsubscribe(topic, handler, () => { + ipfs1.pubsub.unsubscribe(topic, handler, (err) => { + expect(err).to.not.exist() + ipfs1.pubsub.ls((err, topics) => { expect(err).to.not.exist() expect(topics).to.be.empty() @@ -190,7 +194,9 @@ module.exports = (common) => { expect(msg).to.have.property('topicIDs').eql([topic]) expect(msg).to.have.property('from', ipfs1.peerId.id) - ipfs1.pubsub.unsubscribe(topic, handler, () => { + ipfs1.pubsub.unsubscribe(topic, handler, (err) => { + expect(err).to.not.exist() + ipfs1.pubsub.ls((err, topics) => { expect(err).to.not.exist() expect(topics).to.be.empty() @@ -251,9 +257,7 @@ module.exports = (common) => { ipfs1.pubsub.unsubscribe(topic, handler, check) } - ipfs1.pubsub.subscribe(topic, { - discover: true - }, handler, (err) => { + ipfs1.pubsub.subscribe(topic, handler, { discover: true }, (err) => { expect(err).to.not.exist() ipfs1.pubsub.publish(topic, Buffer.from('hi'), check) }) From 61d76019e1fc4449f913905197c14707600f7d63 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 9 May 2018 15:45:57 +0100 Subject: [PATCH 5/8] docs: add default value for discover License: MIT Signed-off-by: Alan Shaw --- SPEC/PUBSUB.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SPEC/PUBSUB.md b/SPEC/PUBSUB.md index f40a1592..f21d48a2 100644 --- a/SPEC/PUBSUB.md +++ b/SPEC/PUBSUB.md @@ -7,8 +7,6 @@ pubsub API * [pubsub.ls](#pubsubls) * [pubsub.peers](#pubsubpeers) ---- - #### `pubsub.subscribe` > Subscribe to a pubsub topic. @@ -20,7 +18,7 @@ pubsub API - `topic: String` - `handler: (msg) => {}` - Event handler which will be called with a message object everytime one is received. The `msg` has the format `{from: String, seqno: Buffer, data: Buffer, topicIDs: Array}`. - `options: Object` - (Optional) Object containing the following properties: - - `discover: Boolean` - Will use the DHT to find other peers. + - `discover: Boolean` - (Default: `false`) Will use the DHT to find other peers. - `callback: (Error) => {}` - (Optional) Called once the subscription is established. If no `callback` is passed, a [promise][] is returned. From f798597110c05a90f8204bf3bc3173f94db9060f Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 9 May 2018 16:59:06 +0100 Subject: [PATCH 6/8] fix: remove duplicate async.each License: MIT Signed-off-by: Alan Shaw --- js/src/pubsub.js | 1 - 1 file changed, 1 deletion(-) diff --git a/js/src/pubsub.js b/js/src/pubsub.js index 116d0c10..46cbe748 100644 --- a/js/src/pubsub.js +++ b/js/src/pubsub.js @@ -11,7 +11,6 @@ const each = require('async/each') const waterfall = require('async/waterfall') const parallel = require('async/parallel') const whilst = require('async/whilst') -const each = require('async/each') const hat = require('hat') // On Browsers it will be false, but the tests currently aren't run From 2019c453813cd50c96f4f38b0422e20b605586c5 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 9 May 2018 20:56:35 +0100 Subject: [PATCH 7/8] fix: many fixes for pubsub tests with new async unsubscribe License: MIT Signed-off-by: Alan Shaw --- js/src/pubsub.js | 65 +++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/js/src/pubsub.js b/js/src/pubsub.js index 46cbe748..4be556d8 100644 --- a/js/src/pubsub.js +++ b/js/src/pubsub.js @@ -63,7 +63,7 @@ function makeCheck (n, done) { } module.exports = (common) => { - describe('.pubsub', function () { + describe.only('.pubsub', function () { this.timeout(80 * 1000) const getTopic = () => 'pubsub-tests-' + hat() @@ -205,7 +205,7 @@ module.exports = (common) => { } ipfs1.pubsub - .subscribe(topic, {}, handler) + .subscribe(topic, handler, {}) .then(() => ipfs1.pubsub.publish(topic, Buffer.from('hi'), check)) .catch((err) => expect(err).to.not.exist()) }) @@ -218,7 +218,7 @@ module.exports = (common) => { expect(msg.data.toString()).to.eql('hello') series([ - (cb) => ipfs1.pubsub.unsubscribe(topic, handler1, cb) + (cb) => ipfs1.pubsub.unsubscribe(topic, handler1, cb), (cb) => ipfs1.pubsub.ls(cb), (cb) => ipfs1.pubsub.unsubscribe(topic, handler2, cb), (cb) => ipfs1.pubsub.ls(cb) @@ -226,9 +226,9 @@ module.exports = (common) => { expect(err).to.not.exist() // Still subscribed as there is one listener left - expect(res[0]).to.eql([topic]) + expect(res[1]).to.eql([topic]) // Now all listeners are gone no subscription anymore - expect(res[2]).to.eql([]) + expect(res[3]).to.eql([]) check() }) } @@ -310,12 +310,13 @@ module.exports = (common) => { ipfs1.pubsub.peers(topic, (err, peers) => { expect(err).to.not.exist() - expect(peers).to.be.empty() - ipfs1.pubsub.unsubscribe(topic, sub1) - ipfs2.pubsub.unsubscribe(topicOther, sub2) - ipfs3.pubsub.unsubscribe(topicOther, sub3) - done() + + parallel([ + (cb) => ipfs1.pubsub.unsubscribe(topic, sub1, cb), + (cb) => ipfs2.pubsub.unsubscribe(topicOther, sub2, cb), + (cb) => ipfs3.pubsub.unsubscribe(topicOther, sub3, cb) + ], done) }) }) }) @@ -335,11 +336,12 @@ module.exports = (common) => { (cb) => waitForPeers(ipfs1, topic, [ipfs2.peerId.id], cb) ], (err) => { expect(err).to.not.exist() - ipfs1.pubsub.unsubscribe(topic, sub1) - ipfs2.pubsub.unsubscribe(topic, sub2) - ipfs3.pubsub.unsubscribe(topic, sub3) - done() + parallel([ + (cb) => ipfs1.pubsub.unsubscribe(topic, sub1, cb), + (cb) => ipfs2.pubsub.unsubscribe(topic, sub2, cb), + (cb) => ipfs3.pubsub.unsubscribe(topic, sub3, cb) + ], done) }) }) @@ -359,11 +361,12 @@ module.exports = (common) => { ], cb) ], (err) => { expect(err).to.not.exist() - ipfs1.pubsub.unsubscribe(topic, sub1) - ipfs2.pubsub.unsubscribe(topic, sub2) - ipfs3.pubsub.unsubscribe(topic, sub3) - done() + parallel([ + (cb) => ipfs1.pubsub.unsubscribe(topic, sub1, cb), + (cb) => ipfs2.pubsub.unsubscribe(topic, sub2, cb), + (cb) => ipfs3.pubsub.unsubscribe(topic, sub3, cb) + ], done) }) }) }) @@ -415,11 +418,9 @@ module.exports = (common) => { expect(list.sort()) .to.eql(topics.map((t) => t.name).sort()) - topics.forEach((t) => { - ipfs1.pubsub.unsubscribe(t.name, t.handler) - }) - - done() + parallel(topics.map((t) => { + return (cb) => ipfs1.pubsub.unsubscribe(t.name, t.handler, cb) + }), done) }) }) }) @@ -436,8 +437,8 @@ module.exports = (common) => { afterEach((done) => { parallel([ - (cb) => ipfs1.pubsub.unsubscribe(topic, sub1, cb) - (cb) => ipfs2.pubsub.unsubscribe(topic, sub2, cb) + (cb) => ipfs1.pubsub.unsubscribe(topic, sub1, cb), + (cb) => ipfs2.pubsub.unsubscribe(topic, sub2, cb) ], done) }) @@ -589,9 +590,11 @@ module.exports = (common) => { topic = getTopic() }) - afterEach(() => { - ipfs1.pubsub.unsubscribe(topic, sub1) - ipfs2.pubsub.unsubscribe(topic, sub2) + afterEach((done) => { + parallel([ + (cb) => ipfs1.pubsub.unsubscribe(topic, sub1, cb), + (cb) => ipfs2.pubsub.unsubscribe(topic, sub2, cb) + ], done) }) it('send/receive 10k messages', function (done) { @@ -672,7 +675,7 @@ module.exports = (common) => { expect(err).to.not.exist() each( handlers, - (handler, cb) => ipfs1.pubsub.unsubscribe(someTopic, handler, cb) + (handler, cb) => ipfs1.pubsub.unsubscribe(someTopic, handler, cb), (err) => { expect(err).to.not.exist() ipfs1.pubsub.ls((err, topics) => { @@ -680,8 +683,8 @@ module.exports = (common) => { expect(topics).to.eql([]) done() }) - }) - }) + } + ) } ) }) From 251cffd271ab392ac2b45ee806d44b90b3d8e8f2 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 9 May 2018 21:00:13 +0100 Subject: [PATCH 8/8] fix: remove .only License: MIT Signed-off-by: Alan Shaw --- js/src/pubsub.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/src/pubsub.js b/js/src/pubsub.js index 4be556d8..2d019071 100644 --- a/js/src/pubsub.js +++ b/js/src/pubsub.js @@ -63,7 +63,7 @@ function makeCheck (n, done) { } module.exports = (common) => { - describe.only('.pubsub', function () { + describe('.pubsub', function () { this.timeout(80 * 1000) const getTopic = () => 'pubsub-tests-' + hat()