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
195 lines (177 loc) · 5.32 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const pull = require('pull-stream')
const collect = require('pull-stream/sinks/collect')
const expect = chai.expect
chai.use(dirtyChai)
const parallel = require('async/parallel')
const series = require('async/series')
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(function (done) {
this.timeout(30 * 1000) // slow CI
series([
(cb) => {
f.spawn({ initOptions: { bits: 1024, profile: 'test' } }, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = ipfsClient(_ipfsd.apiAddr)
cb()
})
},
(cb) => {
f.spawn({ initOptions: { bits: 1024, profile: 'test' } }, (err, node) => {
expect(err).to.not.exist()
other = node.api
otherd = node
cb()
})
},
(cb) => {
parallel([
(cb) => {
ipfs.id((err, id) => {
expect(err).to.not.exist()
const ma = id.addresses[0]
other.swarm.connect(ma, cb)
})
},
(cb) => {
other.id((err, id) => {
expect(err).to.not.exist()
otherId = id.id
cb()
})
}
], cb)
}
], done)
})
after((done) => {
parallel([
(cb) => {
if (!ipfsd) return cb()
ipfsd.stop(cb)
},
(cb) => {
if (!otherd) return cb()
otherd.stop(cb)
}
], done)
})
it('.ping with default n', (done) => {
ipfs.ping(otherId, (err, res) => {
expect(err).to.not.exist()
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()
done()
})
})
it('.ping with count = 2', (done) => {
ipfs.ping(otherId, { count: 2 }, (err, res) => {
expect(err).to.not.exist()
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()
done()
})
})
it('.ping with n = 2', (done) => {
ipfs.ping(otherId, { n: 2 }, (err, res) => {
expect(err).to.not.exist()
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()
done()
})
})
it('.ping fails with count & n', function (done) {
this.timeout(20 * 1000)
ipfs.ping(otherId, { count: 2, n: 2 }, (err, res) => {
expect(err).to.exist()
done()
})
})
it('.ping with Promises', () => {
return ipfs.ping(otherId)
.then((res) => {
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')
})
})