Skip to content

Commit

Permalink
Revert "avoid using same port number for test (denoland/deno#4147)"
Browse files Browse the repository at this point in the history
Ref denoland/deno#4467

This reverts commit 60cee4f.
  • Loading branch information
bartlomieju authored Mar 24, 2020
1 parent baa8fe2 commit aba948a
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 105 deletions.
6 changes: 2 additions & 4 deletions examples/chat/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ async function wsHandler(ws: WebSocket): Promise<void> {
}
}

const addr = Deno.args[0] ?? "127.0.0.1:8080";

listenAndServe(addr, async req => {
listenAndServe({ port: 8080 }, async req => {
if (req.method === "GET" && req.url === "/") {
//Serve with hack
const u = new URL("./index.html", import.meta.url);
Expand Down Expand Up @@ -77,4 +75,4 @@ listenAndServe(addr, async req => {
}
}
});
console.log(`chat server starting on ${addr}....`);
console.log("chat server starting on :8080....");
15 changes: 3 additions & 12 deletions examples/chat/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@ 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({
cmd: [
Deno.execPath(),
"--allow-net",
"--allow-read",
"server.ts",
`127.0.0.1:${port}`
],
cmd: [Deno.execPath(), "--allow-net", "--allow-read", "server.ts"],
cwd: "examples/chat",
stdout: "piped"
});
Expand All @@ -44,7 +35,7 @@ test({
async fn() {
const server = await startServer();
try {
const resp = await fetch(`http://127.0.0.1:${port}/`);
const resp = await fetch("http://127.0.0.1:8080/");
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html");
const html = await resp.body.text();
Expand All @@ -64,7 +55,7 @@ test({
const server = await startServer();
let ws: WebSocket | undefined;
try {
ws = await connectWebSocket(`http://127.0.0.1:${port}/ws`);
ws = await connectWebSocket("http://127.0.0.1:8080/ws");
const it = ws.receive();
assertEquals((await it.next()).value, "Connected: [1]");
ws.send("Hello");
Expand Down
2 changes: 1 addition & 1 deletion examples/echo_server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
const hostname = "0.0.0.0";
const port = +(Deno.args[0] ?? "8080");
const port = 8080;
const listener = Deno.listen({ hostname, port });
console.log(`Listening on ${hostname}:${port}`);
for await (const conn of listener) {
Expand Down
11 changes: 2 additions & 9 deletions examples/tests/curl_test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// 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",
fn: async () => {
const server = serve({ port });
const server = serve({ port: 8081 });
const serverPromise = (async (): Promise<void> => {
for await (const req of server) {
req.respond({ body: "Hello world" });
Expand All @@ -16,12 +14,7 @@ Deno.test({

const decoder = new TextDecoder();
const process = Deno.run({
cmd: [
Deno.execPath(),
"--allow-net",
"curl.ts",
"http://localhost:" + port
],
cmd: [Deno.execPath(), "--allow-net", "curl.ts", "http://localhost:8081"],
cwd: "examples",
stdout: "piped"
});
Expand Down
8 changes: 3 additions & 5 deletions examples/tests/echo_server_test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// 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({
cmd: [Deno.execPath(), "--allow-net", "echo_server.ts", `${port}`],
cmd: [Deno.execPath(), "--allow-net", "echo_server.ts"],
cwd: "examples",
stdout: "piped"
});
Expand All @@ -21,10 +19,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:" + port
"Listening on 0.0.0.0:8080"
);

conn = await Deno.connect({ hostname: "127.0.0.1", port });
conn = await Deno.connect({ hostname: "127.0.0.1", port: 8080 });
const connReader = new BufReader(conn);

await conn.write(encoder.encode("Hello echo_server\n"));
Expand Down
28 changes: 8 additions & 20 deletions http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
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({
cmd: [
Expand All @@ -16,9 +14,7 @@ async function startFileServer(): Promise<void> {
"--allow-net",
"http/file_server.ts",
".",
"--cors",
"--port",
`${port}`
"--cors"
],
stdout: "piped",
stderr: "null"
Expand All @@ -38,7 +34,7 @@ function killFileServer(): void {
test(async function serveFile(): Promise<void> {
await startFileServer();
try {
const res = await fetch(`http://localhost:${port}/README.md`);
const res = await fetch("http://localhost:4500/README.md");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assert(res.headers.has("content-type"));
Expand All @@ -56,7 +52,7 @@ test(async function serveFile(): Promise<void> {
test(async function serveDirectory(): Promise<void> {
await startFileServer();
try {
const res = await fetch(`http://localhost:${port}/`);
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();
Expand All @@ -78,7 +74,7 @@ test(async function serveDirectory(): Promise<void> {
test(async function serveFallback(): Promise<void> {
await startFileServer();
try {
const res = await fetch(`http://localhost:${port}/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-headers"));
assertEquals(res.status, 404);
Expand All @@ -91,12 +87,12 @@ test(async function serveFallback(): Promise<void> {
test(async function serveWithUnorthodoxFilename(): Promise<void> {
await startFileServer();
try {
let res = await fetch(`http://localhost:${port}/http/testdata/%`);
let res = await fetch("http://localhost:4500/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:${port}/http/testdata/test%20file.txt`);
res = await fetch("http://localhost:4500/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);
Expand All @@ -107,16 +103,8 @@ test(async function serveWithUnorthodoxFilename(): Promise<void> {
});

test(async function servePermissionDenied(): Promise<void> {
const _port = randomPort();
const deniedServer = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--allow-net",
"http/file_server.ts",
"-p",
`${_port}`
],
cmd: [Deno.execPath(), "run", "--allow-net", "http/file_server.ts"],
stdout: "piped",
stderr: "piped"
});
Expand All @@ -128,7 +116,7 @@ test(async function servePermissionDenied(): Promise<void> {
assert(s !== Deno.EOF && s.includes("server listening"));

try {
const res = await fetch(`http://localhost:${_port}/`);
const res = await fetch("http://localhost:4500/");
res.body.close();
assertStrContains(
(await errReader.readLine()) as string,
Expand Down
3 changes: 1 addition & 2 deletions http/http_bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// 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:" + randomPort();
const addr = Deno.args[0] || "127.0.0.1:4500";
const server = serve(addr);
const body = new TextEncoder().encode("Hello World");

Expand Down
2 changes: 1 addition & 1 deletion http/racing_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { serve, ServerRequest } from "./server.ts";
import { delay } from "../util/async.ts";

const addr = Deno.args[0] || "127.0.0.1:4501";
const addr = Deno.args[1] || "127.0.0.1:4501";
const server = serve(addr);

function body(i: number): string {
Expand Down
12 changes: 2 additions & 10 deletions http/racing_server_test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
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({
cmd: [
Deno.execPath(),
"run",
"-A",
"http/racing_server.ts",
"127.0.0.1:" + port
],
cmd: [Deno.execPath(), "run", "-A", "http/racing_server.ts"],
stdout: "piped"
});
// Once racing server is ready it will write to its stdout.
Expand Down Expand Up @@ -69,7 +61,7 @@ Step7
test(async function serverPipelineRace(): Promise<void> {
await startServer();

const conn = await connect({ port });
const conn = await connect({ port: 4501 });
const r = new TextProtoReader(new BufReader(conn));
const w = new BufWriter(conn);
await w.write(new TextEncoder().encode(input));
Expand Down
23 changes: 7 additions & 16 deletions http/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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;

Expand Down Expand Up @@ -356,14 +355,8 @@ test({
ignore: true,
fn: async (): Promise<void> => {
// Runs a simple server as another process
const port = randomPort();
const p = Deno.run({
cmd: [
Deno.execPath(),
"--allow-net",
"http/testdata/simple_server.ts",
`${port}`
],
cmd: [Deno.execPath(), "--allow-net", "http/testdata/simple_server.ts"],
stdout: "piped"
});

Expand Down Expand Up @@ -402,15 +395,13 @@ 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({
cmd: [
Deno.execPath(),
"--allow-net",
"--allow-read",
"http/testdata/simple_https_server.ts",
`${port}`
"http/testdata/simple_https_server.ts"
],
stdout: "piped"
});
Expand All @@ -422,6 +413,7 @@ 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();
Expand All @@ -432,7 +424,7 @@ test({
// Requests to the server and immediately closes the connection
const conn = await Deno.connectTLS({
hostname: "localhost",
port,
port: 4503,
certFile: "http/testdata/tls/RootCA.pem"
});
await Deno.writeAll(
Expand All @@ -456,7 +448,7 @@ test({
});

test("close server while iterating", async (): Promise<void> => {
const server = serve({ port: randomPort() });
const server = serve(":8123");
const nextWhileClosing = server[Symbol.asyncIterator]().next();
server.close();
assertEquals(await nextWhileClosing, { value: undefined, done: true });
Expand Down Expand Up @@ -499,9 +491,8 @@ test({
test({
name: "respond error closes connection",
async fn(): Promise<void> {
const port = randomPort();
const serverRoutine = async (): Promise<void> => {
const server = serve(":" + port);
const server = serve(":8124");
// @ts-ignore
for await (const req of server) {
await assertThrowsAsync(async () => {
Expand All @@ -518,7 +509,7 @@ test({
const p = serverRoutine();
const conn = await Deno.connect({
hostname: "127.0.0.1",
port
port: 8124
});
await Deno.writeAll(
conn,
Expand Down
20 changes: 0 additions & 20 deletions http/test_util.ts

This file was deleted.

3 changes: 1 addition & 2 deletions http/testdata/simple_https_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// 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,
port: 4503,
certFile: "./http/testdata/tls/localhost.crt",
keyFile: "./http/testdata/tls/localhost.key"
};
Expand Down
5 changes: 2 additions & 3 deletions http/testdata/simple_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// This is an example of a server that responds with an empty body
import { serve } from "../server.ts";

const port = parseInt(Deno.args[0] || "4502");
const addr: Deno.ListenOptions = { port };
console.log(`Simple server listening on ${port}`);
const addr = "0.0.0.0:4502";
console.log(`Simple server listening on ${addr}`);
for await (const req of serve(addr)) {
req.respond({});
}

0 comments on commit aba948a

Please sign in to comment.