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

fix: do not rely on discovery for ping tests #311

Merged
merged 1 commit into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions js/src/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const pump = require('pump')
const { Writable } = require('stream')
const series = require('async/series')
const { spawnNodesWithId } = require('./utils/spawn')
const { waitUntilConnected } = require('./utils/connections')

const expect = chai.expect
chai.use(dirtyChai)
Expand All @@ -29,8 +28,8 @@ function isPong (pingResponse) {

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

before(function (done) {
this.timeout(60 * 1000)
Expand All @@ -42,12 +41,12 @@ module.exports = (common) => {
(cb) => {
spawnNodesWithId(2, factory, (err, nodes) => {
if (err) return cb(err)
ipfsdA = nodes[0]
ipfsdB = nodes[1]
ipfsA = nodes[0]
ipfsB = nodes[1]
cb()
})
},
(cb) => waitUntilConnected(ipfsdA, ipfsdB, cb)
(cb) => ipfsA.swarm.connect(ipfsB.peerId.addresses[0], cb)
], done)
})
})
Expand All @@ -59,7 +58,7 @@ module.exports = (common) => {

it('sends the specified number of packets', (done) => {
const count = 3
ipfsdA.ping(ipfsdB.peerId.id, { count }, (err, responses) => {
ipfsA.ping(ipfsB.peerId.id, { count }, (err, responses) => {
expect(err).to.not.exist()
responses.forEach(expectIsPingResponse)
const pongs = responses.filter(isPong)
Expand All @@ -72,7 +71,7 @@ module.exports = (common) => {
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
const count = 2

ipfsdA.ping(unknownPeerId, { count }, (err, responses) => {
ipfsA.ping(unknownPeerId, { count }, (err, responses) => {
expect(err).to.exist()
expect(responses[0].text).to.include('Looking up')
expect(responses[1].success).to.be.false()
Expand All @@ -83,7 +82,7 @@ module.exports = (common) => {
it('fails when pinging an invalid peer', (done) => {
const invalidPeerId = 'not a peer ID'
const count = 2
ipfsdA.ping(invalidPeerId, { count }, (err, responses) => {
ipfsA.ping(invalidPeerId, { count }, (err, responses) => {
expect(err).to.exist()
expect(err.message).to.include('failed to parse peer address')
done()
Expand All @@ -98,7 +97,7 @@ module.exports = (common) => {
let packetNum = 0
const count = 3
pull(
ipfsdA.pingPullStream(ipfsdB.peerId.id, { count }),
ipfsA.pingPullStream(ipfsB.peerId.id, { count }),
pull.drain((res) => {
expect(res.success).to.be.true()
// It's a pong
Expand All @@ -118,7 +117,7 @@ module.exports = (common) => {
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
const count = 2
pull(
ipfsdA.pingPullStream(unknownPeerId, { count }),
ipfsA.pingPullStream(unknownPeerId, { count }),
pull.drain((res) => {
expectIsPingResponse(res)
messageNum++
Expand All @@ -143,7 +142,7 @@ module.exports = (common) => {
const invalidPeerId = 'not a peer ID'
const count = 2
pull(
ipfsdA.pingPullStream(invalidPeerId, { count }),
ipfsA.pingPullStream(invalidPeerId, { count }),
pull.collect((err) => {
expect(err).to.exist()
expect(err.message).to.include('failed to parse peer address')
Expand All @@ -161,7 +160,7 @@ module.exports = (common) => {
const count = 3

pump(
ipfsdA.pingReadableStream(ipfsdB.peerId.id, { count }),
ipfsA.pingReadableStream(ipfsB.peerId.id, { count }),
new Writable({
objectMode: true,
write (res, enc, cb) {
Expand All @@ -188,7 +187,7 @@ module.exports = (common) => {
const count = 2

pump(
ipfsdA.pingReadableStream(unknownPeerId, { count }),
ipfsA.pingReadableStream(unknownPeerId, { count }),
new Writable({
objectMode: true,
write (res, enc, cb) {
Expand Down Expand Up @@ -220,7 +219,7 @@ module.exports = (common) => {
const count = 2

pump(
ipfsdA.pingReadableStream(invalidPeerId, { count }),
ipfsA.pingReadableStream(invalidPeerId, { count }),
new Writable({
objectMode: true,
write: (chunk, enc, cb) => cb()
Expand Down
51 changes: 0 additions & 51 deletions js/src/utils/connections.js

This file was deleted.

23 changes: 14 additions & 9 deletions js/src/utils/spawn.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const waterfall = require('async/waterfall')
const timesSeries = require('async/timesSeries')
const map = require('async/map')

function identify (node, cb) {
node.id((err, id) => {
if (err) return cb(err)
node.peerId = id
cb(null, node)
})
}

// Spawn a node, get it's id and set it as `peerId` on the node
function spawnNodeWithId (factory, callback) {
waterfall([
(cb) => factory.spawnNode(cb),
(node, cb) => node.id((err, id) => {
if (err) return cb(err)
node.peerId = id
cb(null, node)
})
], callback)
waterfall([(cb) => factory.spawnNode(cb), identify], callback)
}

exports.spawnNodeWithId = spawnNodeWithId
Expand All @@ -24,7 +26,10 @@ exports.spawnNodes = spawnNodes

// Spawn n nodes, getting their id's and setting them as `peerId` on the nodes
function spawnNodesWithId (n, factory, callback) {
timesSeries(n, (_, cb) => spawnNodeWithId(factory, cb), callback)
spawnNodes(n, factory, (err, nodes) => {
if (err) return callback(err)
map(nodes, identify, callback)
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies the change in this file should have been proposed a separate PR - this simply ensures the ID calls are done in parallel which will speed up the tests a little.

}

exports.spawnNodesWithId = spawnNodesWithId