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

Commit

Permalink
Merge pull request #273 from ipfs/test/ping
Browse files Browse the repository at this point in the history
test: add ping, pingPullStream and pingReadableStream tests
  • Loading branch information
alanshaw authored May 16, 2018
2 parents fd09dce + 30badaa commit ef9658e
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 1 deletion.
1 change: 1 addition & 0 deletions js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports.swarm = require('./swarm')
exports.block = require('./block')
exports.dht = require('./dht')
exports.dag = require('./dag')
exports.ping = require('./ping')
exports.pubsub = require('./pubsub')
exports.key = require('./key')
exports.stats = require('./stats')
Expand Down
225 changes: 225 additions & 0 deletions js/src/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const pull = require('pull-stream')
const pump = require('pump')
const { Writable } = require('stream')
const { spawnNodesWithId } = require('./utils/spawn')

const expect = chai.expect
chai.use(dirtyChai)

function expectIsPingResponse (obj) {
expect(obj).to.have.a.property('success')
expect(obj).to.have.a.property('time')
expect(obj).to.have.a.property('text')
expect(obj.success).to.be.a('boolean')
expect(obj.time).to.be.a('number')
expect(obj.text).to.be.a('string')
}

module.exports = (common) => {
describe('.ping', function () {
let ipfsdA
let ipfsdB

before(function (done) {
this.timeout(30 * 1000)

common.setup((err, factory) => {
if (err) return done(err)

spawnNodesWithId(2, factory, (err, nodes) => {
if (err) return done(err)
ipfsdA = nodes[0]
ipfsdB = nodes[1]
done()
})
})
})

after((done) => common.teardown(done))

describe('.ping', function () {
this.timeout(15 * 1000)

it('sends the specified number of packets', (done) => {
const count = 3
ipfsdA.ping(ipfsdB.peerId.id, { count }, (err, responses) => {
expect(err).to.not.exist()
responses.forEach(expectIsPingResponse)
const pongs = responses.filter(r => Boolean(r.time))
expect(pongs.length).to.equal(count)
done()
})
})

it('fails when pinging an unknown peer', (done) => {
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
const count = 2

ipfsdA.ping(unknownPeerId, { count }, (err, responses) => {
expect(err).to.exist()
expect(responses[0].text).to.include('Looking up')
expect(responses[1].success).to.be.false()
done()
})
})

it('fails when pinging an invalid peer', (done) => {
const invalidPeerId = 'not a peer ID'
const count = 2
ipfsdA.ping(invalidPeerId, { count }, (err, responses) => {
expect(err).to.exist()
expect(err.message).to.include('failed to parse peer address')
done()
})
})
})

describe('.pingPullStream', function () {
this.timeout(15 * 1000)

it('sends the specified number of packets', (done) => {
let packetNum = 0
const count = 3
pull(
ipfsdA.pingPullStream(ipfsdB.peerId.id, { count }),
pull.drain(({ success, time }) => {
expect(success).to.be.true()
// It's a pong
if (time) {
packetNum++
}
}, (err) => {
expect(err).to.not.exist()
expect(packetNum).to.equal(count)
done()
})
)
})

it('fails when pinging an unknown peer', (done) => {
let messageNum = 0
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
const count = 2
pull(
ipfsdA.pingPullStream(unknownPeerId, { count }),
pull.drain((res) => {
expectIsPingResponse(res)
messageNum++

// First message should be "looking up" response
if (messageNum === 1) {
expect(res.text).to.include('Looking up')
}

// Second message should be a failure response
if (messageNum === 2) {
expect(res.success).to.be.false()
}
}, (err) => {
expect(err).to.exist()
done()
})
)
})

it('fails when pinging an invalid peer', (done) => {
const invalidPeerId = 'not a peer ID'
const count = 2
pull(
ipfsdA.pingPullStream(invalidPeerId, { count }),
pull.collect((err) => {
expect(err).to.exist()
expect(err.message).to.include('failed to parse peer address')
done()
})
)
})
})

describe('.pingReadableStream', function () {
this.timeout(15 * 1000)

it('sends the specified number of packets', (done) => {
let packetNum = 0
const count = 3

pump(
ipfsdA.pingReadableStream(ipfsdB.peerId.id, { count }),
new Writable({
objectMode: true,
write ({ success, time }, enc, cb) {
expect(success).to.be.true()
// It's a pong
if (time) {
packetNum++
}

cb()
}
}),
(err) => {
expect(err).to.not.exist()
expect(packetNum).to.equal(count)
done()
}
)
})

it('fails when pinging an unknown peer', (done) => {
let messageNum = 0
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
const count = 2

pump(
ipfsdA.pingReadableStream(unknownPeerId, { count }),
new Writable({
objectMode: true,
write (res, enc, cb) {
expectIsPingResponse(res)
messageNum++

// First message should be "looking up" response
if (messageNum === 1) {
expect(res.text).to.include('Looking up')
}

// Second message should be a failure response
if (messageNum === 2) {
expect(res.success).to.be.false()
}

cb()
}
}),
(err) => {
expect(err).to.exist()
done()
}
)
})

it('fails when pinging an invalid peer', (done) => {
const invalidPeerId = 'not a peer ID'
const count = 2

pump(
ipfsdA.pingReadableStream(invalidPeerId, { count }),
new Writable({
objectMode: true,
write: (chunk, enc, cb) => cb()
}),
(err) => {
expect(err).to.exist()
expect(err.message).to.include('failed to parse peer address')
done()
}
)
})
})
})
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"multihashing-async": "~0.4.8",
"peer-id": "~0.10.7",
"peer-info": "^0.14.1",
"pull-stream": "^3.6.7"
"pull-stream": "^3.6.7",
"pump": "^3.0.0"
},
"devDependencies": {},
"contributors": [
Expand Down

0 comments on commit ef9658e

Please sign in to comment.