-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent GC of db during
clear()
and other operations
- Loading branch information
Showing
4 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,5 @@ jobs: | |
uses: GabrielBB/xvfb-action@v1 | ||
with: | ||
run: npm run test-electron | ||
- name: Test GC | ||
run: npm run test-gc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'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() | ||
}) | ||
} | ||
|
||
test('db without ref does not get GCed while clear() is in progress', function (t) { | ||
t.plan(4) | ||
|
||
let db = testCommon.factory() | ||
|
||
db.open(function (err) { | ||
t.ifError(err, 'no open error') | ||
|
||
// Insert test data | ||
db.batch(sourceData.slice(), function (err) { | ||
t.ifError(err, 'no batch error') | ||
|
||
// Start async work | ||
db.clear(function () { | ||
t.pass('got callback') | ||
|
||
// Give GC another chance to run, to rule out other issues. | ||
setImmediate(function () { | ||
if (global.gc) global.gc() | ||
t.pass() | ||
}) | ||
}) | ||
|
||
// Remove reference. The db should not get garbage collected | ||
// until after the clear() callback, thanks to a napi_ref. | ||
db = null | ||
|
||
// Useful for manual testing with "node --expose-gc". | ||
// The pending tap assertion may also allow GC to kick in. | ||
if (global.gc) global.gc() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters