Skip to content

Commit

Permalink
Merge branch 'NodeClusterCompatibility' of https://github.com/penartu…
Browse files Browse the repository at this point in the history
  • Loading branch information
lloyd committed May 26, 2012
2 parents be91e35 + 09a6483 commit 12c45fd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/compute-cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ ComputeCluster.prototype._onWorkerExit = function(pid) {
}
};

ComputeCluster.prototype._getEnvForWorker = function() {
var env = {};
for (var i in process.env) {
env[i] = process.env[i];
}

delete env.NODE_WORKER_ID; //Node.js cluster worker marker for v0.6
delete env.NODE_UNIQUE_ID; //Node.js cluster worker marker for v0.7

return env;
};

ComputeCluster.prototype._getFreeWorker = function() {
var self = this;

Expand All @@ -79,7 +91,7 @@ ComputeCluster.prototype._getFreeWorker = function() {
worker: child_process.fork(
this._module,
[],
{ env: process.env }
{ env: this._getEnvForWorker() }
) };
k.worker.on('exit', this._onWorkerExit(k.worker.pid));
this._kids[k.worker.pid] = k;
Expand Down
55 changes: 55 additions & 0 deletions test/inside-nodecluster-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env node

const
vows = require('vows'),
assert = require('assert'),
nodeCluster = require('cluster'),
computeCluster = require('../lib/compute-cluster'),
path = require('path');

if(nodeCluster.isMaster) {
nodeCluster.fork();
return;
}

var suite = vows.describe('basic tests');

// disable vows (often flakey?) async error behavior
suite.options.error = false;

suite.addBatch({
"allocation of a compute cluster": {
topic: function() {
return new computeCluster({
module: path.join(__dirname, 'workers', 'echo.js')
});
},
"runs without issue": function (cc) {
assert.isObject(cc);
},
"and invocation against this cluster": {
topic: function(cc) {
var cb = this.callback;
cc.enqueue("hello", function(e, r) {
cb.call(self, { cc: cc, r: r });
});

},
"succeeds": function (r) {
assert.equal(r.r, 'hello');
},
"finally, exit": {
topic: function(r) {
r.cc.exit(this.callback);
},
"also succeeds": function(err) {
assert.isNull(err);
}
}
}
}
});

// run or export the suite.
if (process.argv[1] === __filename) suite.run();
else suite.export(module);

0 comments on commit 12c45fd

Please sign in to comment.