summaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorYusuke Sakurai <kerokerokerop@gmail.com>2020-03-21 22:53:47 +0900
committerGitHub <noreply@github.com>2020-03-21 09:53:47 -0400
commit60cee4f045778777a16b6fffd6d5b9a1400d7246 (patch)
treea477bd147fbd548d478a289af5bd0681b1a34c4e /std
parent0adc86f105204b2475126c36dfc10e678f67df56 (diff)
avoid using same port number for test (#4147)
Diffstat (limited to 'std')
-rw-r--r--std/examples/chat/server.ts6
-rw-r--r--std/examples/chat/server_test.ts15
-rw-r--r--std/examples/echo_server.ts2
-rw-r--r--std/examples/tests/curl_test.ts6
-rw-r--r--std/examples/tests/echo_server_test.ts8
-rw-r--r--std/http/file_server_test.ts28
-rw-r--r--std/http/http_bench.ts3
-rw-r--r--std/http/racing_server.ts2
-rw-r--r--std/http/racing_server_test.ts12
-rw-r--r--std/http/server_test.ts23
-rw-r--r--std/http/test_util.ts20
-rw-r--r--std/http/testdata/simple_https_server.ts3
-rw-r--r--std/http/testdata/simple_server.ts5
13 files changed, 100 insertions, 33 deletions
diff --git a/std/examples/chat/server.ts b/std/examples/chat/server.ts
index 08aede05b..d28f9f43f 100644
--- a/std/examples/chat/server.ts
+++ b/std/examples/chat/server.ts
@@ -29,7 +29,9 @@ async function wsHandler(ws: WebSocket): Promise<void> {
}
}
-listenAndServe({ port: 8080 }, async req => {
+const addr = Deno.args[0] ?? "127.0.0.1:8080";
+
+listenAndServe(addr, async req => {
if (req.method === "GET" && req.url === "/") {
//Serve with hack
const u = new URL("./index.html", import.meta.url);
@@ -75,4 +77,4 @@ listenAndServe({ port: 8080 }, async req => {
}
}
});
-console.log("chat server starting on :8080....");
+console.log(`chat server starting on ${addr}....`);
diff --git a/std/examples/chat/server_test.ts b/std/examples/chat/server_test.ts
index 0d21b3787..65e0b5958 100644
--- a/std/examples/chat/server_test.ts
+++ b/std/examples/chat/server_test.ts
@@ -3,13 +3,22 @@ import { assert, assertEquals } from "../../testing/asserts.ts";
import { TextProtoReader } from "../../textproto/mod.ts";
import { BufReader } from "../../io/bufio.ts";
import { connectWebSocket, WebSocket } from "../../ws/mod.ts";
+import { randomPort } from "../../http/test_util.ts";
import { delay } from "../../util/async.ts";
+const port = randomPort();
+
const { test, build } = Deno;
async function startServer(): Promise<Deno.Process> {
const server = Deno.run({
- args: [Deno.execPath(), "--allow-net", "--allow-read", "server.ts"],
+ args: [
+ Deno.execPath(),
+ "--allow-net",
+ "--allow-read",
+ "server.ts",
+ `127.0.0.1:${port}`
+ ],
cwd: "examples/chat",
stdout: "piped"
});
@@ -35,7 +44,7 @@ test({
async fn() {
const server = await startServer();
try {
- const resp = await fetch("http://127.0.0.1:8080/");
+ const resp = await fetch(`http://127.0.0.1:${port}/`);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html");
const html = await resp.body.text();
@@ -55,7 +64,7 @@ test({
const server = await startServer();
let ws: WebSocket | undefined;
try {
- ws = await connectWebSocket("http://127.0.0.1:8080/ws");
+ ws = await connectWebSocket(`http://127.0.0.1:${port}/ws`);
const it = ws.receive();
assertEquals((await it.next()).value, "Connected: [1]");
ws.send("Hello");
diff --git a/std/examples/echo_server.ts b/std/examples/echo_server.ts
index dbcc9b5ae..cdd98fea7 100644
--- a/std/examples/echo_server.ts
+++ b/std/examples/echo_server.ts
@@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
const hostname = "0.0.0.0";
-const port = 8080;
+const port = +(Deno.args[0] ?? "8080");
const listener = Deno.listen({ hostname, port });
console.log(`Listening on ${hostname}:${port}`);
for await (const conn of listener) {
diff --git a/std/examples/tests/curl_test.ts b/std/examples/tests/curl_test.ts
index bc413b23f..b23e73210 100644
--- a/std/examples/tests/curl_test.ts
+++ b/std/examples/tests/curl_test.ts
@@ -1,14 +1,16 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { serve } from "../../http/server.ts";
import { assertStrictEq } from "../../testing/asserts.ts";
+import { randomPort } from "../../http/test_util.ts";
+const port = randomPort();
Deno.test({
name: "[examples/curl] send a request to a specified url",
// FIXME(bartlomieju): this test is leaking both resources and ops,
// and causes interference with other tests
ignore: true,
fn: async () => {
- const server = serve({ port: 8081 });
+ const server = serve({ port });
(async (): Promise<void> => {
for await (const req of server) {
req.respond({ body: "Hello world" });
@@ -21,7 +23,7 @@ Deno.test({
Deno.execPath(),
"--allow-net",
"curl.ts",
- "http://localhost:8081"
+ "http://localhost:" + port
],
cwd: "examples",
stdout: "piped"
diff --git a/std/examples/tests/echo_server_test.ts b/std/examples/tests/echo_server_test.ts
index 20fd7479c..3c1893342 100644
--- a/std/examples/tests/echo_server_test.ts
+++ b/std/examples/tests/echo_server_test.ts
@@ -1,12 +1,14 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assertStrictEq, assertNotEquals } from "../../testing/asserts.ts";
import { BufReader, ReadLineResult } from "../../io/bufio.ts";
+import { randomPort } from "../../http/test_util.ts";
+const port = randomPort();
Deno.test("[examples/echo_server]", async () => {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const process = Deno.run({
- args: [Deno.execPath(), "--allow-net", "echo_server.ts"],
+ args: [Deno.execPath(), "--allow-net", "echo_server.ts", `${port}`],
cwd: "examples",
stdout: "piped"
});
@@ -19,10 +21,10 @@ Deno.test("[examples/echo_server]", async () => {
assertNotEquals(message, Deno.EOF);
assertStrictEq(
decoder.decode((message as ReadLineResult).line).trim(),
- "Listening on 0.0.0.0:8080"
+ "Listening on 0.0.0.0:" + port
);
- conn = await Deno.connect({ hostname: "127.0.0.1", port: 8080 });
+ conn = await Deno.connect({ hostname: "127.0.0.1", port });
const connReader = new BufReader(conn);
await conn.write(encoder.encode("Hello echo_server\n"));
diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts
index 1fbe3ba97..b7cd8a7e1 100644
--- a/std/http/file_server_test.ts
+++ b/std/http/file_server_test.ts
@@ -2,9 +2,11 @@
import { assert, assertEquals, assertStrContains } from "../testing/asserts.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
+import { randomPort } from "./test_util.ts";
const { test } = Deno;
let fileServer: Deno.Process;
+const port = randomPort();
async function startFileServer(): Promise<void> {
fileServer = Deno.run({
args: [
@@ -14,7 +16,9 @@ async function startFileServer(): Promise<void> {
"--allow-net",
"http/file_server.ts",
".",
- "--cors"
+ "--cors",
+ "--port",
+ `${port}`
],
stdout: "piped",
stderr: "null"
@@ -34,7 +38,7 @@ function killFileServer(): void {
test(async function serveFile(): Promise<void> {
await startFileServer();
try {
- const res = await fetch("http://localhost:4500/README.md");
+ const res = await fetch(`http://localhost:${port}/README.md`);
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assert(res.headers.has("content-type"));
@@ -52,7 +56,7 @@ test(async function serveFile(): Promise<void> {
test(async function serveDirectory(): Promise<void> {
await startFileServer();
try {
- const res = await fetch("http://localhost:4500/");
+ const res = await fetch(`http://localhost:${port}/`);
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
const page = await res.text();
@@ -74,7 +78,7 @@ test(async function serveDirectory(): Promise<void> {
test(async function serveFallback(): Promise<void> {
await startFileServer();
try {
- const res = await fetch("http://localhost:4500/badfile.txt");
+ const res = await fetch(`http://localhost:${port}/badfile.txt`);
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 404);
@@ -87,12 +91,12 @@ test(async function serveFallback(): Promise<void> {
test(async function serveWithUnorthodoxFilename(): Promise<void> {
await startFileServer();
try {
- let res = await fetch("http://localhost:4500/http/testdata/%");
+ let res = await fetch(`http://localhost:${port}/http/testdata/%`);
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 200);
res.body.close();
- res = await fetch("http://localhost:4500/http/testdata/test%20file.txt");
+ res = await fetch(`http://localhost:${port}/http/testdata/test%20file.txt`);
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 200);
@@ -103,8 +107,16 @@ test(async function serveWithUnorthodoxFilename(): Promise<void> {
});
test(async function servePermissionDenied(): Promise<void> {
+ const _port = randomPort();
const deniedServer = Deno.run({
- args: [Deno.execPath(), "run", "--allow-net", "http/file_server.ts"],
+ args: [
+ Deno.execPath(),
+ "run",
+ "--allow-net",
+ "http/file_server.ts",
+ "-p",
+ `${_port}`
+ ],
stdout: "piped",
stderr: "piped"
});
@@ -116,7 +128,7 @@ test(async function servePermissionDenied(): Promise<void> {
assert(s !== Deno.EOF && s.includes("server listening"));
try {
- const res = await fetch("http://localhost:4500/");
+ const res = await fetch(`http://localhost:${_port}/`);
res.body.close();
assertStrContains(
(await errReader.readLine()) as string,
diff --git a/std/http/http_bench.ts b/std/http/http_bench.ts
index 9d1912831..060d0ad88 100644
--- a/std/http/http_bench.ts
+++ b/std/http/http_bench.ts
@@ -1,7 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { serve } from "./server.ts";
+import { randomPort } from "./test_util.ts";
-const addr = Deno.args[0] || "127.0.0.1:4500";
+const addr = Deno.args[0] || "127.0.0.1:" + randomPort();
const server = serve(addr);
const body = new TextEncoder().encode("Hello World");
diff --git a/std/http/racing_server.ts b/std/http/racing_server.ts
index 0b0e5a8a5..693531a7f 100644
--- a/std/http/racing_server.ts
+++ b/std/http/racing_server.ts
@@ -2,7 +2,7 @@
import { serve, ServerRequest } from "./server.ts";
import { delay } from "../util/async.ts";
-const addr = Deno.args[1] || "127.0.0.1:4501";
+const addr = Deno.args[0] || "127.0.0.1:4501";
const server = serve(addr);
function body(i: number): string {
diff --git a/std/http/racing_server_test.ts b/std/http/racing_server_test.ts
index d82acebab..2f88a4703 100644
--- a/std/http/racing_server_test.ts
+++ b/std/http/racing_server_test.ts
@@ -1,12 +1,20 @@
import { assert, assertEquals } from "../testing/asserts.ts";
import { BufReader, BufWriter } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
+import { randomPort } from "./test_util.ts";
+const port = randomPort();
const { connect, run, test } = Deno;
let server: Deno.Process;
async function startServer(): Promise<void> {
server = run({
- args: [Deno.execPath(), "run", "-A", "http/racing_server.ts"],
+ args: [
+ Deno.execPath(),
+ "run",
+ "-A",
+ "http/racing_server.ts",
+ "127.0.0.1:" + port
+ ],
stdout: "piped"
});
// Once racing server is ready it will write to its stdout.
@@ -61,7 +69,7 @@ Step7
test(async function serverPipelineRace(): Promise<void> {
await startServer();
- const conn = await connect({ port: 4501 });
+ const conn = await connect({ port });
const r = new TextProtoReader(new BufReader(conn));
const w = new BufWriter(conn);
await w.write(new TextEncoder().encode(input));
diff --git a/std/http/server_test.ts b/std/http/server_test.ts
index 2a7c46134..d37111620 100644
--- a/std/http/server_test.ts
+++ b/std/http/server_test.ts
@@ -18,6 +18,7 @@ import { BufReader, BufWriter } from "../io/bufio.ts";
import { delay } from "../util/async.ts";
import { encode, decode } from "../strings/mod.ts";
import { mockConn } from "./mock.ts";
+import { randomPort } from "./test_util.ts";
const { Buffer, test } = Deno;
@@ -355,8 +356,14 @@ test({
ignore: true,
fn: async (): Promise<void> => {
// Runs a simple server as another process
+ const port = randomPort();
const p = Deno.run({
- args: [Deno.execPath(), "--allow-net", "http/testdata/simple_server.ts"],
+ args: [
+ Deno.execPath(),
+ "--allow-net",
+ "http/testdata/simple_server.ts",
+ `${port}`
+ ],
stdout: "piped"
});
@@ -395,13 +402,15 @@ test({
// FIXME(bartlomieju): hangs on windows, cause can't do `Deno.kill`
ignore: true,
fn: async (): Promise<void> => {
+ const port = randomPort();
// Runs a simple server as another process
const p = Deno.run({
args: [
Deno.execPath(),
"--allow-net",
"--allow-read",
- "http/testdata/simple_https_server.ts"
+ "http/testdata/simple_https_server.ts",
+ `${port}`
],
stdout: "piped"
});
@@ -413,7 +422,6 @@ test({
serverIsRunning = false;
})
.catch((_): void => {}); // Ignores the error when closing the process.
-
try {
const r = new TextProtoReader(new BufReader(p.stdout!));
const s = await r.readLine();
@@ -424,7 +432,7 @@ test({
// Requests to the server and immediately closes the connection
const conn = await Deno.connectTLS({
hostname: "localhost",
- port: 4503,
+ port,
certFile: "http/testdata/tls/RootCA.pem"
});
await Deno.writeAll(
@@ -448,7 +456,7 @@ test({
});
test("close server while iterating", async (): Promise<void> => {
- const server = serve(":8123");
+ const server = serve({ port: randomPort() });
const nextWhileClosing = server[Symbol.asyncIterator]().next();
server.close();
assertEquals(await nextWhileClosing, { value: undefined, done: true });
@@ -491,8 +499,9 @@ test({
test({
name: "respond error closes connection",
async fn(): Promise<void> {
+ const port = randomPort();
const serverRoutine = async (): Promise<void> => {
- const server = serve(":8124");
+ const server = serve(":" + port);
// @ts-ignore
for await (const req of server) {
await assertThrowsAsync(async () => {
@@ -509,7 +518,7 @@ test({
const p = serverRoutine();
const conn = await Deno.connect({
hostname: "127.0.0.1",
- port: 8124
+ port
});
await Deno.writeAll(
conn,
diff --git a/std/http/test_util.ts b/std/http/test_util.ts
new file mode 100644
index 000000000..c86a339a7
--- /dev/null
+++ b/std/http/test_util.ts
@@ -0,0 +1,20 @@
+import { assert } from "../testing/asserts.ts";
+
+function* portIterator(): IterableIterator<number> {
+ // use 55001 ~ 65535 (rest (49152~55000) are for cli/js)
+ let i = 55001;
+ while (true) {
+ yield i;
+ i++;
+ if (i > 65535) {
+ i = 55001;
+ }
+ }
+}
+const it = portIterator();
+/** Obtain (maybe) safe port number for net tests */
+export function randomPort(): number {
+ const { value } = it.next();
+ assert(value != null);
+ return value;
+}
diff --git a/std/http/testdata/simple_https_server.ts b/std/http/testdata/simple_https_server.ts
index 9330b4172..9e3045609 100644
--- a/std/http/testdata/simple_https_server.ts
+++ b/std/http/testdata/simple_https_server.ts
@@ -2,9 +2,10 @@
// This is an example of a https server
import { serveTLS } from "../server.ts";
+const port = parseInt(Deno.args[0] || "4503");
const tlsOptions = {
hostname: "localhost",
- port: 4503,
+ port,
certFile: "./http/testdata/tls/localhost.crt",
keyFile: "./http/testdata/tls/localhost.key"
};
diff --git a/std/http/testdata/simple_server.ts b/std/http/testdata/simple_server.ts
index d8ca4cc97..05f169705 100644
--- a/std/http/testdata/simple_server.ts
+++ b/std/http/testdata/simple_server.ts
@@ -2,8 +2,9 @@
// This is an example of a server that responds with an empty body
import { serve } from "../server.ts";
-const addr = "0.0.0.0:4502";
-console.log(`Simple server listening on ${addr}`);
+const port = parseInt(Deno.args[0] || "4502");
+const addr: Deno.ListenOptions = { port };
+console.log(`Simple server listening on ${port}`);
for await (const req of serve(addr)) {
req.respond({});
}