Skip to content

Commit 795a21d

Browse files
addaleaxcodebytere
authored andcommitted
test: fix flaky test-inspector-connect-main-thread
Previously, the test waited for a (any) message from the workers, and then attached another event listener to a specific kind of message. However, it was possible that the second listener was attached after the Worker had already exited, thus never receiving the message it was supposed to receive. (This is the race condition here – usually, the Worker thread would exit *after* the second listener was attached.) Solve this by keeping a single `'message'` event listener attached to the worker instance during its entire lifetime. Fixes: #31226 PR-URL: #31637 Reviewed-By: Eugene Ostroukhov <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent c34672a commit 795a21d

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

test/parallel/test-inspector-connect-main-thread.js

+15-25
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,19 @@ function startWorker(skipChild, sharedBuffer) {
5050
console.error(e);
5151
throw e;
5252
});
53-
worker.once('message', (m) => {
53+
// Add 2 promises to the worker, one resolved when a message with a
54+
// .doConsoleLog property is received and one resolved when a .messagesSent
55+
// property is received.
56+
let resolveConsoleRequest;
57+
let resolveMessagesSent;
58+
worker.onConsoleRequest =
59+
new Promise((resolve) => resolveConsoleRequest = resolve);
60+
worker.onMessagesSent =
61+
new Promise((resolve) => resolveMessagesSent = resolve);
62+
worker.on('message', (m) => {
5463
resolve(worker);
55-
});
56-
});
57-
}
58-
59-
function waitForConsoleRequest(worker) {
60-
return new Promise((resolve) => {
61-
worker.on('message', ({ doConsoleLog }) => {
62-
if (doConsoleLog) {
63-
resolve();
64-
}
65-
});
66-
});
67-
}
68-
69-
function waitForMessagesSent(worker) {
70-
return new Promise((resolve) => {
71-
worker.on('message', ({ messagesSent }) => {
72-
if (messagesSent) {
73-
resolve(messagesSent);
74-
}
64+
if (m.doConsoleLog) resolveConsoleRequest();
65+
if (m.messagesSent) resolveMessagesSent(m.messagesSent);
7566
});
7667
});
7768
}
@@ -107,10 +98,9 @@ async function main() {
10798
const arrayBuffer = new Uint8Array(sharedBuffer);
10899
arrayBuffer[0] = 1;
109100
const worker = await startWorker(false, sharedBuffer);
110-
waitForConsoleRequest(worker).then(doConsoleLog.bind(null, arrayBuffer));
111-
const workerDonePromise = waitForMessagesSent(worker);
101+
worker.onConsoleRequest.then(doConsoleLog.bind(null, arrayBuffer));
112102
assert.strictEqual(toDebug(), 400);
113-
assert.deepStrictEqual(await workerDonePromise, [
103+
assert.deepStrictEqual(await worker.onMessagesSent, [
114104
'Debugger.enable',
115105
'Runtime.enable',
116106
'Debugger.setBreakpointByUrl',
@@ -122,7 +112,7 @@ async function main() {
122112
async function childMain() {
123113
// Ensures the worker does not terminate too soon
124114
parentPort.on('message', () => { });
125-
await waitForMessagesSent(await startWorker(true));
115+
await (await startWorker(true)).onMessagesSent;
126116
const session = new Session();
127117
session.connectToMainThread();
128118
await post(session, 'Debugger.enable');

0 commit comments

Comments
 (0)