Skip to content

Commit

Permalink
test: run test suites with helpers
Browse files Browse the repository at this point in the history
PR-URL: #976
Reviewed-By: Michael Dawson <midawson@redhat.com>
  • Loading branch information
legendecas authored and mhdawson committed Apr 23, 2021
1 parent fbcdf00 commit b6f5eb1
Show file tree
Hide file tree
Showing 67 changed files with 191 additions and 260 deletions.
5 changes: 2 additions & 3 deletions test/addon.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
module.exports = require('./common').runTest(test);

function test(binding) {
assert.strictEqual(binding.addon.increment(), 43);
Expand Down
9 changes: 2 additions & 7 deletions test/addon_data.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const { spawn } = require('child_process');
const readline = require('readline');
const path = require('path');

module.exports =
test(path.resolve(__dirname, `./build/${buildType}/binding.node`))
.then(() =>
test(path.resolve(__dirname,
`./build/${buildType}/binding_noexcept.node`)));
module.exports = require('./common').runTestWithBindingPath(test);

// Make sure the instance data finalizer is called at process exit. If the hint
// is non-zero, it will be printed out by the child process.
Expand Down
5 changes: 2 additions & 3 deletions test/arraybuffer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const testUtil = require('./testUtil');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = require('./common').runTest(test);

function test(binding) {
return testUtil.runGCTests([
Expand Down
49 changes: 31 additions & 18 deletions test/asynccontext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const common = require('./common');

Expand All @@ -17,14 +17,27 @@ function checkAsyncHooks() {
return false;
}

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
module.exports = common.runTest(test);

function installAsyncHooksForTest() {
return new Promise((resolve, reject) => {
let id;
const events = [];
const hook = async_hooks.createHook({
/**
* TODO(legendecas): investigate why resolving & disabling hooks in
* destroy callback causing crash with case 'callbackscope.js'.
*/
let hook;
let destroyed = false;
const interval = setInterval(() => {
if (destroyed) {
hook.disable();
clearInterval(interval);
resolve(events);
}
}, 10);

hook = async_hooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (id === undefined && type === 'async_context_test') {
id = asyncId;
Expand All @@ -44,30 +57,30 @@ function installAsyncHooksForTest() {
destroy(asyncId) {
if (asyncId === id) {
events.push({ eventName: 'destroy' });
hook.disable();
resolve(events);
destroyed = true;
}
}
}).enable();
});
}

function test(binding) {
binding.asynccontext.makeCallback(common.mustCall(), { foo: 'foo' });
if (!checkAsyncHooks())
if (!checkAsyncHooks()) {
return;
}

const hooks = installAsyncHooksForTest();
const triggerAsyncId = async_hooks.executionAsyncId();
hooks.then(actual => {
assert.deepStrictEqual(actual, [
{ eventName: 'init',
type: 'async_context_test',
triggerAsyncId: triggerAsyncId,
resource: { foo: 'foo' } },
{ eventName: 'before' },
{ eventName: 'after' },
{ eventName: 'destroy' }
]);
binding.asynccontext.makeCallback(common.mustCall(), { foo: 'foo' });
return hooks.then(actual => {
assert.deepStrictEqual(actual, [
{ eventName: 'init',
type: 'async_context_test',
triggerAsyncId: triggerAsyncId,
resource: { foo: 'foo' } },
{ eventName: 'before' },
{ eventName: 'after' },
{ eventName: 'destroy' }
]);
}).catch(common.mustNotCall());
}
6 changes: 2 additions & 4 deletions test/asyncprogressqueueworker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const common = require('./common')
const assert = require('assert');
const os = require('os');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = common.runTest(test);

async function test({ asyncprogressqueueworker }) {
await success(asyncprogressqueueworker);
Expand Down
5 changes: 2 additions & 3 deletions test/asyncprogressworker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const common = require('./common')
const assert = require('assert');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = common.runTest(test);

async function test({ asyncprogressworker }) {
await success(asyncprogressworker);
Expand Down
5 changes: 2 additions & 3 deletions test/asyncworker-nocallback.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const common = require('./common');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = common.runTest(test);

async function test(binding) {
await binding.asyncworker.doWorkNoCallback(true, {})
Expand Down
13 changes: 5 additions & 8 deletions test/asyncworker-persistent.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const common = require('./common');
const binding = require(`./build/${buildType}/binding.node`);
const noexceptBinding = require(`./build/${buildType}/binding_noexcept.node`);

function test(binding, succeed) {
return new Promise((resolve) =>
Expand All @@ -21,7 +18,7 @@ function test(binding, succeed) {
}));
}

module.exports = test(binding.persistentasyncworker, false)
.then(() => test(binding.persistentasyncworker, true))
.then(() => test(noexceptBinding.persistentasyncworker, false))
.then(() => test(noexceptBinding.persistentasyncworker, true));
module.exports = require('./common').runTest(async binding => {
await test(binding.persistentasyncworker, false);
await test(binding.persistentasyncworker, true);
});
23 changes: 17 additions & 6 deletions test/asyncworker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const common = require('./common');

Expand All @@ -17,14 +16,27 @@ function checkAsyncHooks() {
return false;
}

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = common.runTest(test);

function installAsyncHooksForTest() {
return new Promise((resolve, reject) => {
let id;
const events = [];
const hook = async_hooks.createHook({
/**
* TODO(legendecas): investigate why resolving & disabling hooks in
* destroy callback causing crash with case 'callbackscope.js'.
*/
let hook;
let destroyed = false;
const interval = setInterval(() => {
if (destroyed) {
hook.disable();
clearInterval(interval);
resolve(events);
}
}, 10);

hook = async_hooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (id === undefined && type === 'TestResource') {
id = asyncId;
Expand All @@ -44,8 +56,7 @@ function installAsyncHooksForTest() {
destroy(asyncId) {
if (asyncId === id) {
events.push({ eventName: 'destroy' });
hook.disable();
resolve(events);
destroyed = true;
}
}
}).enable();
Expand Down
4 changes: 1 addition & 3 deletions test/basic_types/array.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {

Expand Down
5 changes: 2 additions & 3 deletions test/basic_types/boolean.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {
const bool1 = binding.basic_types_boolean.createBoolean(true);
Expand Down
4 changes: 1 addition & 3 deletions test/basic_types/number.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {
const MIN_INT32 = -2147483648;
Expand Down
4 changes: 1 addition & 3 deletions test/basic_types/value.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {
const externalValue = binding.basic_types_value.createExternal();
Expand Down
4 changes: 1 addition & 3 deletions test/bigint.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
module.exports = require('./common').runTest(test);

function test(binding) {
const {
Expand Down
5 changes: 2 additions & 3 deletions test/buffer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;

const assert = require('assert');
const testUtil = require('./testUtil');
const safeBuffer = require('safe-buffer');

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));
module.exports = require('./common').runTest(test);

function test(binding) {
return testUtil.runGCTests([
Expand Down
15 changes: 8 additions & 7 deletions test/callbackscope.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const common = require('./common');

// we only check async hooks on 8.x an higher were
// they are closer to working properly
Expand All @@ -17,16 +15,15 @@ function checkAsyncHooks() {
return false;
}

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
module.exports = require('./common').runTest(test);

function test(binding) {
if (!checkAsyncHooks())
return;

let id;
let insideHook = false;
async_hooks.createHook({
const hook = async_hooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (id === undefined && type === 'callback_scope_test') {
id = asyncId;
Expand All @@ -42,7 +39,11 @@ function test(binding) {
}
}).enable();

binding.callbackscope.runInCallbackScope(function() {
assert(insideHook);
return new Promise(resolve => {
binding.callbackscope.runInCallbackScope(function() {
assert(insideHook);
hook.disable();
resolve();
});
});
}
27 changes: 27 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,30 @@ exports.mustNotCall = function(msg) {
assert.fail(msg || 'function should not have been called');
};
};

exports.runTest = async function(test, buildType) {
buildType = buildType || process.config.target_defaults.default_configuration;

const bindings = [
`../build/${buildType}/binding.node`,
`../build/${buildType}/binding_noexcept.node`,
].map(it => require.resolve(it));

for (const item of bindings) {
await Promise.resolve(test(require(item)))
.finally(exports.mustCall());
}
}

exports.runTestWithBindingPath = async function(test, buildType) {
buildType = buildType || process.config.target_defaults.default_configuration;

const bindings = [
`../build/${buildType}/binding.node`,
`../build/${buildType}/binding_noexcept.node`,
].map(it => require.resolve(it));

for (const item of bindings) {
await test(item);
}
}
5 changes: 1 addition & 4 deletions test/dataview/dataview.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict';

const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));
module.exports = require('../common').runTest(test);

function test(binding) {
function testDataViewCreation(factory, arrayBuffer, offset, length) {
Expand Down
Loading

0 comments on commit b6f5eb1

Please sign in to comment.