This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathping.spec.js
94 lines (80 loc) · 2.67 KB
/
ping.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* eslint-env mocha */
'use strict'
const { expect } = require('interface-ipfs-core/src/utils/mocha')
const pull = require('pull-stream/pull')
const collect = require('pull-stream/sinks/collect')
const f = require('./utils/factory')
// Determine if a ping response object is a pong, or something else, like a status message
function isPong (pingResponse) {
return Boolean(pingResponse && pingResponse.success && !pingResponse.text)
}
describe('.ping', function () {
this.timeout(20 * 1000)
let ipfs
let other
let otherId
before(async function () {
this.timeout(30 * 1000) // slow CI
ipfs = (await f.spawn()).api
other = (await f.spawn()).api
const ma = (await ipfs.id()).addresses[0]
await other.swarm.connect(ma)
otherId = (await other.id()).id
})
after(() => f.clean())
it('.ping with default count', async () => {
const res = await ipfs.ping(otherId)
expect(res).to.be.an('array')
expect(res.filter(isPong)).to.have.lengthOf(10)
res.forEach(packet => {
expect(packet).to.have.keys('success', 'time', 'text')
expect(packet.time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.text.includes('Average latency'))
expect(resultMsg).to.exist()
})
it('.ping with count = 2', async () => {
const res = await ipfs.ping(otherId, { count: 2 })
expect(res).to.be.an('array')
expect(res.filter(isPong)).to.have.lengthOf(2)
res.forEach(packet => {
expect(packet).to.have.keys('success', 'time', 'text')
expect(packet.time).to.be.a('number')
})
const resultMsg = res.find(packet => packet.text.includes('Average latency'))
expect(resultMsg).to.exist()
})
it('.pingPullStream', (done) => {
pull(
ipfs.pingPullStream(otherId, { count: 2 }),
collect((err, data) => {
expect(err).to.not.exist()
expect(data).to.be.an('array')
expect(data.filter(isPong)).to.have.lengthOf(2)
data.forEach(packet => {
expect(packet).to.have.keys('success', 'time', 'text')
expect(packet.time).to.be.a('number')
})
const resultMsg = data.find(packet => packet.text.includes('Average latency'))
expect(resultMsg).to.exist()
done()
})
)
})
it('.pingReadableStream', (done) => {
let packetNum = 0
ipfs.pingReadableStream(otherId, { count: 2 })
.on('data', data => {
expect(data).to.be.an('object')
expect(data).to.have.keys('success', 'time', 'text')
if (isPong(data)) packetNum++
})
.on('error', err => {
expect(err).not.to.exist()
})
.on('end', () => {
expect(packetNum).to.equal(2)
done()
})
})
})