-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
317 lines (243 loc) · 8.58 KB
/
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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
'use strict'
const test = require('tape')
const { MemoryLevel } = require('memory-level')
const { ReadableStream, WritableStream } = require('./streams')
const { EntryStream, KeyStream, ValueStream, BatchStream } = require('.')
let db
let lastIterator
const data = [
{ key: 'a', value: '1' },
{ key: 'b', value: '2' },
{ key: 'c', value: '3' }
]
test('setup', function (t) {
db = new MemoryLevel()
// Keep track of last created iterator for test purposes
for (const method of ['iterator', 'keys', 'values']) {
const original = db[method]
db[method] = function (...args) {
const it = lastIterator = original.apply(this, args)
return it
}
}
db.open(function (err) {
t.error(err, 'no error')
db.batch(data.map(x => ({ type: 'put', ...x })), function (err) {
t.error(err, 'no error')
t.end()
})
})
})
test('EntryStream', async function (t) {
t.same(await concat(new EntryStream(db)), data.map(kv => [kv.key, kv.value]))
})
test('KeyStream', async function (t) {
t.same(await concat(new KeyStream(db)), data.map(kv => kv.key))
})
test('ValueStream', async function (t) {
t.same(await concat(new ValueStream(db)), data.map(kv => kv.value))
})
for (const Ctor of [EntryStream, KeyStream, ValueStream]) {
const name = Ctor.name
test(name + ': natural end closes iterator', async function (t) {
const stream = new Ctor(db)
const order = monitor()
const items = await concat(stream)
t.is(items.length, 3, 'got items')
t.same(order, ['_nextv', '_nextv', '_close'], 'order was ok')
})
test(name + ': bubbles up error from iterator.nextv', async function (t) {
t.plan(2)
const stream = new Ctor(db)
const order = monitor()
lastIterator._nextv = function (size, options, cb) {
process.nextTick(cb, new Error('nextv test error'))
}
try {
await concat(stream)
} catch (err) {
t.is(err.message, 'nextv test error', 'got error')
t.same(order, ['_close'], 'order was ok')
}
})
test(name + ': cancelation closes iterator', async function (t) {
t.plan(3)
const stream = new Ctor(db, { highWaterMark: 1 })
const order = monitor()
let count = 0
try {
await stream.pipeTo(new WritableStream({
write (entry) {
if (++count > 1) throw new Error('test error')
}
}))
} catch (err) {
t.is(count, 2)
t.is(err.message, 'test error')
t.is(order.pop(), '_close', 'iterator was closed')
}
})
test(name + ': signal abort closes iterator', async function (t) {
const stream = new Ctor(db)
const order = monitor()
const controller = new AbortController()
try {
await stream.pipeTo(new WritableStream({
write (item) {
controller.abort()
}
}), { signal: controller.signal })
} catch (err) {
t.is(err.name, 'AbortError')
t.is(order[order.length - 1], '_close', 'iterator was closed')
}
})
}
// Done with readable stream tests; subsequent tests use a fresh db
test('teardown', function (t) {
return db.close()
})
test('BatchStream: basic default operation', async function (t) {
const { db, batchCalls } = await monkeyBatched()
const stream = new BatchStream(db)
await from([{ key: 'a', value: '1' }]).pipeTo(stream)
t.same(await db.iterator().all(), [['a', '1']])
t.same(batchCalls, [[[{ key: 'a', value: '1', type: 'put' }], {}]])
})
test('BatchStream: basic put operation', async function (t) {
const { db, batchCalls } = await monkeyBatched()
await from([{ type: 'put', key: 'a', value: '1' }]).pipeTo(new BatchStream(db))
await from([{ key: 'b', value: '2' }]).pipeTo(new BatchStream(db, { type: 'put' }))
t.same(await db.iterator().all(), [['a', '1'], ['b', '2']])
t.same(batchCalls, [
[[{ key: 'a', value: '1', type: 'put' }], {}],
[[{ key: 'b', value: '2', type: 'put' }], {}]
])
})
test('BatchStream: basic del operation', async function (t) {
const { db, batchCalls } = await monkeyBatched()
await db.put('a', '1')
await db.put('b', '2')
t.same(await db.iterator().all(), [['a', '1'], ['b', '2']])
await from([{ type: 'del', key: 'a' }]).pipeTo(new BatchStream(db))
await from([{ key: 'b' }]).pipeTo(new BatchStream(db, { type: 'del' }))
t.same(await db.iterator().all(), [])
t.same(batchCalls, [
[[{ key: 'a', type: 'del' }], {}],
[[{ key: 'b', type: 'del' }], {}]
])
})
test('BatchStream: operation type takes precedence', async function (t) {
const { db } = await monkeyBatched()
await db.put('a', '1')
t.same(await db.values().all(), ['1'])
await from([{ type: 'put', key: 'a', value: '2' }]).pipeTo(new BatchStream(db, { type: 'del' }))
t.same(await db.values().all(), ['2'])
})
test('BatchStream: basic operation with custom property', async function (t) {
const { db, batchCalls } = await monkeyBatched()
const stream = new BatchStream(db)
await from([{ key: 'a', value: '1', foo: 123 }]).pipeTo(stream)
t.same(batchCalls, [[[{ key: 'a', value: '1', foo: 123, type: 'put' }], {}]])
})
test('BatchStream: basic operation with options', async function (t) {
const { db, batchCalls } = await monkeyBatched()
const stream = new BatchStream(db, { foo: 123 })
await from([{ key: 'a', type: 'del' }]).pipeTo(stream)
t.same(batchCalls, [[[{ key: 'a', type: 'del' }], { foo: 123 }]])
})
test('BatchStream: basic entry', async function (t) {
const { db, batchCalls } = await monkeyBatched()
await from([['a', '1']]).pipeTo(new BatchStream(db))
t.same(await db.iterator().all(), [['a', '1']])
t.same(batchCalls, [[[{ key: 'a', value: '1', type: 'put' }], {}]])
})
test('BatchStream: basic entry with del type', async function (t) {
const { db, batchCalls } = await monkeyBatched()
await db.put('a', '1')
await db.put('b', '2')
t.same(await db.iterator().all(), [['a', '1'], ['b', '2']])
await from([['a', '1']]).pipeTo(new BatchStream(db, { type: 'del' }))
await from([['b']]).pipeTo(new BatchStream(db, { type: 'del' }))
t.same(await db.iterator().all(), [])
t.same(batchCalls, [
[[{ key: 'a', value: '1', type: 'del' }], {}],
[[{ key: 'b', value: undefined, type: 'del' }], {}]
])
})
test('BatchStream: uses batches of fixed size', async function (t) {
const { db, batchCalls } = await monkeyBatched()
const entries = new Array(17).fill().map((_, i) => [String(i), String(i)])
await from(entries).pipeTo(new BatchStream(db, { highWaterMark: 5 }))
t.is(batchCalls.length, 4, '4 batches')
t.same(batchCalls.map(args => args[0].length), [5, 5, 5, 2], 'batch size is fixed')
})
test('BatchStream: flushes remainder on close', async function (t) {
const { db, batchCalls } = await monkeyBatched()
const entries = new Array(5).fill().map((_, i) => [String(i), String(i)])
await from(entries).pipeTo(new BatchStream(db, { highWaterMark: 1e3 }))
t.is(batchCalls.length, 1, '1 batch')
t.is(batchCalls[0][0].length, 5, '5 operations')
})
test('BatchStream: does not batch() in parallel', async function (t) {
t.plan(5)
const db = new MemoryLevel()
const originalBatch = db.batch
const entries = new Array(10).fill().map((_, i) => [String(i), String(i)])
// Our monkey-patched batch() does not handle deferred open
await db.open()
let inflight = 0
db.batch = async function (...args) {
t.is(inflight++, 0, 'not parallel')
await originalBatch.apply(this, args)
await new Promise((resolve) => setTimeout(resolve, 50))
inflight = 0
}
await from(entries).pipeTo(new BatchStream(db, { highWaterMark: 2 }))
})
// Create a fresh db with a spy on batch()
const monkeyBatched = async function () {
const db = new MemoryLevel()
const originalBatch = db.batch
const batchCalls = []
db.batch = function (...args) {
batchCalls.push(args)
return originalBatch.apply(this, args)
}
// Our monkey-patched batch() does not handle deferred open
await db.open()
return { db, batchCalls }
}
const monitor = function () {
const order = []
;['_next', '_nextv', '_close'].forEach(function (method) {
const original = lastIterator[method]
lastIterator[method] = function () {
order.push(method)
original.apply(this, arguments)
}
})
return order
}
const concat = async function (stream) {
const items = []
await stream.pipeTo(new WritableStream({
write (item) {
items.push(item)
}
}))
return items
}
// Create a readable stream from an array of items
const from = function (items) {
let position = 0
return new ReadableStream({
pull (controller) {
if (position >= items.length) {
controller.close()
} else {
controller.enqueue(items[position++])
}
}
})
}