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

Commit 688f4d7

Browse files
committed
fix: correctly differentiate pong responses
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 <[email protected]>
1 parent 2df22b0 commit 688f4d7

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

js/src/ping.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ function expectIsPingResponse (obj) {
2222
expect(obj.text).to.be.a('string')
2323
}
2424

25+
// Determine if a ping response object is a pong, or something else, like a status message
26+
function isPong (pingResponse) {
27+
return Boolean(pingResponse && pingResponse.success && !pingResponse.text)
28+
}
29+
2530
module.exports = (common) => {
2631
describe('.ping', function () {
2732
let ipfsdA
@@ -57,7 +62,7 @@ module.exports = (common) => {
5762
ipfsdA.ping(ipfsdB.peerId.id, { count }, (err, responses) => {
5863
expect(err).to.not.exist()
5964
responses.forEach(expectIsPingResponse)
60-
const pongs = responses.filter(r => Boolean(r.time))
65+
const pongs = responses.filter(isPong)
6166
expect(pongs.length).to.equal(count)
6267
done()
6368
})
@@ -94,10 +99,10 @@ module.exports = (common) => {
9499
const count = 3
95100
pull(
96101
ipfsdA.pingPullStream(ipfsdB.peerId.id, { count }),
97-
pull.drain(({ success, time }) => {
98-
expect(success).to.be.true()
102+
pull.drain((res) => {
103+
expect(res.success).to.be.true()
99104
// It's a pong
100-
if (time) {
105+
if (isPong(res)) {
101106
packetNum++
102107
}
103108
}, (err) => {
@@ -159,10 +164,10 @@ module.exports = (common) => {
159164
ipfsdA.pingReadableStream(ipfsdB.peerId.id, { count }),
160165
new Writable({
161166
objectMode: true,
162-
write ({ success, time }, enc, cb) {
163-
expect(success).to.be.true()
167+
write (res, enc, cb) {
168+
expect(res.success).to.be.true()
164169
// It's a pong
165-
if (time) {
170+
if (isPong(res)) {
166171
packetNum++
167172
}
168173

0 commit comments

Comments
 (0)