diff options
Diffstat (limited to 'std/examples')
-rw-r--r-- | std/examples/chat/server_test.ts | 67 | ||||
-rw-r--r-- | std/examples/test.ts | 4 | ||||
-rw-r--r-- | std/examples/tests/cat_test.ts | 2 | ||||
-rw-r--r-- | std/examples/tests/catj_test.ts | 11 | ||||
-rw-r--r-- | std/examples/tests/colors_test.ts | 2 | ||||
-rw-r--r-- | std/examples/tests/curl_test.ts | 66 | ||||
-rw-r--r-- | std/examples/tests/echo_server_test.ts | 1 | ||||
-rw-r--r-- | std/examples/tests/welcome_test.ts | 2 | ||||
-rw-r--r-- | std/examples/tests/xeval_test.ts | 29 |
9 files changed, 97 insertions, 87 deletions
diff --git a/std/examples/chat/server_test.ts b/std/examples/chat/server_test.ts index c63453a19..8004055a4 100644 --- a/std/examples/chat/server_test.ts +++ b/std/examples/chat/server_test.ts @@ -3,10 +3,12 @@ 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 { delay } from "../../util/async.ts"; -let server: Deno.Process | undefined; -async function startServer(): Promise<void> { - server = Deno.run({ +const { test, build } = Deno; + +async function startServer(): Promise<Deno.Process> { + const server = Deno.run({ args: [Deno.execPath(), "--allow-net", "--allow-read", "server.ts"], cwd: "examples/chat", stdout: "piped" @@ -17,54 +19,51 @@ async function startServer(): Promise<void> { const s = await r.readLine(); assert(s !== Deno.EOF && s.includes("chat server starting")); } catch { + server.stdout!.close(); server.close(); } -} -const { test, build } = Deno; + return server; +} // TODO: https://github.com/denoland/deno/issues/4108 const skip = build.os == "win"; test({ skip, - name: "beforeAll", - async fn() { - await startServer(); - } -}); - -test({ - skip, name: "GET / should serve html", async fn() { - 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(); - assert(html.includes("ws chat example"), "body is ok"); + const server = await startServer(); + try { + 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(); + assert(html.includes("ws chat example"), "body is ok"); + } finally { + server.close(); + server.stdout!.close(); + } + await delay(10); } }); -let ws: WebSocket | undefined; - test({ skip, name: "GET /ws should upgrade conn to ws", async fn() { - ws = await connectWebSocket("http://127.0.0.1:8080/ws"); - const it = ws.receive(); - assertEquals((await it.next()).value, "Connected: [1]"); - ws.send("Hello"); - assertEquals((await it.next()).value, "[1]: Hello"); - } -}); - -test({ - skip, - name: "afterAll", - fn() { - server?.close(); - ws?.conn.close(); + const server = await startServer(); + let ws: WebSocket | undefined; + try { + ws = await connectWebSocket("http://127.0.0.1:8080/ws"); + const it = ws.receive(); + assertEquals((await it.next()).value, "Connected: [1]"); + ws.send("Hello"); + assertEquals((await it.next()).value, "[1]: Hello"); + } finally { + server.close(); + server.stdout!.close(); + ws!.conn.close(); + } } }); diff --git a/std/examples/test.ts b/std/examples/test.ts index 641a2ef74..1c817dfd1 100644 --- a/std/examples/test.ts +++ b/std/examples/test.ts @@ -21,8 +21,10 @@ Deno.test(async function catSmoke(): Promise<void> { "examples/cat.ts", "README.md" ], - stdout: "piped" + stdout: "null", + stderr: "null" }); const s = await p.status(); assertEquals(s.code, 0); + p.close(); }); diff --git a/std/examples/tests/cat_test.ts b/std/examples/tests/cat_test.ts index 93e884f61..34b60bc5d 100644 --- a/std/examples/tests/cat_test.ts +++ b/std/examples/tests/cat_test.ts @@ -16,7 +16,7 @@ Deno.test("[examples/cat] print multiple files", async () => { }); try { - const output = await Deno.readAll(process.stdout!); + const output = await process.output(); const actual = decoder.decode(output).trim(); assertStrictEq(actual, "Hello\nWorld"); } finally { diff --git a/std/examples/tests/catj_test.ts b/std/examples/tests/catj_test.ts index 9267f532b..2d667dacc 100644 --- a/std/examples/tests/catj_test.ts +++ b/std/examples/tests/catj_test.ts @@ -5,7 +5,7 @@ Deno.test("[examples/catj] print an array", async () => { const decoder = new TextDecoder(); const process = catj("testdata/catj/array.json"); try { - const output = await Deno.readAll(process.stdout!); + const output = await process.output(); const actual = decoder.decode(output).trim(); const expected = [ '.[0] = "string"', @@ -17,6 +17,7 @@ Deno.test("[examples/catj] print an array", async () => { assertStrictEq(actual, expected); } finally { + process.stdin!.close(); process.close(); } }); @@ -25,7 +26,7 @@ Deno.test("[examples/catj] print an object", async () => { const decoder = new TextDecoder(); const process = catj("testdata/catj/object.json"); try { - const output = await Deno.readAll(process.stdout!); + const output = await process.output(); const actual = decoder.decode(output).trim(); const expected = [ '.string = "foobar"', @@ -35,6 +36,7 @@ Deno.test("[examples/catj] print an object", async () => { assertStrictEq(actual, expected); } finally { + process.stdin!.close(); process.close(); } }); @@ -46,12 +48,13 @@ Deno.test("[examples/catj] print multiple files", async () => { "testdata/catj/simple-array.json" ); try { - const output = await Deno.readAll(process.stdout!); + const output = await process.output(); const actual = decoder.decode(output).trim(); const expected = ['.message = "hello"', ".[0] = 1", ".[1] = 2"].join("\n"); assertStrictEq(actual, expected); } finally { + process.stdin!.close(); process.close(); } }); @@ -63,7 +66,7 @@ Deno.test("[examples/catj] read from stdin", async () => { try { await process.stdin!.write(new TextEncoder().encode(input)); process.stdin!.close(); - const output = await Deno.readAll(process.stdout!); + const output = await process.output(); const actual = decoder.decode(output).trim(); assertStrictEq(actual, '.foo = "bar"'); diff --git a/std/examples/tests/colors_test.ts b/std/examples/tests/colors_test.ts index dcb485f6c..e01e4d558 100644 --- a/std/examples/tests/colors_test.ts +++ b/std/examples/tests/colors_test.ts @@ -9,7 +9,7 @@ Deno.test("[examples/colors] print a colored text", async () => { stdout: "piped" }); try { - const output = await Deno.readAll(process.stdout!); + const output = await process.output(); const actual = decoder.decode(output).trim(); const expected = "[44m[3m[31m[1mHello world![22m[39m[23m[49m"; assertStrictEq(actual, expected); diff --git a/std/examples/tests/curl_test.ts b/std/examples/tests/curl_test.ts index 71a0fe67f..593e5b8f7 100644 --- a/std/examples/tests/curl_test.ts +++ b/std/examples/tests/curl_test.ts @@ -1,41 +1,41 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { Server, serve } from "../../http/server.ts"; +import { serve } from "../../http/server.ts"; import { assertStrictEq } from "../../testing/asserts.ts"; -let server: Server | undefined; +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 + skip: true, + fn: async () => { + const server = serve({ port: 8081 }); + (async (): Promise<void> => { + for await (const req of server) { + req.respond({ body: "Hello world" }); + } + })(); -async function startTestServer(): Promise<void> { - server = await serve({ port: 8080 }); - (async (): Promise<void> => { - for await (const req of server) { - req.respond({ body: "Hello world" }); - } - })(); -} - -Deno.test("[examples/curl] beforeAll", async () => { - await startTestServer(); -}); - -Deno.test("[examples/curl] send a request to a specified url", async () => { - const decoder = new TextDecoder(); - const process = Deno.run({ - args: [Deno.execPath(), "--allow-net", "curl.ts", "http://localhost:8080"], - cwd: "examples", - stdout: "piped" - }); + const decoder = new TextDecoder(); + const process = Deno.run({ + args: [ + Deno.execPath(), + "--allow-net", + "curl.ts", + "http://localhost:8081" + ], + cwd: "examples", + stdout: "piped" + }); - try { - const output = await Deno.readAll(process.stdout!); - const actual = decoder.decode(output).trim(); - const expected = "Hello world"; + try { + const output = await process.output(); + const actual = decoder.decode(output).trim(); + const expected = "Hello world"; - assertStrictEq(actual, expected); - } finally { - process.close(); + assertStrictEq(actual, expected); + } finally { + process.close(); + server.close(); + } } }); - -Deno.test("[examples/curl] afterAll", () => { - server?.close(); -}); diff --git a/std/examples/tests/echo_server_test.ts b/std/examples/tests/echo_server_test.ts index 49a1feb31..20fd7479c 100644 --- a/std/examples/tests/echo_server_test.ts +++ b/std/examples/tests/echo_server_test.ts @@ -38,6 +38,7 @@ Deno.test("[examples/echo_server]", async () => { assertStrictEq(actualResponse, expectedResponse); } finally { conn?.close(); + process.stdout!.close(); process.close(); } }); diff --git a/std/examples/tests/welcome_test.ts b/std/examples/tests/welcome_test.ts index 76209cfad..304a57868 100644 --- a/std/examples/tests/welcome_test.ts +++ b/std/examples/tests/welcome_test.ts @@ -9,7 +9,7 @@ Deno.test("[examples/welcome] print a welcome message", async () => { stdout: "piped" }); try { - const output = await Deno.readAll(process.stdout!); + const output = await process.output(); const actual = decoder.decode(output).trim(); const expected = "Welcome to Deno 🦕"; assertStrictEq(actual, expected); diff --git a/std/examples/tests/xeval_test.ts b/std/examples/tests/xeval_test.ts index 128b6e3da..db11c215c 100644 --- a/std/examples/tests/xeval_test.ts +++ b/std/examples/tests/xeval_test.ts @@ -25,18 +25,22 @@ Deno.test(async function xevalDelimiter(): Promise<void> { const xevalPath = "examples/xeval.ts"; -Deno.test(async function xevalCliReplvar(): Promise<void> { - const p = run({ - args: [execPath(), xevalPath, "--replvar=abc", "console.log(abc)"], - stdin: "piped", - stdout: "piped", - stderr: "null" - }); - assert(p.stdin != null); - await p.stdin.write(encode("hello")); - await p.stdin.close(); - assertEquals(await p.status(), { code: 0, success: true }); - assertEquals(decode(await p.output()).trimEnd(), "hello"); +Deno.test({ + name: "xevalCliReplvar", + fn: async function(): Promise<void> { + const p = run({ + args: [execPath(), xevalPath, "--replvar=abc", "console.log(abc)"], + stdin: "piped", + stdout: "piped", + stderr: "null" + }); + assert(p.stdin != null); + await p.stdin.write(encode("hello")); + p.stdin.close(); + assertEquals(await p.status(), { code: 0, success: true }); + assertEquals(decode(await p.output()).trimEnd(), "hello"); + p.close(); + } }); Deno.test(async function xevalCliSyntaxError(): Promise<void> { @@ -49,4 +53,5 @@ Deno.test(async function xevalCliSyntaxError(): Promise<void> { assertEquals(await p.status(), { code: 1, success: false }); assertEquals(decode(await p.output()), ""); assertStrContains(decode(await p.stderrOutput()), "Uncaught SyntaxError"); + p.close(); }); |