diff --git a/test/fixtures/sigint.js b/test/fixtures/sigint.js index fe984229..64192f14 100644 --- a/test/fixtures/sigint.js +++ b/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() { diff --git a/test/misc/sigint.test.js b/test/misc/sigint.test.js index b0728351..6fa8ca74 100644 --- a/test/misc/sigint.test.js +++ b/test/misc/sigint.test.js @@ -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(); + }); + }); }); diff --git a/test/utils.js b/test/utils.js index 4a96c92a..f579a202 100644 --- a/test/utils.js +++ b/test/utils.js @@ -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, @@ -132,4 +142,5 @@ module.exports = { appcoffee: appcoffee, monitorForChange: monitorForChange, port: port, + isRunning: isRunning };