Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: graceful termination on --test only #43977

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const {
ERR_TEST_FAILURE,
},
} = require('internal/errors');
const { getOptionValue } = require('internal/options');
const { Test, ItTest, Suite } = require('internal/test_runner/test');


const isTestRunner = getOptionValue('--test');
const testResources = new SafeMap();
const root = new Test({ __proto__: null, name: '<root>' });
let wasRootSetup = false;
Expand Down Expand Up @@ -134,8 +135,11 @@ function setup(root) {
process.on('uncaughtException', exceptionHandler);
process.on('unhandledRejection', rejectionHandler);
process.on('beforeExit', exitHandler);
process.on('SIGINT', terminationHandler);
process.on('SIGTERM', terminationHandler);
// TODO(MoLow): Make it configurable to hook when isTestRunner === false.
if (isTestRunner) {
process.on('SIGINT', terminationHandler);
process.on('SIGTERM', terminationHandler);
}

root.reporter.pipe(process.stdout);
root.reporter.version();
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/test-runner/never_ending_async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const test = require('node:test');
const { setTimeout } = require('timers/promises');

test('never ending test', () => {
return setTimeout(100_000_000);
});
MoLow marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions test/fixtures/test-runner/never_ending_sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const test = require('node:test');

test('never ending test', () => {
while (true) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC @tniessen :)


}
});
MoLow marked this conversation as resolved.
Show resolved Hide resolved
MoLow marked this conversation as resolved.
Show resolved Hide resolved
51 changes: 33 additions & 18 deletions test/parallel/test-runner-exit-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const { spawnSync } = require('child_process');
const { setTimeout } = require('timers/promises');
const { spawnSync, spawn } = require('child_process');
const { once } = require('events');
const { finished } = require('stream/promises');

async function runAndKill(file) {
let stdout = '';
child = spawn(process.execPath, ['--test', file]);
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => {
if (!stdout.length) child.kill('SIGINT');
stdout += chunk;
})
await Promise.all([once('exit'), finished(child.stdout)]);
assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
return stdout;
}

if (process.argv[2] === 'child') {
const test = require('node:test');
Expand All @@ -17,12 +32,6 @@ if (process.argv[2] === 'child') {
test('failing test', () => {
assert.strictEqual(true, false);
});
} else if (process.argv[3] === 'never_ends') {
assert.strictEqual(process.argv[3], 'never_ends');
test('never ending test', () => {
return setTimeout(100_000_000);
});
process.kill(process.pid, 'SIGINT');
} else assert.fail('unreachable');
} else {
let child = spawnSync(process.execPath, [__filename, 'child', 'pass']);
Expand All @@ -37,14 +46,20 @@ if (process.argv[2] === 'child') {
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);

child = spawnSync(process.execPath, [__filename, 'child', 'never_ends']);
assert.strictEqual(child.status, 1);
assert.strictEqual(child.signal, null);
if (common.isWindows) {
common.printSkipMessage('signals are not supported in windows');
} else {
const stdout = child.stdout.toString();
assert.match(stdout, /not ok 1 - never ending test/);
assert.match(stdout, /# cancelled 1/);
}
runAndKill(fixtures.path('test-runner', 'never_ending_sync.js')).then(common.mustCall(async (stdout) => {
if (common.isWindows) {
common.printSkipMessage('signals are not supported in windows');
} else {
assert.match(stdout, /not ok 1/);
assert.match(stdout, /# cancelled 1\n/);
}
}));
runAndKill(fixtures.path('test-runner', 'never_ending_async.js')).then(common.mustCall(async (stdout) => {
if (common.isWindows) {
common.printSkipMessage('signals are not supported in windows');
} else {
assert.match(stdout, /not ok 1/);
assert.match(stdout, /# cancelled 1\n/);
}
}));
MoLow marked this conversation as resolved.
Show resolved Hide resolved
}