Skip to content

Commit c4f6723

Browse files
committed
Use thread IDs
1 parent af30e73 commit c4f6723

File tree

4 files changed

+8
-20
lines changed

4 files changed

+8
-20
lines changed

lib/fork.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ export function _testOnlyReplaceWorkerPath(replacement) {
1414
workerPath = replacement;
1515
}
1616

17-
let forkCounter = 0;
18-
1917
const additionalExecArgv = ['--enable-source-maps'];
2018

2119
const createWorker = (options, execArgv) => {
@@ -68,9 +66,6 @@ const createWorker = (options, execArgv) => {
6866
};
6967

7068
export default function loadFork(file, options, execArgv = process.execArgv) {
71-
// TODO: this can be changed to use `threadId` when using worker_threads
72-
const forkId = `fork/${++forkCounter}`;
73-
7469
let finished = false;
7570

7671
const emitter = new Emittery();
@@ -83,7 +78,6 @@ export default function loadFork(file, options, execArgv = process.execArgv) {
8378
options = {
8479
baseDir: process.cwd(),
8580
file,
86-
forkId,
8781
...options,
8882
};
8983

@@ -161,7 +155,7 @@ export default function loadFork(file, options, execArgv = process.execArgv) {
161155

162156
return {
163157
file,
164-
forkId,
158+
threadId: worker.threadId,
165159
promise,
166160

167161
exit() {

lib/plugin-support/shared-worker-loader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {EventEmitter, on} from 'node:events';
22
import process from 'node:process';
3-
import {workerData, parentPort} from 'node:worker_threads';
3+
import {workerData, parentPort, threadId} from 'node:worker_threads';
44

55
import pkg from '../pkg.cjs';
66

@@ -117,7 +117,7 @@ async function * receiveMessages(fromTestWorker, replyTo) {
117117
}
118118

119119
let messageCounter = 0;
120-
const messageIdPrefix = `${workerData.id}/message`;
120+
const messageIdPrefix = `${threadId}/message`;
121121
const nextMessageId = () => `${messageIdPrefix}/${++messageCounter}`;
122122

123123
function publishMessage(testWorker, data, replyTo) {

lib/plugin-support/shared-workers.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import serializeError from '../serialize-error.js';
66

77
const LOADER = new URL('shared-worker-loader.js', import.meta.url);
88

9-
let sharedWorkerCounter = 0;
109
const launchedWorkers = new Map();
1110

1211
const waitForAvailable = async worker => {
@@ -22,14 +21,11 @@ function launchWorker(filename, initialData) {
2221
return launchedWorkers.get(filename);
2322
}
2423

25-
// TODO: remove the custom id and use the built-in thread id.
26-
const id = `shared-worker/${++sharedWorkerCounter}`;
2724
const worker = new Worker(LOADER, {
2825
// Ensure the worker crashes for unhandled rejections, rather than allowing undefined behavior.
2926
execArgv: ['--unhandled-rejections=strict'],
3027
workerData: {
3128
filename,
32-
id,
3329
initialData,
3430
},
3531
});
@@ -69,7 +65,7 @@ export async function observeWorkerProcess(fork, runStatus) {
6965
const launched = launchWorker(filename, initialData);
7066

7167
const handleWorkerMessage = async message => {
72-
if (message.type === 'deregistered-test-worker' && message.id === fork.forkId) {
68+
if (message.type === 'deregistered-test-worker' && message.id === fork.threadId) {
7369
launched.worker.off('message', handleWorkerMessage);
7470

7571
registrationCount--;
@@ -95,15 +91,15 @@ export async function observeWorkerProcess(fork, runStatus) {
9591

9692
launched.worker.postMessage({
9793
type: 'register-test-worker',
98-
id: fork.forkId,
94+
id: fork.threadId,
9995
file: pathToFileURL(fork.file).toString(),
10096
port,
10197
}, [port]);
10298

10399
fork.promise.finally(() => {
104100
launched.worker.postMessage({
105101
type: 'deregister-test-worker',
106-
id: fork.forkId,
102+
id: fork.threadId,
107103
});
108104
});
109105

lib/worker/channel.cjs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
'use strict';
22
const events = require('events');
33
const process = require('process');
4-
const {MessageChannel} = require('worker_threads');
4+
const {MessageChannel, threadId} = require('worker_threads');
55

66
const pEvent = require('p-event');
77

88
const timers = require('../now-and-timers.cjs');
99

10-
const {get: getOptions} = require('./options.cjs');
1110
const {isRunningInChildProcess, isRunningInThread} = require('./utils.cjs');
1211

1312
const selectAvaMessage = type => message => message.ava && message.ava.type === type;
@@ -150,8 +149,7 @@ function createChannelEmitter(channelId) {
150149
}
151150

152151
function registerSharedWorker(filename, initialData) {
153-
const {forkId} = getOptions();
154-
const channelId = `${forkId}/channel/${++channelCounter}`;
152+
const channelId = `${threadId}/channel/${++channelCounter}`;
155153

156154
const {port1: ourPort, port2: theirPort} = new MessageChannel();
157155
const sharedWorkerHandle = new MessagePortHandle(ourPort);

0 commit comments

Comments
 (0)