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: add test for reused AbortController with execfile() #36644

Merged
merged 4 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 9 additions & 6 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ function execFile(file /* , args, options, callback */) {
}
}

function abortHandler() {
if (!ex)
ex = new AbortError();
process.nextTick(() => kill());
}

if (options.timeout > 0) {
timeoutId = setTimeout(function delayedKill() {
kill();
Expand All @@ -376,14 +382,11 @@ function execFile(file /* , args, options, callback */) {
}
if (options.signal) {
if (options.signal.aborted) {
process.nextTick(() => kill());
process.nextTick(abortHandler);
} else {
const childController = new AbortController();
options.signal.addEventListener('abort', () => {
if (!ex)
ex = new AbortError();
kill();
}, { signal: childController.signal });
options.signal.addEventListener('abort', abortHandler,
{ signal: childController.signal });
child.once('close', () => childController.abort());
}
}
Expand Down
17 changes: 12 additions & 5 deletions test/parallel/test-child-process-execfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ const execOpts = { encoding: 'utf8', shell: true };
const ac = new AbortController();
const { signal } = ac;

const callback = common.mustCall((err) => {
assert.strictEqual(err.code, 'ABORT_ERR');
assert.strictEqual(err.name, 'AbortError');
});
execFile(process.execPath, [echoFixture, 0], { signal }, callback);
const test = () => {
const check = common.mustCall((err) => {
assert.strictEqual(err.code, 'ABORT_ERR');
assert.strictEqual(err.name, 'AbortError');
assert.strictEqual(err.signal, undefined);
});
execFile(process.execPath, [echoFixture, 0], { signal }, check);
};

test();
ac.abort();
// Verify that it still works the same way now that the signal is aborted.
test();
}

{
Expand Down
44 changes: 32 additions & 12 deletions test/parallel/test-child-process-spawn-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,38 @@ const common = require('../common');
const assert = require('assert');
const cp = require('child_process');

// Verify that passing an AbortSignal works
const controller = new AbortController();
const { signal } = controller;
{
// Verify that passing an AbortSignal works
const controller = new AbortController();
const { signal } = controller;

const echo = cp.spawn('echo', ['fun'], {
encoding: 'utf8',
shell: true,
signal
});
const echo = cp.spawn('echo', ['fun'], {
encoding: 'utf8',
shell: true,
signal
});

echo.on('error', common.mustCall((e) => {
assert.strictEqual(e.name, 'AbortError');
}));
echo.on('error', common.mustCall((e) => {
assert.strictEqual(e.name, 'AbortError');
}));

controller.abort();
controller.abort();
}

{
// Verify that passing an already-aborted signal works.
const controller = new AbortController();
const { signal } = controller;

controller.abort();

const echo = cp.spawn('echo', ['fun'], {
encoding: 'utf8',
shell: true,
signal
});

echo.on('error', common.mustCall((e) => {
assert.strictEqual(e.name, 'AbortError');
}));
}