Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky test-inspector-connect-main-thread #31637

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions test/parallel/test-inspector-connect-main-thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,19 @@ function startWorker(skipChild, sharedBuffer) {
console.error(e);
throw e;
});
worker.once('message', (m) => {
// Add 2 promises to the worker, one resolved when a message with a
// .doConsoleLog property is received and one resolved when a .messagesSent
// property is received.
let resolveConsoleRequest;
let resolveMessagesSent;
worker.onConsoleRequest =
new Promise((resolve) => resolveConsoleRequest = resolve);
worker.onMessagesSent =
new Promise((resolve) => resolveMessagesSent = resolve);
worker.on('message', (m) => {
resolve(worker);
});
});
}

function waitForConsoleRequest(worker) {
return new Promise((resolve) => {
worker.on('message', ({ doConsoleLog }) => {
if (doConsoleLog) {
resolve();
}
});
});
}

function waitForMessagesSent(worker) {
return new Promise((resolve) => {
worker.on('message', ({ messagesSent }) => {
if (messagesSent) {
resolve(messagesSent);
}
if (m.doConsoleLog) resolveConsoleRequest();
if (m.messagesSent) resolveMessagesSent(m.messagesSent);
});
});
}
Expand Down Expand Up @@ -107,10 +98,9 @@ async function main() {
const arrayBuffer = new Uint8Array(sharedBuffer);
arrayBuffer[0] = 1;
const worker = await startWorker(false, sharedBuffer);
waitForConsoleRequest(worker).then(doConsoleLog.bind(null, arrayBuffer));
const workerDonePromise = waitForMessagesSent(worker);
worker.onConsoleRequest.then(doConsoleLog.bind(null, arrayBuffer));
assert.strictEqual(toDebug(), 400);
assert.deepStrictEqual(await workerDonePromise, [
assert.deepStrictEqual(await worker.onMessagesSent, [
'Debugger.enable',
'Runtime.enable',
'Debugger.setBreakpointByUrl',
Expand All @@ -122,7 +112,7 @@ async function main() {
async function childMain() {
// Ensures the worker does not terminate too soon
parentPort.on('message', () => { });
await waitForMessagesSent(await startWorker(true));
await (await startWorker(true)).onMessagesSent;
const session = new Session();
session.connectToMainThread();
await post(session, 'Debugger.enable');
Expand Down