-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathiterator-gc-test.js
74 lines (57 loc) · 2.01 KB
/
iterator-gc-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
'use strict'
const test = require('tape')
const testCommon = require('./common')
const sourceData = []
for (let i = 0; i < 1e3; i++) {
sourceData.push({
type: 'put',
key: i.toString(),
value: Math.random().toString()
})
}
// When you have a database open with an active iterator, but no references to
// the db, V8 will GC the database and you'll get an failed assert from LevelDB.
test('db without ref does not get GCed while iterating', async function (t) {
let db = testCommon.factory()
await db.open()
// Insert test data
await db.batch(sourceData.slice())
// Set highWaterMarkBytes to 0 so that we don't preemptively fetch.
const it = db.iterator({ highWaterMarkBytes: 0 })
// Remove reference
db = null
if (global.gc) {
// This is the reliable way to trigger GC (and the bug if it exists).
// Useful for manual testing with "node --expose-gc".
global.gc()
} else {
// But a timeout usually also allows GC to kick in. If not, the time
// between iterator ticks might. That's when "highWaterMarkBytes: 0" helps.
await new Promise(resolve => setTimeout(resolve, 1e3))
}
// No reference to db here, could be GCed. It shouldn't..
const entries = await it.all()
t.is(entries.length, sourceData.length, 'got data')
// Because we also have a reference on the iterator. That's the fix.
t.ok(it.db, 'abstract iterator has reference to db')
// Which as luck would have it, also allows us to properly end this test.
return it.db.close()
})
// Same as above but also nullifying the iterator
test('db and iterator without ref does not get GCed while iterating', async function (t) {
let db = testCommon.factory()
await db.open()
await db.batch(sourceData.slice())
let it = db.iterator({
highWaterMarkBytes: sourceData.length * 32
})
t.is((await it.nextv(1000)).length, sourceData.length, 'got data')
// Remove references
it = null
db = null
if (global.gc) {
global.gc()
} else {
await new Promise(resolve => setTimeout(resolve, 1e3))
}
})