summaryrefslogtreecommitdiff
path: root/tests/unit/broadcast_channel_test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/broadcast_channel_test.ts')
-rw-r--r--tests/unit/broadcast_channel_test.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/unit/broadcast_channel_test.ts b/tests/unit/broadcast_channel_test.ts
new file mode 100644
index 000000000..c5d7f7e7f
--- /dev/null
+++ b/tests/unit/broadcast_channel_test.ts
@@ -0,0 +1,34 @@
+// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+import { assertEquals } from "@test_util/std/assert/mod.ts";
+
+Deno.test("BroadcastChannel worker", async () => {
+ const intercom = new BroadcastChannel("intercom");
+ let count = 0;
+
+ const url = import.meta.resolve(
+ "../testdata/workers/broadcast_channel.ts",
+ );
+ const worker = new Worker(url, { type: "module", name: "worker" });
+ worker.onmessage = () => intercom.postMessage(++count);
+
+ const { promise, resolve } = Promise.withResolvers<void>();
+
+ intercom.onmessage = function (e) {
+ assertEquals(count, e.data);
+ if (count < 42) {
+ intercom.postMessage(++count);
+ } else {
+ worker.terminate();
+ intercom.close();
+ resolve();
+ }
+ };
+
+ await promise;
+});
+
+Deno.test("BroadcastChannel immediate close after post", () => {
+ const bc = new BroadcastChannel("internal_notification");
+ bc.postMessage("New listening connected!");
+ bc.close();
+});