0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -05:00

chore: fix flaky netListenUnrefAndRef (#16892)

Closes #16890
This commit is contained in:
David Sherret 2022-12-02 12:41:52 -05:00 committed by GitHub
parent b638bc183d
commit f4b8c2ea7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 16 deletions

View file

@ -30,10 +30,10 @@ There are two ways to run `unit_test_runner.ts`:
```sh ```sh
# Run all tests. # Run all tests.
target/debug/deno test --allow-all --unstable --location=http://js-unit-tests/foo/bar cli/tests/unit/ cargo run --bin deno -- test --allow-all --unstable --location=http://js-unit-tests/foo/bar cli/tests/unit/
# Run a specific test module # Run a specific test module
target/debug/deno test --allow-all --unstable --location=http://js-unit-tests/foo/bar cli/tests/unit/files_test.ts cargo run --bin deno -- test --allow-all --unstable --location=http://js-unit-tests/foo/bar cli/tests/unit/files_test.ts
``` ```
### Http server ### Http server

View file

@ -8,6 +8,7 @@ import {
deferred, deferred,
delay, delay,
execCode, execCode,
execCode2,
} from "./test_util.ts"; } from "./test_util.ts";
import { join } from "../../../test_util/std/path/mod.ts"; import { join } from "../../../test_util/std/path/mod.ts";
@ -853,25 +854,23 @@ Deno.test(
Deno.test( Deno.test(
{ permissions: { read: true, run: true, net: true } }, { permissions: { read: true, run: true, net: true } },
async function netListenUnrefAndRef() { async function netListenUnrefAndRef() {
const p = execCode(` const p = execCode2(`
async function main() { async function main() {
const listener = Deno.listen({ port: 3500 }); const listener = Deno.listen({ port: 3500 });
listener.unref(); listener.unref();
listener.ref(); // This restores 'ref' state of listener listener.ref(); // This restores 'ref' state of listener
console.log("started");
await listener.accept(); await listener.accept();
console.log("accepted") console.log("accepted")
} }
main(); main();
`); `);
// TODO(kt3k): This is racy. Find a correct way to await p.waitStdoutText("started");
// wait for the server to be ready const conn = await Deno.connect({ port: 3500 });
setTimeout(async () => { conn.close();
const conn = await Deno.connect({ port: 3500 }); const [statusCode, output] = await p.finished();
conn.close();
}, 200);
const [statusCode, output] = await p;
assertEquals(statusCode, 0); assertEquals(statusCode, 0);
assertEquals(output.trim(), "accepted"); assertEquals(output.trim(), "started\naccepted");
}, },
); );

View file

@ -29,16 +29,53 @@ export function pathToAbsoluteFileUrl(path: string): URL {
return new URL(`file://${Deno.build.os === "windows" ? "/" : ""}${path}`); return new URL(`file://${Deno.build.os === "windows" ? "/" : ""}${path}`);
} }
const decoder = new TextDecoder(); export function execCode(code: string): Promise<readonly [number, string]> {
return execCode2(code).finished();
}
export async function execCode(code: string): Promise<[number, string]> { export function execCode2(code: string) {
const output = await new Deno.Command(Deno.execPath(), { const command = new Deno.Command(Deno.execPath(), {
args: [ args: [
"eval", "eval",
"--unstable", "--unstable",
"--no-check", "--no-check",
code, code,
], ],
}).output(); stdout: "piped",
return [output.code, decoder.decode(output.stdout)]; stderr: "inherit",
});
const child = command.spawn();
const stdout = child.stdout.pipeThrough(new TextDecoderStream()).getReader();
let output = "";
return {
async waitStdoutText(text: string) {
while (true) {
const readData = await stdout.read();
if (readData.value) {
output += readData.value;
if (output.includes(text)) {
return;
}
}
if (readData.done) {
throw new Error(`Did not find text '${text}' in stdout.`);
}
}
},
async finished() {
while (true) {
const readData = await stdout.read();
if (readData.value) {
output += readData.value;
}
if (readData.done) {
break;
}
}
const status = await child.status;
return [status.code, output] as const;
},
};
} }