Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
fix: correctly differentiate pong responses
Browse files Browse the repository at this point in the history
Investigation discovered that pong responses CAN have `time: 0` (they can be very quick). Previously pong messages were differentiated by time greater than 0, but considering it can be 0 this was incorrect.

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
alanshaw committed May 24, 2018
1 parent 2df22b0 commit 688f4d7
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions js/src/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function expectIsPingResponse (obj) {
expect(obj.text).to.be.a('string')
}

// Determine if a ping response object is a pong, or something else, like a status message
function isPong (pingResponse) {
return Boolean(pingResponse && pingResponse.success && !pingResponse.text)
}

module.exports = (common) => {
describe('.ping', function () {
let ipfsdA
Expand Down Expand Up @@ -57,7 +62,7 @@ module.exports = (common) => {
ipfsdA.ping(ipfsdB.peerId.id, { count }, (err, responses) => {
expect(err).to.not.exist()
responses.forEach(expectIsPingResponse)
const pongs = responses.filter(r => Boolean(r.time))
const pongs = responses.filter(isPong)
expect(pongs.length).to.equal(count)
done()
})
Expand Down Expand Up @@ -94,10 +99,10 @@ module.exports = (common) => {
const count = 3
pull(
ipfsdA.pingPullStream(ipfsdB.peerId.id, { count }),
pull.drain(({ success, time }) => {
expect(success).to.be.true()
pull.drain((res) => {
expect(res.success).to.be.true()
// It's a pong
if (time) {
if (isPong(res)) {
packetNum++
}
}, (err) => {
Expand Down Expand Up @@ -159,10 +164,10 @@ module.exports = (common) => {
ipfsdA.pingReadableStream(ipfsdB.peerId.id, { count }),
new Writable({
objectMode: true,
write ({ success, time }, enc, cb) {
expect(success).to.be.true()
write (res, enc, cb) {
expect(res.success).to.be.true()
// It's a pong
if (time) {
if (isPong(res)) {
packetNum++
}

Expand Down

0 comments on commit 688f4d7

Please sign in to comment.