From 795836ed2985c5e86cdf65c519760c8220eb47a1 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Mon, 18 Sep 2023 11:09:24 +0200 Subject: [PATCH] tests: de-flake tests and re-enable sanitizers (#1786) --- .github/workflows/ci.yml | 2 +- tests/build_test.ts | 8 +++- tests/cli_update_check_test.ts | 1 - tests/init_test.ts | 24 +++++++---- tests/islands_test.ts | 63 ---------------------------- tests/islands_wasm_test.ts | 6 +-- tests/layouts_test.ts | 3 -- tests/main_test.ts | 74 ++++++++++++++------------------- tests/plugin_test.ts | 6 +-- tests/server_components_test.ts | 18 -------- tests/signal_test.ts | 3 -- tests/test_utils.ts | 32 +++++++++----- tests/twind_test.ts | 22 ++-------- www/main_test.ts | 12 +++--- 14 files changed, 89 insertions(+), 185 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d3a1b3c2..0476b762 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - deno: ["v1.x"] + deno: ["canary"] os: [macOS-latest, windows-latest, ubuntu-latest] include: - os: ubuntu-latest diff --git a/tests/build_test.ts b/tests/build_test.ts index b899eea1..9b10206e 100644 --- a/tests/build_test.ts +++ b/tests/build_test.ts @@ -115,9 +115,11 @@ async function testBuild( await browser.close(); } } finally { - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } } }); @@ -137,9 +139,11 @@ async function testBuild( "Restoring from snapshot message should not appear in dev mode", ); } finally { - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } } }); } finally { diff --git a/tests/cli_update_check_test.ts b/tests/cli_update_check_test.ts index af19f4c3..c1e32f24 100644 --- a/tests/cli_update_check_test.ts +++ b/tests/cli_update_check_test.ts @@ -116,7 +116,6 @@ Deno.test({ await Deno.remove(tmpDirName, { recursive: true }); }, - sanitizeResources: false, }); Deno.test({ diff --git a/tests/init_test.ts b/tests/init_test.ts index 478c7658..8fe3a6bd 100644 --- a/tests/init_test.ts +++ b/tests/init_test.ts @@ -134,14 +134,15 @@ Deno.test({ await page.close(); await browser.close(); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }); await retry(() => Deno.remove(tmpDirName, { recursive: true })); }, - sanitizeResources: false, }); Deno.test({ @@ -227,14 +228,15 @@ Deno.test({ await page.close(); await browser.close(); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }); await retry(() => Deno.remove(tmpDirName, { recursive: true })); }, - sanitizeResources: false, }); Deno.test("fresh-init error(help)", async function (t) { @@ -357,14 +359,15 @@ Deno.test({ await delay(100); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }); await retry(() => Deno.remove(tmpDirName, { recursive: true })); }, - sanitizeResources: false, }); Deno.test({ @@ -405,9 +408,11 @@ Deno.test({ const doc = await fetchHtml(`${address}/env`); assertTextMany(doc, "h1", ["true"]); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }); await t.step("build code and start server again", async () => { @@ -435,12 +440,13 @@ Deno.test({ const doc = await fetchHtml(`${address}/env`); assertTextMany(doc, "h1", ["true"]); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }); await retry(() => Deno.remove(tmpDirName, { recursive: true })); }, - sanitizeResources: false, }); diff --git a/tests/islands_test.ts b/tests/islands_test.ts index 72c3727c..8a635164 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -58,9 +58,6 @@ Deno.test({ }); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -88,9 +85,6 @@ Deno.test({ }); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); function withPage(fn: (page: Page, address: string) => Promise) { @@ -122,9 +116,6 @@ Deno.test({ }); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -146,9 +137,6 @@ Deno.test({ await waitForText(page, `#island-parent`, "HelloWorldI'm rendered now"); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -176,9 +164,6 @@ Deno.test({ ); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -193,9 +178,6 @@ Deno.test({ await page.waitForSelector(".added-by-use-effect"); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -212,9 +194,6 @@ Deno.test({ assert(await page.waitForSelector("#browser-true")); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -232,9 +211,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -252,9 +228,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -275,9 +248,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -298,9 +268,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -318,9 +285,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -343,9 +307,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -369,9 +330,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -391,9 +349,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -418,9 +373,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -439,9 +391,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -457,9 +406,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -475,9 +421,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -495,9 +438,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -560,9 +500,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test("throws when passing non-jsx children to an island", async (t) => { diff --git a/tests/islands_wasm_test.ts b/tests/islands_wasm_test.ts index 0a3ba2bf..f61d2df5 100644 --- a/tests/islands_wasm_test.ts +++ b/tests/islands_wasm_test.ts @@ -39,10 +39,10 @@ Deno.test({ await browser.close(); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }, - sanitizeOps: false, - sanitizeResources: false, }); diff --git a/tests/layouts_test.ts b/tests/layouts_test.ts index e7a6f4b3..2b0b9b52 100644 --- a/tests/layouts_test.ts +++ b/tests/layouts_test.ts @@ -194,7 +194,4 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); diff --git a/tests/main_test.ts b/tests/main_test.ts index 8e2e6932..fe05115a 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -724,10 +724,7 @@ Deno.test({ }, }); -Deno.test("jsx pragma works", { - sanitizeOps: false, - sanitizeResources: false, -}, async (t) => { +Deno.test("jsx pragma works", async (t) => { // Preparation const { serverProcess, lines, address } = await startFreshServer({ args: ["run", "-A", "./tests/fixture_jsx_pragma/main.ts"], @@ -756,15 +753,14 @@ Deno.test("jsx pragma works", { await browser.close(); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }); -Deno.test("preact/debug is active in dev mode", { - sanitizeOps: false, - sanitizeResources: false, -}, async (t) => { +Deno.test("preact/debug is active in dev mode", async (t) => { // Preparation const { serverProcess, lines, address } = await startFreshServer({ args: ["run", "-A", "./tests/fixture_render_error/main.ts"], @@ -794,15 +790,14 @@ Deno.test("preact/debug is active in dev mode", { await browser.close(); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }); -Deno.test("preloading javascript files", { - sanitizeOps: false, - sanitizeResources: false, -}, async () => { +Deno.test("preloading javascript files", async () => { // Preparation const { serverProcess, lines, address } = await startFreshServer({ args: ["run", "-A", "./tests/fixture/main.ts"], @@ -843,16 +838,15 @@ Deno.test("preloading javascript files", { } finally { await browser.close(); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } } }); -Deno.test("PORT environment variable", { - sanitizeOps: false, - sanitizeResources: false, -}, async () => { +Deno.test("PORT environment variable", async () => { const PORT = "8765"; // Preparation const { serverProcess, lines } = await startFreshServer({ @@ -866,27 +860,30 @@ Deno.test("PORT environment variable", { await resp.body?.cancel(); assert(resp); assertEquals(resp.status, Status.OK); + await resp.body!.cancel(); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }); -Deno.test("throw on route export 'handlers' instead of 'handler'", { - sanitizeOps: false, - sanitizeResources: false, -}, async () => { - const result = await new Deno.Command(Deno.execPath(), { - args: ["run", "-A", "./tests/fixture_invalid_handlers/main.ts"], - stderr: "piped", - stdout: "piped", - }).output(); +Deno.test( + "throw on route export 'handlers' instead of 'handler'", + async () => { + const result = await new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "./tests/fixture_invalid_handlers/main.ts"], + stderr: "piped", + stdout: "piped", + }).output(); - assertEquals(result.code, 1); + assertEquals(result.code, 1); - const text = new TextDecoder().decode(result.stderr); - assertMatch(text, /Did you mean "handler"\?/); -}); + const text = new TextDecoder().decode(result.stderr); + assertMatch(text, /Did you mean "handler"\?/); + }, +); Deno.test("rendering custom _500.tsx page for default handlers", async (t) => { await withFresh("./tests/fixture_custom_500/main.ts", async (address) => { @@ -905,10 +902,7 @@ Deno.test("rendering custom _500.tsx page for default handlers", async (t) => { }); }); -Deno.test("renders error boundary", { - sanitizeOps: false, - sanitizeResources: false, -}, async () => { +Deno.test("renders error boundary", async () => { await withPageName("./tests/fixture/main.ts", async (page, address) => { await page.goto(`${address}/error_boundary`); const text = await page.$eval("body", (el) => el.textContent); @@ -978,9 +972,6 @@ Deno.test({ await waitForText(page, "p", "it works"); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -1010,9 +1001,6 @@ Deno.test({ }); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test("adds refresh script to html", async () => { diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index e219a36e..8f4d298c 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -122,10 +122,10 @@ Deno.test({ await browser.close(); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }, - sanitizeOps: false, - sanitizeResources: false, }); diff --git a/tests/server_components_test.ts b/tests/server_components_test.ts index 1ee3fc5a..5f62ab3c 100644 --- a/tests/server_components_test.ts +++ b/tests/server_components_test.ts @@ -20,9 +20,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -38,9 +35,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -62,9 +56,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -107,9 +98,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -127,9 +115,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -153,9 +138,6 @@ Deno.test({ }, ); }, - - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ diff --git a/tests/signal_test.ts b/tests/signal_test.ts index 31e7f49a..46aa3402 100644 --- a/tests/signal_test.ts +++ b/tests/signal_test.ts @@ -16,7 +16,4 @@ Deno.test({ await waitForText(page, "#counter-2 p", "3"); }); }, - - sanitizeOps: false, - sanitizeResources: false, }); diff --git a/tests/test_utils.ts b/tests/test_utils.ts index c29d67b5..a7e25ce2 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -166,12 +166,13 @@ export async function withFresh( try { await fn(address); } finally { - await lines.cancel(); - serverProcess.kill("SIGTERM"); // Wait until the process exits await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } } } @@ -194,19 +195,20 @@ export async function withPageName( await browser.close(); } } finally { - await lines.cancel(); - serverProcess.kill("SIGTERM"); // Wait until the process exits await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } } } export async function startFreshServerExpectErrors( options: Deno.CommandOptions, ) { - const { serverProcess, address } = await spawnServer(options, true); + const { serverProcess, lines, address } = await spawnServer(options, true); if (address) { throw Error("Server started correctly"); @@ -222,6 +224,16 @@ export async function startFreshServerExpectErrors( for await (const line of errorLines) { output += line + "\n"; } + + try { + serverProcess.kill("SIGTERM"); + } catch { + // ignore the error, this may throw on windows if the process has already + // exited + } + await serverProcess.status; + for await (const _ of lines) { /* noop */ } + return output; } @@ -315,16 +327,14 @@ async function spawnServer( stderr: expectErrors ? "piped" : "inherit", }).spawn(); - const decoder = new TextDecoderStream(); const lines: ReadableStream = serverProcess.stdout - .pipeThrough(decoder) - .pipeThrough(new TextLineStream(), { - preventCancel: true, - }); + .pipeThrough(new TextDecoderStream()) + .pipeThrough(new TextLineStream()); const output: string[] = []; let address = ""; - for await (const line of lines) { + // @ts-ignore yes it does + for await (const line of lines.values({ preventCancel: true })) { output.push(line); const match = line.match(/https?:\/\/localhost:\d+/g); if (match) { diff --git a/tests/twind_test.ts b/tests/twind_test.ts index 5db357af..879548fe 100644 --- a/tests/twind_test.ts +++ b/tests/twind_test.ts @@ -22,25 +22,16 @@ async function setUpServer(path: string) { * terminate server */ const terminate = async () => { - await lines.cancel(); await browser.close(); serverProcess.kill("SIGKILL"); await serverProcess.status; - // TextDecoder leaks, so close it manually. - const denoResourcesMap = new Map( - Object.entries(Deno.resources()).map(([rid, representation]) => { - return [representation, parseInt(rid)]; - }), - ); - const textDecoderRid = denoResourcesMap.get("textDecoder"); - if (textDecoderRid != null) { - Deno.close(textDecoderRid); - } + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }; - return { page: page, terminate: terminate, address }; + return { page: page, terminate, address }; } /** @@ -312,8 +303,6 @@ Deno.test({ }, ); }, - sanitizeOps: false, - sanitizeResources: false, }); Deno.test({ @@ -347,8 +336,6 @@ Deno.test({ }, ); }, - sanitizeOps: false, - sanitizeResources: false, }); // Test for: https://github.com/denoland/fresh/issues/1655 @@ -370,9 +357,8 @@ Deno.test({ }, ); }, - sanitizeOps: false, - sanitizeResources: false, }); + // Test for: https://github.com/denoland/fresh/issues/1655 Deno.test("don't duplicate css class with twindV1", async () => { await withFresh( diff --git a/www/main_test.ts b/www/main_test.ts index f794ce65..43beec7a 100644 --- a/www/main_test.ts +++ b/www/main_test.ts @@ -5,9 +5,7 @@ import VERSIONS from "../versions.json" assert { type: "json" }; const dir = dirname(import.meta.url); -Deno.test("CORS should not set on GET /fresh-badge.svg", { - sanitizeResources: false, -}, async () => { +Deno.test("CORS should not set on GET /fresh-badge.svg", async () => { const { serverProcess, lines, address } = await startFreshServer({ args: ["run", "-A", join(dir, "./main.ts")], }); @@ -17,14 +15,14 @@ Deno.test("CORS should not set on GET /fresh-badge.svg", { assertEquals(res.headers.get("cross-origin-resource-policy"), null); - await lines.cancel(); serverProcess.kill("SIGTERM"); await serverProcess.status; + + // Drain the lines stream + for await (const _ of lines) { /* noop */ } }); -Deno.test("shows version selector", { - sanitizeResources: false, -}, async () => { +Deno.test("shows version selector", async () => { await withPageName(join(dir, "./main.ts"), async (page, address) => { await page.goto(`${address}/docs`); await page.waitForSelector("#version");