11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// This test asserts the semantics of dgram::socket.bind({ exclusive })
261cb0ef41Sopenharmony_ci// when called from a cluster.Worker
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst assert = require('assert');
291cb0ef41Sopenharmony_ciconst cluster = require('cluster');
301cb0ef41Sopenharmony_ciconst dgram = require('dgram');
311cb0ef41Sopenharmony_ciconst BYE = 'bye';
321cb0ef41Sopenharmony_ciconst WORKER2_NAME = 'wrker2';
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
351cb0ef41Sopenharmony_ci  const worker1 = cluster.fork();
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  if (common.isWindows) {
381cb0ef41Sopenharmony_ci    worker1.on('error', common.mustCall((err) => {
391cb0ef41Sopenharmony_ci      console.log(err);
401cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ENOTSUP');
411cb0ef41Sopenharmony_ci      worker1.kill();
421cb0ef41Sopenharmony_ci    }));
431cb0ef41Sopenharmony_ci    return;
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  worker1.on('message', common.mustCall((msg) => {
471cb0ef41Sopenharmony_ci    console.log(msg);
481cb0ef41Sopenharmony_ci    assert.strictEqual(msg, 'success');
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    const worker2 = cluster.fork({ WORKER2_NAME });
511cb0ef41Sopenharmony_ci    worker2.on('message', common.mustCall((msg) => {
521cb0ef41Sopenharmony_ci      console.log(msg);
531cb0ef41Sopenharmony_ci      assert.strictEqual(msg, 'socket3:EADDRINUSE');
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci      // finish test
561cb0ef41Sopenharmony_ci      worker1.send(BYE);
571cb0ef41Sopenharmony_ci      worker2.send(BYE);
581cb0ef41Sopenharmony_ci    }));
591cb0ef41Sopenharmony_ci    worker2.on('exit', common.mustCall((code, signal) => {
601cb0ef41Sopenharmony_ci      assert.strictEqual(signal, null);
611cb0ef41Sopenharmony_ci      assert.strictEqual(code, 0);
621cb0ef41Sopenharmony_ci    }));
631cb0ef41Sopenharmony_ci  }));
641cb0ef41Sopenharmony_ci  worker1.on('exit', common.mustCall((code, signal) => {
651cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
661cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
671cb0ef41Sopenharmony_ci  }));
681cb0ef41Sopenharmony_ci  // end primary code
691cb0ef41Sopenharmony_ci} else {
701cb0ef41Sopenharmony_ci  // worker code
711cb0ef41Sopenharmony_ci  process.on('message', common.mustCallAtLeast((msg) => {
721cb0ef41Sopenharmony_ci    if (msg === BYE) process.exit(0);
731cb0ef41Sopenharmony_ci  }), 1);
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  const isSecondWorker = process.env.WORKER2_NAME === WORKER2_NAME;
761cb0ef41Sopenharmony_ci  const socket1 = dgram.createSocket('udp4', common.mustNotCall());
771cb0ef41Sopenharmony_ci  const socket2 = dgram.createSocket('udp4', common.mustNotCall());
781cb0ef41Sopenharmony_ci  const socket3 = dgram.createSocket('udp4', common.mustNotCall());
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  socket1.on('error', (err) => assert.fail(err));
811cb0ef41Sopenharmony_ci  socket2.on('error', (err) => assert.fail(err));
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  // First worker should bind, second should err
841cb0ef41Sopenharmony_ci  const socket3OnBind =
851cb0ef41Sopenharmony_ci    isSecondWorker ?
861cb0ef41Sopenharmony_ci      common.mustNotCall() :
871cb0ef41Sopenharmony_ci      common.mustCall(() => {
881cb0ef41Sopenharmony_ci        const port3 = socket3.address().port;
891cb0ef41Sopenharmony_ci        assert.strictEqual(typeof port3, 'number');
901cb0ef41Sopenharmony_ci        process.send('success');
911cb0ef41Sopenharmony_ci      });
921cb0ef41Sopenharmony_ci  // An error is expected only in the second worker
931cb0ef41Sopenharmony_ci  const socket3OnError =
941cb0ef41Sopenharmony_ci    !isSecondWorker ?
951cb0ef41Sopenharmony_ci      common.mustNotCall() :
961cb0ef41Sopenharmony_ci      common.mustCall((err) => {
971cb0ef41Sopenharmony_ci        process.send(`socket3:${err.code}`);
981cb0ef41Sopenharmony_ci      });
991cb0ef41Sopenharmony_ci  const address = common.localhostIPv4;
1001cb0ef41Sopenharmony_ci  const opt1 = { address, port: 0, exclusive: false };
1011cb0ef41Sopenharmony_ci  const opt2 = { address, port: common.PORT, exclusive: false };
1021cb0ef41Sopenharmony_ci  const opt3 = { address, port: common.PORT + 1, exclusive: true };
1031cb0ef41Sopenharmony_ci  socket1.bind(opt1, common.mustCall(() => {
1041cb0ef41Sopenharmony_ci    const port1 = socket1.address().port;
1051cb0ef41Sopenharmony_ci    assert.strictEqual(typeof port1, 'number');
1061cb0ef41Sopenharmony_ci    socket2.bind(opt2, common.mustCall(() => {
1071cb0ef41Sopenharmony_ci      const port2 = socket2.address().port;
1081cb0ef41Sopenharmony_ci      assert.strictEqual(typeof port2, 'number');
1091cb0ef41Sopenharmony_ci      socket3.on('error', socket3OnError);
1101cb0ef41Sopenharmony_ci      socket3.bind(opt3, socket3OnBind);
1111cb0ef41Sopenharmony_ci    }));
1121cb0ef41Sopenharmony_ci  }));
1131cb0ef41Sopenharmony_ci}
114