Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Fix test-cluster-message so it passes on Windows
Browse files Browse the repository at this point in the history
The test was relying on a particular order of events that cannot be
guaranteed.

Also fixes some typos.
  • Loading branch information
piscisaureus committed Jun 18, 2012
1 parent f805139 commit 1b7d23e
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions test/simple/test-cluster-message.js
Expand Up @@ -32,23 +32,34 @@ function forEach(obj, fn) {
}

if (cluster.isWorker) {
// Create a tcp server
// this will be used as cluster-shared-server
// and as an alternativ IPC channel
// Create a tcp server. This will be used as cluster-shared-server and as an
// alternative IPC channel.
var server = net.Server();
server.on('connection', function(socket) {

// Tell master using TCP socket that a message is received
process.on('message', function(message) {
socket.write(JSON.stringify({
code: 'received message',
echo: message
}));
});
var socket, message;

function maybeReply() {
if (!socket || !message) return;

// Tell master using TCP socket that a message is received.
socket.write(JSON.stringify({
code: 'received message',
echo: message
}));
}

server.on('connection', function(socket_) {
socket = socket_;
maybeReply();

// Send a message back over the IPC channel.
process.send('message from worker');
});

process.on('message', function(message_) {
message = message_;
maybeReply();
});

server.listen(common.PORT, '127.0.0.1');
}

Expand Down Expand Up @@ -93,8 +104,7 @@ else if (cluster.isMaster) {
worker.on('listening', function() {

client = net.connect(common.PORT, function() {

//Send message to worker
// Send message to worker.
worker.send('message from master');
});

Expand All @@ -105,7 +115,7 @@ else if (cluster.isMaster) {
if (data.code === 'received message') {
check('worker', data.echo === 'message from master');
} else {
throw new Error('worng TCP message recived: ' + data);
throw new Error('wrong TCP message recived: ' + data);
}
});

Expand All @@ -117,7 +127,6 @@ else if (cluster.isMaster) {
worker.on('exit', function() {
process.exit(0);
});

});

process.once('exit', function() {
Expand Down

0 comments on commit 1b7d23e

Please sign in to comment.