mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
chore(ext/node): make process_test more reliable (#22703)
This commit is contained in:
parent
c99a0492b7
commit
10557ff15c
1 changed files with 78 additions and 50 deletions
|
@ -19,6 +19,7 @@ import {
|
||||||
assertObjectMatch,
|
assertObjectMatch,
|
||||||
assertStrictEquals,
|
assertStrictEquals,
|
||||||
assertThrows,
|
assertThrows,
|
||||||
|
fail,
|
||||||
} from "@std/assert/mod.ts";
|
} from "@std/assert/mod.ts";
|
||||||
import { stripColor } from "@std/fmt/colors.ts";
|
import { stripColor } from "@std/fmt/colors.ts";
|
||||||
import * as path from "@std/path/mod.ts";
|
import * as path from "@std/path/mod.ts";
|
||||||
|
@ -184,29 +185,46 @@ Deno.test({
|
||||||
name: "process.on signal",
|
name: "process.on signal",
|
||||||
ignore: Deno.build.os == "windows",
|
ignore: Deno.build.os == "windows",
|
||||||
async fn() {
|
async fn() {
|
||||||
const process = new Deno.Command(Deno.execPath(), {
|
const testTimeout = setTimeout(() => fail("Test timed out"), 10_000);
|
||||||
args: [
|
try {
|
||||||
"eval",
|
const process = new Deno.Command(Deno.execPath(), {
|
||||||
`
|
args: [
|
||||||
import process from "node:process";
|
"eval",
|
||||||
setInterval(() => {}, 1000);
|
`
|
||||||
process.on("SIGINT", () => {
|
import process from "node:process";
|
||||||
console.log("foo");
|
setInterval(() => {}, 1000);
|
||||||
});
|
console.log("ready");
|
||||||
`,
|
process.on("SIGINT", () => {
|
||||||
],
|
console.log("foo");
|
||||||
stdout: "piped",
|
});
|
||||||
stderr: "null",
|
`,
|
||||||
}).spawn();
|
],
|
||||||
await delay(500);
|
stdout: "piped",
|
||||||
for (const _ of Array(3)) {
|
stderr: "null",
|
||||||
process.kill("SIGINT");
|
}).spawn();
|
||||||
await delay(20);
|
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",
|
name: "process.off signal",
|
||||||
ignore: Deno.build.os == "windows",
|
ignore: Deno.build.os == "windows",
|
||||||
async fn() {
|
async fn() {
|
||||||
const process = new Deno.Command(Deno.execPath(), {
|
const testTimeout = setTimeout(() => fail("Test timed out"), 10_000);
|
||||||
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);
|
|
||||||
try {
|
try {
|
||||||
process.kill("SIGTERM");
|
const process = new Deno.Command(Deno.execPath(), {
|
||||||
} catch { /* should be dead, avoid hanging just in case */ }
|
args: [
|
||||||
const output = await process.output();
|
"eval",
|
||||||
assertEquals(new TextDecoder().decode(output.stdout), "foo\n");
|
`
|
||||||
|
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);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue