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‎

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,19 @@ function startWorker(skipChild, sharedBuffer){
5050
console.error(e);
5151
throwe;
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+
letresolveConsoleRequest;
57+
letresolveMessagesSent;
58+
worker.onConsoleRequest=
59+
newPromise((resolve)=>resolveConsoleRequest=resolve);
60+
worker.onMessagesSent=
61+
newPromise((resolve)=>resolveMessagesSent=resolve);
62+
worker.on('message',(m)=>{
5463
resolve(worker);
55-
});
56-
});
57-
}
58-
59-
functionwaitForConsoleRequest(worker){
60-
returnnewPromise((resolve)=>{
61-
worker.on('message',({ doConsoleLog })=>{
62-
if(doConsoleLog){
63-
resolve();
64-
}
65-
});
66-
});
67-
}
68-
69-
functionwaitForMessagesSent(worker){
70-
returnnewPromise((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
constarrayBuffer=newUint8Array(sharedBuffer);
10899
arrayBuffer[0]=1;
109100
constworker=awaitstartWorker(false,sharedBuffer);
110-
waitForConsoleRequest(worker).then(doConsoleLog.bind(null,arrayBuffer));
111-
constworkerDonePromise=waitForMessagesSent(worker);
101+
worker.onConsoleRequest.then(doConsoleLog.bind(null,arrayBuffer));
112102
assert.strictEqual(toDebug(),400);
113-
assert.deepStrictEqual(awaitworkerDonePromise,[
103+
assert.deepStrictEqual(awaitworker.onMessagesSent,[
114104
'Debugger.enable',
115105
'Runtime.enable',
116106
'Debugger.setBreakpointByUrl',
@@ -122,7 +112,7 @@ async function main(){
122112
asyncfunctionchildMain(){
123113
// Ensures the worker does not terminate too soon
124114
parentPort.on('message',()=>{});
125-
awaitwaitForMessagesSent(awaitstartWorker(true));
115+
await(awaitstartWorker(true)).onMessagesSent;
126116
constsession=newSession();
127117
session.connectToMainThread();
128118
awaitpost(session,'Debugger.enable');

0 commit comments

Comments
(0)