mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 18:25:13 +08:00
Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/64460 Fixes: https://github.com/nodejs/node/issues/64456 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Stefan Stojanovic <stefan.stojanovic@janeasystems.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
// This test verifies that a listening net.Server can be transferred to a worker
|
|
// thread via worker_threads postMessage()'s transferList. The parent thread
|
|
// binds and listens, then hands the server off; the worker accepts connections and
|
|
// responds, proving the listening socket (and its accept queue) moved loops.
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
const net = require('net');
|
|
const {
|
|
Worker,
|
|
parentPort,
|
|
threadId,
|
|
workerData,
|
|
} = require('worker_threads');
|
|
|
|
if (workerData?.role === 'server') {
|
|
parentPort.on('message', common.mustCall(({ server }) => {
|
|
assert.ok(server instanceof net.Server);
|
|
server.on('connection', (socket) => {
|
|
socket.end(`served-by:${threadId}`);
|
|
});
|
|
}));
|
|
return;
|
|
}
|
|
|
|
const worker = new Worker(__filename, { workerData: { role: 'server' } });
|
|
|
|
const server = net.createServer();
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const { port } = server.address();
|
|
|
|
// Move the listening server to the worker thread.
|
|
worker.postMessage({ server }, [server]);
|
|
assert.strictEqual(server._handle, null);
|
|
|
|
const N = 3;
|
|
let done = 0;
|
|
for (let i = 0; i < N; i++) {
|
|
const client = net.connect(port);
|
|
client.setEncoding('utf8');
|
|
let response = '';
|
|
client.on('data', (chunk) => { response += chunk; });
|
|
client.on('end', common.mustCall(() => {
|
|
assert.match(response, /^served-by:\d+$/);
|
|
assert.notStrictEqual(response, `served-by:${threadId}`);
|
|
if (++done === N) {
|
|
worker.terminate();
|
|
}
|
|
}));
|
|
}
|
|
}));
|