Skip to content

Commit

Permalink
fix: race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed May 12, 2017
1 parent 09220b9 commit 8dd9f3b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
14 changes: 8 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -311,7 +313,7 @@ class FloodSub extends EventEmitter {
if (peer.isWritable) {
peer.sendSubscriptions(topics)
} else {
setTimeout(checkIfReady.bind(peer), 100)
setImmediate(checkIfReady.bind(peer))
}
}
}
Expand Down
17 changes: 8 additions & 9 deletions test/2-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
})

Expand Down Expand Up @@ -232,7 +233,7 @@ describe('basics between 2 nodes', () => {
expectSet(first(fsA.peers).topics, ['Zb'])

done()
}, 250)
}, 1000)
})
})

Expand Down Expand Up @@ -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)
})
})
})
Expand Down

0 comments on commit 8dd9f3b

Please sign in to comment.