From 93290487eb6dc3d7c6bf304c1a7c13e4139e4c40 Mon Sep 17 00:00:00 2001 From: Andreu Botella Date: Mon, 13 Sep 2021 13:47:17 +0200 Subject: [PATCH] fix: worker_message_before_close was flaky (#12019) --- .../testdata/worker_message_before_close.js | 23 +++++++++++++++---- .../worker_message_before_close.js.out | 5 +--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cli/tests/testdata/worker_message_before_close.js b/cli/tests/testdata/worker_message_before_close.js index 6a9149af13..8ad35b3fc5 100644 --- a/cli/tests/testdata/worker_message_before_close.js +++ b/cli/tests/testdata/worker_message_before_close.js @@ -1,16 +1,29 @@ +let messagesReceived = 0; + for (let i = 0; i < 4; i++) { const worker = new Worker( new URL("./workers/message_before_close.js", import.meta.url).href, { type: "module", name: String(i) }, ); - worker.addEventListener("message", (message) => { - // Only print responses after all reception logs. - setTimeout(() => { - console.log("response from worker %d received", message.data); - }, 500); + worker.addEventListener("message", () => { + messagesReceived += 1; + + if (messagesReceived == 4) { + console.log("received all 4 responses from the workers"); + } }); + worker.postMessage(i); } +globalThis.addEventListener("unload", () => { + if (messagesReceived !== 4) { + console.log( + "received only %d responses from the workers", + messagesReceived, + ); + } +}); + export {}; diff --git a/cli/tests/testdata/worker_message_before_close.js.out b/cli/tests/testdata/worker_message_before_close.js.out index f1cc558a24..02a50ea955 100644 --- a/cli/tests/testdata/worker_message_before_close.js.out +++ b/cli/tests/testdata/worker_message_before_close.js.out @@ -2,7 +2,4 @@ message received in worker 0 message received in worker 1 message received in worker 2 message received in worker 3 -response from worker 0 received -response from worker 1 received -response from worker 2 received -response from worker 3 received +received all 4 responses from the workers