node/test/parallel/test-child-process-spawn-timeout-kill-signal.js
René 9c2500295e
events: repurpose events.listenerCount() to accept EventTargets
PR-URL: https://github.com/nodejs/node/pull/60214
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2025-11-26 11:09:07 +00:00

51 lines
1.4 KiB
JavaScript

'use strict';
const { mustCall } = require('../common');
const assert = require('assert');
const fixtures = require('../common/fixtures');
const { spawn } = require('child_process');
const { listenerCount } = require('events');
const aliveForeverFile = 'child-process-stay-alive-forever.js';
{
// Verify default signal + closes
const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], {
timeout: 5,
});
cp.on('exit', mustCall((code, ks) => assert.strictEqual(ks, 'SIGTERM')));
}
{
// Verify SIGKILL signal + closes
const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], {
timeout: 6,
killSignal: 'SIGKILL',
});
cp.on('exit', mustCall((code, ks) => assert.strictEqual(ks, 'SIGKILL')));
}
{
// Verify timeout verification
assert.throws(() => spawn(process.execPath, [fixtures.path(aliveForeverFile)], {
timeout: 'badValue',
}), /ERR_INVALID_ARG_TYPE/);
assert.throws(() => spawn(process.execPath, [fixtures.path(aliveForeverFile)], {
timeout: {},
}), /ERR_INVALID_ARG_TYPE/);
}
{
// Verify abort signal gets unregistered
const controller = new AbortController();
const { signal } = controller;
const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], {
timeout: 6,
signal,
});
assert.strictEqual(listenerCount(signal, 'abort'), 1);
cp.on('exit', mustCall(() => {
assert.strictEqual(listenerCount(signal, 'abort'), 0);
}));
}