From 90b29e317334718816ca4d4f578ba495812370ec Mon Sep 17 00:00:00 2001 From: Sebastian Streich Date: Thu, 15 Aug 2024 16:18:39 +0100 Subject: [PATCH] Fix leaking test (#36) The test suit complains about leaking handles, so let's fix that :) --- package.json | 2 +- src/shared/ipc.js | 12 ++++++++---- tests/jest/utils/ipc.test.mjs | 28 ++++++++++------------------ 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index b07f7f0..fcae595 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "typecheck": "tsc --noEmit", "build": "web-ext build --source-dir src --filename mozilla-vpn-extension.xpi", "start": "web-ext run --verbose --source-dir src --pref=ui.popup.disable_autohide=true ", - "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --collect-coverage" + "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --collect-coverage --detectOpenHandles" }, "repository": { "type": "git", diff --git a/src/shared/ipc.js b/src/shared/ipc.js index ef3e879..74f0578 100644 --- a/src/shared/ipc.js +++ b/src/shared/ipc.js @@ -635,14 +635,18 @@ export const requestFromPort = ( port.addEventListener("message", listener); port.postMessage(message); }); - const timeout = new Promise((_, reject) => - setTimeout(() => { + const timeout = new Promise((_, reject) => { + const timer = setTimeout(() => { reject( new Error( `Timed out while waiting for (${message.type})${message.name}:${message.id}` ) ); - }, maxTimeout) - ); + }, maxTimeout); + // Cancel the timeout on success + hasMessage.then(() => { + clearTimeout(timer); + }); + }); return Promise.race([hasMessage, timeout]); }; diff --git a/tests/jest/utils/ipc.test.mjs b/tests/jest/utils/ipc.test.mjs index a97c6b0..3b5effb 100644 --- a/tests/jest/utils/ipc.test.mjs +++ b/tests/jest/utils/ipc.test.mjs @@ -491,24 +491,6 @@ describe("getResponse", () => { }); }); describe("requestFromPort", () => { - it("it Sends the passed ipcmessage", async () => { - const { port1, port2 } = new MessageChannel(); - const message = { - ...new IPCMessage(), - id: 19991, - }; - const done = new Promise((r) => { - port2.addEventListener("message", (ev) => { - r(ev.data); - port2.postMessage({ ...ev.data }); - }); - }); - - requestFromPort(port1, message); - - const outMessage = await done; - expect(outMessage.id).toBe(message.id); - }); it("it resolves on recieving a response", async () => { const { port1, port2 } = new MessageChannel(); const message = { @@ -531,6 +513,7 @@ describe("requestFromPort", () => { expect(didSendData).toBe(true); expect(response.id).toBe(message.id); expect(response.out).toBe("response"); + port1.close(); }); it("it rejects after a timeout", async () => { const { port1 } = new MessageChannel(); @@ -544,6 +527,7 @@ describe("requestFromPort", () => { } catch (error) { expect(error.toString()).toContain("Timed out while waiting for "); } + port1.close(); }); }); @@ -570,6 +554,7 @@ describe("pushBindables", () => { const response = await hasMessage; expect(response.data).toBe("updated"); + port1.close(); }); }); @@ -591,6 +576,7 @@ describe("createReplicaFunction", () => { }); await fakeFunc(); expect(was_called).toBe(true); + port1.close(); }); it("Passes Function results", async () => { const testobj = { @@ -607,6 +593,7 @@ describe("createReplicaFunction", () => { port2.postMessage(response); }); expect(await fakeFunc()).toBe(43); + port1.close(); }); }); it("Passes Function arguments", async () => { @@ -632,6 +619,7 @@ it("Passes Function arguments", async () => { expect(a).toBe(true); expect(b).toBe(99); expect(c).toBe("hello"); + port1.close(); }); describe("createReplicaGetter", () => { @@ -648,6 +636,7 @@ describe("createReplicaGetter", () => { port2.postMessage(response); }); expect(await fakeFunc()).toBe(43); + port1.close(); }); }); @@ -666,6 +655,7 @@ describe("createReplicaSetter", () => { }); await fakeFunc(999); expect(testobj.hi).toBe(999); + port1.close(); }); }); @@ -691,6 +681,7 @@ describe("createReplicaBindable", () => { }); const otherReplica = await createReplicaBindable("hello", port1); expect(otherReplica.value).toBe(43); + port1.close(); }); it("forwards changes", async () => { const { port1, port2 } = new MessageChannel(); @@ -712,5 +703,6 @@ describe("createReplicaBindable", () => { }); obj.hello.set(69); await done; + port1.close(); }); });