From 8dd9f3bca930f852cd73cc0ce786c054fe4c1245 Mon Sep 17 00:00:00 2001 From: David Dias Date: Fri, 12 May 2017 23:27:11 +0200 Subject: [PATCH] fix: race condition --- src/index.js | 14 ++++++++------ test/2-nodes.js | 17 ++++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index 78b27da0fa..f1414daf0a 100644 --- a/src/index.js +++ b/src/index.js @@ -133,14 +133,16 @@ class FloodSub extends EventEmitter { const subs = rpc.subscriptions const msgs = rpc.msgs - if (subs && subs.length) { - const peer = this.peers.get(idB58Str) - peer.updateSubscriptions(subs) - } - if (msgs && msgs.length) { this._processRpcMessages(rpc.msgs) } + + if (subs && subs.length) { + const peer = this.peers.get(idB58Str) + if (peer) { + peer.updateSubscriptions(subs) + } + } } _processRpcMessages (msgs) { @@ -311,7 +313,7 @@ class FloodSub extends EventEmitter { if (peer.isWritable) { peer.sendSubscriptions(topics) } else { - setTimeout(checkIfReady.bind(peer), 100) + setImmediate(checkIfReady.bind(peer)) } } } diff --git a/test/2-nodes.js b/test/2-nodes.js index 972a8b7118..b472c72ecb 100644 --- a/test/2-nodes.js +++ b/test/2-nodes.js @@ -14,6 +14,7 @@ const utils = require('./utils') const first = utils.first const createNode = utils.createNode const expectSet = utils.expectSet +const Buffer = require('safe-buffer').Buffer describe('basics between 2 nodes', () => { describe('fresh nodes', () => { @@ -70,7 +71,7 @@ describe('basics between 2 nodes', () => { expect(fsA.peers.size).to.equal(1) expect(fsB.peers.size).to.equal(1) cb() - }, 250) + }, 1000) ], done) }) @@ -232,7 +233,7 @@ describe('basics between 2 nodes', () => { expectSet(first(fsA.peers).topics, ['Zb']) done() - }, 250) + }, 1000) }) }) @@ -293,13 +294,11 @@ describe('basics between 2 nodes', () => { expect(fsA.peers.size).to.equal(1) expect(fsB.peers.size).to.equal(1) - fsA.stop(() => { - setTimeout(() => { - expect(fsB.peers.size).to.equal(0) - done() - }, 250) - }) - }, 250) + fsA.stop(() => setTimeout(() => { + expect(fsB.peers.size).to.equal(0) + done() + }, 250)) + }, 1000) }) }) })