11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst net = require('net');
51cb0ef41Sopenharmony_ciconst cluster = require('cluster');
61cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// The core has bug in handling pipe handle by ipc when platform is win32,
91cb0ef41Sopenharmony_ci// it can be triggered on win32. I will fix it in another pr.
101cb0ef41Sopenharmony_ciif (common.isWindows)
111cb0ef41Sopenharmony_ci  common.skip('no setSimultaneousAccepts on pipe handle');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cilet connectionCount = 0;
141cb0ef41Sopenharmony_cilet listenCount = 0;
151cb0ef41Sopenharmony_cilet worker1;
161cb0ef41Sopenharmony_cilet worker2;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cifunction request(path) {
191cb0ef41Sopenharmony_ci  for (let i = 0; i < 10; i++) {
201cb0ef41Sopenharmony_ci    net.connect(path);
211cb0ef41Sopenharmony_ci  }
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cifunction handleMessage(message) {
251cb0ef41Sopenharmony_ci  assert.match(message.action, /listen|connection/);
261cb0ef41Sopenharmony_ci  if (message.action === 'listen') {
271cb0ef41Sopenharmony_ci    if (++listenCount === 2) {
281cb0ef41Sopenharmony_ci      request(common.PIPE);
291cb0ef41Sopenharmony_ci    }
301cb0ef41Sopenharmony_ci  } else if (message.action === 'connection') {
311cb0ef41Sopenharmony_ci    if (++connectionCount === 10) {
321cb0ef41Sopenharmony_ci      worker1.send({ action: 'disconnect' });
331cb0ef41Sopenharmony_ci      worker2.send({ action: 'disconnect' });
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
391cb0ef41Sopenharmony_ci  cluster.schedulingPolicy = cluster.SCHED_RR;
401cb0ef41Sopenharmony_ci  tmpdir.refresh();
411cb0ef41Sopenharmony_ci  worker1 = cluster.fork({ maxConnections: 1, pipePath: common.PIPE });
421cb0ef41Sopenharmony_ci  worker2 = cluster.fork({ maxConnections: 9, pipePath: common.PIPE });
431cb0ef41Sopenharmony_ci  worker1.on('message', common.mustCall((message) => {
441cb0ef41Sopenharmony_ci    handleMessage(message);
451cb0ef41Sopenharmony_ci  }, 2));
461cb0ef41Sopenharmony_ci  worker2.on('message', common.mustCall((message) => {
471cb0ef41Sopenharmony_ci    handleMessage(message);
481cb0ef41Sopenharmony_ci  }, 10));
491cb0ef41Sopenharmony_ci} else {
501cb0ef41Sopenharmony_ci  const server = net.createServer(common.mustCall((socket) => {
511cb0ef41Sopenharmony_ci    process.send({ action: 'connection' });
521cb0ef41Sopenharmony_ci  }, +process.env.maxConnections));
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  server.listen(process.env.pipePath, common.mustCall(() => {
551cb0ef41Sopenharmony_ci    process.send({ action: 'listen' });
561cb0ef41Sopenharmony_ci  }));
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  server.maxConnections = +process.env.maxConnections;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  process.on('message', common.mustCall((message) => {
611cb0ef41Sopenharmony_ci    assert.strictEqual(message.action, 'disconnect');
621cb0ef41Sopenharmony_ci    process.disconnect();
631cb0ef41Sopenharmony_ci  }));
641cb0ef41Sopenharmony_ci}
65