deno/tests/unit/broadcast_channel_test.ts
Asher Gomez 92f6188253
chore: use @std import instead of @test_util/std (#22398)
This PR:
1. Replaces `@test_util/std`-prefixed imports with `@std`.
2. Adds `@std/` import map entries to a few `deno.json` files.
2024-02-13 02:05:10 +00:00

35 lines
962 B
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@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();
});