node/test/parallel/test-net-listen-handle-in-cluster-1.js
theanarkh 20525f14b9
lib: fix listen with handle in cluster worker
PR-URL: https://github.com/nodejs/node/pull/52056
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2024-03-14 14:54:31 +00:00

28 lines
822 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const cluster = require('cluster');
// Test if the worker can listen with handle successfully
if (cluster.isPrimary) {
const worker = cluster.fork();
const server = net.createServer();
worker.on('online', common.mustCall(() => {
server.listen(common.mustCall(() => {
// Send the server to worker
worker.send(null, server);
}));
}));
worker.on('exit', common.mustCall(() => {
server.close();
}));
} else {
// The `got` function of net.Server will create a TCP server by listen(handle)
// See lib/internal/child_process.js
process.on('message', common.mustCall((_, server) => {
assert.strictEqual(server instanceof net.Server, true);
process.exit(0);
}));
}