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 pathfind-provs.js
116 lines (95 loc) · 3.07 KB
/
find-provs.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
/* eslint-env mocha */
import { expect } from 'aegir/utils/chai.js'
import { getDescribe, getIt } from '../utils/mocha.js'
import all from 'it-all'
import drain from 'it-drain'
import { fakeCid } from './utils.js'
import testTimeout from '../utils/test-timeout.js'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
export function testFindProvs (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.dht.findProvs', function () {
this.timeout(20000)
/** @type {import('ipfs-core-types').IPFS} */
let nodeA
/** @type {import('ipfs-core-types').IPFS} */
let nodeB
/** @type {import('ipfs-core-types').IPFS} */
let nodeC
/** @type {import('ipfs-core-types/src/root').IDResult} */
let nodeAId
/** @type {import('ipfs-core-types/src/root').IDResult} */
let nodeBId
/** @type {import('ipfs-core-types/src/root').IDResult} */
let nodeCId
before(async () => {
nodeA = (await factory.spawn()).api
nodeB = (await factory.spawn()).api
nodeC = (await factory.spawn()).api
nodeAId = await nodeA.id()
nodeBId = await nodeB.id()
nodeCId = await nodeC.id()
await Promise.all([
nodeB.swarm.connect(nodeAId.addresses[0]),
nodeC.swarm.connect(nodeBId.addresses[0])
])
})
after(() => factory.clean())
/**
* @type {import('multiformats/cid').CID}
*/
let providedCid
before('add providers for the same cid', async function () {
this.timeout(10 * 1000)
const cids = await Promise.all([
nodeB.object.new('unixfs-dir'),
nodeC.object.new('unixfs-dir')
])
providedCid = cids[0]
await Promise.all([
all(nodeB.dht.provide(providedCid)),
all(nodeC.dht.provide(providedCid))
])
})
it('should respect timeout option when finding providers on the DHT', () => {
return testTimeout(() => drain(nodeA.dht.findProvs(providedCid, {
timeout: 1
})))
})
it('should be able to find providers', async function () {
// @ts-ignore this is mocha
this.timeout(20 * 1000)
const provs = await all(nodeA.dht.findProvs(providedCid, { numProviders: 2 }))
const providerIds = provs.map((p) => p.id.toString())
expect(providerIds).to.have.members([
nodeBId.id,
nodeCId.id
])
})
it('should take options to override timeout config', async function () {
const options = {
timeout: 1
}
const cidV0 = await fakeCid()
const start = Date.now()
let res
try {
res = await all(nodeA.dht.findProvs(cidV0, options))
} catch (/** @type {any} */ err) {
// rejected by http client
expect(err).to.have.property('name', 'TimeoutError')
return
}
// rejected by the server, errors don't work over http - https://github.com/ipfs/js-ipfs/issues/2519
expect(res).to.be.an('array').with.lengthOf(0)
expect(Date.now() - start).to.be.lessThan(100)
})
})
}