11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
41cb0ef41Sopenharmony_ciif (common.isWindows)
51cb0ef41Sopenharmony_ci  common.skip('dgram clustering is currently not supported on Windows.');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst cluster = require('cluster');
81cb0ef41Sopenharmony_ciconst dgram = require('dgram');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Test an edge case when using `cluster` and `dgram.Socket.bind()`
111cb0ef41Sopenharmony_ci// the port of `0`.
121cb0ef41Sopenharmony_ciconst kPort = 0;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction child() {
151cb0ef41Sopenharmony_ci  const kTime = 2;
161cb0ef41Sopenharmony_ci  const countdown = new Countdown(kTime * 2, () => {
171cb0ef41Sopenharmony_ci    process.exit(0);
181cb0ef41Sopenharmony_ci  });
191cb0ef41Sopenharmony_ci  for (let i = 0; i < kTime; i += 1) {
201cb0ef41Sopenharmony_ci    const socket = new dgram.Socket('udp4');
211cb0ef41Sopenharmony_ci    socket.bind(kPort, common.mustCall(() => {
221cb0ef41Sopenharmony_ci      // `process.nextTick()` or `socket2.close()` would throw
231cb0ef41Sopenharmony_ci      // ERR_SOCKET_DGRAM_NOT_RUNNING
241cb0ef41Sopenharmony_ci      process.nextTick(() => {
251cb0ef41Sopenharmony_ci        socket.close(countdown.dec());
261cb0ef41Sopenharmony_ci        const socket2 = new dgram.Socket('udp4');
271cb0ef41Sopenharmony_ci        socket2.bind(kPort, common.mustCall(() => {
281cb0ef41Sopenharmony_ci          process.nextTick(() => {
291cb0ef41Sopenharmony_ci            socket2.close(countdown.dec());
301cb0ef41Sopenharmony_ci          });
311cb0ef41Sopenharmony_ci        }));
321cb0ef41Sopenharmony_ci      });
331cb0ef41Sopenharmony_ci    }));
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciif (cluster.isMaster)
381cb0ef41Sopenharmony_ci  cluster.fork(__filename);
391cb0ef41Sopenharmony_cielse
401cb0ef41Sopenharmony_ci  child();
41