node/test/parallel/test-child-process-spawn-timeout-kill-signal.js
Nam Yooseong 39d73036e7
lib: use validators for argument validation
This refactors internal validation helpers in `child_process` to use
the common validators in `lib/internal/validators.js` where possible.

This improves code consistency and maintainability.

PR-URL: https://github.com/nodejs/node/pull/59416
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-09-21 02:47:14 +00:00

51 lines
1.4 KiB
JavaScript

'use strict';
const { mustCall } = require('../common');
const { strictEqual, throws } = require('assert');
const fixtures = require('../common/fixtures');
const { spawn } = require('child_process');
const { getEventListeners } = 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) => 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) => strictEqual(ks, 'SIGKILL')));
}
{
// Verify timeout verification
throws(() => spawn(process.execPath, [fixtures.path(aliveForeverFile)], {
timeout: 'badValue',
}), /ERR_INVALID_ARG_TYPE/);
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,
});
strictEqual(getEventListeners(signal, 'abort').length, 1);
cp.on('exit', mustCall(() => {
strictEqual(getEventListeners(signal, 'abort').length, 0);
}));
}