node/test/parallel/test-cluster-worker-events.js
cjihrig 67368d8553 cluster: expose result of send()
There are several places in the cluster module where a version
of process.send() is called, but the result is swallowed. Most
of these cases are internal, but Worker.prototype.send(), which
is publicly documented, also suffers from this problem. This
commit exposes the return value to facilitate better error
handling, and bring Worker.prototype.send() into compliance
with the documentation.

PR-URL: https://github.com/nodejs/node/pull/6998
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ron Korving <ron@ronkorving.nl>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2016-05-31 09:45:14 -04:00

59 lines
1011 B
JavaScript

'use strict';
require('../common');
var assert = require('assert');
var cluster = require('cluster');
var OK = 2;
if (cluster.isMaster) {
var worker = cluster.fork();
worker.on('exit', function(code) {
assert.equal(code, OK);
process.exit(0);
});
const result = worker.send('SOME MESSAGE');
assert.strictEqual(result, true);
return;
}
// Messages sent to a worker will be emitted on both the process object and the
// process.worker object.
assert(cluster.isWorker);
var sawProcess;
var sawWorker;
process.on('message', function(m) {
assert(!sawProcess);
sawProcess = true;
check(m);
});
cluster.worker.on('message', function(m) {
assert(!sawWorker);
sawWorker = true;
check(m);
});
var messages = [];
function check(m) {
messages.push(m);
if (messages.length < 2) return;
assert.deepStrictEqual(messages[0], messages[1]);
cluster.worker.once('error', function(e) {
assert.equal(e, 'HI');
process.exit(OK);
});
process.emit('error', 'HI');
}