test: fix flaky test-cluster-shared-leak
[platform/upstream/nodejs.git] / test / parallel / test-cluster-shared-leak.js
1 // In Node 4.2.1 on operating systems other than Linux, this test triggers an
2 // assertion in cluster.js. The assertion protects against memory leaks.
3 // https://github.com/nodejs/node/pull/3510
4
5 'use strict';
6 const common = require('../common');
7 const assert = require('assert');
8 const net = require('net');
9 const cluster = require('cluster');
10 cluster.schedulingPolicy = cluster.SCHED_NONE;
11
12 if (cluster.isMaster) {
13   var conn, worker1, worker2;
14
15   worker1 = cluster.fork();
16   worker1.on('message', common.mustCall(function() {
17     worker2 = cluster.fork();
18     worker2.on('online', function() {
19       conn = net.connect(common.PORT, common.mustCall(function() {
20         worker1.disconnect();
21         worker2.disconnect();
22       }));
23       conn.on('error', function(e) {
24         // ECONNRESET is OK
25         if (e.code !== 'ECONNRESET')
26           throw e;
27       });
28     });
29   }));
30
31   cluster.on('exit', function(worker, exitCode, signalCode) {
32     assert(worker === worker1 || worker === worker2);
33     assert.strictEqual(exitCode, 0);
34     assert.strictEqual(signalCode, null);
35     if (Object.keys(cluster.workers).length === 0)
36       conn.destroy();
37   });
38
39   return;
40 }
41
42 const server = net.createServer(function(c) {
43   c.on('error', function(e) {
44     // ECONNRESET is OK, so we don't exit with code !== 0
45     if (e.code !== 'ECONNRESET')
46       throw e;
47   });
48   c.end('bye');
49 });
50
51 server.listen(common.PORT, function() {
52   process.send('listening');
53 });