mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 18:37:06 +00:00

PR-URL: https://github.com/nodejs/node/pull/37798 Refs: https://github.com/whatwg/dom/pull/960 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com>
107 lines
2.4 KiB
JavaScript
107 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { spawn } = require('child_process');
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
const aliveScript = fixtures.path('child-process-stay-alive-forever.js');
|
|
{
|
|
// Verify that passing an AbortSignal works
|
|
const controller = new AbortController();
|
|
const { signal } = controller;
|
|
|
|
const cp = spawn(process.execPath, [aliveScript], {
|
|
signal,
|
|
});
|
|
|
|
cp.on('exit', common.mustCall((code, killSignal) => {
|
|
assert.strictEqual(code, null);
|
|
assert.strictEqual(killSignal, 'SIGTERM');
|
|
}));
|
|
|
|
cp.on('error', common.mustCall((e) => {
|
|
assert.strictEqual(e.name, 'AbortError');
|
|
}));
|
|
|
|
controller.abort();
|
|
}
|
|
|
|
{
|
|
// Verify that passing an already-aborted signal works.
|
|
const signal = AbortSignal.abort();
|
|
|
|
const cp = spawn(process.execPath, [aliveScript], {
|
|
signal,
|
|
});
|
|
cp.on('exit', common.mustCall((code, killSignal) => {
|
|
assert.strictEqual(code, null);
|
|
assert.strictEqual(killSignal, 'SIGTERM');
|
|
}));
|
|
|
|
cp.on('error', common.mustCall((e) => {
|
|
assert.strictEqual(e.name, 'AbortError');
|
|
}));
|
|
}
|
|
|
|
{
|
|
// Verify that waiting a bit and closing works
|
|
const controller = new AbortController();
|
|
const { signal } = controller;
|
|
|
|
const cp = spawn(process.execPath, [aliveScript], {
|
|
signal,
|
|
});
|
|
|
|
cp.on('exit', common.mustCall((code, killSignal) => {
|
|
assert.strictEqual(code, null);
|
|
assert.strictEqual(killSignal, 'SIGTERM');
|
|
}));
|
|
|
|
cp.on('error', common.mustCall((e) => {
|
|
assert.strictEqual(e.name, 'AbortError');
|
|
}));
|
|
|
|
setTimeout(() => controller.abort(), 1);
|
|
}
|
|
|
|
{
|
|
// Test passing a different killSignal
|
|
const controller = new AbortController();
|
|
const { signal } = controller;
|
|
|
|
const cp = spawn(process.execPath, [aliveScript], {
|
|
signal,
|
|
killSignal: 'SIGKILL',
|
|
});
|
|
|
|
cp.on('exit', common.mustCall((code, killSignal) => {
|
|
assert.strictEqual(code, null);
|
|
assert.strictEqual(killSignal, 'SIGKILL');
|
|
}));
|
|
|
|
cp.on('error', common.mustCall((e) => {
|
|
assert.strictEqual(e.name, 'AbortError');
|
|
}));
|
|
|
|
setTimeout(() => controller.abort(), 1);
|
|
}
|
|
|
|
{
|
|
// Test aborting a cp before close but after exit
|
|
const controller = new AbortController();
|
|
const { signal } = controller;
|
|
|
|
const cp = spawn(process.execPath, [aliveScript], {
|
|
signal,
|
|
});
|
|
|
|
cp.on('exit', common.mustCall(() => {
|
|
controller.abort();
|
|
}));
|
|
|
|
cp.on('error', common.mustNotCall());
|
|
|
|
setTimeout(() => cp.kill(), 1);
|
|
}
|