-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
8 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
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,33 @@ | ||
'use strict' | ||
|
||
const test = require('tape') | ||
const fork = require('child_process').fork | ||
const path = require('path') | ||
|
||
// Test env_cleanup_hook at several stages of a db lifetime | ||
addTest(['create']) | ||
addTest(['create', 'open']) | ||
addTest(['create', 'open', 'create-iterator']) | ||
addTest(['create', 'open', 'create-iterator', 'close']) | ||
addTest(['create', 'open', 'create-iterator', 'nexting']) | ||
addTest(['create', 'open', 'create-iterator', 'nexting', 'close']) | ||
addTest(['create', 'open', 'close']) | ||
addTest(['create', 'open-error']) | ||
|
||
function addTest (steps) { | ||
test(`cleanup on environment exit (${steps.join(', ')})`, function (t) { | ||
t.plan(3) | ||
|
||
const child = fork(path.join(__dirname, 'env-cleanup-hook.js'), steps) | ||
|
||
child.on('message', function (m) { | ||
t.is(m, steps[steps.length - 1], `got to step: ${m}`) | ||
child.disconnect() | ||
}) | ||
|
||
child.on('exit', function (code, sig) { | ||
t.is(code, 0, 'child exited normally') | ||
t.is(sig, null, 'not terminated due to signal') | ||
}) | ||
}) | ||
} |
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,62 @@ | ||
'use strict' | ||
|
||
const testCommon = require('./common') | ||
|
||
function test (steps) { | ||
let step | ||
|
||
function nextStep () { | ||
step = steps.shift() || step | ||
return step | ||
} | ||
|
||
if (nextStep() !== 'create') { | ||
// Send a message indicating at which step we stopped, and | ||
// triggering an environment exit. | ||
return process.send(step) | ||
} | ||
|
||
// Create a db, expected only to be garbage-collected because | ||
// the environment cleanup hook is not added until open(). | ||
const db = testCommon.factory() | ||
|
||
if (nextStep() !== 'open') { | ||
if (nextStep() === 'open-error') { | ||
// If opening fails we'll still have the cleanup hook but it will be a noop. | ||
db.open({ createIfMissing: false, errorIfExists: true }, function (err) { | ||
if (!err) throw new Error('Expected an open() error') | ||
}) | ||
} | ||
|
||
return process.send(step) | ||
} | ||
|
||
// Open the db, expected to be closed by the cleanup hook. | ||
db.open(function (err) { | ||
if (err) throw err | ||
|
||
if (nextStep() === 'create-iterator') { | ||
// Create an iterator, expected to be ended by the cleanup hook. | ||
const it = db.iterator() | ||
|
||
if (nextStep() === 'nexting') { | ||
// This async work should finish before the cleanup hook is called. | ||
it.next(function (err) { | ||
if (err) throw err | ||
}) | ||
} | ||
} | ||
|
||
if (nextStep() === 'close') { | ||
// Close the db, expected only to be garbage-collected because we | ||
// synchronously remove the cleanup hook in close(). | ||
db.close(function (err) { | ||
if (err) throw err | ||
}) | ||
} | ||
|
||
process.send(step) | ||
}) | ||
} | ||
|
||
test(process.argv.slice(2)) |
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