2019-12-10 19:26:54 +01:00
|
|
|
'use strict'
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
2020-10-15 15:31:33 +01:00
|
|
|
const { expect } = require('aegir/utils/chai')
|
2019-12-10 19:26:54 +01:00
|
|
|
|
|
|
|
const pTimes = require('p-times')
|
2020-02-03 14:50:40 +00:00
|
|
|
const pipe = require('it-pipe')
|
2019-12-10 19:26:54 +01:00
|
|
|
|
|
|
|
const peerUtils = require('../utils/creators/peer')
|
|
|
|
const baseOptions = require('../utils/base-options')
|
2020-02-03 14:50:40 +00:00
|
|
|
const { PROTOCOL } = require('../../src/ping/constants')
|
2019-12-10 19:26:54 +01:00
|
|
|
|
|
|
|
describe('ping', () => {
|
|
|
|
let nodes
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
nodes = await peerUtils.createPeer({
|
2020-07-27 10:53:23 +02:00
|
|
|
number: 3,
|
2019-12-10 19:26:54 +01:00
|
|
|
config: baseOptions
|
|
|
|
})
|
2020-04-14 14:05:30 +02:00
|
|
|
|
2020-04-28 16:21:25 +02:00
|
|
|
nodes[0].peerStore.addressBook.set(nodes[1].peerId, nodes[1].multiaddrs)
|
|
|
|
nodes[1].peerStore.addressBook.set(nodes[0].peerId, nodes[0].multiaddrs)
|
2019-12-10 19:26:54 +01:00
|
|
|
})
|
|
|
|
|
2021-04-15 09:40:02 +02:00
|
|
|
afterEach(() => Promise.all(nodes.map(n => n.stop())))
|
|
|
|
|
2020-07-27 10:53:23 +02:00
|
|
|
it('ping once from peer0 to peer1 using a multiaddr', async () => {
|
|
|
|
const ma = `${nodes[2].multiaddrs[0]}/p2p/${nodes[2].peerId.toB58String()}`
|
|
|
|
const latency = await nodes[0].ping(ma)
|
|
|
|
|
|
|
|
expect(latency).to.be.a('Number')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('ping once from peer0 to peer1 using a peerId', async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
const latency = await nodes[0].ping(nodes[1].peerId)
|
2019-12-10 19:26:54 +01:00
|
|
|
|
|
|
|
expect(latency).to.be.a('Number')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('ping several times for getting an average', async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
const latencies = await pTimes(5, () => nodes[1].ping(nodes[0].peerId))
|
2019-12-10 19:26:54 +01:00
|
|
|
|
|
|
|
const averageLatency = latencies.reduce((p, c) => p + c, 0) / latencies.length
|
|
|
|
expect(averageLatency).to.be.a('Number')
|
|
|
|
})
|
2020-02-03 14:50:40 +00:00
|
|
|
|
|
|
|
it('only waits for the first response to arrive', async () => {
|
|
|
|
nodes[1].handle(PROTOCOL, async ({ connection, stream }) => {
|
|
|
|
let firstInvocation = true
|
|
|
|
|
|
|
|
await pipe(
|
|
|
|
stream,
|
|
|
|
function (stream) {
|
|
|
|
const output = {
|
|
|
|
[Symbol.asyncIterator]: () => output,
|
|
|
|
next: async () => {
|
|
|
|
if (firstInvocation) {
|
|
|
|
firstInvocation = false
|
|
|
|
|
2021-04-15 09:40:02 +02:00
|
|
|
// eslint-disable-next-line no-unreachable-loop
|
2020-02-03 14:50:40 +00:00
|
|
|
for await (const data of stream) {
|
|
|
|
return {
|
|
|
|
value: data,
|
|
|
|
done: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return new Promise() // never resolve
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
},
|
|
|
|
stream
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-04-14 14:05:30 +02:00
|
|
|
const latency = await nodes[0].ping(nodes[1].peerId)
|
2020-02-03 14:50:40 +00:00
|
|
|
|
|
|
|
expect(latency).to.be.a('Number')
|
|
|
|
})
|
2019-12-10 19:26:54 +01:00
|
|
|
})
|