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

Commit

Permalink
fix: more robust ping tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
alanshaw authored and daviddias committed May 20, 2018
1 parent db9e545 commit fc6d301
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions test/ping.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ const IPFSApi = require('../src')
const PingMessageStream = require('../src/utils/ping-message-stream')
const f = require('./utils/factory')

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

describe.only('.ping', function () {
let ipfs
let ipfsd
let other
Expand Down Expand Up @@ -76,7 +81,7 @@ describe('.ping', function () {
ipfs.ping(otherId, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(3)
expect(res.filter(isPong)).to.have.lengthOf(1)
res.forEach(packet => {
expect(packet).to.have.keys('success', 'time', 'text')
expect(packet.time).to.be.a('number')
Expand All @@ -91,7 +96,7 @@ describe('.ping', function () {
ipfs.ping(otherId, { count: 2 }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(4)
expect(res.filter(isPong)).to.have.lengthOf(2)
res.forEach(packet => {
expect(packet).to.have.keys('success', 'time', 'text')
expect(packet.time).to.be.a('number')
Expand All @@ -106,7 +111,7 @@ describe('.ping', function () {
ipfs.ping(otherId, { n: 2 }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(4)
expect(res.filter(isPong)).to.have.lengthOf(2)
res.forEach(packet => {
expect(packet).to.have.keys('success', 'time', 'text')
expect(packet.time).to.be.a('number')
Expand All @@ -130,7 +135,7 @@ describe('.ping', function () {
return ipfs.ping(otherId)
.then((res) => {
expect(res).to.be.an('array')
expect(res).to.have.lengthOf(3)
expect(res.filter(isPong)).to.have.lengthOf(1)
res.forEach(packet => {
expect(packet).to.have.keys('success', 'time', 'text')
expect(packet.time).to.be.a('number')
Expand All @@ -146,7 +151,7 @@ describe('.ping', function () {
collect((err, data) => {
expect(err).to.not.exist()
expect(data).to.be.an('array')
expect(data).to.have.lengthOf(3)
expect(data.filter(isPong)).to.have.lengthOf(1)
data.forEach(packet => {
expect(packet).to.have.keys('success', 'time', 'text')
expect(packet.time).to.be.a('number')
Expand All @@ -164,13 +169,13 @@ describe('.ping', function () {
.on('data', data => {
expect(data).to.be.an('object')
expect(data).to.have.keys('success', 'time', 'text')
packetNum++
if (isPong(data)) packetNum++
})
.on('error', err => {
expect(err).not.to.exist()
})
.on('end', () => {
expect(packetNum).to.be.above(2)
expect(packetNum).to.equal(1)
done()
})
})
Expand Down

0 comments on commit fc6d301

Please sign in to comment.