• 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');
24
25// Test that errors propagated from cluster workers are properly
26// received in their master. Creates an EADDRINUSE condition by forking
27// a process in child cluster and propagates the error to the master.
28
29const assert = require('assert');
30const cluster = require('cluster');
31const fork = require('child_process').fork;
32const net = require('net');
33
34if (cluster.isMaster && process.argv.length !== 3) {
35  // cluster.isMaster
36  const tmpdir = require('../common/tmpdir');
37  tmpdir.refresh();
38  const PIPE_NAME = common.PIPE;
39  const worker = cluster.fork({ PIPE_NAME });
40
41  // Makes sure master is able to fork the worker
42  cluster.on('fork', common.mustCall());
43
44  // Makes sure the worker is ready
45  worker.on('online', common.mustCall());
46
47  worker.on('message', common.mustCall(function(err) {
48    // Disconnect first, so that we will not leave zombies
49    worker.disconnect();
50    assert.strictEqual(err.code, 'EADDRINUSE');
51  }));
52} else if (process.argv.length !== 3) {
53  // cluster.worker
54  const PIPE_NAME = process.env.PIPE_NAME;
55  const cp = fork(__filename, [PIPE_NAME], { stdio: 'inherit' });
56
57  // Message from the child indicates it's ready and listening
58  cp.on('message', common.mustCall(function() {
59    const server = net.createServer().listen(PIPE_NAME, function() {
60      // Message child process so that it can exit
61      cp.send('end');
62      // Inform master about the unexpected situation
63      process.send('PIPE should have been in use.');
64    });
65
66    server.on('error', function(err) {
67      // Message to child process tells it to exit
68      cp.send('end');
69      // Propagate error to parent
70      process.send(err);
71    });
72  }));
73} else if (process.argv.length === 3) {
74  // Child process (of cluster.worker)
75  const PIPE_NAME = process.argv[2];
76
77  const server = net.createServer().listen(PIPE_NAME, common.mustCall(() => {
78    process.send('listening');
79  }));
80  process.once('message', common.mustCall(() => server.close()));
81} else {
82  assert.fail('Impossible state');
83}
84