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

Commit 2181568

Browse files
committed
fix(ping): tests were failing and there it was missing to catch when count and n are used at the same time
1 parent 427322d commit 2181568

File tree

2 files changed

+85
-210
lines changed

2 files changed

+85
-210
lines changed

src/ping.js

+6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ module.exports = (arg) => {
1212
callback = opts
1313
opts = {}
1414
}
15+
16+
if (opts.n && opts.count) {
17+
return callback(new Error('Use either n or count, not both'))
18+
}
19+
1520
// Default number of packtes to 1
1621
if (!opts.n && !opts.count) {
1722
opts.n = 1
1823
}
24+
1925
const request = {
2026
path: 'ping',
2127
args: id,

test/ping.spec.js

+79-210
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ describe('.ping', function () {
3434
})
3535
},
3636
(cb) => {
37-
console.log('going to spawn second node')
3837
f.spawn({ initOptions: { bits: 1024 } }, (err, node) => {
3938
expect(err).to.not.exist()
4039
other = node.api
@@ -66,236 +65,106 @@ describe('.ping', function () {
6665
], done)
6766
})
6867

69-
describe('callback API', () => {
70-
it('ping another peer with default packet count', (done) => {
71-
ipfs.ping(otherId, (err, res) => {
72-
expect(err).to.not.exist()
73-
expect(res).to.be.an('array')
74-
expect(res).to.have.lengthOf(3)
75-
res.forEach(packet => {
76-
expect(packet).to.have.keys('Success', 'Time', 'Text')
77-
expect(packet.Time).to.be.a('number')
78-
})
79-
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
80-
expect(resultMsg).to.exist()
81-
done()
68+
it('.ping with default n', (done) => {
69+
ipfs.ping(otherId, (err, res) => {
70+
expect(err).to.not.exist()
71+
expect(res).to.be.an('array')
72+
expect(res).to.have.lengthOf(3)
73+
res.forEach(packet => {
74+
expect(packet).to.have.keys('Success', 'Time', 'Text')
75+
expect(packet.Time).to.be.a('number')
8276
})
77+
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
78+
expect(resultMsg).to.exist()
79+
done()
8380
})
81+
})
8482

85-
it('ping another peer with a specifc packet count through parameter count', (done) => {
86-
ipfs.ping(otherId, {count: 3}, (err, res) => {
87-
expect(err).to.not.exist()
83+
it('.ping with count = 2', (done) => {
84+
ipfs.ping(otherId, { count: 2 }, (err, res) => {
85+
expect(err).to.not.exist()
86+
expect(res).to.be.an('array')
87+
expect(res).to.have.lengthOf(4)
88+
res.forEach(packet => {
89+
expect(packet).to.have.keys('Success', 'Time', 'Text')
90+
expect(packet.Time).to.be.a('number')
91+
})
92+
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
93+
expect(resultMsg).to.exist()
94+
done()
95+
})
96+
})
97+
98+
it('.ping with n = 2', (done) => {
99+
ipfs.ping(otherId, { n: 2 }, (err, res) => {
100+
expect(err).to.not.exist()
101+
expect(res).to.be.an('array')
102+
expect(res).to.have.lengthOf(4)
103+
res.forEach(packet => {
104+
expect(packet).to.have.keys('Success', 'Time', 'Text')
105+
expect(packet.Time).to.be.a('number')
106+
})
107+
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
108+
expect(resultMsg).to.exist()
109+
done()
110+
})
111+
})
112+
113+
it('.ping fails with count & n', function (done) {
114+
this.timeout(20 * 1000)
115+
116+
ipfs.ping(otherId, {count: 2, n: 2}, (err, res) => {
117+
expect(err).to.exist()
118+
done()
119+
})
120+
})
121+
122+
it('.ping with Promises', () => {
123+
return ipfs.ping(otherId)
124+
.then((res) => {
88125
expect(res).to.be.an('array')
89-
expect(res).to.have.lengthOf(5)
126+
expect(res).to.have.lengthOf(3)
90127
res.forEach(packet => {
91128
expect(packet).to.have.keys('Success', 'Time', 'Text')
92129
expect(packet.Time).to.be.a('number')
93130
})
94131
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
95132
expect(resultMsg).to.exist()
96-
done()
97133
})
98-
})
134+
})
99135

100-
it('ping another peer with a specifc packet count through parameter n', (done) => {
101-
ipfs.ping(otherId, {n: 3}, (err, res) => {
136+
it('.pingPullStream', (done) => {
137+
pull(
138+
ipfs.pingPullStream(otherId),
139+
collect((err, data) => {
102140
expect(err).to.not.exist()
103-
expect(res).to.be.an('array')
104-
expect(res).to.have.lengthOf(5)
105-
res.forEach(packet => {
141+
expect(data).to.be.an('array')
142+
expect(data).to.have.lengthOf(3)
143+
data.forEach(packet => {
106144
expect(packet).to.have.keys('Success', 'Time', 'Text')
107145
expect(packet.Time).to.be.a('number')
108146
})
109-
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
147+
const resultMsg = data.find(packet => packet.Text.includes('Average latency'))
110148
expect(resultMsg).to.exist()
111149
done()
112150
})
113-
})
151+
)
152+
})
114153

115-
it('sending both n and count should fail', (done) => {
116-
ipfs.ping(otherId, {count: 10, n: 10}, (err, res) => {
117-
expect(err).to.exist()
154+
it('.pingReadableStream', (done) => {
155+
let packetNum = 0
156+
ipfs.pingReadableStream(otherId)
157+
.on('data', data => {
158+
packetNum++
159+
expect(data).to.be.an('object')
160+
expect(data).to.have.keys('Success', 'Time', 'Text')
161+
})
162+
.on('error', err => {
163+
expect(err).not.to.exist()
164+
})
165+
.on('end', () => {
166+
expect(packetNum).to.equal(3)
118167
done()
119168
})
120-
})
121-
})
122-
123-
describe('promise API', () => {
124-
it('ping another peer with default packet count', () => {
125-
return ipfs.ping(otherId)
126-
.then((res) => {
127-
expect(res).to.be.an('array')
128-
expect(res).to.have.lengthOf(3)
129-
res.forEach(packet => {
130-
expect(packet).to.have.keys('Success', 'Time', 'Text')
131-
expect(packet.Time).to.be.a('number')
132-
})
133-
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
134-
expect(resultMsg).to.exist()
135-
})
136-
})
137-
138-
it('ping another peer with a specifc packet count through parameter count', () => {
139-
return ipfs.ping(otherId, {count: 3})
140-
.then((res) => {
141-
expect(res).to.be.an('array')
142-
expect(res).to.have.lengthOf(5)
143-
res.forEach(packet => {
144-
expect(packet).to.have.keys('Success', 'Time', 'Text')
145-
expect(packet.Time).to.be.a('number')
146-
})
147-
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
148-
expect(resultMsg).to.exist()
149-
})
150-
})
151-
152-
it('ping another peer with a specifc packet count through parameter n', () => {
153-
return ipfs.ping(otherId, {n: 3})
154-
.then((res) => {
155-
expect(res).to.be.an('array')
156-
expect(res).to.have.lengthOf(5)
157-
res.forEach(packet => {
158-
expect(packet).to.have.keys('Success', 'Time', 'Text')
159-
expect(packet.Time).to.be.a('number')
160-
})
161-
const resultMsg = res.find(packet => packet.Text.includes('Average latency'))
162-
expect(resultMsg).to.exist()
163-
})
164-
})
165-
166-
it('sending both n and count should fail', (done) => {
167-
ipfs.ping(otherId, {n: 3, count: 3})
168-
.catch(err => {
169-
expect(err).to.exist()
170-
done()
171-
})
172-
})
173-
})
174-
175-
describe('pull stream API', () => {
176-
it('ping another peer with the default packet count', (done) => {
177-
pull(
178-
ipfs.pingPullStream(otherId),
179-
collect((err, data) => {
180-
expect(err).to.not.exist()
181-
expect(data).to.be.an('array')
182-
expect(data).to.have.lengthOf(3)
183-
data.forEach(packet => {
184-
expect(packet).to.have.keys('Success', 'Time', 'Text')
185-
expect(packet.Time).to.be.a('number')
186-
})
187-
const resultMsg = data.find(packet => packet.Text.includes('Average latency'))
188-
expect(resultMsg).to.exist()
189-
done()
190-
})
191-
)
192-
})
193-
194-
it('ping another peer with a specifc packet count through parameter count', (done) => {
195-
pull(
196-
ipfs.pingPullStream(otherId, {count: 3}),
197-
collect((err, data) => {
198-
expect(err).to.not.exist()
199-
expect(data).to.be.an('array')
200-
expect(data).to.have.lengthOf(5)
201-
data.forEach(packet => {
202-
expect(packet).to.have.keys('Success', 'Time', 'Text')
203-
expect(packet.Time).to.be.a('number')
204-
})
205-
const resultMsg = data.find(packet => packet.Text.includes('Average latency'))
206-
expect(resultMsg).to.exist()
207-
done()
208-
})
209-
)
210-
})
211-
212-
it('ping another peer with a specifc packet count through parameter n', (done) => {
213-
pull(
214-
ipfs.pingPullStream(otherId, {n: 3}),
215-
collect((err, data) => {
216-
expect(err).to.not.exist()
217-
expect(data).to.be.an('array')
218-
expect(data).to.have.lengthOf(5)
219-
data.forEach(packet => {
220-
expect(packet).to.have.keys('Success', 'Time', 'Text')
221-
expect(packet.Time).to.be.a('number')
222-
})
223-
const resultMsg = data.find(packet => packet.Text.includes('Average latency'))
224-
expect(resultMsg).to.exist()
225-
done()
226-
})
227-
)
228-
})
229-
230-
it('sending both n and count should fail', (done) => {
231-
pull(
232-
ipfs.pingPullStream(otherId, {n: 3, count: 3}),
233-
collect(err => {
234-
expect(err).to.exist()
235-
done()
236-
})
237-
)
238-
})
239-
})
240-
241-
describe('readable stream API', () => {
242-
it('ping another peer with the default packet count', (done) => {
243-
let packetNum = 0
244-
ipfs.pingReadableStream(otherId)
245-
.on('data', data => {
246-
packetNum++
247-
expect(data).to.be.an('object')
248-
expect(data).to.have.keys('Success', 'Time', 'Text')
249-
})
250-
.on('error', err => {
251-
expect(err).not.to.exist()
252-
})
253-
.on('end', () => {
254-
expect(packetNum).to.equal(3)
255-
done()
256-
})
257-
})
258-
259-
it('ping another peer with a specifc packet count through parameter count', (done) => {
260-
let packetNum = 0
261-
ipfs.pingReadableStream(otherId, {count: 3})
262-
.on('data', data => {
263-
packetNum++
264-
expect(data).to.be.an('object')
265-
expect(data).to.have.keys('Success', 'Time', 'Text')
266-
})
267-
.on('error', err => {
268-
expect(err).not.to.exist()
269-
})
270-
.on('end', () => {
271-
expect(packetNum).to.equal(5)
272-
done()
273-
})
274-
})
275-
276-
it('ping another peer with a specifc packet count through parameter n', (done) => {
277-
let packetNum = 0
278-
ipfs.pingReadableStream(otherId, {n: 3})
279-
.on('data', data => {
280-
packetNum++
281-
expect(data).to.be.an('object')
282-
expect(data).to.have.keys('Success', 'Time', 'Text')
283-
})
284-
.on('error', err => {
285-
expect(err).not.to.exist()
286-
})
287-
.on('end', () => {
288-
expect(packetNum).to.equal(5)
289-
done()
290-
})
291-
})
292-
293-
it('sending both n and count should fail', (done) => {
294-
ipfs.pingReadableStream(otherId, {n: 3, count: 3})
295-
.on('error', err => {
296-
expect(err).to.exist()
297-
done()
298-
})
299-
})
300169
})
301170
})

0 commit comments

Comments
 (0)