node/test/async-hooks/test-graph.signal.js
James M Snell 8caa1dcee6 test: rely less on duplicative common test harness utilities
There are several cleanups here that are not just style nits...

1. The `common.isMainThread` was just a passthrough to the
   `isMainThread` export on the worker_thread module. It's
   use was inconsistent and just obfuscated the fact that
   the test file depend on the `worker_threads` built-in.
   By eliminating it we simplify the test harness a bit and
   make it clearer which tests depend on the worker_threads
   check.
2. The `common.isDumbTerminal` is fairly unnecesary since
   that just wraps a public API check.
3. Several of the `common.skipIf....` checks were inconsistently
   used and really don't need to be separate utility functions.

A key part of the motivation here is to work towards making more
of the tests more self-contained and less reliant on the common
test harness where possible.

PR-URL: https://github.com/nodejs/node/pull/56712
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-01-25 07:23:09 +00:00

70 lines
2.3 KiB
JavaScript

'use strict';
const common = require('../common');
if (common.isWindows) {
common.skip('no signals on Windows');
}
const { isMainThread } = require('worker_threads');
if (!isMainThread) {
common.skip('No signal handling available in Workers');
}
const initHooks = require('./init-hooks');
const verifyGraph = require('./verify-graph');
const { exec } = require('child_process');
const hooks = initHooks();
hooks.enable();
const interval = setInterval(() => {}, 9999); // Keep event loop open
process.on('SIGUSR2', common.mustCall(onsigusr2, 2));
let count = 0;
exec(`kill -USR2 ${process.pid}`);
function onsigusr2() {
count++;
if (count === 1) {
// Trigger same signal handler again
exec(`kill -USR2 ${process.pid}`);
} else {
// Install another signal handler
process.removeAllListeners('SIGUSR2');
process.on('SIGUSR2', common.mustCall(onsigusr2Again));
exec(`kill -USR2 ${process.pid}`);
}
}
function onsigusr2Again() {
clearInterval(interval); // Let the event loop close
}
process.on('exit', onexit);
function onexit() {
hooks.disable();
verifyGraph(
hooks,
[ { type: 'SIGNALWRAP', id: 'signal:1', triggerAsyncId: null },
{ type: 'PROCESSWRAP', id: 'process:1', triggerAsyncId: null },
{ type: 'PIPEWRAP', id: 'pipe:1', triggerAsyncId: null },
{ type: 'PIPEWRAP', id: 'pipe:2', triggerAsyncId: null },
{ type: 'PIPEWRAP', id: 'pipe:3', triggerAsyncId: null },
{ type: 'PROCESSWRAP', id: 'process:2', triggerAsyncId: 'signal:1' },
{ type: 'PIPEWRAP', id: 'pipe:4', triggerAsyncId: 'signal:1' },
{ type: 'PIPEWRAP', id: 'pipe:5', triggerAsyncId: 'signal:1' },
{ type: 'PIPEWRAP', id: 'pipe:6', triggerAsyncId: 'signal:1' },
{ type: 'SIGNALWRAP', id: 'signal:2',
// TEST_THREAD_ID is set by tools/test.py. Adjust test results depending
// on whether the test was invoked via test.py or from the shell
// directly.
triggerAsyncId: process.env.TEST_THREAD_ID ? 'signal:1' : 'pipe:2' },
{ type: 'PROCESSWRAP', id: 'process:3', triggerAsyncId: 'signal:1' },
{ type: 'PIPEWRAP', id: 'pipe:7', triggerAsyncId: 'signal:1' },
{ type: 'PIPEWRAP', id: 'pipe:8', triggerAsyncId: 'signal:1' },
{ type: 'PIPEWRAP', id: 'pipe:9', triggerAsyncId: 'signal:1' } ],
);
}