forked from libp2p/js-libp2p-kad-dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviders.spec.js
167 lines (148 loc) · 4.74 KB
/
providers.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
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Store = require('interface-datastore').MemoryDatastore
const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const CID = require('cids')
const multihashing = require('multihashing-async')
const map = require('async/map')
const timesSeries = require('async/timesSeries')
const each = require('async/each')
const eachSeries = require('async/eachSeries')
const range = require('lodash.range')
const LevelStore = require('datastore-level')
const path = require('path')
const os = require('os')
const Providers = require('../src/providers')
const createPeerInfo = require('./utils/create-peer-info')
const createValues = require('./utils/create-values')
describe('Providers', () => {
let infos
before(function (done) {
this.timeout(10 * 1000)
createPeerInfo(3, (err, peers) => {
if (err) {
return done(err)
}
infos = peers
done()
})
})
it('simple add and get of providers', (done) => {
const providers = new Providers(new Store(), infos[2].id)
const cid = new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
parallel([
(cb) => providers.addProvider(cid, infos[0].id, cb),
(cb) => providers.addProvider(cid, infos[1].id, cb)
], (err) => {
expect(err).to.not.exist()
providers.getProviders(cid, (err, provs) => {
expect(err).to.not.exist()
expect(provs).to.be.eql([infos[0].id, infos[1].id])
providers.stop()
done()
})
})
})
it('more providers than space in the lru cache', (done) => {
const providers = new Providers(new Store(), infos[2].id, 10)
waterfall([
(cb) => map(
range(100),
(i, cb) => multihashing(Buffer.from(`hello ${i}`), 'sha2-256', cb),
cb
),
(hashes, cb) => {
const cids = hashes.map((h) => new CID(h))
map(cids, (cid, cb) => {
providers.addProvider(cid, infos[0].id, cb)
}, (err) => cb(err, cids))
},
(cids, cb) => {
map(cids, (cid, cb) => {
providers.getProviders(cid, cb)
}, (err, provs) => {
expect(err).to.not.exist()
expect(provs).to.have.length(100)
provs.forEach((p) => {
expect(p[0].id).to.be.eql(infos[0].id.id)
})
providers.stop()
cb()
})
}
], done)
})
it('expires', (done) => {
const providers = new Providers(new Store(), infos[2].id)
providers.cleanupInterval = 100
providers.provideValidity = 200
const cid = new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
parallel([
(cb) => providers.addProvider(cid, infos[0].id, cb),
(cb) => providers.addProvider(cid, infos[1].id, cb)
], (err) => {
expect(err).to.not.exist()
providers.getProviders(cid, (err, provs) => {
expect(err).to.not.exist()
expect(provs).to.have.length(2)
expect(provs[0].id).to.be.eql(infos[0].id.id)
expect(provs[1].id).to.be.eql(infos[1].id.id)
})
setTimeout(() => {
providers.getProviders(cid, (err, provs) => {
expect(err).to.not.exist()
expect(provs).to.have.length(0)
providers.stop()
done()
})
}, 300)
})
})
// slooow so only run when you need to
it.skip('many', (done) => {
const p = path.join(
os.tmpdir(), (Math.random() * 100).toString()
)
const store = new LevelStore(p)
const providers = new Providers(store, infos[2].id, 10)
console.log('starting')
waterfall([
(cb) => parallel([
(cb) => createValues(100, cb),
(cb) => createPeerInfo(600, cb)
], cb),
(res, cb) => {
console.log('got values and peers')
const values = res[0]
const peers = res[1]
let total = Date.now()
eachSeries(values, (v, cb) => {
eachSeries(peers, (p, cb) => {
providers.addProvider(v.cid, p.id, cb)
}, cb)
}, (err) => {
console.log('addProvider %s peers %s cids in %sms', peers.length, values.length, Date.now() - total)
expect(err).to.not.exist()
console.log('starting profile with %s peers and %s cids', peers.length, values.length)
timesSeries(3, (i, cb) => {
const start = Date.now()
each(values, (v, cb) => {
providers.getProviders(v.cid, cb)
}, (err) => {
expect(err).to.not.exist()
console.log('query %sms', (Date.now() - start))
cb()
})
}, cb)
})
}
], (err) => {
expect(err).to.not.exist()
store.close(done)
})
})
})