11cb0ef41Sopenharmony_ci// In Node 4.2.1 on operating systems other than Linux, this test triggers an
21cb0ef41Sopenharmony_ci// assertion in cluster.js. The assertion protects against memory leaks.
31cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/pull/3510
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci'use strict';
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst net = require('net');
91cb0ef41Sopenharmony_ciconst cluster = require('cluster');
101cb0ef41Sopenharmony_cicluster.schedulingPolicy = cluster.SCHED_NONE;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciif (cluster.isPrimary) {
131cb0ef41Sopenharmony_ci  let conn, worker2;
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  const worker1 = cluster.fork();
161cb0ef41Sopenharmony_ci  worker1.on('listening', common.mustCall(function(address) {
171cb0ef41Sopenharmony_ci    worker2 = cluster.fork();
181cb0ef41Sopenharmony_ci    worker2.on('online', function() {
191cb0ef41Sopenharmony_ci      conn = net.connect(address.port, common.mustCall(function() {
201cb0ef41Sopenharmony_ci        worker1.disconnect();
211cb0ef41Sopenharmony_ci        worker2.disconnect();
221cb0ef41Sopenharmony_ci      }));
231cb0ef41Sopenharmony_ci      conn.on('error', function(e) {
241cb0ef41Sopenharmony_ci        // ECONNRESET is OK
251cb0ef41Sopenharmony_ci        if (e.code !== 'ECONNRESET')
261cb0ef41Sopenharmony_ci          throw e;
271cb0ef41Sopenharmony_ci      });
281cb0ef41Sopenharmony_ci    });
291cb0ef41Sopenharmony_ci  }));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  cluster.on('exit', function(worker, exitCode, signalCode) {
321cb0ef41Sopenharmony_ci    assert(worker === worker1 || worker === worker2);
331cb0ef41Sopenharmony_ci    assert.strictEqual(exitCode, 0);
341cb0ef41Sopenharmony_ci    assert.strictEqual(signalCode, null);
351cb0ef41Sopenharmony_ci    if (Object.keys(cluster.workers).length === 0)
361cb0ef41Sopenharmony_ci      conn.destroy();
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  return;
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciconst server = net.createServer(function(c) {
431cb0ef41Sopenharmony_ci  c.on('error', function(e) {
441cb0ef41Sopenharmony_ci    // ECONNRESET is OK, so we don't exit with code !== 0
451cb0ef41Sopenharmony_ci    if (e.code !== 'ECONNRESET')
461cb0ef41Sopenharmony_ci      throw e;
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci  c.end('bye');
491cb0ef41Sopenharmony_ci});
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciserver.listen(0);
52