node/test/parallel/test-cluster-worker-wait-server-close.js
Michael Dawson 15164cebce lib,src: update cluster to use Parent
Doc deprecate isMaster and setupMaster in favor
of isPrimary and setupPrimary.

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: https://github.com/nodejs/node/pull/36478
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Beth Griggs <bgriggs@redhat.com>
2021-01-05 15:41:45 -05:00

49 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const net = require('net');
let serverClosed = false;
if (cluster.isWorker) {
const server = net.createServer(function(socket) {
// Wait for any data, then close connection
socket.write('.');
socket.on('data', () => {});
}).listen(0, common.localhostIPv4);
server.once('close', function() {
serverClosed = true;
});
// Although not typical, the worker process can exit before the disconnect
// event fires. Use this to keep the process open until the event has fired.
const keepOpen = setInterval(() => {}, 9999);
// Check worker events and properties
process.once('disconnect', function() {
// Disconnect should occur after socket close
assert(serverClosed);
clearInterval(keepOpen);
});
} else if (cluster.isPrimary) {
// start worker
const worker = cluster.fork();
// Disconnect worker when it is ready
worker.once('listening', function(address) {
const socket = net.createConnection(address.port, common.localhostIPv4);
socket.on('connect', function() {
socket.on('data', function() {
console.log('got data from client');
// Socket definitely connected to worker if we got data
worker.disconnect();
socket.end();
});
});
});
}