-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
252 lines (226 loc) · 5.98 KB
/
index.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const parallel = require('run-parallel')
function makeBuffer (num) {
const buf = Buffer.alloc(10)
buf.fill(num)
return buf
}
module.exports = function (test, Store) {
test('basic put, then get', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('0123456789'), function (err) {
t.error(err)
store.get(0, function (err, chunk) {
t.error(err)
t.deepEqual(chunk, Buffer.from('0123456789'))
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
})
test('put invalid chunk length gives error', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('0123'), function (err) {
t.ok(err instanceof Error)
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
test('concurrent puts, then concurrent gets', function (t) {
const store = new Store(10)
function makePutTask (i) {
return function (cb) {
store.put(i, makeBuffer(i), cb)
}
}
function makeGetTask (i) {
return function (cb) {
store.get(i, function (err, data) {
if (err) return cb(err)
t.deepEqual(data, makeBuffer(i))
cb(null)
})
}
}
let tasks = []
for (let i = 0; i < 100; i++) {
tasks.push(makePutTask(i))
}
parallel(tasks, function (err) {
t.error(err)
tasks = []
for (let i = 0; i < 100; i++) {
tasks.push(makeGetTask(i))
}
parallel(tasks, function (err) {
t.error(err)
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
})
test('interleaved puts and gets', function (t) {
const store = new Store(10)
const tasks = []
function makeTask (i) {
return function (cb) {
store.put(i, makeBuffer(i), function (err) {
if (err) return cb(err)
store.get(i, function (err, data) {
t.error(err)
t.deepEqual(data, makeBuffer(i))
cb(null)
})
})
}
}
for (let i = 0; i < 100; i++) {
tasks.push(makeTask(i))
}
parallel(tasks, function (err) {
t.error(err)
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
test('get with `offset` and `length` options', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('0123456789'), function (err) {
t.error(err)
store.get(0, { offset: 2, length: 3 }, function (err, chunk) {
t.error(err)
t.deepEqual(chunk, Buffer.from('234'))
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
})
test('get with null option', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('0123456789'), function (err) {
t.error(err)
store.get(0, null, function (err, chunk) {
t.error(err)
t.deepEqual(chunk, Buffer.from('0123456789'))
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
})
test('get with empty object option', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('0123456789'), function (err) {
t.error(err)
store.get(0, {}, function (err, chunk) {
t.error(err)
t.deepEqual(chunk, Buffer.from('0123456789'))
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
})
test('get with `offset` option', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('0123456789'), function (err) {
t.error(err)
store.get(0, { offset: 2 }, function (err, chunk) {
t.error(err)
t.deepEqual(chunk, Buffer.from('23456789'))
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
})
test('get with `length` option', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('0123456789'), function (err) {
t.error(err)
store.get(0, { length: 5 }, function (err, chunk) {
t.error(err)
t.deepEqual(chunk, Buffer.from('01234'))
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
})
test('test for sparsely populated support', function (t) {
const store = new Store(10)
store.put(10, Buffer.from('0123456789'), function (err) {
t.error(err)
store.get(10, function (err, chunk) {
t.error(err)
t.deepEqual(chunk, Buffer.from('0123456789'))
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
})
test('test `put` without callback - error should be silent', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('01234'))
store.destroy(function (err) {
t.error(err)
t.end()
})
})
test('test `put` without callback - success should be silent', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('0123456789'))
store.destroy(function (err) {
t.error(err)
t.end()
})
})
test('chunkLength property', function (t) {
const store = new Store(10)
t.equal(store.chunkLength, 10)
store.destroy(function (err) {
t.error(err)
t.end()
})
})
test('test `get` on non-existent index', function (t) {
const store = new Store(10)
store.get(0, function (err, chunk) {
t.ok(err instanceof Error)
store.destroy(function (err) {
t.error(err)
t.end()
})
})
})
test('test empty store\'s `close` calls its callback', function (t) {
const store = new Store(10)
store.close(function (err) {
t.error(err)
t.end()
})
})
test('test non-empty store\'s `close` calls its callback', function (t) {
const store = new Store(10)
store.put(0, Buffer.from('0123456789'))
store.close(function (err) {
t.error(err)
t.end()
})
})
}