summaryrefslogtreecommitdiff
path: root/cli/tests/unit/broadcast_channel_test.ts
blob: d56324cc048b25d4f0aed2d17456f92c3062fb73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../../../test_util/std/testing/asserts.ts";
import { deferred } from "../../../test_util/std/async/deferred.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 = deferred();

  intercom.onmessage = function (e) {
    assertEquals(count, e.data);
    if (count < 42) {
      intercom.postMessage(++count);
    } else {
      worker.terminate();
      intercom.close();
      promise.resolve();
    }
  };

  await promise;
});