This repository was archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathproviders.spec.js
147 lines (116 loc) · 4.55 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
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const { MemoryDatastore } = require('interface-datastore')
const CID = require('cids')
const LevelStore = require('datastore-level')
const path = require('path')
const os = require('os')
const multihashing = require('multihashing-async')
const { Buffer } = require('buffer')
const Providers = require('../src/providers')
const createPeerId = require('./utils/create-peer-id')
const createValues = require('./utils/create-values')
describe('Providers', () => {
let peerIds
let providers
before(async function () {
this.timeout(10 * 1000)
peerIds = await createPeerId(3)
})
afterEach(() => {
providers && providers.stop()
})
it('simple add and get of providers', async () => {
providers = new Providers(new MemoryDatastore(), peerIds[2])
const cid = new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
await Promise.all([
providers.addProvider(cid, peerIds[0]),
providers.addProvider(cid, peerIds[1])
])
const provs = await providers.getProviders(cid)
const ids = new Set(provs.map((peerId) => peerId.toB58String()))
expect(ids.has(peerIds[0].toB58String())).to.be.eql(true)
expect(ids.has(peerIds[1].toB58String())).to.be.eql(true)
})
it('duplicate add of provider is deduped', async () => {
providers = new Providers(new MemoryDatastore(), peerIds[2])
const cid = new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
await Promise.all([
providers.addProvider(cid, peerIds[0]),
providers.addProvider(cid, peerIds[0]),
providers.addProvider(cid, peerIds[1]),
providers.addProvider(cid, peerIds[1]),
providers.addProvider(cid, peerIds[1])
])
const provs = await providers.getProviders(cid)
expect(provs).to.have.length(2)
const ids = new Set(provs.map((peerId) => peerId.toB58String()))
expect(ids.has(peerIds[0].toB58String())).to.be.eql(true)
expect(ids.has(peerIds[1].toB58String())).to.be.eql(true)
})
it('more providers than space in the lru cache', async () => {
providers = new Providers(new MemoryDatastore(), peerIds[2], 10)
const hashes = await Promise.all([...new Array(100)].map((i) => {
return multihashing(Buffer.from(`hello ${i}`), 'sha2-256')
}))
const cids = hashes.map((h) => new CID(h))
await Promise.all(cids.map(cid => providers.addProvider(cid, peerIds[0])))
const provs = await Promise.all(cids.map(cid => providers.getProviders(cid)))
expect(provs).to.have.length(100)
for (const p of provs) {
expect(p[0].id).to.be.eql(peerIds[0].id)
}
})
it('expires', async () => {
providers = new Providers(new MemoryDatastore(), peerIds[2])
providers.cleanupInterval = 100
providers.provideValidity = 200
const cid = new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
await Promise.all([
providers.addProvider(cid, peerIds[0]),
providers.addProvider(cid, peerIds[1])
])
const provs = await providers.getProviders(cid)
expect(provs).to.have.length(2)
expect(provs[0].id).to.be.eql(peerIds[0].id)
expect(provs[1].id).to.be.eql(peerIds[1].id)
await new Promise(resolve => setTimeout(resolve, 400))
const provsAfter = await providers.getProviders(cid)
expect(provsAfter).to.have.length(0)
})
// slooow so only run when you need to
it.skip('many', async function () {
const p = path.join(
os.tmpdir(), (Math.random() * 100).toString()
)
const store = new LevelStore(p)
providers = new Providers(store, peerIds[2], 10)
console.log('starting') // eslint-disable-line no-console
const res = await Promise.all([
createValues(100),
createPeerId(600)
])
console.log('got values and peers') // eslint-disable-line no-console
const values = res[0]
const peers = res[1]
const total = Date.now()
for (const v of values) {
for (const p of peers) {
await providers.addProvider(v.cid, p.id)
}
}
console.log('addProvider %s peers %s cids in %sms', peers.length, values.length, Date.now() - total) // eslint-disable-line no-console
console.log('starting profile with %s peers and %s cids', peers.length, values.length) // eslint-disable-line no-console
for (let i = 0; i < 3; i++) {
const start = Date.now()
for (const v of values) {
await providers.getProviders(v.cid)
console.log('query %sms', (Date.now() - start)) // eslint-disable-line no-console
}
}
await store.close()
})
})