1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00

Remove race-condition in file_server tests (denoland/deno_std#125)

Original: e28c9a4079
This commit is contained in:
Andy Hayden 2019-01-17 10:08:59 -08:00 committed by Ryan Dahl
parent 811128864c
commit b2e54bad61
6 changed files with 47 additions and 49 deletions

View file

@ -13,7 +13,8 @@ test(function t2() {
/** A more complicated test that runs a subprocess. */ /** A more complicated test that runs a subprocess. */
test(async function catSmoke() { test(async function catSmoke() {
const p = run({ const p = run({
args: ["deno", "examples/cat.ts", "README.md"] args: ["deno", "examples/cat.ts", "README.md"],
stdout: "piped"
}); });
const s = await p.status(); const s = await p.status();
assertEqual(s.code, 0); assertEqual(s.code, 0);

View file

@ -1,23 +1,29 @@
import { readFile } from "deno"; import { readFile, run } from "deno";
import { test, assert, assertEqual } from "../testing/mod.ts"; import { test, assert, assertEqual } from "../testing/mod.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
// Promise to completeResolve when all tests completes let fileServer;
let completeResolve; async function startFileServer() {
export const completePromise = new Promise(res => (completeResolve = res)); fileServer = run({
let completedTestCount = 0; args: ["deno", "--allow-net", "http/file_server.ts", ".", "--cors"],
stdout: "piped"
function maybeCompleteTests() { });
completedTestCount++; // Once fileServer is ready it will write to its stdout.
// Change this when adding more tests const r = new TextProtoReader(new BufReader(fileServer.stdout));
if (completedTestCount === 3) { const [s, err] = await r.readLine();
completeResolve(); assert(err == null);
assert(s.includes("server listening"));
} }
function killFileServer() {
fileServer.close();
fileServer.stdout.close();
} }
export function runTests(serverReadyPromise: Promise<any>) {
test(async function serveFile() { test(async function serveFile() {
await serverReadyPromise; await startFileServer();
try {
const res = await fetch("http://localhost:4500/azure-pipelines.yml"); const res = await fetch("http://localhost:4500/azure-pipelines.yml");
assert(res.headers.has("access-control-allow-origin")); assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers")); assert(res.headers.has("access-control-allow-headers"));
@ -27,25 +33,32 @@ export function runTests(serverReadyPromise: Promise<any>) {
await readFile("./azure-pipelines.yml") await readFile("./azure-pipelines.yml")
); );
assertEqual(downloadedFile, localFile); assertEqual(downloadedFile, localFile);
maybeCompleteTests(); } finally {
killFileServer();
}
}); });
test(async function serveDirectory() { test(async function serveDirectory() {
await serverReadyPromise; await startFileServer();
try {
const res = await fetch("http://localhost:4500/"); const res = await fetch("http://localhost:4500/");
assert(res.headers.has("access-control-allow-origin")); assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers")); assert(res.headers.has("access-control-allow-headers"));
const page = await res.text(); const page = await res.text();
assert(page.includes("azure-pipelines.yml")); assert(page.includes("azure-pipelines.yml"));
maybeCompleteTests(); } finally {
killFileServer();
}
}); });
test(async function serveFallback() { test(async function serveFallback() {
await serverReadyPromise; await startFileServer();
try {
const res = await fetch("http://localhost:4500/badfile.txt"); const res = await fetch("http://localhost:4500/badfile.txt");
assert(res.headers.has("access-control-allow-origin")); assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers")); assert(res.headers.has("access-control-allow-headers"));
assertEqual(res.status, 404); assertEqual(res.status, 404);
maybeCompleteTests(); } finally {
}); killFileServer();
} }
});

View file

@ -2,7 +2,7 @@ import { listen, Conn, toAsyncIterator, Reader, copy } from "deno";
import { BufReader, BufState, BufWriter } from "../io/bufio.ts"; import { BufReader, BufState, BufWriter } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts"; import { TextProtoReader } from "../textproto/mod.ts";
import { STATUS_TEXT } from "./http_status.ts"; import { STATUS_TEXT } from "./http_status.ts";
import { assert } from "../io/util.ts"; import { assert } from "../testing/mod.ts";
interface Deferred { interface Deferred {
promise: Promise<{}>; promise: Promise<{}>;

View file

@ -4,7 +4,8 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
import { Reader, ReadResult, Writer } from "deno"; import { Reader, ReadResult, Writer } from "deno";
import { assert, charCode, copyBytes } from "./util.ts"; import { charCode, copyBytes } from "./util.ts";
import { assert } from "../testing/mod.ts";
const DEFAULT_BUF_SIZE = 4096; const DEFAULT_BUF_SIZE = 4096;
const MIN_BUF_SIZE = 16; const MIN_BUF_SIZE = 16;

View file

@ -1,11 +1,5 @@
import { Buffer, Reader } from "deno"; import { Buffer, Reader } from "deno";
export function assert(cond: boolean, msg = "assert") {
if (!cond) {
throw Error(msg);
}
}
// `off` is the offset into `dst` where it will at which to begin writing values // `off` is the offset into `dst` where it will at which to begin writing values
// from `src`. // from `src`.
// Returns the number of bytes copied. // Returns the number of bytes copied.

17
test.ts
View file

@ -1,6 +1,4 @@
#!/usr/bin/env deno --allow-run --allow-net --allow-write #!/usr/bin/env deno --allow-run --allow-net --allow-write
import { run } from "deno";
import "colors/test.ts"; import "colors/test.ts";
import "datetime/test.ts"; import "datetime/test.ts";
import "examples/test.ts"; import "examples/test.ts";
@ -16,21 +14,12 @@ import "fs/path/relative_test.ts";
import "fs/path/resolve_test.ts"; import "fs/path/resolve_test.ts";
import "fs/path/zero_length_strings_test.ts"; import "fs/path/zero_length_strings_test.ts";
import "io/bufio_test.ts"; import "io/bufio_test.ts";
import "io/ioutil_test.ts";
import "http/http_test.ts"; import "http/http_test.ts";
import "http/file_server_test.ts";
import "log/test.ts"; import "log/test.ts";
import "media_types/test.ts"; import "media_types/test.ts";
import "testing/test.ts"; import "testing/test.ts";
import "textproto/test.ts"; import "textproto/test.ts";
import "ws/sha1_test.ts";
import "ws/test.ts"; import "ws/test.ts";
import { runTests, completePromise } from "http/file_server_test.ts";
const fileServer = run({
args: ["deno", "--allow-net", "http/file_server.ts", ".", "--cors"]
});
runTests(new Promise(res => setTimeout(res, 5000)));
(async () => {
await completePromise;
fileServer.close();
})();