1'use strict'; 2 3const common = require('../common'); 4if (common.isWindows) 5 common.skip('no signals on Windows'); 6if (!common.isMainThread) 7 common.skip('No signal handling available in Workers'); 8 9const initHooks = require('./init-hooks'); 10const verifyGraph = require('./verify-graph'); 11const { exec } = require('child_process'); 12 13const hooks = initHooks(); 14 15hooks.enable(); 16const interval = setInterval(() => {}, 9999); // Keep event loop open 17process.on('SIGUSR2', common.mustCall(onsigusr2, 2)); 18 19let count = 0; 20exec(`kill -USR2 ${process.pid}`); 21 22function onsigusr2() { 23 count++; 24 25 if (count === 1) { 26 // Trigger same signal handler again 27 exec(`kill -USR2 ${process.pid}`); 28 } else { 29 // Install another signal handler 30 process.removeAllListeners('SIGUSR2'); 31 process.on('SIGUSR2', common.mustCall(onsigusr2Again)); 32 33 exec(`kill -USR2 ${process.pid}`); 34 } 35} 36 37function onsigusr2Again() { 38 clearInterval(interval); // Let the event loop close 39} 40 41process.on('exit', onexit); 42 43function onexit() { 44 hooks.disable(); 45 verifyGraph( 46 hooks, 47 [ { type: 'SIGNALWRAP', id: 'signal:1', triggerAsyncId: null }, 48 { type: 'PROCESSWRAP', id: 'process:1', triggerAsyncId: null }, 49 { type: 'PIPEWRAP', id: 'pipe:1', triggerAsyncId: null }, 50 { type: 'PIPEWRAP', id: 'pipe:2', triggerAsyncId: null }, 51 { type: 'PIPEWRAP', id: 'pipe:3', triggerAsyncId: null }, 52 { type: 'PROCESSWRAP', id: 'process:2', triggerAsyncId: 'signal:1' }, 53 { type: 'PIPEWRAP', id: 'pipe:4', triggerAsyncId: 'signal:1' }, 54 { type: 'PIPEWRAP', id: 'pipe:5', triggerAsyncId: 'signal:1' }, 55 { type: 'PIPEWRAP', id: 'pipe:6', triggerAsyncId: 'signal:1' }, 56 { type: 'SIGNALWRAP', id: 'signal:2', 57 // TEST_THREAD_ID is set by tools/test.py. Adjust test results depending 58 // on whether the test was invoked via test.py or from the shell 59 // directly. 60 triggerAsyncId: process.env.TEST_THREAD_ID ? 'signal:1' : 'pipe:2' }, 61 { type: 'PROCESSWRAP', id: 'process:3', triggerAsyncId: 'signal:1' }, 62 { type: 'PIPEWRAP', id: 'pipe:7', triggerAsyncId: 'signal:1' }, 63 { type: 'PIPEWRAP', id: 'pipe:8', triggerAsyncId: 'signal:1' }, 64 { type: 'PIPEWRAP', id: 'pipe:9', triggerAsyncId: 'signal:1' } ] 65 ); 66} 67