1
1
/* eslint-env mocha */
2
2
'use strict'
3
3
4
- const chai = require ( 'chai' )
5
- chai . use ( require ( 'dirty-chai' ) )
6
- const expect = chai . expect
4
+ const { expect } = require ( 'aegir/utils/chai' )
7
5
8
6
const Block = require ( 'ipld-block' )
9
7
const _ = require ( 'lodash' )
10
8
const { collect } = require ( 'streaming-iterables' )
11
9
const CID = require ( 'cids' )
12
10
const multihashing = require ( 'multihashing-async' )
11
+ const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
12
+ const drain = require ( 'it-drain' )
13
13
14
14
const BlockService = require ( '../src' )
15
15
@@ -22,10 +22,10 @@ module.exports = (repo) => {
22
22
bs = new BlockService ( repo )
23
23
24
24
const data = [
25
- Buffer . from ( '1' ) ,
26
- Buffer . from ( '2' ) ,
27
- Buffer . from ( '3' ) ,
28
- Buffer . from ( 'A random data block' )
25
+ uint8ArrayFromString ( '1' ) ,
26
+ uint8ArrayFromString ( '2' ) ,
27
+ uint8ArrayFromString ( '3' ) ,
28
+ uint8ArrayFromString ( 'A random data block' )
29
29
]
30
30
31
31
testBlocks = await Promise . all ( data . map ( async ( d ) => {
@@ -53,8 +53,16 @@ module.exports = (repo) => {
53
53
}
54
54
} )
55
55
56
- it ( 'store many blocks' , ( ) => {
57
- return bs . putMany ( testBlocks )
56
+ it ( 'store many blocks' , async ( ) => {
57
+ await drain ( bs . putMany ( testBlocks ) )
58
+
59
+ expect (
60
+ await Promise . all (
61
+ testBlocks . map ( b => bs . get ( b . cid ) )
62
+ )
63
+ ) . to . deep . equal (
64
+ testBlocks
65
+ )
58
66
} )
59
67
60
68
it ( 'get many blocks through .get' , async ( ) => {
@@ -69,7 +77,7 @@ module.exports = (repo) => {
69
77
} )
70
78
71
79
it ( 'delete a block' , async ( ) => {
72
- const data = Buffer . from ( 'Will not live that much' )
80
+ const data = uint8ArrayFromString ( 'Will not live that much' )
73
81
74
82
const hash = await multihashing ( data , 'sha2-256' )
75
83
const b = new Block ( data , new CID ( hash ) )
@@ -81,7 +89,7 @@ module.exports = (repo) => {
81
89
} )
82
90
83
91
it ( 'does not delete a block it does not have' , async ( ) => {
84
- const data = Buffer . from ( 'Will not live that much ' + Date . now ( ) )
92
+ const data = uint8ArrayFromString ( 'Will not live that much ' + Date . now ( ) )
85
93
const cid = new CID ( await multihashing ( data , 'sha2-256' ) )
86
94
87
95
await bs . delete ( cid )
@@ -92,41 +100,37 @@ module.exports = (repo) => {
92
100
} )
93
101
94
102
it ( 'deletes lots of blocks' , async ( ) => {
95
- const data = Buffer . from ( 'Will not live that much' )
103
+ const data = uint8ArrayFromString ( 'Will not live that much' )
96
104
97
105
const hash = await multihashing ( data , 'sha2-256' )
98
106
const b = new Block ( data , new CID ( hash ) )
99
107
100
108
await bs . put ( b )
101
- await bs . deleteMany ( [ b . cid ] )
109
+ await drain ( bs . deleteMany ( [ b . cid ] ) )
102
110
const res = await bs . _repo . blocks . has ( b . cid )
103
- expect ( res ) . to . be . eql ( false )
111
+ expect ( res ) . to . be . false ( )
104
112
} )
105
113
106
114
it ( 'does not delete a blocks it does not have' , async ( ) => {
107
- const data = Buffer . from ( 'Will not live that much ' + Date . now ( ) )
115
+ const data = uint8ArrayFromString ( 'Will not live that much ' + Date . now ( ) )
108
116
const cid = new CID ( await multihashing ( data , 'sha2-256' ) )
109
117
110
- await bs . deleteMany ( [ cid ] )
111
- . then (
112
- ( ) => expect . fail ( 'Should have thrown' ) ,
113
- ( err ) => expect ( err ) . to . have . property ( 'code' , 'ERR_BLOCK_NOT_FOUND' )
114
- )
118
+ await expect ( drain ( bs . deleteMany ( [ cid ] ) ) ) . to . eventually . be . rejected ( ) . with . property ( 'code' , 'ERR_BLOCK_NOT_FOUND' )
115
119
} )
116
120
117
121
it ( 'stores and gets lots of blocks' , async function ( ) {
118
- this . timeout ( 8 * 1000 )
122
+ this . timeout ( 20 * 1000 )
119
123
120
124
const data = _ . range ( 1000 ) . map ( ( i ) => {
121
- return Buffer . from ( `hello-${ i } -${ Math . random ( ) } ` )
125
+ return uint8ArrayFromString ( `hello-${ i } -${ Math . random ( ) } ` )
122
126
} )
123
127
124
128
const blocks = await Promise . all ( data . map ( async ( d ) => {
125
129
const hash = await multihashing ( d , 'sha2-256' )
126
130
return new Block ( d , new CID ( hash ) )
127
131
} ) )
128
132
129
- await bs . putMany ( blocks )
133
+ await drain ( bs . putMany ( blocks ) )
130
134
131
135
const res = await Promise . all ( blocks . map ( b => bs . get ( b . cid ) ) )
132
136
expect ( res ) . to . be . eql ( blocks )
@@ -155,13 +159,13 @@ module.exports = (repo) => {
155
159
// returns a block with a value equal to its key
156
160
const bitswap = {
157
161
get ( cid ) {
158
- return new Block ( Buffer . from ( 'secret' ) , cid )
162
+ return new Block ( uint8ArrayFromString ( 'secret' ) , cid )
159
163
}
160
164
}
161
165
162
166
bs . setExchange ( bitswap )
163
167
164
- const data = Buffer . from ( 'secret' )
168
+ const data = uint8ArrayFromString ( 'secret' )
165
169
166
170
const hash = await multihashing ( data , 'sha2-256' )
167
171
const block = await bs . get ( new CID ( hash ) )
@@ -178,7 +182,7 @@ module.exports = (repo) => {
178
182
}
179
183
bs . setExchange ( bitswap )
180
184
181
- const data = Buffer . from ( 'secret sauce' )
185
+ const data = uint8ArrayFromString ( 'secret sauce' )
182
186
183
187
const hash = await multihashing ( data , 'sha2-256' )
184
188
await bs . put ( new Block ( data , new CID ( hash ) ) )
0 commit comments