Skip to content

Commit

Permalink
Fix leaking test (#36)
Browse files Browse the repository at this point in the history
The test suit complains about leaking handles, so let's fix that :)
  • Loading branch information
strseb authored Aug 15, 2024
1 parent 88cd26a commit 90b29e3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 8 additions & 4 deletions src/shared/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
};
28 changes: 10 additions & 18 deletions tests/jest/utils/ipc.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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();
Expand All @@ -544,6 +527,7 @@ describe("requestFromPort", () => {
} catch (error) {
expect(error.toString()).toContain("Timed out while waiting for ");
}
port1.close();
});
});

Expand All @@ -570,6 +554,7 @@ describe("pushBindables", () => {

const response = await hasMessage;
expect(response.data).toBe("updated");
port1.close();
});
});

Expand All @@ -591,6 +576,7 @@ describe("createReplicaFunction", () => {
});
await fakeFunc();
expect(was_called).toBe(true);
port1.close();
});
it("Passes Function results", async () => {
const testobj = {
Expand All @@ -607,6 +593,7 @@ describe("createReplicaFunction", () => {
port2.postMessage(response);
});
expect(await fakeFunc()).toBe(43);
port1.close();
});
});
it("Passes Function arguments", async () => {
Expand All @@ -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", () => {
Expand All @@ -648,6 +636,7 @@ describe("createReplicaGetter", () => {
port2.postMessage(response);
});
expect(await fakeFunc()).toBe(43);
port1.close();
});
});

Expand All @@ -666,6 +655,7 @@ describe("createReplicaSetter", () => {
});
await fakeFunc(999);
expect(testobj.hi).toBe(999);
port1.close();
});
});

Expand All @@ -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();
Expand All @@ -712,5 +703,6 @@ describe("createReplicaBindable", () => {
});
obj.hello.set(69);
await done;
port1.close();
});
});

0 comments on commit 90b29e3

Please sign in to comment.