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 299
/
Copy pathping.spec.js
160 lines (135 loc) · 4.33 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* 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 ipfsClient = require('../src')
const PingMessageStream = require('../src/utils/ping-message-stream')
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 ipfsd
let other
let otherd
let otherId
before(async function () {
this.timeout(30 * 1000) // slow CI
ipfsd = await f.spawn({
initOptions: {
bits: 1024,
profile: 'test'
}
})
ipfs = ipfsClient(ipfsd.apiAddr)
otherd = await f.spawn({
initOptions: {
bits: 1024,
profile: 'test'
}
})
other = otherd.api
const ma = (await ipfs.id()).addresses[0]
await other.swarm.connect(ma)
otherId = (await other.id()).id
})
after(async () => {
if (ipfsd) {
await ipfsd.stop()
}
if (otherd) {
await otherd.stop()
}
})
it('.ping with default n', async () => {
const res = await ipfs.ping(otherId)
expect(res).to.be.an('array')
expect(res.filter(isPong)).to.have.lengthOf(1)
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('.ping with n = 2', async () => {
const res = await ipfs.ping(otherId, { n: 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('.ping fails with count & n', async function () {
this.timeout(20 * 1000)
await expect(ipfs.ping(otherId, { count: 2, n: 2 })).to.be.rejected()
})
it('.ping with Promises', async () => {
const res = await ipfs.ping(otherId)
expect(res).to.be.an('array')
expect(res.filter(isPong)).to.have.lengthOf(1)
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),
collect((err, data) => {
expect(err).to.not.exist()
expect(data).to.be.an('array')
expect(data.filter(isPong)).to.have.lengthOf(1)
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)
.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(1)
done()
})
})
it('message conversion fails if invalid message is received', () => {
const messageConverter = new PingMessageStream()
expect(() => {
messageConverter.write({ some: 'InvalidMessage' })
}).to.throw('Invalid ping message received')
})
})