diff options
author | 2023-08-01 19:12:30 -0700 | |
---|---|---|
committer | 2023-08-01 19:12:30 -0700 | |
commit | 9bbac35fd072d90cf77daf2dd085898145a57d62 (patch) | |
tree | 499293a0a3411eb99e6c6e6c3414afeda34abb5b | |
parent | eba27540b057e300bc1bce830fd0206bea260df4 (diff) | |
download | bun-9bbac35fd072d90cf77daf2dd085898145a57d62.tar.gz bun-9bbac35fd072d90cf77daf2dd085898145a57d62.tar.zst bun-9bbac35fd072d90cf77daf2dd085898145a57d62.zip |
close `BroadcastChannel` in tests
-rw-r--r-- | test/js/web/broadcastchannel/broadcast-channel-worker-simple.ts | 4 | ||||
-rw-r--r-- | test/js/web/broadcastchannel/broadcast-channel.test.ts | 46 |
2 files changed, 49 insertions, 1 deletions
diff --git a/test/js/web/broadcastchannel/broadcast-channel-worker-simple.ts b/test/js/web/broadcastchannel/broadcast-channel-worker-simple.ts new file mode 100644 index 000000000..057619f08 --- /dev/null +++ b/test/js/web/broadcastchannel/broadcast-channel-worker-simple.ts @@ -0,0 +1,4 @@ +var bc = new BroadcastChannel("sleep"); +bc.onmessage = function (e) { + bc.postMessage("done!"); +}; diff --git a/test/js/web/broadcastchannel/broadcast-channel.test.ts b/test/js/web/broadcastchannel/broadcast-channel.test.ts index b60bbb18b..918bbd36b 100644 --- a/test/js/web/broadcastchannel/broadcast-channel.test.ts +++ b/test/js/web/broadcastchannel/broadcast-channel.test.ts @@ -9,6 +9,8 @@ test("postMessage results in correct event", done => { expect(e.origin).toBe(""); expect(e.data).toBe("hello world"); expect(e.source).toBe(null); + c1.close(); + c2.close(); done(); }; @@ -24,6 +26,21 @@ test("broadcast channel properties", () => { expect(c1.postMessage).toBeInstanceOf(Function); expect(c1.ref).toBeInstanceOf(Function); expect(c1.unref).toBeInstanceOf(Function); + c1.close(); +}); + +test("broadcast channel worker wait", done => { + var worker = new Worker(new URL("broadcast-channel-worker-simple.ts", import.meta.url).href); + worker.ref(); + Bun.sleepSync(500); + var bc = new BroadcastChannel("sleep"); + bc.onmessage = (e: MessageEvent) => { + expect(e.data).toBe("done!"); + bc.close(); + worker.unref(); + done(); + }; + bc.postMessage("rise and shine!"); }); test("messages are delivered in port creation order", done => { @@ -51,6 +68,9 @@ test("messages are delivered in port creation order", done => { expect(events[4].data).toBe("done"); expect(events[5].target).toBe(c3); expect(events[5].data).toBe("done"); + c1.close(); + c2.close(); + c3.close(); done(); } } @@ -75,18 +95,38 @@ test("messages aren't deliverd to a closed port.", done => { }; c2.close(); c3.onmessage = () => { + c1.close(); + c3.close(); done(); }; c1.postMessage("test"); }); +test("close broadcast channel and create another with the same name", done => { + let c1 = new BroadcastChannel("close-and-create"); + c1.close(); + let c2 = new BroadcastChannel("close-and-create"); + let c3 = new BroadcastChannel("close-and-create"); + c2.onmessage = (e: MessageEvent) => { + expect(e.data).toBe("done"); + c2.close(); + c3.close(); + done(); + }; + c3.postMessage("done"); +}); + test("messages aren't delivered to a port closed after calling postMessage.", done => { let c1 = new BroadcastChannel("closed"); let c2 = new BroadcastChannel("closed"); let c3 = new BroadcastChannel("closed"); c2.onmessage = () => expect().fail(); - c3.onmessage = () => done(); + c3.onmessage = () => { + c1.close(); + c3.close(); + done(); + }; c1.postMessage("test"); c2.close(); }); @@ -101,6 +141,8 @@ test("closing and creating channels during message delivery works correctly.", d let c3 = new BroadcastChannel("create-in-onmessage"); c3.onmessage = (event: MessageEvent) => { expect(event.data).toBe("done"); + c1.close(); + c3.close(); done(); }; c1.postMessage("done"); @@ -127,6 +169,8 @@ test("Closing a channel in onmessage prevents already queued tasks from firing o c3.addEventListener("message", (e: MessageEvent) => { if (e.data == "done") { expect(events).toEqual(["c2: first", "c3: first", "c3: done"]); + c1.close(); + c3.close(); done(); } }); |