This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathping.js
61 lines (50 loc) · 1.82 KB
/
ping.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
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import all from 'it-all'
import { isWebWorker } from 'ipfs-utils/src/env.js'
import { peerIdFromString } from '@libp2p/peer-id'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testPing (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.ping', function () {
this.timeout(60 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfsA
/** @type {import('ipfs-core-types').IPFS} */
let ipfsB
/** @type {import('ipfs-core-types/src/root').IDResult} */
let nodeBId
before(async () => {
ipfsA = (await factory.spawn({ type: 'proc' })).api
// webworkers are not dialable because webrtc is not available
ipfsB = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api
nodeBId = await ipfsB.id()
await ipfsA.swarm.connect(nodeBId.addresses[0])
})
after(() => factory.clean())
it('should send the specified number of packets', async () => {
const count = 3
const responses = await all(ipfsA.ping(nodeBId.id, { count }))
expect(responses.length).to.be.ok()
expect(responses[0].success).to.be.true()
})
it('should fail when pinging a peer that is not available', () => {
const notAvailablePeerId = peerIdFromString('QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn')
const count = 2
return expect(all(ipfsA.ping(notAvailablePeerId, { count }))).to.eventually.be.rejected()
})
it('can ping without options', async () => {
const res = await all(ipfsA.ping(nodeBId.id))
expect(res.length).to.be.ok()
expect(res[0].success).to.be.true()
})
})
}