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

Commit fc6d301

Browse files
alanshawdaviddias
authored andcommittedMay 20, 2018
fix: more robust ping tests
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent db9e545 commit fc6d301

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed
 

‎test/ping.spec.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ const IPFSApi = require('../src')
1515
const PingMessageStream = require('../src/utils/ping-message-stream')
1616
const f = require('./utils/factory')
1717

18-
describe('.ping', function () {
18+
// Determine if a ping response object is a pong, or something else, like a status message
19+
function isPong (pingResponse) {
20+
return Boolean(pingResponse && pingResponse.time)
21+
}
22+
23+
describe.only('.ping', function () {
1924
let ipfs
2025
let ipfsd
2126
let other
@@ -76,7 +81,7 @@ describe('.ping', function () {
7681
ipfs.ping(otherId, (err, res) => {
7782
expect(err).to.not.exist()
7883
expect(res).to.be.an('array')
79-
expect(res).to.have.lengthOf(3)
84+
expect(res.filter(isPong)).to.have.lengthOf(1)
8085
res.forEach(packet => {
8186
expect(packet).to.have.keys('success', 'time', 'text')
8287
expect(packet.time).to.be.a('number')
@@ -91,7 +96,7 @@ describe('.ping', function () {
9196
ipfs.ping(otherId, { count: 2 }, (err, res) => {
9297
expect(err).to.not.exist()
9398
expect(res).to.be.an('array')
94-
expect(res).to.have.lengthOf(4)
99+
expect(res.filter(isPong)).to.have.lengthOf(2)
95100
res.forEach(packet => {
96101
expect(packet).to.have.keys('success', 'time', 'text')
97102
expect(packet.time).to.be.a('number')
@@ -106,7 +111,7 @@ describe('.ping', function () {
106111
ipfs.ping(otherId, { n: 2 }, (err, res) => {
107112
expect(err).to.not.exist()
108113
expect(res).to.be.an('array')
109-
expect(res).to.have.lengthOf(4)
114+
expect(res.filter(isPong)).to.have.lengthOf(2)
110115
res.forEach(packet => {
111116
expect(packet).to.have.keys('success', 'time', 'text')
112117
expect(packet.time).to.be.a('number')
@@ -130,7 +135,7 @@ describe('.ping', function () {
130135
return ipfs.ping(otherId)
131136
.then((res) => {
132137
expect(res).to.be.an('array')
133-
expect(res).to.have.lengthOf(3)
138+
expect(res.filter(isPong)).to.have.lengthOf(1)
134139
res.forEach(packet => {
135140
expect(packet).to.have.keys('success', 'time', 'text')
136141
expect(packet.time).to.be.a('number')
@@ -146,7 +151,7 @@ describe('.ping', function () {
146151
collect((err, data) => {
147152
expect(err).to.not.exist()
148153
expect(data).to.be.an('array')
149-
expect(data).to.have.lengthOf(3)
154+
expect(data.filter(isPong)).to.have.lengthOf(1)
150155
data.forEach(packet => {
151156
expect(packet).to.have.keys('success', 'time', 'text')
152157
expect(packet.time).to.be.a('number')
@@ -164,13 +169,13 @@ describe('.ping', function () {
164169
.on('data', data => {
165170
expect(data).to.be.an('object')
166171
expect(data).to.have.keys('success', 'time', 'text')
167-
packetNum++
172+
if (isPong(data)) packetNum++
168173
})
169174
.on('error', err => {
170175
expect(err).not.to.exist()
171176
})
172177
.on('end', () => {
173-
expect(packetNum).to.be.above(2)
178+
expect(packetNum).to.equal(1)
174179
done()
175180
})
176181
})

0 commit comments

Comments
 (0)
This repository has been archived.