Skip to content

Commit c834beb

Browse files
authored
Improve failure tests (#85)
* Improve failure tests * Improve error tests
1 parent 3bca30f commit c834beb

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

.changeset/eager-banks-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@valtown/deno-http-worker": patch
3+
---
4+
5+
Improve error testing

src/DenoHTTPWorker.test.ts

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { it as _it, beforeAll, describe, expect, test } from "vitest";
22
import { type DenoHTTPWorker, newDenoHTTPWorker } from "./index.js";
33
import fs from "node:fs";
4-
import path from "node:path";
4+
import os from "node:os";
5+
import fsp from "node:fs/promises";
6+
import path, { join } from "node:path";
57
import { Worker } from "node:worker_threads";
68
import { EarlyExitDenoHTTPWorkerError } from "./DenoHTTPWorker.js";
79

@@ -72,6 +74,68 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
7274
await worker.terminate();
7375
});
7476

77+
it("crash on a corrupted lockfile", async () => {
78+
const tmpDir = await fsp.mkdtemp(join(os.tmpdir(), "vt-"));
79+
const lockfile = join(tmpDir, "deno.lock");
80+
fs.writeFileSync(lockfile, "{");
81+
await expect(
82+
newDenoHTTPWorker(
83+
`
84+
export default { async fetch (req: Request): Promise<Response> {
85+
await Deno.removeSync(Deno.args[0]);
86+
return Response.json({ ok: req.url })
87+
} }
88+
`,
89+
{ printOutput: false, runFlags: [`--lock=${lockfile}`] }
90+
)
91+
).rejects.toMatchObject({
92+
message: "Deno exited before being ready",
93+
stdout: undefined,
94+
stderr: expect.stringContaining("Lockfile may be corrupt"),
95+
});
96+
});
97+
98+
// This test uses the network so it is disabled by default, but use
99+
// it for debugging lockfile errors
100+
it.skip("crash on an invalid lockfile", async () => {
101+
const tmpDir = await fsp.mkdtemp(join(os.tmpdir(), "vt-"));
102+
const lockfile = join(tmpDir, "deno.lock");
103+
fs.writeFileSync(join(tmpDir, "deno.json"), "{}");
104+
fs.writeFileSync(
105+
lockfile,
106+
JSON.stringify({
107+
version: "5",
108+
specifiers: {
109+
"npm:is-number@*": "7.0.0",
110+
"npm:is-number@7.0.0": "7.0.0",
111+
},
112+
npm: {
113+
"is-number@7.0.0": {
114+
integrity:
115+
"sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0104MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==",
116+
},
117+
},
118+
})
119+
);
120+
121+
await expect(
122+
newDenoHTTPWorker(
123+
`
124+
import isN from "npm:is-number@7.0.0";
125+
export default { async fetch (req: Request): Promise<Response> {
126+
await Deno.removeSync(Deno.args[0]);
127+
return Response.json({ ok: isN('hi') })
128+
} }
129+
`,
130+
{ printOutput: false, runFlags: ["--reload", `--lock=${lockfile}`] }
131+
)
132+
).rejects.toMatchObject({
133+
message: "Deno exited before being ready",
134+
stdout: undefined,
135+
stderr: expect.stringContaining("Tarball checksum"),
136+
});
137+
});
138+
75139
it("don't crash on socket removal", async () => {
76140
const worker = await newDenoHTTPWorker(
77141
`
@@ -187,9 +251,38 @@ describe("DenoHTTPWorker", { timeout: 1000 }, () => {
187251
worker.addEventListener("exit", (code) => res(code));
188252
});
189253

190-
jsonRequest(worker, "https://localhost/hello?isee=you", {
191-
headers: { accept: "application/json" },
192-
}).catch(() => {});
254+
await expect(
255+
jsonRequest(worker, "https://localhost/hello?isee=you", {
256+
headers: { accept: "application/json" },
257+
})
258+
).resolves.toEqual(null);
259+
260+
expect(await codePromise).toEqual(1);
261+
await worker.terminate();
262+
});
263+
264+
it("intentional deno exits cause a socket error", async () => {
265+
const worker = await newDenoHTTPWorker(
266+
`
267+
export default { async fetch (req: Request): Promise<Response> {
268+
Deno.exit(1);
269+
}}
270+
`,
271+
{ printOutput: false }
272+
);
273+
const codePromise = new Promise((res) => {
274+
worker.addEventListener("exit", (code) => {
275+
res(code);
276+
});
277+
});
278+
279+
await expect(
280+
jsonRequest(worker, "https://localhost/hello?isee=you", {
281+
headers: { accept: "application/json" },
282+
})
283+
).rejects.toMatchObject({
284+
message: "other side closed",
285+
});
193286

194287
expect(await codePromise).toEqual(1);
195288
await worker.terminate();

0 commit comments

Comments
 (0)