summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorBert Belder <bertbelder@gmail.com>2012-06-18 15:35:40 +0200
committerBert Belder <bertbelder@gmail.com>2012-06-18 15:36:26 +0200
commit1b7d23e81e9c4d507e96558c52c2a3962f0cd39f (patch)
tree0ec9d9a0bf67fa1d436711440e2d3a78b5ffebda /test
parentf80513974ec525fe8e1a3aab7054af167d862d99 (diff)
downloadnodejs-1b7d23e81e9c4d507e96558c52c2a3962f0cd39f.tar.gz
nodejs-1b7d23e81e9c4d507e96558c52c2a3962f0cd39f.tar.bz2
nodejs-1b7d23e81e9c4d507e96558c52c2a3962f0cd39f.zip
Fix test-cluster-message so it passes on Windows
The test was relying on a particular order of events that cannot be guaranteed. Also fixes some typos.
Diffstat (limited to 'test')
-rw-r--r--test/simple/test-cluster-message.js41
1 files changed, 25 insertions, 16 deletions
diff --git a/test/simple/test-cluster-message.js b/test/simple/test-cluster-message.js
index e60fbf0cb..313355f7f 100644
--- a/test/simple/test-cluster-message.js
+++ b/test/simple/test-cluster-message.js
@@ -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');
}
@@ -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');
});
@@ -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);
}
});
@@ -117,7 +127,6 @@ else if (cluster.isMaster) {
worker.on('exit', function() {
process.exit(0);
});
-
});
process.once('exit', function() {