|
| 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 | + conn = net.connect(common.PORT, common.mustCall(function() { |
| 19 | + worker1.send('die'); |
| 20 | + worker2.send('die'); |
| 21 | + })); |
| 22 | + conn.on('error', function(e) { |
| 23 | + // ECONNRESET is OK |
| 24 | + if (e.code !== 'ECONNRESET') |
| 25 | + throw e; |
| 26 | + }); |
| 27 | + })); |
| 28 | + |
| 29 | + cluster.on('exit', function(worker, exitCode, signalCode) { |
| 30 | + assert(worker === worker1 || worker === worker2); |
| 31 | + assert.strictEqual(exitCode, 0); |
| 32 | + assert.strictEqual(signalCode, null); |
| 33 | + if (Object.keys(cluster.workers).length === 0) |
| 34 | + conn.destroy(); |
| 35 | + }); |
| 36 | + |
| 37 | + return; |
| 38 | +} |
| 39 | + |
| 40 | +var server = net.createServer(function(c) { |
| 41 | + c.end('bye'); |
| 42 | +}); |
| 43 | + |
| 44 | +server.listen(common.PORT, function() { |
| 45 | + process.send('listening'); |
| 46 | +}); |
| 47 | + |
| 48 | +process.on('message', function(msg) { |
| 49 | + if (msg !== 'die') return; |
| 50 | + server.close(function() { |
| 51 | + setImmediate(() => process.disconnect()); |
| 52 | + }); |
| 53 | +}); |
0 commit comments