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

Commit c0edbc5

Browse files
authored
Merge pull request #311 from ipfs/fix/ping-tests-discovery
fix: do not rely on discovery for ping tests
2 parents f238e9f + 3acd6fd commit c0edbc5

File tree

3 files changed

+28
-75
lines changed

3 files changed

+28
-75
lines changed

Diff for: js/src/ping.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const pump = require('pump')
88
const { Writable } = require('stream')
99
const series = require('async/series')
1010
const { spawnNodesWithId } = require('./utils/spawn')
11-
const { waitUntilConnected } = require('./utils/connections')
1211

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

3029
module.exports = (common) => {
3130
describe('.ping', function () {
32-
let ipfsdA
33-
let ipfsdB
31+
let ipfsA
32+
let ipfsB
3433

3534
before(function (done) {
3635
this.timeout(60 * 1000)
@@ -42,12 +41,12 @@ module.exports = (common) => {
4241
(cb) => {
4342
spawnNodesWithId(2, factory, (err, nodes) => {
4443
if (err) return cb(err)
45-
ipfsdA = nodes[0]
46-
ipfsdB = nodes[1]
44+
ipfsA = nodes[0]
45+
ipfsB = nodes[1]
4746
cb()
4847
})
4948
},
50-
(cb) => waitUntilConnected(ipfsdA, ipfsdB, cb)
49+
(cb) => ipfsA.swarm.connect(ipfsB.peerId.addresses[0], cb)
5150
], done)
5251
})
5352
})
@@ -59,7 +58,7 @@ module.exports = (common) => {
5958

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

75-
ipfsdA.ping(unknownPeerId, { count }, (err, responses) => {
74+
ipfsA.ping(unknownPeerId, { count }, (err, responses) => {
7675
expect(err).to.exist()
7776
expect(responses[0].text).to.include('Looking up')
7877
expect(responses[1].success).to.be.false()
@@ -83,7 +82,7 @@ module.exports = (common) => {
8382
it('fails when pinging an invalid peer', (done) => {
8483
const invalidPeerId = 'not a peer ID'
8584
const count = 2
86-
ipfsdA.ping(invalidPeerId, { count }, (err, responses) => {
85+
ipfsA.ping(invalidPeerId, { count }, (err, responses) => {
8786
expect(err).to.exist()
8887
expect(err.message).to.include('failed to parse peer address')
8988
done()
@@ -98,7 +97,7 @@ module.exports = (common) => {
9897
let packetNum = 0
9998
const count = 3
10099
pull(
101-
ipfsdA.pingPullStream(ipfsdB.peerId.id, { count }),
100+
ipfsA.pingPullStream(ipfsB.peerId.id, { count }),
102101
pull.drain((res) => {
103102
expect(res.success).to.be.true()
104103
// It's a pong
@@ -118,7 +117,7 @@ module.exports = (common) => {
118117
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
119118
const count = 2
120119
pull(
121-
ipfsdA.pingPullStream(unknownPeerId, { count }),
120+
ipfsA.pingPullStream(unknownPeerId, { count }),
122121
pull.drain((res) => {
123122
expectIsPingResponse(res)
124123
messageNum++
@@ -143,7 +142,7 @@ module.exports = (common) => {
143142
const invalidPeerId = 'not a peer ID'
144143
const count = 2
145144
pull(
146-
ipfsdA.pingPullStream(invalidPeerId, { count }),
145+
ipfsA.pingPullStream(invalidPeerId, { count }),
147146
pull.collect((err) => {
148147
expect(err).to.exist()
149148
expect(err.message).to.include('failed to parse peer address')
@@ -161,7 +160,7 @@ module.exports = (common) => {
161160
const count = 3
162161

163162
pump(
164-
ipfsdA.pingReadableStream(ipfsdB.peerId.id, { count }),
163+
ipfsA.pingReadableStream(ipfsB.peerId.id, { count }),
165164
new Writable({
166165
objectMode: true,
167166
write (res, enc, cb) {
@@ -188,7 +187,7 @@ module.exports = (common) => {
188187
const count = 2
189188

190189
pump(
191-
ipfsdA.pingReadableStream(unknownPeerId, { count }),
190+
ipfsA.pingReadableStream(unknownPeerId, { count }),
192191
new Writable({
193192
objectMode: true,
194193
write (res, enc, cb) {
@@ -220,7 +219,7 @@ module.exports = (common) => {
220219
const count = 2
221220

222221
pump(
223-
ipfsdA.pingReadableStream(invalidPeerId, { count }),
222+
ipfsA.pingReadableStream(invalidPeerId, { count }),
224223
new Writable({
225224
objectMode: true,
226225
write: (chunk, enc, cb) => cb()

Diff for: js/src/utils/connections.js

-51
This file was deleted.

Diff for: js/src/utils/spawn.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
const waterfall = require('async/waterfall')
22
const timesSeries = require('async/timesSeries')
3+
const map = require('async/map')
4+
5+
function identify (node, cb) {
6+
node.id((err, id) => {
7+
if (err) return cb(err)
8+
node.peerId = id
9+
cb(null, node)
10+
})
11+
}
312

413
// Spawn a node, get it's id and set it as `peerId` on the node
514
function spawnNodeWithId (factory, callback) {
6-
waterfall([
7-
(cb) => factory.spawnNode(cb),
8-
(node, cb) => node.id((err, id) => {
9-
if (err) return cb(err)
10-
node.peerId = id
11-
cb(null, node)
12-
})
13-
], callback)
15+
waterfall([(cb) => factory.spawnNode(cb), identify], callback)
1416
}
1517

1618
exports.spawnNodeWithId = spawnNodeWithId
@@ -24,7 +26,10 @@ exports.spawnNodes = spawnNodes
2426

2527
// Spawn n nodes, getting their id's and setting them as `peerId` on the nodes
2628
function spawnNodesWithId (n, factory, callback) {
27-
timesSeries(n, (_, cb) => spawnNodeWithId(factory, cb), callback)
29+
spawnNodes(n, factory, (err, nodes) => {
30+
if (err) return callback(err)
31+
map(nodes, identify, callback)
32+
})
2833
}
2934

3035
exports.spawnNodesWithId = spawnNodesWithId

0 commit comments

Comments
 (0)