2020-12-22 13:27:14 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-02-11 18:59:00 +00:00
|
|
|
const { mustCall, mustNotCall } = require('../common');
|
2020-12-22 13:27:14 +00:00
|
|
|
const { strictEqual } = require('assert');
|
|
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
const { fork } = require('child_process');
|
|
|
|
|
|
|
|
{
|
|
|
|
// Test aborting a forked child_process after calling fork
|
|
|
|
const ac = new AbortController();
|
|
|
|
const { signal } = ac;
|
|
|
|
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
|
|
|
|
signal
|
|
|
|
});
|
2021-02-11 18:59:00 +00:00
|
|
|
cp.on('exit', mustCall((code, killSignal) => {
|
|
|
|
strictEqual(code, null);
|
|
|
|
strictEqual(killSignal, 'SIGTERM');
|
|
|
|
}));
|
2020-12-22 13:27:14 +00:00
|
|
|
cp.on('error', mustCall((err) => {
|
|
|
|
strictEqual(err.name, 'AbortError');
|
|
|
|
}));
|
|
|
|
process.nextTick(() => ac.abort());
|
|
|
|
}
|
2023-05-08 08:17:03 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Test aborting with custom error
|
|
|
|
const ac = new AbortController();
|
|
|
|
const { signal } = ac;
|
|
|
|
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
|
|
|
|
signal
|
|
|
|
});
|
|
|
|
cp.on('exit', mustCall((code, killSignal) => {
|
|
|
|
strictEqual(code, null);
|
|
|
|
strictEqual(killSignal, 'SIGTERM');
|
|
|
|
}));
|
|
|
|
cp.on('error', mustCall((err) => {
|
|
|
|
strictEqual(err.name, 'AbortError');
|
|
|
|
strictEqual(err.cause.name, 'Error');
|
|
|
|
strictEqual(err.cause.message, 'boom');
|
|
|
|
}));
|
|
|
|
process.nextTick(() => ac.abort(new Error('boom')));
|
|
|
|
}
|
|
|
|
|
2020-12-22 13:27:14 +00:00
|
|
|
{
|
|
|
|
// Test passing an already aborted signal to a forked child_process
|
2021-03-18 16:52:08 +00:00
|
|
|
const signal = AbortSignal.abort();
|
2020-12-22 13:27:14 +00:00
|
|
|
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
|
|
|
|
signal
|
|
|
|
});
|
2021-02-11 18:59:00 +00:00
|
|
|
cp.on('exit', mustCall((code, killSignal) => {
|
|
|
|
strictEqual(code, null);
|
|
|
|
strictEqual(killSignal, 'SIGTERM');
|
|
|
|
}));
|
|
|
|
cp.on('error', mustCall((err) => {
|
|
|
|
strictEqual(err.name, 'AbortError');
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-05-08 08:17:03 +00:00
|
|
|
{
|
|
|
|
// Test passing an aborted signal with custom error to a forked child_process
|
|
|
|
const signal = AbortSignal.abort(new Error('boom'));
|
|
|
|
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
|
|
|
|
signal
|
|
|
|
});
|
|
|
|
cp.on('exit', mustCall((code, killSignal) => {
|
|
|
|
strictEqual(code, null);
|
|
|
|
strictEqual(killSignal, 'SIGTERM');
|
|
|
|
}));
|
|
|
|
cp.on('error', mustCall((err) => {
|
|
|
|
strictEqual(err.name, 'AbortError');
|
|
|
|
strictEqual(err.cause.name, 'Error');
|
|
|
|
strictEqual(err.cause.message, 'boom');
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:59:00 +00:00
|
|
|
{
|
|
|
|
// Test passing a different kill signal
|
2021-03-18 16:52:08 +00:00
|
|
|
const signal = AbortSignal.abort();
|
2021-02-11 18:59:00 +00:00
|
|
|
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
|
|
|
|
signal,
|
|
|
|
killSignal: 'SIGKILL',
|
|
|
|
});
|
|
|
|
cp.on('exit', mustCall((code, killSignal) => {
|
|
|
|
strictEqual(code, null);
|
|
|
|
strictEqual(killSignal, 'SIGKILL');
|
|
|
|
}));
|
2020-12-22 13:27:14 +00:00
|
|
|
cp.on('error', mustCall((err) => {
|
|
|
|
strictEqual(err.name, 'AbortError');
|
|
|
|
}));
|
|
|
|
}
|
2021-02-11 18:59:00 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Test aborting a cp before close but after exit
|
|
|
|
const ac = new AbortController();
|
|
|
|
const { signal } = ac;
|
|
|
|
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
|
|
|
|
signal
|
|
|
|
});
|
|
|
|
cp.on('exit', mustCall(() => {
|
|
|
|
ac.abort();
|
|
|
|
}));
|
|
|
|
cp.on('error', mustNotCall());
|
|
|
|
|
|
|
|
setTimeout(() => cp.kill(), 1);
|
|
|
|
}
|