summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Hayden <andyhayden1@gmail.com>2019-01-17 10:08:59 -0800
committerRyan Dahl <ry@tinyclouds.org>2019-01-17 13:08:59 -0500
commitb2e54bad61b37f3a186dd72237c694ada77ab94f (patch)
treebc30bea78e49fe9c854be93e0509aabc76f77392
parent811128864c4db40409c5c5538b56466e4f4e704c (diff)
Remove race-condition in file_server tests (denoland/deno_std#125)
Original: https://github.com/denoland/deno_std/commit/e28c9a407951f10d952993ff6a7b248ca11243e1
-rw-r--r--examples/test.ts3
-rw-r--r--http/file_server_test.ts65
-rw-r--r--http/http.ts2
-rw-r--r--io/bufio.ts3
-rw-r--r--io/util.ts6
-rwxr-xr-xtest.ts17
6 files changed, 47 insertions, 49 deletions
diff --git a/examples/test.ts b/examples/test.ts
index f42db1bba..8d16e1703 100644
--- a/examples/test.ts
+++ b/examples/test.ts
@@ -13,7 +13,8 @@ test(function t2() {
/** A more complicated test that runs a subprocess. */
test(async function catSmoke() {
const p = run({
- args: ["deno", "examples/cat.ts", "README.md"]
+ args: ["deno", "examples/cat.ts", "README.md"],
+ stdout: "piped"
});
const s = await p.status();
assertEqual(s.code, 0);
diff --git a/http/file_server_test.ts b/http/file_server_test.ts
index bd00d749b..f8b2429b0 100644
--- a/http/file_server_test.ts
+++ b/http/file_server_test.ts
@@ -1,23 +1,29 @@
-import { readFile } from "deno";
+import { readFile, run } from "deno";
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 completeResolve;
-export const completePromise = new Promise(res => (completeResolve = res));
-let completedTestCount = 0;
-
-function maybeCompleteTests() {
- completedTestCount++;
- // Change this when adding more tests
- if (completedTestCount === 3) {
- completeResolve();
- }
+let fileServer;
+async function startFileServer() {
+ fileServer = run({
+ args: ["deno", "--allow-net", "http/file_server.ts", ".", "--cors"],
+ stdout: "piped"
+ });
+ // Once fileServer is ready it will write to its stdout.
+ const r = new TextProtoReader(new BufReader(fileServer.stdout));
+ const [s, err] = await r.readLine();
+ 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() {
- await serverReadyPromise;
+test(async function serveFile() {
+ await startFileServer();
+ try {
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-headers"));
@@ -27,25 +33,32 @@ export function runTests(serverReadyPromise: Promise<any>) {
await readFile("./azure-pipelines.yml")
);
assertEqual(downloadedFile, localFile);
- maybeCompleteTests();
- });
+ } finally {
+ killFileServer();
+ }
+});
- test(async function serveDirectory() {
- await serverReadyPromise;
+test(async function serveDirectory() {
+ await startFileServer();
+ try {
const res = await fetch("http://localhost:4500/");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
const page = await res.text();
assert(page.includes("azure-pipelines.yml"));
- maybeCompleteTests();
- });
+ } finally {
+ killFileServer();
+ }
+});
- test(async function serveFallback() {
- await serverReadyPromise;
+test(async function serveFallback() {
+ await startFileServer();
+ try {
const res = await fetch("http://localhost:4500/badfile.txt");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEqual(res.status, 404);
- maybeCompleteTests();
- });
-}
+ } finally {
+ killFileServer();
+ }
+});
diff --git a/http/http.ts b/http/http.ts
index da7bc0169..fe25a5f6e 100644
--- a/http/http.ts
+++ b/http/http.ts
@@ -2,7 +2,7 @@ import { listen, Conn, toAsyncIterator, Reader, copy } from "deno";
import { BufReader, BufState, BufWriter } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { STATUS_TEXT } from "./http_status.ts";
-import { assert } from "../io/util.ts";
+import { assert } from "../testing/mod.ts";
interface Deferred {
promise: Promise<{}>;
diff --git a/io/bufio.ts b/io/bufio.ts
index 0dd2b94b4..05e6b0a0c 100644
--- a/io/bufio.ts
+++ b/io/bufio.ts
@@ -4,7 +4,8 @@
// license that can be found in the LICENSE file.
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 MIN_BUF_SIZE = 16;
diff --git a/io/util.ts b/io/util.ts
index 811940b4d..3266e5018 100644
--- a/io/util.ts
+++ b/io/util.ts
@@ -1,11 +1,5 @@
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
// from `src`.
// Returns the number of bytes copied.
diff --git a/test.ts b/test.ts
index 994148178..a128ce0a1 100755
--- a/test.ts
+++ b/test.ts
@@ -1,6 +1,4 @@
#!/usr/bin/env deno --allow-run --allow-net --allow-write
-import { run } from "deno";
-
import "colors/test.ts";
import "datetime/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/zero_length_strings_test.ts";
import "io/bufio_test.ts";
+import "io/ioutil_test.ts";
import "http/http_test.ts";
+import "http/file_server_test.ts";
import "log/test.ts";
import "media_types/test.ts";
import "testing/test.ts";
import "textproto/test.ts";
+import "ws/sha1_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();
-})();