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
86 lines (70 loc) · 2.2 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
/* eslint-env mocha */
'use strict'
const { getDescribe, getIt, expect } = require('../utils/mocha')
const all = require('it-all')
const { fakeCid } = require('./utils')
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
* @param {Object} options
*/
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.dht.findProvs', function () {
this.timeout(20000)
let nodeA
let nodeB
let nodeC
before(async () => {
nodeA = (await common.spawn()).api
nodeB = (await common.spawn()).api
nodeC = (await common.spawn()).api
await Promise.all([
nodeB.swarm.connect(nodeA.peerId.addresses[0]),
nodeC.swarm.connect(nodeB.peerId.addresses[0])
])
})
after(() => common.clean())
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 be able to find providers', async function () {
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([
nodeB.peerId.id,
nodeC.peerId.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 (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)
})
})
}