-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
363 lines (298 loc) · 9.02 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
'use strict'
const test = require('tape')
const { MemoryLevel } = require('memory-level')
const { EntryStream, KeyStream, ValueStream } = require('.')
const { Writable, pipeline } = require('readable-stream')
const addSecretListener = require('secret-event-listener')
const delayedPipeline = async (...args) => {
await pipeline(...args)
await new Promise(setImmediate)
}
let db
const kLastIterator = Symbol('lastIterator')
const data = [
{ key: 'a', value: '1' },
{ key: 'b', value: '2' },
{ key: 'c', value: '3' }
]
test('setup', async 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 = db[kLastIterator] = original.apply(this, args)
return it
}
}
await db.open()
await db.batch(data.map(x => ({ type: 'put', ...x })))
})
test('EntryStream', async function (t) {
t.plan(1)
// TODO: pipeline returns before Concat calls the callback
await delayedPipeline(new EntryStream(db), new Concat((acc) => {
t.same(acc, data)
}))
})
test('KeyStream', async function (t) {
t.plan(1)
await delayedPipeline(new KeyStream(db), new Concat((acc) => {
t.same(acc, data.map(x => x.key))
}))
})
test('ValueStream', async function (t) {
t.plan(1)
await delayedPipeline(new ValueStream(db), new Concat((acc) => {
t.same(acc, data.map(x => x.value))
}))
})
for (const Ctor of [EntryStream, KeyStream, ValueStream]) {
const name = Ctor.name
test(name + ': normal event order', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order.filter(withoutDataEvents), ['_close', 'end', 'close'])
t.end()
})
stream.resume()
})
test(name + ': error from iterator.nextv', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order, ['_close', 'error: nextv', 'close'], 'event order')
t.end()
})
db[kLastIterator]._nextv = async function (size, options) {
throw new Error('nextv')
}
stream.resume()
})
test(name + ': destroy', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order, ['_close', 'close'])
t.end()
})
stream.destroy()
})
test(name + ': destroy(err)', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order, ['_close', 'error: user', 'close'])
t.end()
})
stream.destroy(new Error('user'))
})
test(name + ': destroy(err, callback)', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order, ['_close', 'callback', 'close'])
t.end()
})
stream.destroy(new Error('user'), function (err) {
order.push('callback')
t.is(err.message, 'user', 'got error')
})
})
test(name + ': destroy(null, callback)', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order, ['_close', 'callback', 'close'])
t.end()
})
stream.destroy(null, function (err) {
order.push('callback')
t.ifError(err, 'no error')
})
})
test(name + ': destroy() during iterator.nextv', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order, ['_close', 'close'], 'event order')
t.end()
})
db[kLastIterator].nextv = async function () {
stream.destroy()
}
stream.resume()
})
test(name + ': destroy(err) during iterator.nextv', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order, ['_close', 'error: user', 'close'], 'event order')
t.end()
})
db[kLastIterator].nextv = async function (size, options) {
stream.destroy(new Error('user'))
}
stream.resume()
})
test(name + ': destroy(err, callback) during iterator.nextv', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order, ['_close', 'callback', 'close'], 'event order')
t.end()
})
db[kLastIterator].nextv = async function (size, options) {
return new Promise((resolve) => {
stream.destroy(new Error('user'), function (err) {
order.push('callback')
t.is(err.message, 'user', 'got error')
resolve()
})
})
}
stream.resume()
})
test(name + ': destroy(null, callback) during iterator.nextv', function (t) {
const stream = new Ctor(db)
const order = monitor(stream, function () {
t.same(order, ['_close', 'callback', 'close'], 'event order')
t.end()
})
db[kLastIterator].nextv = async function (size, options) {
return new Promise((resolve) => {
stream.destroy(null, function (err) {
order.push('callback')
t.ifError(err, 'no error')
resolve()
})
})
}
stream.resume()
})
test(name + ': destroy during iterator.nextv 1', function (t) {
const stream = new Ctor(db)
const iterator = db[kLastIterator]
const nextv = iterator.nextv.bind(iterator)
iterator.nextv = async function (size) {
t.pass('should be called once')
const promise = nextv(size)
stream.destroy()
return promise
}
stream.on('data', function (data) {
t.fail('should not be called')
})
stream.on('close', t.end.bind(t))
})
test(name + ': destroy during iterator.nextv 2', function (t) {
const stream = new Ctor(db)
const iterator = db[kLastIterator]
const nextv = iterator.nextv.bind(iterator)
let count = 0
iterator.nextv = async function (size) {
t.pass('should be called')
const promise = nextv(size)
if (++count === 2) {
stream.destroy()
}
return promise
}
stream.on('data', function (data) {
t.pass('should be called')
})
stream.on('close', t.end.bind(t))
})
test(name + ': destroy after iterator.nextv 1', function (t) {
const stream = new Ctor(db)
const iterator = db[kLastIterator]
const nextv = iterator.nextv.bind(iterator)
iterator.nextv = async function (size) {
const result = await nextv(size)
stream.destroy()
t.pass('should be called')
return result
}
stream.on('data', function (data) {
t.fail('should not be called')
})
stream.on('close', t.end.bind(t))
})
test(name + ': destroy after iterator.nextv 2', function (t) {
const stream = new Ctor(db)
const iterator = db[kLastIterator]
const nextv = iterator.nextv.bind(iterator)
let count = 0
iterator.nextv = async function (size) {
const result = await nextv(size)
if (++count === 2) {
stream.destroy()
}
t.pass('should be called')
return result
}
stream.on('data', function (data) {
t.pass('should be called')
})
stream.on('close', t.end.bind(t))
})
test(name + ': destroy during data listener', function (t) {
const stream = new Ctor(db)
stream.once('data', function (data) {
stream.destroy()
t.ok(data, 'has data')
t.end()
})
})
test(name + ': has reference to db', function (t) {
const stream = new Ctor(db)
stream.on('close', function () {
t.is(stream.db, db, 'has reference to db')
t.end()
})
stream.resume()
})
}
// Note: also serves as teardown of above tests
test('it is safe to close db on end of stream', function (t) {
// Set highWaterMark to 0 so that we don't preemptively fetch.
const stream = new EntryStream(db, { highWaterMark: 0 })
stream.on('end', function () {
// Although the underlying iterator is still alive at this point (before
// the 'close' event has been emitted) it's safe to close the db because
// leveldown (v5) ends any open iterators before closing.
db.close().then(t.end.bind(t), t.fail.bind(t))
})
stream.resume()
})
function monitor (stream, onClose) {
const order = []
;['_next', '_nextv', '_close'].forEach(function (method) {
const original = db[kLastIterator][method]
db[kLastIterator][method] = async function () {
order.push(method)
return original.apply(this, arguments)
}
})
;['data', 'end', 'error', 'close'].forEach(function (event) {
// Add listener without side effects (like triggering flowing mode)
addSecretListener(stream, event, function (err) {
if (event === 'error') order.push('error: ' + err.message)
else order.push(event)
})
})
if (onClose) {
addSecretListener(stream, 'close', onClose)
}
return order
}
function withoutDataEvents (event) {
return event !== '_next' && event !== '_nextv' && event !== 'data'
}
class Concat extends Writable {
constructor (fn) {
super({ objectMode: true })
this.acc = []
this.fn = fn
}
_write (data, _, next) {
this.acc.push(data)
next()
}
_final (callback) {
this.fn(this.acc)
callback()
}
}