Skip to content

Instantly share code, notes, and snippets.

@AndreasMadsen
Created February 5, 2012 19:08
Show Gist options
  • Save AndreasMadsen/1747317 to your computer and use it in GitHub Desktop.
Save AndreasMadsen/1747317 to your computer and use it in GitHub Desktop.
test signals
{ SIGUSR2: { died: true, message: 'exit emitted' },
SIGUSR1: { died: false, message: 'not emitted' },
SIGINFO:
{ died: false,
message: 'signal not supported' },
SIGWINCH: { died: false, message: 'not emitted' },
SIGPROF: { died: true, message: 'exit emitted' },
SIGVTALRM: { died: true, message: 'exit emitted' },
SIGXFSZ: { died: true, message: 'exit emitted' },
SIGXCPU: { died: true, message: 'exit emitted' },
SIGIO: { died: false, message: 'not emitted' },
SIGTTOU: { died: false, message: 'not emitted' },
SIGTTIN: { died: false, message: 'not emitted' },
SIGCHILD:
{ died: false,
message: 'signal not supported' },
SIGCONT: { died: false, message: 'not emitted' },
SIGTSTP: { died: false, message: 'not emitted' },
SIGSTOP: { died: false, message: 'not emitted' },
SIGURG: { died: false, message: 'not emitted' },
SIGTERM: { died: true, message: 'exit emitted' },
SIGALRM: { died: true, message: 'exit emitted' },
SIGPIPE: { died: false, message: 'not emitted' },
SIGSYS: { died: false, message: 'not emitted' },
SIGSEGV: { died: false, message: 'not emitted' },
SIGBUS: { died: true, message: 'exit emitted' },
SIGKILL: { died: true, message: 'exit emitted' },
SIGFPE: { died: false, message: 'not emitted' },
SIGEMT:
{ died: false,
message: 'signal not supported' },
SIGABRT: { died: false, message: 'not emitted' },
SIGTRAP: { died: true, message: 'exit emitted' },
SIGILL: { died: true, message: 'exit emitted' },
SIGQUIT: { died: false, message: 'not emitted' },
SIGINT: { died: true, message: 'exit emitted' },
SIGHUP: { died: true, message: 'exit emitted' } }
if (process.argv[2] === 'child') {
console.log(process.argv[3] + ' started with pid = ' + process.pid);
setInterval(function () {
console.log(process.argv[3] + ' alive');
}, 500);
process.send('ready');
} else {
var child_process = require('child_process');
var constants = require('constants');
var util = require('util');
var signals = ['HUP', 'INT', 'QUIT', 'ILL', 'TRAP', 'ABRT', 'EMT', 'FPE', 'KILL', 'BUS', 'SEGV',
'SYS', 'PIPE', 'ALRM', 'TERM', 'URG', 'STOP', 'TSTP', 'CONT', 'CHILD', 'TTIN',
'TTOU', 'IO', 'XCPU', 'XFSZ', 'VTALRM', 'PROF', 'WINCH', 'INFO', 'USR1', 'USR2'];
var result = {};
var i = signals.length;
while (i--) {
result['SIG' + signals[i]] = {
died: false,
message: 'message is missing'
};
}
var isDead = function (pid) {
try {
process.kill(pid, 0);
return false;
} catch (e) {
return true;
}
};
var missing = signals.length;
var done = function () {
missing -= 1;
if (missing === 0) {
console.log(util.inspect(result, false, 2, true));
}
};
var testSignal = function (signal) {
if (constants[signal] === undefined) {
result[signal].message = 'signal not supported';
done();
return;
}
function check(pid) {
result[signal].died = isDead(pid);
done();
}
var child = child_process.fork(process.argv[1], ['child', signal]);
child.on('message', function () {
var dieHandle = function () {
console.log('die handle');
result[signal].message = 'exit emitted';
clearTimeout(timeout);
check(child.pid);
console.log(signal + ' dead');
};
var timeout = setTimeout(function () {
console.log('timeout');
result[signal].message = 'not emitted';
child.removeListener('exit', dieHandle);
process.kill(child.pid, 'SIGINT');
check(child.pid);
}, 200);
child.once('exit', dieHandle);
process.kill(child.pid, signal);
});
};
// run tests
var i = signals.length;
while (i--) {
console.log('testing signal: SIG' + signals[i]);
testSignal('SIG' + signals[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment