Skip to content

Commit 10d4e4e

Browse files
committed
test: fix net transfer tests in worker mode
Use explicit workerData roles for helper workers so the test file remains the coordinator when run via tools/test.py --worker. Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent 40e16b5 commit 10d4e4e

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

test/parallel/test-net-server-transfer-worker.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
// This test verifies that a listening net.Server can be transferred to a worker
4-
// thread via worker_threads postMessage()'s transferList. The main thread binds
5-
// and listens, then hands the server off; the worker accepts connections and
4+
// thread via worker_threads postMessage()'s transferList. The parent thread
5+
// binds and listens, then hands the server off; the worker accepts connections and
66
// responds, proving the listening socket (and its accept queue) moved loops.
77

88
const common = require('../common');
@@ -16,12 +16,12 @@ const assert = require('assert');
1616
const net = require('net');
1717
const {
1818
Worker,
19-
isMainThread,
2019
parentPort,
2120
threadId,
21+
workerData,
2222
} = require('worker_threads');
2323

24-
if (!isMainThread) {
24+
if (workerData?.role === 'server') {
2525
parentPort.on('message', common.mustCall(({ server }) => {
2626
assert.ok(server instanceof net.Server);
2727
server.on('connection', (socket) => {
@@ -31,7 +31,7 @@ if (!isMainThread) {
3131
return;
3232
}
3333

34-
const worker = new Worker(__filename);
34+
const worker = new Worker(__filename, { workerData: { role: 'server' } });
3535

3636
const server = net.createServer();
3737

test/parallel/test-net-socket-transfer-worker-http.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
// This test verifies HTTP load balancing on top of transferred TCP sockets:
4-
// the main thread accepts connections and distributes each one round-robin to a
5-
// pool of worker threads, where an http.Server handles the request. It exercises
4+
// the parent thread accepts connections and distributes each one round-robin to
5+
// a pool of worker threads, where an http.Server handles the request. It exercises
66
// the intended use case for transferring net.Socket handles across threads.
77

88
const common = require('../common');
@@ -17,17 +17,17 @@ const net = require('net');
1717
const http = require('http');
1818
const {
1919
Worker,
20-
isMainThread,
2120
parentPort,
2221
threadId,
22+
workerData,
2323
} = require('worker_threads');
2424

2525
const POOL = 4;
2626
const REQ_PER_WORKER = 5;
2727
const TOTAL = POOL * REQ_PER_WORKER;
2828

29-
if (!isMainThread) {
30-
// Each worker serves HTTP over connections transferred from the main thread.
29+
if (workerData?.role === 'http-server') {
30+
// Each worker serves HTTP over connections transferred from the parent thread.
3131
const server = http.createServer((req, res) => {
3232
res.end(`thread:${threadId}`);
3333
});
@@ -40,7 +40,7 @@ if (!isMainThread) {
4040

4141
const workers = [];
4242
for (let i = 0; i < POOL; i++)
43-
workers.push(new Worker(__filename));
43+
workers.push(new Worker(__filename, { workerData: { role: 'http-server' } }));
4444

4545
let next = 0;
4646
const front = net.createServer((socket) => {

test/parallel/test-net-socket-transfer-worker.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// This test verifies that an accepted net.Socket connection can be transferred
44
// to a worker thread via worker_threads postMessage()'s transferList, and that
5-
// the worker can read from and write to it. The main thread accepts the
5+
// the worker can read from and write to it. The parent thread accepts the
66
// connection and hands it off; the worker echoes data back.
77

88
const common = require('../common');
@@ -16,11 +16,11 @@ const assert = require('assert');
1616
const net = require('net');
1717
const {
1818
Worker,
19-
isMainThread,
2019
parentPort,
20+
workerData,
2121
} = require('worker_threads');
2222

23-
if (!isMainThread) {
23+
if (workerData?.role === 'socket') {
2424
// Worker side: receive the transferred connection and echo everything back.
2525
parentPort.on('message', common.mustCall(({ socket }) => {
2626
assert.ok(socket instanceof net.Socket);
@@ -32,7 +32,7 @@ if (!isMainThread) {
3232
return;
3333
}
3434

35-
const worker = new Worker(__filename);
35+
const worker = new Worker(__filename, { workerData: { role: 'socket' } });
3636

3737
const server = net.createServer(common.mustCall((socket) => {
3838
// Hand the freshly accepted connection off to the worker. It must not be

0 commit comments

Comments
 (0)