Skip to content

Commit

Permalink
test: fork child node processes
Browse files Browse the repository at this point in the history
  • Loading branch information
heisian committed Dec 6, 2017
1 parent 183add9 commit f711fb7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
17 changes: 15 additions & 2 deletions lib/monitor/run.js
Expand Up @@ -4,6 +4,7 @@ var bus = utils.bus;
var childProcess = require('child_process');
var spawn = childProcess.spawn;
var exec = childProcess.exec;
var fork = childProcess.fork;
var watch = require('./watch').watch;
var config = require('../config');
var child = null; // the actual child process we spawn
Expand Down Expand Up @@ -82,7 +83,16 @@ function run(options) {
});
}

child = spawn.apply(null, spawnArgs);
if (executable === 'node') {
var forkArgs = cmd.args.slice(1);
var env = utils.merge(options.execOptions.env, process.env);
child = fork(options.execOptions.script, forkArgs, {
env: env,
silent: true,
});
} else {
child = spawn.apply(null, spawnArgs);
}

if (config.required) {
var emit = {
Expand Down Expand Up @@ -227,10 +237,13 @@ function run(options) {
callback();
});
}
} else if (!noRestart) {
} else if (!noRestart && executable !== 'node') {
// if there's no child, then we need to manually start the process
// this is because as there was no child, the child.on('exit') event
// handler doesn't exist which would normally trigger the restart.
//
// We only perform this for non-node processes since `fork` automatically
// handles cleanup of child processes when the parent dies.
bus.once('start', callback);
restart();
} else {
Expand Down
2 changes: 2 additions & 0 deletions lib/monitor/watch.js
Expand Up @@ -146,6 +146,8 @@ function filterAndRestart(files) {
// reset the last check so we're only looking at recently modified files
config.lastStarted = Date.now();

console.log(matched.result);

if (matched.result.length) {
if (config.options.delay > 0) {
utils.log.detail('delaying restart for ' + config.options.delay + 'ms');
Expand Down
53 changes: 25 additions & 28 deletions test/monitor/run.test.js
Expand Up @@ -153,32 +153,29 @@ describe('when nodemon runs (2)', function () {
}, 1500);
});

// it('should kill child on SIGINT', function (done) {
// fs.writeFileSync(tmp, 'setTimeout(function () { var n = 10; }, 10000)');

// nodemon({ script: tmp, verbose: true }).on('start', function () {
// assert(true, 'nodemon is waiting for a change');

// setTimeout(function () {
// // process.once('SIGINT', function () {
// // // do nothing
// // console.log('not going to exit');
// // });

// process.kill(process.pid, 'SIGINT');
// }, 1000);
// }).on('crash', function () {
// assert(false, 'detected crashed state');
// }).on('exit', function () {
// assert(true, 'quit correctly');
// nodemon.reset();
// done();

// setTimeout(function () {
// process.kill(process.pid, 'SIGINT');
// }, 1000);

// });

// });
it('should kill child on SIGINT', function (done) {
fs.writeFileSync(tmp, 'setTimeout(function () { var n = 10; }, 10000)');

nodemon({ script: tmp, verbose: true }).on('start', function () {
assert(true, 'nodemon is waiting for a change');

setTimeout(function () {
process.once('SIGINT', function () {
// do nothing
});

process.kill(process.pid, 'SIGINT');
}, 1000);
}).on('crash', function () {
assert(false, 'detected crashed state');
}).on('exit', function () {
assert(true, 'quit correctly');
nodemon.reset();
done();

setTimeout(function () {
process.kill(process.pid, 'SIGINT');
}, 1000);
});
});
});

0 comments on commit f711fb7

Please sign in to comment.