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

Commit ef9658e

Browse files
authored
Merge pull request #273 from ipfs/test/ping
test: add ping, pingPullStream and pingReadableStream tests
2 parents fd09dce + 30badaa commit ef9658e

File tree

3 files changed

+228
-1
lines changed

3 files changed

+228
-1
lines changed

js/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ exports.swarm = require('./swarm')
1111
exports.block = require('./block')
1212
exports.dht = require('./dht')
1313
exports.dag = require('./dag')
14+
exports.ping = require('./ping')
1415
exports.pubsub = require('./pubsub')
1516
exports.key = require('./key')
1617
exports.stats = require('./stats')

js/src/ping.js

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const pull = require('pull-stream')
7+
const pump = require('pump')
8+
const { Writable } = require('stream')
9+
const { spawnNodesWithId } = require('./utils/spawn')
10+
11+
const expect = chai.expect
12+
chai.use(dirtyChai)
13+
14+
function expectIsPingResponse (obj) {
15+
expect(obj).to.have.a.property('success')
16+
expect(obj).to.have.a.property('time')
17+
expect(obj).to.have.a.property('text')
18+
expect(obj.success).to.be.a('boolean')
19+
expect(obj.time).to.be.a('number')
20+
expect(obj.text).to.be.a('string')
21+
}
22+
23+
module.exports = (common) => {
24+
describe('.ping', function () {
25+
let ipfsdA
26+
let ipfsdB
27+
28+
before(function (done) {
29+
this.timeout(30 * 1000)
30+
31+
common.setup((err, factory) => {
32+
if (err) return done(err)
33+
34+
spawnNodesWithId(2, factory, (err, nodes) => {
35+
if (err) return done(err)
36+
ipfsdA = nodes[0]
37+
ipfsdB = nodes[1]
38+
done()
39+
})
40+
})
41+
})
42+
43+
after((done) => common.teardown(done))
44+
45+
describe('.ping', function () {
46+
this.timeout(15 * 1000)
47+
48+
it('sends the specified number of packets', (done) => {
49+
const count = 3
50+
ipfsdA.ping(ipfsdB.peerId.id, { count }, (err, responses) => {
51+
expect(err).to.not.exist()
52+
responses.forEach(expectIsPingResponse)
53+
const pongs = responses.filter(r => Boolean(r.time))
54+
expect(pongs.length).to.equal(count)
55+
done()
56+
})
57+
})
58+
59+
it('fails when pinging an unknown peer', (done) => {
60+
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
61+
const count = 2
62+
63+
ipfsdA.ping(unknownPeerId, { count }, (err, responses) => {
64+
expect(err).to.exist()
65+
expect(responses[0].text).to.include('Looking up')
66+
expect(responses[1].success).to.be.false()
67+
done()
68+
})
69+
})
70+
71+
it('fails when pinging an invalid peer', (done) => {
72+
const invalidPeerId = 'not a peer ID'
73+
const count = 2
74+
ipfsdA.ping(invalidPeerId, { count }, (err, responses) => {
75+
expect(err).to.exist()
76+
expect(err.message).to.include('failed to parse peer address')
77+
done()
78+
})
79+
})
80+
})
81+
82+
describe('.pingPullStream', function () {
83+
this.timeout(15 * 1000)
84+
85+
it('sends the specified number of packets', (done) => {
86+
let packetNum = 0
87+
const count = 3
88+
pull(
89+
ipfsdA.pingPullStream(ipfsdB.peerId.id, { count }),
90+
pull.drain(({ success, time }) => {
91+
expect(success).to.be.true()
92+
// It's a pong
93+
if (time) {
94+
packetNum++
95+
}
96+
}, (err) => {
97+
expect(err).to.not.exist()
98+
expect(packetNum).to.equal(count)
99+
done()
100+
})
101+
)
102+
})
103+
104+
it('fails when pinging an unknown peer', (done) => {
105+
let messageNum = 0
106+
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
107+
const count = 2
108+
pull(
109+
ipfsdA.pingPullStream(unknownPeerId, { count }),
110+
pull.drain((res) => {
111+
expectIsPingResponse(res)
112+
messageNum++
113+
114+
// First message should be "looking up" response
115+
if (messageNum === 1) {
116+
expect(res.text).to.include('Looking up')
117+
}
118+
119+
// Second message should be a failure response
120+
if (messageNum === 2) {
121+
expect(res.success).to.be.false()
122+
}
123+
}, (err) => {
124+
expect(err).to.exist()
125+
done()
126+
})
127+
)
128+
})
129+
130+
it('fails when pinging an invalid peer', (done) => {
131+
const invalidPeerId = 'not a peer ID'
132+
const count = 2
133+
pull(
134+
ipfsdA.pingPullStream(invalidPeerId, { count }),
135+
pull.collect((err) => {
136+
expect(err).to.exist()
137+
expect(err.message).to.include('failed to parse peer address')
138+
done()
139+
})
140+
)
141+
})
142+
})
143+
144+
describe('.pingReadableStream', function () {
145+
this.timeout(15 * 1000)
146+
147+
it('sends the specified number of packets', (done) => {
148+
let packetNum = 0
149+
const count = 3
150+
151+
pump(
152+
ipfsdA.pingReadableStream(ipfsdB.peerId.id, { count }),
153+
new Writable({
154+
objectMode: true,
155+
write ({ success, time }, enc, cb) {
156+
expect(success).to.be.true()
157+
// It's a pong
158+
if (time) {
159+
packetNum++
160+
}
161+
162+
cb()
163+
}
164+
}),
165+
(err) => {
166+
expect(err).to.not.exist()
167+
expect(packetNum).to.equal(count)
168+
done()
169+
}
170+
)
171+
})
172+
173+
it('fails when pinging an unknown peer', (done) => {
174+
let messageNum = 0
175+
const unknownPeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
176+
const count = 2
177+
178+
pump(
179+
ipfsdA.pingReadableStream(unknownPeerId, { count }),
180+
new Writable({
181+
objectMode: true,
182+
write (res, enc, cb) {
183+
expectIsPingResponse(res)
184+
messageNum++
185+
186+
// First message should be "looking up" response
187+
if (messageNum === 1) {
188+
expect(res.text).to.include('Looking up')
189+
}
190+
191+
// Second message should be a failure response
192+
if (messageNum === 2) {
193+
expect(res.success).to.be.false()
194+
}
195+
196+
cb()
197+
}
198+
}),
199+
(err) => {
200+
expect(err).to.exist()
201+
done()
202+
}
203+
)
204+
})
205+
206+
it('fails when pinging an invalid peer', (done) => {
207+
const invalidPeerId = 'not a peer ID'
208+
const count = 2
209+
210+
pump(
211+
ipfsdA.pingReadableStream(invalidPeerId, { count }),
212+
new Writable({
213+
objectMode: true,
214+
write: (chunk, enc, cb) => cb()
215+
}),
216+
(err) => {
217+
expect(err).to.exist()
218+
expect(err.message).to.include('failed to parse peer address')
219+
done()
220+
}
221+
)
222+
})
223+
})
224+
})
225+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"multihashing-async": "~0.4.8",
5353
"peer-id": "~0.10.7",
5454
"peer-info": "^0.14.1",
55-
"pull-stream": "^3.6.7"
55+
"pull-stream": "^3.6.7",
56+
"pump": "^3.0.0"
5657
},
5758
"devDependencies": {},
5859
"contributors": [

0 commit comments

Comments
 (0)