This repository was archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathblock-service-test.js
194 lines (152 loc) · 5.48 KB
/
block-service-test.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* eslint-env mocha */
'use strict'
const { expect } = require('aegir/utils/chai')
const Block = require('ipld-block')
const _ = require('lodash')
const { collect } = require('streaming-iterables')
const CID = require('cids')
const multihashing = require('multihashing-async')
const uint8ArrayFromString = require('uint8arrays/from-string')
const drain = require('it-drain')
const BlockService = require('../src')
module.exports = (repo) => {
describe('block-service', () => {
let bs
let testBlocks
before(async () => {
bs = new BlockService(repo)
const data = [
uint8ArrayFromString('1'),
uint8ArrayFromString('2'),
uint8ArrayFromString('3'),
uint8ArrayFromString('A random data block')
]
testBlocks = await Promise.all(data.map(async (d) => {
const hash = await multihashing(d, 'sha2-256')
return new Block(d, new CID(hash))
}))
})
describe('fetch only from local Repo', () => {
it('store and get a block', async () => {
const b = testBlocks[3]
await bs.put(b)
const res = await bs.get(b.cid)
expect(res).to.eql(b)
})
it('get a non stored yet block', async () => {
const b = testBlocks[2]
try {
await bs.get(b.cid)
} catch (err) {
expect(err).to.exist()
}
})
it('store many blocks', async () => {
await drain(bs.putMany(testBlocks))
expect(
await Promise.all(
testBlocks.map(b => bs.get(b.cid))
)
).to.deep.equal(
testBlocks
)
})
it('get many blocks through .get', async () => {
const blocks = await Promise.all(testBlocks.map(b => bs.get(b.cid)))
expect(blocks).to.eql(testBlocks)
})
it('get many blocks through .getMany', async () => {
const cids = testBlocks.map(b => b.cid)
const blocks = await collect(bs.getMany(cids))
expect(blocks).to.eql(testBlocks)
})
it('delete a block', async () => {
const data = uint8ArrayFromString('Will not live that much')
const hash = await multihashing(data, 'sha2-256')
const b = new Block(data, new CID(hash))
await bs.put(b)
await bs.delete(b.cid)
const res = await bs._repo.blocks.has(b.cid)
expect(res).to.be.eql(false)
})
it('does not delete a block it does not have', async () => {
const data = uint8ArrayFromString('Will not live that much ' + Date.now())
const cid = new CID(await multihashing(data, 'sha2-256'))
await bs.delete(cid)
.then(
() => expect.fail('Should have thrown'),
(err) => expect(err).to.have.property('code', 'ERR_BLOCK_NOT_FOUND')
)
})
it('deletes lots of blocks', async () => {
const data = uint8ArrayFromString('Will not live that much')
const hash = await multihashing(data, 'sha2-256')
const b = new Block(data, new CID(hash))
await bs.put(b)
await drain(bs.deleteMany([b.cid]))
const res = await bs._repo.blocks.has(b.cid)
expect(res).to.be.false()
})
it('does not delete a blocks it does not have', async () => {
const data = uint8ArrayFromString('Will not live that much ' + Date.now())
const cid = new CID(await multihashing(data, 'sha2-256'))
await expect(drain(bs.deleteMany([cid]))).to.eventually.be.rejected().with.property('code', 'ERR_BLOCK_NOT_FOUND')
})
it('stores and gets lots of blocks', async function () {
this.timeout(20 * 1000)
const data = _.range(1000).map((i) => {
return uint8ArrayFromString(`hello-${i}-${Math.random()}`)
})
const blocks = await Promise.all(data.map(async (d) => {
const hash = await multihashing(d, 'sha2-256')
return new Block(d, new CID(hash))
}))
await drain(bs.putMany(blocks))
const res = await Promise.all(blocks.map(b => bs.get(b.cid)))
expect(res).to.be.eql(blocks)
})
it('sets and unsets exchange', () => {
bs = new BlockService(repo)
bs.setExchange({})
expect(bs.hasExchange()).to.be.eql(true)
bs.unsetExchange()
expect(bs.hasExchange()).to.be.eql(false)
})
})
describe('fetch through Bitswap (has exchange)', () => {
beforeEach(() => {
bs = new BlockService(repo)
})
it('hasExchange returns true when online', () => {
bs.setExchange({})
expect(bs.hasExchange()).to.be.eql(true)
})
it('retrieves a block through bitswap', async () => {
// returns a block with a value equal to its key
const bitswap = {
get (cid) {
return new Block(uint8ArrayFromString('secret'), cid)
}
}
bs.setExchange(bitswap)
const data = uint8ArrayFromString('secret')
const hash = await multihashing(data, 'sha2-256')
const block = await bs.get(new CID(hash))
expect(block.data).to.be.eql(data)
})
it('puts the block through bitswap', async () => {
const puts = []
const bitswap = {
put (block) {
puts.push(block)
}
}
bs.setExchange(bitswap)
const data = uint8ArrayFromString('secret sauce')
const hash = await multihashing(data, 'sha2-256')
await bs.put(new Block(data, new CID(hash)))
expect(puts).to.have.length(1)
})
})
})
}