Skip to content

Commit

Permalink
[api test] Expose .forkShim for communicating between 0.6.x and `…
Browse files Browse the repository at this point in the history
…0.4.x` processes
  • Loading branch information
indexzero committed Jan 6, 2012
1 parent 82e2a7d commit 6588f59
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
11 changes: 10 additions & 1 deletion lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
var events = require('events'),
fs = require('fs'),
path = require('path'),
fork = require('node-fork').fork,
nodeFork = require('node-fork'),
fork = nodeFork.fork,
spawn = require('child_process').spawn,
broadway = require('broadway'),
psTree = require('ps-tree'),
Expand Down Expand Up @@ -84,6 +85,7 @@ var Monitor = exports.Monitor = function (script, options) {
this.spawnWith = options.spawnWith || {};
this.sourceDir = options.sourceDir;
this.fork = options.fork || false;
this.forkShim = options.forkShim || false;
this.cwd = options.cwd || null;
this.hideEnv = options.hideEnv || [];
this._env = options.env || {};
Expand Down Expand Up @@ -224,7 +226,14 @@ Monitor.prototype.trySpawn = function () {
this.spawnWith.env = this._getEnv();

if (this.fork) {
this.spawnWith.silent = true;
this.spawnWith.command = this.command;

if (this.forkShim) {
this.spawnWith.env['FORK_SHIM'] = true;
return nodeFork.shim.fork(this.args[0], this.args.slice(1), this.spawnWith);
}

return fork(this.args[0], this.args.slice(1), this.spawnWith);
}

Expand Down
37 changes: 29 additions & 8 deletions test/monitor/fork-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,37 @@ var assert = require('assert'),
vows.describe('forever/monitor/fork').addBatch({
"When using forever": {
"and spawning a script that uses `process.send()`": {
topic: function () {
var script = path.join(__dirname, '..', '..', 'examples', 'process-send.js'),
child = new (forever.Monitor)(script, { silent: false, minUptime: 2000, max: 1, fork: true });
"using the 'native' fork": {
topic: function () {
var script = path.join(__dirname, '..', '..', 'examples', 'process-send.js'),
child = new (forever.Monitor)(script, { silent: false, minUptime: 2000, max: 1, fork: true });

child.on('message', this.callback.bind(null, null));
child.start();
child.on('message', this.callback.bind(null, null));
child.start();
},
"should reemit the message correctly": function (err, msg) {
assert.isObject(msg);
assert.deepEqual(msg, { from: 'child' });
}
},
"should reemit the message correctly": function (err, msg) {
assert.isObject(msg);
assert.deepEqual(msg, { from: 'child' });
"with `forkShim` true": {
topic: function () {
var script = path.join(__dirname, '..', '..', 'examples', 'process-send.js'),
child = new (forever.Monitor)(script, {
silent: false,
minUptime: 2000,
max: 1,
fork: true,
forkShim: true
});

child.on('message', this.callback.bind(null, null));
child.start();
},
"should reemit the message correctly": function (err, msg) {
assert.isObject(msg);
assert.deepEqual(msg, { from: 'child' });
}
}
}
}
Expand Down

0 comments on commit 6588f59

Please sign in to comment.