summaryrefslogtreecommitdiff
path: root/test/known_issues/test-dgram-bind-shared-ports-after-port-0.js
blob: 5ceac4ebeeb5ecd9d9b5cd3574c5d52ed305ca00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const common = require('../common');

// This test should fail because at present `cluster` does not know how to share
// a socket when `worker1` binds with `port: 0`, and others try to bind to the
// assigned port number from `worker1`
//
// *Note*: since this is a `known_issue` we try to swallow all errors except
// the one we are interested in

const assert = require('assert');
const cluster = require('cluster');
const dgram = require('dgram');
const BYE = 'bye';

if (cluster.isMaster) {
  const worker1 = cluster.fork();

  // Verify that Windows doesn't support this scenario
  worker1.on('error', (err) => {
    if (err.code === 'ENOTSUP') throw err;
  });

  worker1.on('message', (msg) => {
    if (typeof msg !== 'object') process.exit(0);
    if (msg.message !== 'success') process.exit(0);
    if (typeof msg.port1 !== 'number') process.exit(0);

    const worker2 = cluster.fork({ PRT1: msg.port1 });
    worker2.on('message', () => process.exit(0));
    worker2.on('exit', (code, signal) => {
      // This is the droid we are looking for
      assert.strictEqual(code, 0);
      assert.strictEqual(signal, null);
    });

    // cleanup anyway
    process.on('exit', () => {
      worker1.send(BYE);
      worker2.send(BYE);
    });
  });
  // end master code
} else {
  // worker code
  process.on('message', (msg) => msg === BYE && process.exit(0));

  // First worker will bind to '0', second will try the assigned port and fail
  const PRT1 = process.env.PRT1 || 0;
  const socket1 = dgram.createSocket('udp4', () => {});
  socket1.on('error', PRT1 === 0 ? () => {} : assert.fail);
  socket1.bind(
    { address: common.localhostIPv4, port: PRT1, exclusive: false },
    () => process.send({ message: 'success', port1: socket1.address().port })
  );
}