• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4// This test should fail because at present `cluster` does not know how to share
5// a socket when `worker1` binds with `port: 0`, and others try to bind to the
6// assigned port number from `worker1`
7//
8// *Note*: since this is a `known_issue` we try to swallow all errors except
9// the one we are interested in
10
11const assert = require('assert');
12const cluster = require('cluster');
13const dgram = require('dgram');
14const BYE = 'bye';
15
16if (cluster.isPrimary) {
17  const worker1 = cluster.fork();
18
19  // Verify that Windows doesn't support this scenario
20  worker1.on('error', (err) => {
21    if (err.code === 'ENOTSUP') throw err;
22  });
23
24  worker1.on('message', (msg) => {
25    if (typeof msg !== 'object') process.exit(0);
26    if (msg.message !== 'success') process.exit(0);
27    if (typeof msg.port1 !== 'number') process.exit(0);
28
29    const worker2 = cluster.fork({ PRT1: msg.port1 });
30    worker2.on('message', () => process.exit(0));
31    worker2.on('exit', (code, signal) => {
32      // This is the droid we are looking for
33      assert.strictEqual(code, 0);
34      assert.strictEqual(signal, null);
35    });
36
37    // cleanup anyway
38    process.on('exit', () => {
39      worker1.send(BYE);
40      worker2.send(BYE);
41    });
42  });
43  // end primary code
44} else {
45  // worker code
46  process.on('message', (msg) => msg === BYE && process.exit(0));
47
48  // First worker will bind to '0', second will try the assigned port and fail
49  const PRT1 = process.env.PRT1 || 0;
50  const socket1 = dgram.createSocket('udp4', () => {});
51  socket1.on('error', PRT1 === 0 ? () => {} : assert.fail);
52  socket1.bind(
53    { address: common.localhostIPv4, port: PRT1, exclusive: false },
54    () => process.send({ message: 'success', port1: socket1.address().port }),
55  );
56}
57