Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add encapsulated master

  • Loading branch information...
commit ad3ae2457a80beed7aa590f83d0a8b63a1864f8b 1 parent 14d0f28
Tom Yandell authored April 01, 2012
83  test/simple/test-cluster-encapsulated-master.js
... ...
@@ -0,0 +1,83 @@
  1
+// Copyright Joyent, Inc. and other Node contributors.
  2
+//
  3
+// Permission is hereby granted, free of charge, to any person obtaining a
  4
+// copy of this software and associated documentation files (the
  5
+// "Software"), to deal in the Software without restriction, including
  6
+// without limitation the rights to use, copy, modify, merge, publish,
  7
+// distribute, sublicense, and/or sell copies of the Software, and to permit
  8
+// persons to whom the Software is furnished to do so, subject to the
  9
+// following conditions:
  10
+//
  11
+// The above copyright notice and this permission notice shall be included
  12
+// in all copies or substantial portions of the Software.
  13
+//
  14
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21
+
  22
+
  23
+var common = require('../common');
  24
+var assert = require('assert');
  25
+var cluster = require('cluster');
  26
+
  27
+if (cluster.isWorker) {
  28
+
  29
+  // Just keep the worker alive
  30
+  process.send(process.argv[2]);
  31
+
  32
+} else if (cluster.isMaster) {
  33
+
  34
+  var checks = {
  35
+    globalMasterArgs    : false,
  36
+    localMasterArgs     : false,
  37
+    globalMasterWorkers : 0,
  38
+    localMasterWorkers  : 0
  39
+  };
  40
+
  41
+  cluster.setupMaster({
  42
+    'args' : ['custom argument']
  43
+  });
  44
+
  45
+  var localMaster = new cluster.Master();
  46
+
  47
+  localMaster.setupMaster({
  48
+    'args' : ['local custom argument']
  49
+  });
  50
+
  51
+  cluster.on('online', function lisenter(worker) {
  52
+    checks.globalMasterWorkers++;
  53
+
  54
+    worker.once('message', function(data) {
  55
+      checks.globalMasterArgs = (data === 'custom argument');
  56
+      worker.destroy();
  57
+    });
  58
+  });
  59
+
  60
+  localMaster.on('online', function lisenter(worker) {
  61
+    checks.localMasterWorkers++;
  62
+
  63
+    worker.once('message', function(data) {
  64
+      checks.localMasterArgs = (data === 'local custom argument');
  65
+      worker.destroy();
  66
+    });
  67
+  });
  68
+
  69
+  // Start workers
  70
+  cluster.fork();
  71
+  localMaster.fork();
  72
+
  73
+  // Check all values
  74
+  process.once('exit', function() {
  75
+    console.log(checks.globalMasterWorkers);
  76
+    assert.ok(checks.globalMasterWorkers === 1, 'Wrong number of workers for global master');
  77
+    assert.ok(checks.localMasterWorkers === 1, 'Wrong number of workers for local master');
  78
+    assert.ok(checks.globalMasterArgs, 'Worker for global master did not receive custom args');
  79
+    assert.ok(checks.localMasterArgs, 'Worker for local master did not receive custom args');
  80
+  });
  81
+
  82
+}
  83
+

0 notes on commit ad3ae24

Please sign in to comment.
Something went wrong with that request. Please try again.