diff options
author | Matt Mastracci <matthew@mastracci.com> | 2024-03-04 19:52:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-04 19:52:10 -0700 |
commit | 10557ff15c9510cce4a87e854e023ddf7eeb3abd (patch) | |
tree | 43f6573719450da22663e8b33e37d95ad531520a | |
parent | c99a0492b7c02462e4f7e97904fc068b1ee26129 (diff) |
chore(ext/node): make process_test more reliable (#22703)
-rw-r--r-- | tests/unit_node/process_test.ts | 128 |
1 files changed, 78 insertions, 50 deletions
diff --git a/tests/unit_node/process_test.ts b/tests/unit_node/process_test.ts index b685c779a..7679f3681 100644 --- a/tests/unit_node/process_test.ts +++ b/tests/unit_node/process_test.ts @@ -19,6 +19,7 @@ import { assertObjectMatch, assertStrictEquals, assertThrows, + fail, } from "@std/assert/mod.ts"; import { stripColor } from "@std/fmt/colors.ts"; import * as path from "@std/path/mod.ts"; @@ -184,29 +185,46 @@ Deno.test({ name: "process.on signal", ignore: Deno.build.os == "windows", async fn() { - const process = new Deno.Command(Deno.execPath(), { - args: [ - "eval", - ` - import process from "node:process"; - setInterval(() => {}, 1000); - process.on("SIGINT", () => { - console.log("foo"); - }); - `, - ], - stdout: "piped", - stderr: "null", - }).spawn(); - await delay(500); - for (const _ of Array(3)) { - process.kill("SIGINT"); - await delay(20); + const testTimeout = setTimeout(() => fail("Test timed out"), 10_000); + try { + const process = new Deno.Command(Deno.execPath(), { + args: [ + "eval", + ` + import process from "node:process"; + setInterval(() => {}, 1000); + console.log("ready"); + process.on("SIGINT", () => { + console.log("foo"); + }); + `, + ], + stdout: "piped", + stderr: "null", + }).spawn(); + let output = ""; + process.stdout.pipeThrough(new TextDecoderStream()).pipeTo( + new WritableStream({ + write(chunk) { + output += chunk; + }, + }), + ); + while (!output.includes("ready\n")) { + await delay(10); + } + output = ""; + for (const _ of Array(3)) { + process.kill("SIGINT"); + while (!output.includes("foo\n")) { + await delay(10); + } + } + process.kill("SIGTERM"); + await process.status; + } finally { + clearTimeout(testTimeout); } - await delay(20); - process.kill("SIGTERM"); - const output = await process.output(); - assertEquals(new TextDecoder().decode(output.stdout), "foo\nfoo\nfoo\n"); }, }); @@ -214,35 +232,45 @@ Deno.test({ name: "process.off signal", ignore: Deno.build.os == "windows", async fn() { - const process = new Deno.Command(Deno.execPath(), { - args: [ - "eval", - ` - import process from "node:process"; - setInterval(() => {}, 1000); - const listener = () => { - console.log("foo"); - process.off("SIGINT") - }; - process.on("SIGINT", listener); - `, - ], - stdout: "piped", - stderr: "null", - }).spawn(); - await delay(500); - for (const _ of Array(3)) { - try { - process.kill("SIGINT"); - } catch { /* should die after the first one */ } - await delay(20); - } - await delay(20); + const testTimeout = setTimeout(() => fail("Test timed out"), 10_000); try { - process.kill("SIGTERM"); - } catch { /* should be dead, avoid hanging just in case */ } - const output = await process.output(); - assertEquals(new TextDecoder().decode(output.stdout), "foo\n"); + const process = new Deno.Command(Deno.execPath(), { + args: [ + "eval", + ` + import process from "node:process"; + console.log("ready"); + setInterval(() => {}, 1000); + const listener = () => { + console.log("foo"); + process.off("SIGINT") + }; + process.on("SIGINT", listener); + `, + ], + stdout: "piped", + stderr: "null", + }).spawn(); + let output = ""; + process.stdout.pipeThrough(new TextDecoderStream()).pipeTo( + new WritableStream({ + write(chunk) { + output += chunk; + }, + }), + ); + while (!output.includes("ready\n")) { + await delay(10); + } + output = ""; + process.kill("SIGINT"); + while (!output.includes("foo\n")) { + await delay(10); + } + await process.status; + } finally { + clearTimeout(testTimeout); + } }, }); |