Skip to content

Commit

Permalink
test: make sigint test to actually check child pid (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
axxie authored and remy committed Jan 22, 2020
1 parent cd45d74 commit c279760
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 31 deletions.
3 changes: 2 additions & 1 deletion test/fixtures/sigint.js
@@ -1,5 +1,6 @@
process.on('SIGINT', function() {
// do nothing here
if (process.argv.length === 3 && process.argv[2] === '--dont-exit') return;
process.exit();
});
// timer, to keep process running
setInterval(function() {
Expand Down
69 changes: 39 additions & 30 deletions test/misc/sigint.test.js
Expand Up @@ -6,42 +6,51 @@ var utils = require('../utils'),
appjs = path.relative(process.cwd(), path.resolve(__dirname, '..', 'fixtures', 'sigint.js')),
match = utils.match,
cleanup = utils.cleanup,
run = utils.run;
run = utils.run,
isRunning = utils.isRunning;

describe('terminal signals', function () {
it('should kill child with SIGINT (after ~10 seconds)', function (done) {
var childPID = null;
function runAndKill(done, cmdline, exitcb)
{
var childPID = null;

var p = run(appjs, {
output: function (data) {
if (match(data, 'pid: ')) {
data.replace(/pid: (\d+)/, function (m) {
childPID = m;
});
}
},
error: function (data) {
assert(false, 'nodemon failed with ' + data);
cleanup(p, done);
var p = run(cmdline, {
output: function (data) {
if (match(data, 'pid: ')) {
data.replace(/pid: (\d+)/, function (_, p1) {
childPID = p1;
});
}
});
},
error: function (data) {
assert(false, 'nodemon failed with ' + data);
cleanup(p, done);
}
});

p.on('message', function (event) {
if (event.type === 'start') {
setTimeout(function () {
p.kill('SIGINT');
}, 1000);
}
}).on('exit', function () {
// check if the child process is still running
try {
process.kill(childPID, 0);
assert(false, 'child is still running at ' + childPID);
} catch (e) {
assert(true, 'child process was not running');
}
p.on('message', function (event) {
if (event.type === 'start') {
setTimeout(function () {
p.kill('SIGINT');
}, 1000);
}
}).on('exit', function () {
exitcb(childPID);
});
}

describe('terminal signals', function () {
it('should kill child with SIGINT', function (done) {
runAndKill(done, appjs, function (childPID) {
assert(!isRunning(childPID), 'child is still running at ' + childPID);
done();
});
});

it('should terminate nodemon (after ~10 seconds)', function (done) {
runAndKill(done, appjs + ' --dont-exit', function (childPID) {
// make sure we don't keep abandoned child
process.kill(childPID, 'SIGTERM');
done();
});
});
});
11 changes: 11 additions & 0 deletions test/utils.js
Expand Up @@ -121,6 +121,16 @@ function getTriggerCount(msg) {
return changes.pop();
}

function isRunning(pid) {
try {
process.kill(pid, 0)
return true
} catch (error) {
if (error.code && error.code === 'ESRCH') return false
throw error
}
}

module.exports = {
getTriggerCount: getTriggerCount,
Plan: Plan,
Expand All @@ -132,4 +142,5 @@ module.exports = {
appcoffee: appcoffee,
monitorForChange: monitorForChange,
port: port,
isRunning: isRunning
};

0 comments on commit c279760

Please sign in to comment.