diff options
author | Yusuke Sakurai <kerokerokerop@gmail.com> | 2020-03-21 22:53:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-21 09:53:47 -0400 |
commit | 60cee4f045778777a16b6fffd6d5b9a1400d7246 (patch) | |
tree | a477bd147fbd548d478a289af5bd0681b1a34c4e /std/examples | |
parent | 0adc86f105204b2475126c36dfc10e678f67df56 (diff) |
avoid using same port number for test (#4147)
Diffstat (limited to 'std/examples')
-rw-r--r-- | std/examples/chat/server.ts | 6 | ||||
-rw-r--r-- | std/examples/chat/server_test.ts | 15 | ||||
-rw-r--r-- | std/examples/echo_server.ts | 2 | ||||
-rw-r--r-- | std/examples/tests/curl_test.ts | 6 | ||||
-rw-r--r-- | std/examples/tests/echo_server_test.ts | 8 |
5 files changed, 26 insertions, 11 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")); |