aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-cluster-shared-leak.js
blob: 55df57c9831edeff0e290b2171b18cbed4f7d483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// In Node 4.2.1 on operating systems other than Linux, this test triggers an
// assertion in cluster.js. The assertion protects against memory leaks.
// https://github.com/nodejs/node/pull/3510

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const cluster = require('cluster');
cluster.schedulingPolicy = cluster.SCHED_NONE;

if (cluster.isMaster) {
  var conn, worker1, worker2;

  worker1 = cluster.fork();
  worker1.on('message', common.mustCall(function() {
    worker2 = cluster.fork();
    worker2.on('online', function() {
      conn = net.connect(common.PORT, common.mustCall(function() {
        worker1.send('die');
        worker2.send('die');
      }));
      conn.on('error', function(e) {
        // ECONNRESET is OK
        if (e.code !== 'ECONNRESET')
          throw e;
      });
    });
  }));

  cluster.on('exit', function(worker, exitCode, signalCode) {
    assert(worker === worker1 || worker === worker2);
    assert.strictEqual(exitCode, 0);
    assert.strictEqual(signalCode, null);
    if (Object.keys(cluster.workers).length === 0)
      conn.destroy();
  });

  return;
}

var server = net.createServer(function(c) {
  c.end('bye');
});

server.listen(common.PORT, function() {
  process.send('listening');
});

process.on('message', function(msg) {
  if (msg !== 'die') return;
  server.close(function() {
    setImmediate(() => process.disconnect());
  });
});