• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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'use strict';
23const common = require('../common');
24const assert = require('assert');
25const cluster = require('cluster');
26
27if (cluster.isWorker) {
28
29  // Just keep the worker alive
30  process.send(process.argv[2]);
31
32} else if (cluster.isMaster) {
33
34  const checks = {
35    args: false,
36    setupEvent: false,
37    settingsObject: false
38  };
39
40  const totalWorkers = 2;
41  let settings;
42
43  // Setup master
44  cluster.setupMaster({
45    args: ['custom argument'],
46    silent: true
47  });
48
49  cluster.once('setup', function() {
50    checks.setupEvent = true;
51
52    settings = cluster.settings;
53    if (settings &&
54        settings.args && settings.args[0] === 'custom argument' &&
55        settings.silent === true &&
56        settings.exec === process.argv[1]) {
57      checks.settingsObject = true;
58    }
59  });
60
61  let correctInput = 0;
62
63  cluster.on('online', common.mustCall(function listener(worker) {
64
65    worker.once('message', function(data) {
66      correctInput += (data === 'custom argument' ? 1 : 0);
67      if (correctInput === totalWorkers) {
68        checks.args = true;
69      }
70      worker.kill();
71    });
72
73  }, totalWorkers));
74
75  // Start all workers
76  cluster.fork();
77  cluster.fork();
78
79  // Check all values
80  process.once('exit', function() {
81    const argsMsg = 'Arguments was not send for one or more worker. ' +
82                    `${correctInput} workers receive argument, ` +
83                    `but ${totalWorkers} were expected.`;
84    assert.ok(checks.args, argsMsg);
85
86    assert.ok(checks.setupEvent, 'The setup event was never emitted');
87
88    const settingObjectMsg = 'The settingsObject do not have correct ' +
89                             `properties : ${JSON.stringify(settings)}`;
90    assert.ok(checks.settingsObject, settingObjectMsg);
91  });
92
93}
94