11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This test should fail because at present `cluster` does not know how to share
51cb0ef41Sopenharmony_ci// a socket when `worker1` binds with `port: 0`, and others try to bind to the
61cb0ef41Sopenharmony_ci// assigned port number from `worker1`
71cb0ef41Sopenharmony_ci//
81cb0ef41Sopenharmony_ci// *Note*: since this is a `known_issue` we try to swallow all errors except
91cb0ef41Sopenharmony_ci// the one we are interested in
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst cluster = require('cluster');
131cb0ef41Sopenharmony_ciconst dgram = require('dgram');
141cb0ef41Sopenharmony_ciconst BYE = 'bye';
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
171cb0ef41Sopenharmony_ci  const worker1 = cluster.fork();
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  // Verify that Windows doesn't support this scenario
201cb0ef41Sopenharmony_ci  worker1.on('error', (err) => {
211cb0ef41Sopenharmony_ci    if (err.code === 'ENOTSUP') throw err;
221cb0ef41Sopenharmony_ci  });
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  worker1.on('message', (msg) => {
251cb0ef41Sopenharmony_ci    if (typeof msg !== 'object') process.exit(0);
261cb0ef41Sopenharmony_ci    if (msg.message !== 'success') process.exit(0);
271cb0ef41Sopenharmony_ci    if (typeof msg.port1 !== 'number') process.exit(0);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    const worker2 = cluster.fork({ PRT1: msg.port1 });
301cb0ef41Sopenharmony_ci    worker2.on('message', () => process.exit(0));
311cb0ef41Sopenharmony_ci    worker2.on('exit', (code, signal) => {
321cb0ef41Sopenharmony_ci      // This is the droid we are looking for
331cb0ef41Sopenharmony_ci      assert.strictEqual(code, 0);
341cb0ef41Sopenharmony_ci      assert.strictEqual(signal, null);
351cb0ef41Sopenharmony_ci    });
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    // cleanup anyway
381cb0ef41Sopenharmony_ci    process.on('exit', () => {
391cb0ef41Sopenharmony_ci      worker1.send(BYE);
401cb0ef41Sopenharmony_ci      worker2.send(BYE);
411cb0ef41Sopenharmony_ci    });
421cb0ef41Sopenharmony_ci  });
431cb0ef41Sopenharmony_ci  // end primary code
441cb0ef41Sopenharmony_ci} else {
451cb0ef41Sopenharmony_ci  // worker code
461cb0ef41Sopenharmony_ci  process.on('message', (msg) => msg === BYE && process.exit(0));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  // First worker will bind to '0', second will try the assigned port and fail
491cb0ef41Sopenharmony_ci  const PRT1 = process.env.PRT1 || 0;
501cb0ef41Sopenharmony_ci  const socket1 = dgram.createSocket('udp4', () => {});
511cb0ef41Sopenharmony_ci  socket1.on('error', PRT1 === 0 ? () => {} : assert.fail);
521cb0ef41Sopenharmony_ci  socket1.bind(
531cb0ef41Sopenharmony_ci    { address: common.localhostIPv4, port: PRT1, exclusive: false },
541cb0ef41Sopenharmony_ci    () => process.send({ message: 'success', port1: socket1.address().port }),
551cb0ef41Sopenharmony_ci  );
561cb0ef41Sopenharmony_ci}
57