Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit e5ac2d3

Browse files
committed
chore: add ping cli test
1 parent 719f689 commit e5ac2d3

File tree

2 files changed

+75
-25
lines changed

2 files changed

+75
-25
lines changed

test/cli/commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('commands', () => runOnAndOff((thing) => {
1414
ipfs = thing.ipfs
1515
})
1616

17-
it.only('list the commands', () => {
17+
it('list the commands', () => {
1818
return ipfs('commands').then((out) => {
1919
expect(out.split('\n')).to.have.length(commandCount + 1)
2020
})

test/cli/ping.js

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,13 @@ const config = {
2424
}
2525

2626
describe.only('ping', function () {
27-
this.timeout(80 * 1000)
28-
27+
this.timeout(60 * 1000)
2928
let ipfsdA
3029
let ipfsdB
30+
let bMultiaddr
3131
let ipfsdBId
3232
let cli
3333

34-
before(function (done) {
35-
this.timeout(60 * 1000)
36-
37-
df.spawn({
38-
exec: './src/cli/bin.js',
39-
config,
40-
initoptions: { bits: 512 }
41-
}, (err, _ipfsd) => {
42-
expect(err).to.not.exist()
43-
ipfsdA = _ipfsd
44-
done()
45-
})
46-
})
47-
4834
before((done) => {
4935
this.timeout(60 * 1000)
5036
series([
@@ -62,38 +48,102 @@ describe.only('ping', function () {
6248
(cb) => {
6349
ipfsdB.api.id((err, peerInfo) => {
6450
expect(err).to.not.exist()
65-
console.log(peerInfo)
6651
ipfsdBId = peerInfo.id
52+
bMultiaddr = peerInfo.addresses[0]
6753
cb()
6854
})
6955
}
7056
], done)
7157
})
7258

73-
after((done) => ipfsdA.stop(done))
74-
after((done) => ipfsdB.stop(done))
59+
before(function (done) {
60+
this.timeout(60 * 1000)
61+
62+
df.spawn({
63+
exec: './src/cli/bin.js',
64+
config,
65+
initoptions: { bits: 512 }
66+
}, (err, _ipfsd) => {
67+
expect(err).to.not.exist()
68+
ipfsdA = _ipfsd
69+
ipfsdA.api.swarm.connect(bMultiaddr, done)
70+
})
71+
})
7572

7673
before((done) => {
74+
this.timeout(60 * 1000)
7775
cli = ipfsExec(ipfsdA.repoPath)
7876
done()
7977
})
8078

79+
after((done) => ipfsdA.stop(done))
80+
after((done) => ipfsdB.stop(done))
81+
8182
it('ping host', (done) => {
83+
this.timeout(60 * 1000)
8284
const ping = cli(`ping ${ipfsdBId}`)
8385
const result = []
84-
ping.stdout.on('data', (packet) => {
85-
console.log('ON DATA')
86-
result.push(packet.toString())
86+
ping.stdout.on('data', (output) => {
87+
const packets = output.toString().split('\n').slice(0, -1)
88+
result.push(...packets)
8789
})
8890

89-
ping.stdout.on('end', (c) => {
90-
console.log('END', result)
91+
ping.stdout.on('end', () => {
92+
expect(result).to.have.lengthOf(12)
93+
expect(result[0]).to.equal(`PING ${ipfsdBId}`)
94+
for(let i = 1; i < 11; i++) {
95+
expect(result[i]).to.match(/^Pong received: time=\d+ ms$/)
96+
}
97+
expect(result[11]).to.match(/^Average latency: \d+(.\d+)?ms$/)
9198
done()
9299
})
93100

94101
ping.catch((err) => {
95102
expect(err).to.not.exist()
103+
})
104+
})
105+
106+
it('ping host with --n option', (done) => {
107+
this.timeout(60 * 1000)
108+
const ping = cli(`ping --n 1 ${ipfsdBId}`)
109+
const result = []
110+
ping.stdout.on('data', (output) => {
111+
const packets = output.toString().split('\n').slice(0, -1)
112+
result.push(...packets)
113+
})
114+
115+
ping.stdout.on('end', () => {
116+
expect(result).to.have.lengthOf(3)
117+
expect(result[0]).to.equal(`PING ${ipfsdBId}`)
118+
expect(result[1]).to.match(/^Pong received: time=\d+ ms$/)
119+
expect(result[2]).to.match(/^Average latency: \d+(.\d+)?ms$/)
96120
done()
97121
})
122+
123+
ping.catch((err) => {
124+
expect(err).to.not.exist()
125+
})
126+
})
127+
128+
it('ping host with --count option', (done) => {
129+
this.timeout(60 * 1000)
130+
const ping = cli(`ping --count 1 ${ipfsdBId}`)
131+
const result = []
132+
ping.stdout.on('data', (output) => {
133+
const packets = output.toString().split('\n').slice(0, -1)
134+
result.push(...packets)
135+
})
136+
137+
ping.stdout.on('end', () => {
138+
expect(result).to.have.lengthOf(3)
139+
expect(result[0]).to.equal(`PING ${ipfsdBId}`)
140+
expect(result[1]).to.match(/^Pong received: time=\d+ ms$/)
141+
expect(result[2]).to.match(/^Average latency: \d+(.\d+)?ms$/)
142+
done()
143+
})
144+
145+
ping.catch((err) => {
146+
expect(err).to.not.exist()
147+
})
98148
})
99149
})

0 commit comments

Comments
 (0)