mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
feat: Deno.test() sanitizes ops and resources (#4399)
This PR brings assertOps and assertResources sanitizers to Deno.test() API. assertOps checks that test doesn't leak async ops, ie. there are no unresolved promises originating from Deno APIs. Enabled by default, can be disabled using Deno.TestDefinition.disableOpSanitizer. assertResources checks that test doesn't leak resources, ie. all resources used in test are closed. For example; if a file is opened during a test case it must be explicitly closed before test case finishes. It's most useful for asynchronous generators. Enabled by default, can be disabled using Deno.TestDefinition.disableResourceSanitizer. We've used those sanitizers in internal runtime tests and it proved very useful in surfacing incorrect tests which resulted in interference between the tests. All tests have been sanitized. Closes #4208
This commit is contained in:
parent
070464e2cc
commit
6e2df8c64f
28 changed files with 522 additions and 420 deletions
2
cli/js/lib.deno.ns.d.ts
vendored
2
cli/js/lib.deno.ns.d.ts
vendored
|
@ -18,6 +18,8 @@ declare namespace Deno {
|
|||
fn: TestFunction;
|
||||
name: string;
|
||||
skip?: boolean;
|
||||
disableOpSanitizer?: boolean;
|
||||
disableResourceSanitizer?: boolean;
|
||||
}
|
||||
|
||||
/** Register a test which will be run when `deno test` is used on the command
|
||||
|
|
|
@ -4,6 +4,9 @@ import { exit } from "./ops/os.ts";
|
|||
import { Console, stringifyArgs } from "./web/console.ts";
|
||||
import { stdout } from "./files.ts";
|
||||
import { TextEncoder } from "./web/text_encoding.ts";
|
||||
import { metrics } from "./ops/runtime.ts";
|
||||
import { resources } from "./ops/resources.ts";
|
||||
import { assert } from "./util.ts";
|
||||
|
||||
const RED_FAILED = red("FAILED");
|
||||
const GREEN_OK = green("ok");
|
||||
|
@ -15,12 +18,59 @@ function formatDuration(time = 0): string {
|
|||
return gray(italic(timeStr));
|
||||
}
|
||||
|
||||
// Wrap `TestFunction` in additional assertion that makes sure
|
||||
// the test case does not leak async "ops" - ie. number of async
|
||||
// completed ops after the test is the same as number of dispatched
|
||||
// ops. Note that "unref" ops are ignored since in nature that are
|
||||
// optional.
|
||||
function assertOps(fn: TestFunction): TestFunction {
|
||||
return async function asyncOpSanitizer(): Promise<void> {
|
||||
const pre = metrics();
|
||||
await fn();
|
||||
const post = metrics();
|
||||
// We're checking diff because one might spawn HTTP server in the background
|
||||
// that will be a pending async op before test starts.
|
||||
const dispatchedDiff = post.opsDispatchedAsync - pre.opsDispatchedAsync;
|
||||
const completedDiff = post.opsCompletedAsync - pre.opsCompletedAsync;
|
||||
assert(
|
||||
dispatchedDiff === completedDiff,
|
||||
`Test case is leaking async ops.
|
||||
Before:
|
||||
- dispatched: ${pre.opsDispatchedAsync}
|
||||
- completed: ${pre.opsCompletedAsync}
|
||||
After:
|
||||
- dispatched: ${post.opsDispatchedAsync}
|
||||
- completed: ${post.opsCompletedAsync}`
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
// Wrap `TestFunction` in additional assertion that makes sure
|
||||
// the test case does not "leak" resources - ie. resource table after
|
||||
// the test has exactly the same contents as before the test.
|
||||
function assertResources(fn: TestFunction): TestFunction {
|
||||
return async function resourceSanitizer(): Promise<void> {
|
||||
const pre = resources();
|
||||
await fn();
|
||||
const post = resources();
|
||||
|
||||
const preStr = JSON.stringify(pre, null, 2);
|
||||
const postStr = JSON.stringify(post, null, 2);
|
||||
const msg = `Test case is leaking resources.
|
||||
Before: ${preStr}
|
||||
After: ${postStr}`;
|
||||
assert(preStr === postStr, msg);
|
||||
};
|
||||
}
|
||||
|
||||
export type TestFunction = () => void | Promise<void>;
|
||||
|
||||
export interface TestDefinition {
|
||||
fn: TestFunction;
|
||||
name: string;
|
||||
skip?: boolean;
|
||||
disableOpSanitizer?: boolean;
|
||||
disableResourceSanitizer?: boolean;
|
||||
}
|
||||
|
||||
const TEST_REGISTRY: TestDefinition[] = [];
|
||||
|
@ -56,7 +106,16 @@ export function test(
|
|||
if (!t.name) {
|
||||
throw new TypeError("The test name can't be empty");
|
||||
}
|
||||
testDef = { fn: t.fn, name: t.name, skip: Boolean(t.skip) };
|
||||
|
||||
testDef = { ...t, skip: Boolean(t.skip) };
|
||||
}
|
||||
|
||||
if (testDef.disableOpSanitizer !== true) {
|
||||
testDef.fn = assertOps(testDef.fn);
|
||||
}
|
||||
|
||||
if (testDef.disableResourceSanitizer !== true) {
|
||||
testDef.fn = assertResources(testDef.fn);
|
||||
}
|
||||
|
||||
TEST_REGISTRY.push(testDef);
|
||||
|
|
|
@ -112,47 +112,6 @@ function normalizeTestPermissions(perms: UnitTestPermissions): Permissions {
|
|||
};
|
||||
}
|
||||
|
||||
// Wrap `TestFunction` in additional assertion that makes sure
|
||||
// the test case does not leak async "ops" - ie. number of async
|
||||
// completed ops after the test is the same as number of dispatched
|
||||
// ops. Note that "unref" ops are ignored since in nature that are
|
||||
// optional.
|
||||
function assertOps(fn: Deno.TestFunction): Deno.TestFunction {
|
||||
return async function asyncOpSanitizer(): Promise<void> {
|
||||
const pre = Deno.metrics();
|
||||
await fn();
|
||||
const post = Deno.metrics();
|
||||
// We're checking diff because one might spawn HTTP server in the background
|
||||
// that will be a pending async op before test starts.
|
||||
assertEquals(
|
||||
post.opsDispatchedAsync - pre.opsDispatchedAsync,
|
||||
post.opsCompletedAsync - pre.opsCompletedAsync,
|
||||
`Test case is leaking async ops.
|
||||
Before:
|
||||
- dispatched: ${pre.opsDispatchedAsync}
|
||||
- completed: ${pre.opsCompletedAsync}
|
||||
After:
|
||||
- dispatched: ${post.opsDispatchedAsync}
|
||||
- completed: ${post.opsCompletedAsync}`
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
// Wrap `TestFunction` in additional assertion that makes sure
|
||||
// the test case does not "leak" resources - ie. resource table after
|
||||
// the test has exactly the same contents as before the test.
|
||||
function assertResources(fn: Deno.TestFunction): Deno.TestFunction {
|
||||
return async function resourceSanitizer(): Promise<void> {
|
||||
const pre = Deno.resources();
|
||||
await fn();
|
||||
const post = Deno.resources();
|
||||
const msg = `Test case is leaking resources.
|
||||
Before: ${JSON.stringify(pre, null, 2)}
|
||||
After: ${JSON.stringify(post, null, 2)}`;
|
||||
assertEquals(pre, post, msg);
|
||||
};
|
||||
}
|
||||
|
||||
interface UnitTestPermissions {
|
||||
read?: boolean;
|
||||
write?: boolean;
|
||||
|
@ -209,7 +168,7 @@ export function unitTest(
|
|||
|
||||
const unitTestDefinition: UnitTestDefinition = {
|
||||
name,
|
||||
fn: assertResources(assertOps(fn)),
|
||||
fn,
|
||||
skip: !!options.skip,
|
||||
perms: normalizedPerms
|
||||
};
|
||||
|
|
|
@ -27,87 +27,107 @@ export function createResolvable<T>(): Resolvable<T> {
|
|||
return Object.assign(promise, methods!) as Resolvable<T>;
|
||||
}
|
||||
|
||||
Deno.test(async function workersBasic(): Promise<void> {
|
||||
const promise = createResolvable();
|
||||
const jsWorker = new Worker("../tests/subdir/test_worker.js", {
|
||||
type: "module",
|
||||
name: "jsWorker"
|
||||
});
|
||||
const tsWorker = new Worker("../tests/subdir/test_worker.ts", {
|
||||
type: "module",
|
||||
name: "tsWorker"
|
||||
});
|
||||
Deno.test({
|
||||
name: "workersBasic",
|
||||
// FIXME(bartlomieju):
|
||||
disableOpSanitizer: true,
|
||||
fn: async function(): Promise<void> {
|
||||
const promise = createResolvable();
|
||||
const jsWorker = new Worker("../tests/subdir/test_worker.js", {
|
||||
type: "module",
|
||||
name: "jsWorker"
|
||||
});
|
||||
const tsWorker = new Worker("../tests/subdir/test_worker.ts", {
|
||||
type: "module",
|
||||
name: "tsWorker"
|
||||
});
|
||||
|
||||
tsWorker.onmessage = (e): void => {
|
||||
assertEquals(e.data, "Hello World");
|
||||
promise.resolve();
|
||||
};
|
||||
tsWorker.onmessage = (e): void => {
|
||||
assertEquals(e.data, "Hello World");
|
||||
promise.resolve();
|
||||
};
|
||||
|
||||
jsWorker.onmessage = (e): void => {
|
||||
assertEquals(e.data, "Hello World");
|
||||
tsWorker.postMessage("Hello World");
|
||||
};
|
||||
jsWorker.onmessage = (e): void => {
|
||||
assertEquals(e.data, "Hello World");
|
||||
tsWorker.postMessage("Hello World");
|
||||
};
|
||||
|
||||
jsWorker.onerror = (e: Event): void => {
|
||||
e.preventDefault();
|
||||
jsWorker.postMessage("Hello World");
|
||||
};
|
||||
|
||||
jsWorker.onerror = (e: Event): void => {
|
||||
e.preventDefault();
|
||||
jsWorker.postMessage("Hello World");
|
||||
};
|
||||
|
||||
jsWorker.postMessage("Hello World");
|
||||
await promise;
|
||||
await promise;
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test(async function nestedWorker(): Promise<void> {
|
||||
const promise = createResolvable();
|
||||
Deno.test({
|
||||
name: "nestedWorker",
|
||||
// FIXME(bartlomieju):
|
||||
disableOpSanitizer: true,
|
||||
fn: async function(): Promise<void> {
|
||||
const promise = createResolvable();
|
||||
|
||||
const nestedWorker = new Worker("../tests/subdir/nested_worker.js", {
|
||||
type: "module",
|
||||
name: "nested"
|
||||
});
|
||||
const nestedWorker = new Worker("../tests/subdir/nested_worker.js", {
|
||||
type: "module",
|
||||
name: "nested"
|
||||
});
|
||||
|
||||
nestedWorker.onmessage = (e): void => {
|
||||
assert(e.data.type !== "error");
|
||||
promise.resolve();
|
||||
};
|
||||
nestedWorker.onmessage = (e): void => {
|
||||
assert(e.data.type !== "error");
|
||||
promise.resolve();
|
||||
};
|
||||
|
||||
nestedWorker.postMessage("Hello World");
|
||||
await promise;
|
||||
nestedWorker.postMessage("Hello World");
|
||||
await promise;
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test(async function workerThrowsWhenExecuting(): Promise<void> {
|
||||
const promise = createResolvable();
|
||||
const throwingWorker = new Worker("../tests/subdir/throwing_worker.js", {
|
||||
type: "module"
|
||||
});
|
||||
Deno.test({
|
||||
name: "workerThrowsWhenExecuting",
|
||||
// FIXME(bartlomieju):
|
||||
disableOpSanitizer: true,
|
||||
fn: async function(): Promise<void> {
|
||||
const promise = createResolvable();
|
||||
const throwingWorker = new Worker("../tests/subdir/throwing_worker.js", {
|
||||
type: "module"
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
throwingWorker.onerror = (e: any): void => {
|
||||
e.preventDefault();
|
||||
assert(/Uncaught Error: Thrown error/.test(e.message));
|
||||
promise.resolve();
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
throwingWorker.onerror = (e: any): void => {
|
||||
e.preventDefault();
|
||||
assert(/Uncaught Error: Thrown error/.test(e.message));
|
||||
promise.resolve();
|
||||
};
|
||||
|
||||
await promise;
|
||||
await promise;
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test(async function workerCanUseFetch(): Promise<void> {
|
||||
const promise = createResolvable();
|
||||
Deno.test({
|
||||
name: "workerCanUseFetch",
|
||||
// FIXME(bartlomieju):
|
||||
disableOpSanitizer: true,
|
||||
fn: async function(): Promise<void> {
|
||||
const promise = createResolvable();
|
||||
|
||||
const fetchingWorker = new Worker("../tests/subdir/fetching_worker.js", {
|
||||
type: "module"
|
||||
});
|
||||
const fetchingWorker = new Worker("../tests/subdir/fetching_worker.js", {
|
||||
type: "module"
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
fetchingWorker.onerror = (e: any): void => {
|
||||
e.preventDefault();
|
||||
promise.reject(e.message);
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
fetchingWorker.onerror = (e: any): void => {
|
||||
e.preventDefault();
|
||||
promise.reject(e.message);
|
||||
};
|
||||
|
||||
// Defer promise.resolve() to allow worker to shut down
|
||||
fetchingWorker.onmessage = (e): void => {
|
||||
assert(e.data === "Done!");
|
||||
promise.resolve();
|
||||
};
|
||||
// Defer promise.resolve() to allow worker to shut down
|
||||
fetchingWorker.onmessage = (e): void => {
|
||||
assert(e.data === "Done!");
|
||||
promise.resolve();
|
||||
};
|
||||
|
||||
await promise;
|
||||
await promise;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -160,5 +160,3 @@ Deno.test(async function testWriteVarnumLittleEndian(): Promise<void> {
|
|||
await buff.read(data);
|
||||
assertEquals(data, new Uint8Array([0x01, 0x02, 0x03, 0x04]));
|
||||
});
|
||||
|
||||
Deno.runTests();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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"');
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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" });
|
||||
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 process.output();
|
||||
const actual = decoder.decode(output).trim();
|
||||
const expected = "Hello world";
|
||||
|
||||
assertStrictEq(actual, expected);
|
||||
} finally {
|
||||
process.close();
|
||||
server.close();
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
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"
|
||||
});
|
||||
|
||||
try {
|
||||
const output = await Deno.readAll(process.stdout!);
|
||||
const actual = decoder.decode(output).trim();
|
||||
const expected = "Hello world";
|
||||
|
||||
assertStrictEq(actual, expected);
|
||||
} finally {
|
||||
process.close();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("[examples/curl] afterAll", () => {
|
||||
server?.close();
|
||||
});
|
||||
|
|
|
@ -38,6 +38,7 @@ Deno.test("[examples/echo_server]", async () => {
|
|||
assertStrictEq(actualResponse, expectedResponse);
|
||||
} finally {
|
||||
conn?.close();
|
||||
process.stdout!.close();
|
||||
process.close();
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
|
|
@ -23,7 +23,7 @@ Deno.test(async function emptyDirIfItNotExist(): Promise<void> {
|
|||
assertEquals(stat.isDirectory(), true);
|
||||
} finally {
|
||||
// remove the test dir
|
||||
Deno.remove(testDir, { recursive: true });
|
||||
await Deno.remove(testDir, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -39,7 +39,7 @@ Deno.test(function emptyDirSyncIfItNotExist(): void {
|
|||
assertEquals(stat.isDirectory(), true);
|
||||
} finally {
|
||||
// remove the test dir
|
||||
Deno.remove(testDir, { recursive: true });
|
||||
Deno.removeSync(testDir, { recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -218,16 +218,15 @@ for (const s of scenes) {
|
|||
);
|
||||
args.push("testfolder");
|
||||
|
||||
const { stdout } = Deno.run({
|
||||
const p = Deno.run({
|
||||
stdout: "piped",
|
||||
cwd: testdataDir,
|
||||
args: args
|
||||
});
|
||||
|
||||
assert(stdout);
|
||||
|
||||
const output = await Deno.readAll(stdout);
|
||||
|
||||
assert(p.stdout);
|
||||
const output = await p.output();
|
||||
p.close();
|
||||
assertStrContains(new TextDecoder().decode(output), s.output);
|
||||
} catch (err) {
|
||||
await Deno.remove(testfolder, { recursive: true });
|
||||
|
|
|
@ -15,13 +15,13 @@ Deno.test(async function ensureSymlinkIfItNotExist(): Promise<void> {
|
|||
const testDir = path.join(testdataDir, "link_file_1");
|
||||
const testFile = path.join(testDir, "test.txt");
|
||||
|
||||
assertThrowsAsync(
|
||||
await assertThrowsAsync(
|
||||
async (): Promise<void> => {
|
||||
await ensureSymlink(testFile, path.join(testDir, "test1.txt"));
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsAsync(
|
||||
await assertThrowsAsync(
|
||||
async (): Promise<void> => {
|
||||
await Deno.stat(testFile).then((): void => {
|
||||
throw new Error("test file should exists.");
|
||||
|
|
|
@ -121,14 +121,14 @@ for (const s of scenes) {
|
|||
args.push(path.join(testdataDir, s.async ? "exists.ts" : "exists_sync.ts"));
|
||||
args.push(s.file);
|
||||
|
||||
const { stdout } = Deno.run({
|
||||
const p = Deno.run({
|
||||
stdout: "piped",
|
||||
cwd: testdataDir,
|
||||
args: args
|
||||
});
|
||||
|
||||
const output = await Deno.readAll(stdout!);
|
||||
|
||||
const output = await p.output();
|
||||
p.close();
|
||||
assertStrContains(new TextDecoder().decode(output), s.output);
|
||||
});
|
||||
// done
|
||||
|
|
|
@ -131,4 +131,5 @@ Deno.test(async function expandGlobPermError(): Promise<void> {
|
|||
decode(await p.stderrOutput()),
|
||||
"Uncaught PermissionDenied"
|
||||
);
|
||||
p.close();
|
||||
});
|
||||
|
|
|
@ -20,7 +20,7 @@ export async function testWalk(
|
|||
await t();
|
||||
} finally {
|
||||
chdir(origCwd);
|
||||
remove(d, { recursive: true });
|
||||
await remove(d, { recursive: true });
|
||||
}
|
||||
}
|
||||
Deno.test({ skip, name: `[walk] ${name}`, fn });
|
||||
|
@ -46,7 +46,8 @@ export async function walkArray(
|
|||
}
|
||||
|
||||
export async function touch(path: string): Promise<void> {
|
||||
await open(path, "w");
|
||||
const f = await open(path, "w");
|
||||
f.close();
|
||||
}
|
||||
|
||||
function assertReady(expectedLength: number): void {
|
||||
|
|
|
@ -78,6 +78,7 @@ test(async function serveFallback(): Promise<void> {
|
|||
assert(res.headers.has("access-control-allow-origin"));
|
||||
assert(res.headers.has("access-control-allow-headers"));
|
||||
assertEquals(res.status, 404);
|
||||
res.body.close();
|
||||
} finally {
|
||||
killFileServer();
|
||||
}
|
||||
|
@ -90,11 +91,12 @@ test(async function serveWithUnorthodoxFilename(): Promise<void> {
|
|||
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");
|
||||
assert(res.headers.has("access-control-allow-origin"));
|
||||
assert(res.headers.has("access-control-allow-headers"));
|
||||
assertEquals(res.status, 200);
|
||||
res.body.close();
|
||||
} finally {
|
||||
killFileServer();
|
||||
}
|
||||
|
@ -114,7 +116,8 @@ test(async function servePermissionDenied(): Promise<void> {
|
|||
assert(s !== Deno.EOF && s.includes("server listening"));
|
||||
|
||||
try {
|
||||
await fetch("http://localhost:4500/");
|
||||
const res = await fetch("http://localhost:4500/");
|
||||
res.body.close();
|
||||
assertStrContains(
|
||||
(await errReader.readLine()) as string,
|
||||
"run again with the --allow-read flag"
|
||||
|
|
|
@ -73,4 +73,5 @@ test(async function serverPipelineRace(): Promise<void> {
|
|||
assertEquals(s, outLines[i]);
|
||||
}
|
||||
killServer();
|
||||
conn.close();
|
||||
});
|
||||
|
|
|
@ -343,88 +343,101 @@ test(async function requestBodyReaderWithTransferEncoding(): Promise<void> {
|
|||
}
|
||||
});
|
||||
|
||||
test("destroyed connection", async (): Promise<void> => {
|
||||
// Runs a simple server as another process
|
||||
const p = Deno.run({
|
||||
args: [Deno.execPath(), "--allow-net", "http/testdata/simple_server.ts"],
|
||||
stdout: "piped"
|
||||
});
|
||||
|
||||
try {
|
||||
const r = new TextProtoReader(new BufReader(p.stdout!));
|
||||
const s = await r.readLine();
|
||||
assert(s !== Deno.EOF && s.includes("server listening"));
|
||||
test({
|
||||
name: "destroyed connection",
|
||||
// FIXME(bartlomieju): hangs on windows, cause can't do `Deno.kill`
|
||||
skip: true,
|
||||
fn: async (): Promise<void> => {
|
||||
// Runs a simple server as another process
|
||||
const p = Deno.run({
|
||||
args: [Deno.execPath(), "--allow-net", "http/testdata/simple_server.ts"],
|
||||
stdout: "piped"
|
||||
});
|
||||
|
||||
let serverIsRunning = true;
|
||||
p.status()
|
||||
const statusPromise = p
|
||||
.status()
|
||||
.then((): void => {
|
||||
serverIsRunning = false;
|
||||
})
|
||||
.catch((_): void => {}); // Ignores the error when closing the process.
|
||||
|
||||
await delay(100);
|
||||
|
||||
// Reqeusts to the server and immediately closes the connection
|
||||
const conn = await Deno.connect({ port: 4502 });
|
||||
await conn.write(new TextEncoder().encode("GET / HTTP/1.0\n\n"));
|
||||
conn.close();
|
||||
|
||||
// Waits for the server to handle the above (broken) request
|
||||
await delay(100);
|
||||
|
||||
assert(serverIsRunning);
|
||||
} finally {
|
||||
// Stops the sever.
|
||||
p.close();
|
||||
try {
|
||||
const r = new TextProtoReader(new BufReader(p.stdout!));
|
||||
const s = await r.readLine();
|
||||
assert(s !== Deno.EOF && s.includes("server listening"));
|
||||
await delay(100);
|
||||
// Reqeusts to the server and immediately closes the connection
|
||||
const conn = await Deno.connect({ port: 4502 });
|
||||
await conn.write(new TextEncoder().encode("GET / HTTP/1.0\n\n"));
|
||||
conn.close();
|
||||
// Waits for the server to handle the above (broken) request
|
||||
await delay(100);
|
||||
assert(serverIsRunning);
|
||||
} finally {
|
||||
// Stops the sever and allows `p.status()` promise to resolve
|
||||
Deno.kill(p.pid, Deno.Signal.SIGKILL);
|
||||
await statusPromise;
|
||||
p.stdout!.close();
|
||||
p.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("serveTLS", async (): Promise<void> => {
|
||||
// Runs a simple server as another process
|
||||
const p = Deno.run({
|
||||
args: [
|
||||
Deno.execPath(),
|
||||
"--allow-net",
|
||||
"--allow-read",
|
||||
"http/testdata/simple_https_server.ts"
|
||||
],
|
||||
stdout: "piped"
|
||||
});
|
||||
|
||||
try {
|
||||
const r = new TextProtoReader(new BufReader(p.stdout!));
|
||||
const s = await r.readLine();
|
||||
assert(
|
||||
s !== Deno.EOF && s.includes("server listening"),
|
||||
"server must be started"
|
||||
);
|
||||
test({
|
||||
name: "serveTLS",
|
||||
// FIXME(bartlomieju): hangs on windows, cause can't do `Deno.kill`
|
||||
skip: true,
|
||||
fn: async (): Promise<void> => {
|
||||
// Runs a simple server as another process
|
||||
const p = Deno.run({
|
||||
args: [
|
||||
Deno.execPath(),
|
||||
"--allow-net",
|
||||
"--allow-read",
|
||||
"http/testdata/simple_https_server.ts"
|
||||
],
|
||||
stdout: "piped"
|
||||
});
|
||||
|
||||
let serverIsRunning = true;
|
||||
p.status()
|
||||
const statusPromise = p
|
||||
.status()
|
||||
.then((): void => {
|
||||
serverIsRunning = false;
|
||||
})
|
||||
.catch((_): void => {}); // Ignores the error when closing the process.
|
||||
|
||||
// Requests to the server and immediately closes the connection
|
||||
const conn = await Deno.connectTLS({
|
||||
hostname: "localhost",
|
||||
port: 4503,
|
||||
certFile: "http/testdata/tls/RootCA.pem"
|
||||
});
|
||||
await Deno.writeAll(
|
||||
conn,
|
||||
new TextEncoder().encode("GET / HTTP/1.0\r\n\r\n")
|
||||
);
|
||||
const res = new Uint8Array(100);
|
||||
const nread = assertNotEOF(await conn.read(res));
|
||||
conn.close();
|
||||
const resStr = new TextDecoder().decode(res.subarray(0, nread));
|
||||
assert(resStr.includes("Hello HTTPS"));
|
||||
assert(serverIsRunning);
|
||||
} finally {
|
||||
// Stops the sever.
|
||||
p.close();
|
||||
try {
|
||||
const r = new TextProtoReader(new BufReader(p.stdout!));
|
||||
const s = await r.readLine();
|
||||
assert(
|
||||
s !== Deno.EOF && s.includes("server listening"),
|
||||
"server must be started"
|
||||
);
|
||||
// Requests to the server and immediately closes the connection
|
||||
const conn = await Deno.connectTLS({
|
||||
hostname: "localhost",
|
||||
port: 4503,
|
||||
certFile: "http/testdata/tls/RootCA.pem"
|
||||
});
|
||||
await Deno.writeAll(
|
||||
conn,
|
||||
new TextEncoder().encode("GET / HTTP/1.0\r\n\r\n")
|
||||
);
|
||||
const res = new Uint8Array(100);
|
||||
const nread = assertNotEOF(await conn.read(res));
|
||||
conn.close();
|
||||
const resStr = new TextDecoder().decode(res.subarray(0, nread));
|
||||
assert(resStr.includes("Hello HTTPS"));
|
||||
assert(serverIsRunning);
|
||||
} finally {
|
||||
// Stops the sever and allows `p.status()` promise to resolve
|
||||
Deno.kill(p.pid, Deno.Signal.SIGKILL);
|
||||
await statusPromise;
|
||||
p.stdout!.close();
|
||||
p.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -480,6 +493,9 @@ test({
|
|||
}
|
||||
}
|
||||
server.close();
|
||||
// Let event loop do another turn so server
|
||||
// finishes all pending ops.
|
||||
await delay(0);
|
||||
const resources = Deno.resources();
|
||||
assert(reqCount === 1);
|
||||
// Server should be gone
|
||||
|
|
|
@ -38,12 +38,16 @@ test("[io/tuil] copyBytes", function(): void {
|
|||
assertEquals(dst, Uint8Array.of(3, 4, 0, 0));
|
||||
});
|
||||
|
||||
test("[io/util] tempfile", async function(): Promise<void> {
|
||||
const f = await tempFile(".", {
|
||||
prefix: "prefix-",
|
||||
postfix: "-postfix"
|
||||
});
|
||||
const base = path.basename(f.filepath);
|
||||
assert(!!base.match(/^prefix-.+?-postfix$/));
|
||||
await remove(f.filepath);
|
||||
test({
|
||||
name: "[io/util] tempfile",
|
||||
fn: async function(): Promise<void> {
|
||||
const f = await tempFile(".", {
|
||||
prefix: "prefix-",
|
||||
postfix: "-postfix"
|
||||
});
|
||||
const base = path.basename(f.filepath);
|
||||
assert(!!base.match(/^prefix-.+?-postfix$/));
|
||||
f.file.close();
|
||||
await remove(f.filepath);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -98,6 +98,7 @@ test(async function multipartMultipartWriter(): Promise<void> {
|
|||
const f = await open(path.resolve("./mime/testdata/sample.txt"), "r");
|
||||
await mw.writeFile("file", "sample.txt", f);
|
||||
await mw.close();
|
||||
f.close();
|
||||
});
|
||||
|
||||
test(function multipartMultipartWriter2(): void {
|
||||
|
@ -185,6 +186,7 @@ test(async function multipartMultipartReader(): Promise<void> {
|
|||
const file = form["file"] as FormFile;
|
||||
assertEquals(isFormFile(file), true);
|
||||
assert(file.content !== void 0);
|
||||
o.close();
|
||||
});
|
||||
|
||||
test(async function multipartMultipartReader2(): Promise<void> {
|
||||
|
@ -211,5 +213,6 @@ test(async function multipartMultipartReader2(): Promise<void> {
|
|||
if (file.tempfile) {
|
||||
await remove(file.tempfile);
|
||||
}
|
||||
o.close();
|
||||
}
|
||||
});
|
||||
|
|
|
@ -71,8 +71,10 @@ test({
|
|||
name: "Async read returns one file at a time",
|
||||
async fn() {
|
||||
const testDir: string = Deno.makeTempDirSync();
|
||||
Deno.createSync(testDir + "/foo.txt");
|
||||
Deno.createSync(testDir + "/bar.txt");
|
||||
const f1 = Deno.createSync(testDir + "/foo.txt");
|
||||
f1.close();
|
||||
const f2 = Deno.createSync(testDir + "/bar.txt");
|
||||
f2.close();
|
||||
|
||||
try {
|
||||
let secondCallback = false;
|
||||
|
@ -108,8 +110,10 @@ test({
|
|||
name: "Sync read returns one file at a time",
|
||||
fn() {
|
||||
const testDir: string = Deno.makeTempDirSync();
|
||||
Deno.createSync(testDir + "/foo.txt");
|
||||
Deno.createSync(testDir + "/bar.txt");
|
||||
const f1 = Deno.createSync(testDir + "/foo.txt");
|
||||
f1.close();
|
||||
const f2 = Deno.createSync(testDir + "/bar.txt");
|
||||
f2.close();
|
||||
|
||||
try {
|
||||
const dir: Dir = new Dir(testDir);
|
||||
|
@ -135,8 +139,10 @@ test({
|
|||
name: "Async iteration over existing directory",
|
||||
async fn() {
|
||||
const testDir: string = Deno.makeTempDirSync();
|
||||
Deno.createSync(testDir + "/foo.txt");
|
||||
Deno.createSync(testDir + "/bar.txt");
|
||||
const f1 = Deno.createSync(testDir + "/foo.txt");
|
||||
f1.close();
|
||||
const f2 = Deno.createSync(testDir + "/bar.txt");
|
||||
f2.close();
|
||||
|
||||
try {
|
||||
const dir: Dir = new Dir(testDir);
|
||||
|
|
|
@ -15,40 +15,47 @@ if (Deno.build.os !== "win") {
|
|||
);
|
||||
});
|
||||
|
||||
test("signal() iterates for multiple signals", async (): Promise<void> => {
|
||||
// This prevents the program from exiting.
|
||||
const t = setInterval(() => {}, 1000);
|
||||
test({
|
||||
name: "signal() iterates for multiple signals",
|
||||
fn: async (): Promise<void> => {
|
||||
// This prevents the program from exiting.
|
||||
const t = setInterval(() => {}, 1000);
|
||||
|
||||
let c = 0;
|
||||
const sig = signal(
|
||||
Deno.Signal.SIGUSR1,
|
||||
Deno.Signal.SIGUSR2,
|
||||
Deno.Signal.SIGINT
|
||||
);
|
||||
let c = 0;
|
||||
const sig = signal(
|
||||
Deno.Signal.SIGUSR1,
|
||||
Deno.Signal.SIGUSR2,
|
||||
Deno.Signal.SIGINT
|
||||
);
|
||||
|
||||
setTimeout(async () => {
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
|
||||
await delay(20);
|
||||
sig.dispose();
|
||||
});
|
||||
setTimeout(async () => {
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
|
||||
await delay(20);
|
||||
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
|
||||
await delay(20);
|
||||
sig.dispose();
|
||||
});
|
||||
|
||||
for await (const _ of sig) {
|
||||
c += 1;
|
||||
for await (const _ of sig) {
|
||||
c += 1;
|
||||
}
|
||||
|
||||
assertEquals(c, 6);
|
||||
|
||||
clearTimeout(t);
|
||||
// Clear timeout clears interval, but interval promise is not
|
||||
// yet resolved, delay to next turn of event loop otherwise,
|
||||
// we'll be leaking resources.
|
||||
await delay(10);
|
||||
}
|
||||
|
||||
assertEquals(c, 6);
|
||||
|
||||
clearTimeout(t);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -3,56 +3,60 @@ import { bench, runBenchmarks } from "./bench.ts";
|
|||
|
||||
import "./bench_example.ts";
|
||||
|
||||
test(async function benching(): Promise<void> {
|
||||
bench(function forIncrementX1e9(b): void {
|
||||
b.start();
|
||||
for (let i = 0; i < 1e9; i++);
|
||||
b.stop();
|
||||
});
|
||||
test({
|
||||
name: "benching",
|
||||
|
||||
bench(function forDecrementX1e9(b): void {
|
||||
b.start();
|
||||
for (let i = 1e9; i > 0; i--);
|
||||
b.stop();
|
||||
});
|
||||
|
||||
bench(async function forAwaitFetchDenolandX10(b): Promise<void> {
|
||||
b.start();
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const r = await fetch("https://deno.land/");
|
||||
await r.text();
|
||||
}
|
||||
b.stop();
|
||||
});
|
||||
|
||||
bench(async function promiseAllFetchDenolandX10(b): Promise<void> {
|
||||
const urls = new Array(10).fill("https://deno.land/");
|
||||
b.start();
|
||||
await Promise.all(
|
||||
urls.map(
|
||||
async (denoland: string): Promise<void> => {
|
||||
const r = await fetch(denoland);
|
||||
await r.text();
|
||||
}
|
||||
)
|
||||
);
|
||||
b.stop();
|
||||
});
|
||||
|
||||
bench({
|
||||
name: "runs100ForIncrementX1e6",
|
||||
runs: 100,
|
||||
func(b): void {
|
||||
fn: async function(): Promise<void> {
|
||||
bench(function forIncrementX1e9(b): void {
|
||||
b.start();
|
||||
for (let i = 0; i < 1e6; i++);
|
||||
for (let i = 0; i < 1e9; i++);
|
||||
b.stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
bench(function throwing(b): void {
|
||||
b.start();
|
||||
// Throws bc the timer's stop method is never called
|
||||
});
|
||||
bench(function forDecrementX1e9(b): void {
|
||||
b.start();
|
||||
for (let i = 1e9; i > 0; i--);
|
||||
b.stop();
|
||||
});
|
||||
|
||||
await runBenchmarks({ skip: /throw/ });
|
||||
bench(async function forAwaitFetchDenolandX10(b): Promise<void> {
|
||||
b.start();
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const r = await fetch("https://deno.land/");
|
||||
await r.text();
|
||||
}
|
||||
b.stop();
|
||||
});
|
||||
|
||||
bench(async function promiseAllFetchDenolandX10(b): Promise<void> {
|
||||
const urls = new Array(10).fill("https://deno.land/");
|
||||
b.start();
|
||||
await Promise.all(
|
||||
urls.map(
|
||||
async (denoland: string): Promise<void> => {
|
||||
const r = await fetch(denoland);
|
||||
await r.text();
|
||||
}
|
||||
)
|
||||
);
|
||||
b.stop();
|
||||
});
|
||||
|
||||
bench({
|
||||
name: "runs100ForIncrementX1e6",
|
||||
runs: 100,
|
||||
func(b): void {
|
||||
b.start();
|
||||
for (let i = 0; i < 1e6; i++);
|
||||
b.stop();
|
||||
}
|
||||
});
|
||||
|
||||
bench(function throwing(b): void {
|
||||
b.start();
|
||||
// Throws bc the timer's stop method is never called
|
||||
});
|
||||
|
||||
await runBenchmarks({ skip: /throw/ });
|
||||
}
|
||||
});
|
||||
|
|
109
std/ws/test.ts
109
std/ws/test.ts
|
@ -21,6 +21,7 @@ import Writer = Deno.Writer;
|
|||
import Reader = Deno.Reader;
|
||||
import Conn = Deno.Conn;
|
||||
import Buffer = Deno.Buffer;
|
||||
import { delay } from "../util/async.ts";
|
||||
|
||||
test("[ws] read unmasked text frame", async () => {
|
||||
// unmasked single text frame with payload "Hello"
|
||||
|
@ -296,31 +297,32 @@ function delayedWriter(ms: number, dest: Writer): Writer {
|
|||
}
|
||||
};
|
||||
}
|
||||
test("[ws] WebSocket.send(), WebSocket.ping() should be exclusive", async (): Promise<
|
||||
void
|
||||
> => {
|
||||
const buf = new Buffer();
|
||||
const conn = dummyConn(new Buffer(), delayedWriter(1, buf));
|
||||
const sock = createWebSocket({ conn });
|
||||
// Ensure send call
|
||||
await Promise.all([
|
||||
sock.send("first"),
|
||||
sock.send("second"),
|
||||
sock.ping(),
|
||||
sock.send(new Uint8Array([3]))
|
||||
]);
|
||||
const bufr = new BufReader(buf);
|
||||
const first = await readFrame(bufr);
|
||||
const second = await readFrame(bufr);
|
||||
const ping = await readFrame(bufr);
|
||||
const third = await readFrame(bufr);
|
||||
assertEquals(first.opcode, OpCode.TextFrame);
|
||||
assertEquals(decode(first.payload), "first");
|
||||
assertEquals(first.opcode, OpCode.TextFrame);
|
||||
assertEquals(decode(second.payload), "second");
|
||||
assertEquals(ping.opcode, OpCode.Ping);
|
||||
assertEquals(third.opcode, OpCode.BinaryFrame);
|
||||
assertEquals(bytes.equal(third.payload, new Uint8Array([3])), true);
|
||||
test({
|
||||
name: "[ws] WebSocket.send(), WebSocket.ping() should be exclusive",
|
||||
fn: async (): Promise<void> => {
|
||||
const buf = new Buffer();
|
||||
const conn = dummyConn(new Buffer(), delayedWriter(1, buf));
|
||||
const sock = createWebSocket({ conn });
|
||||
// Ensure send call
|
||||
await Promise.all([
|
||||
sock.send("first"),
|
||||
sock.send("second"),
|
||||
sock.ping(),
|
||||
sock.send(new Uint8Array([3]))
|
||||
]);
|
||||
const bufr = new BufReader(buf);
|
||||
const first = await readFrame(bufr);
|
||||
const second = await readFrame(bufr);
|
||||
const ping = await readFrame(bufr);
|
||||
const third = await readFrame(bufr);
|
||||
assertEquals(first.opcode, OpCode.TextFrame);
|
||||
assertEquals(decode(first.payload), "first");
|
||||
assertEquals(first.opcode, OpCode.TextFrame);
|
||||
assertEquals(decode(second.payload), "second");
|
||||
assertEquals(ping.opcode, OpCode.Ping);
|
||||
assertEquals(third.opcode, OpCode.BinaryFrame);
|
||||
assertEquals(bytes.equal(third.payload, new Uint8Array([3])), true);
|
||||
}
|
||||
});
|
||||
|
||||
test("[ws] createSecKeyHasCorrectLength", () => {
|
||||
|
@ -363,29 +365,36 @@ test("[ws] WebSocket shouldn't throw `Deno.errors.UnexpectedEof` on recive()", a
|
|||
assertEquals(done, true);
|
||||
});
|
||||
|
||||
test("[ws] WebSocket should reject sending promise when connection reset forcely", async () => {
|
||||
const buf = new Buffer();
|
||||
let timer: number | undefined;
|
||||
const lazyWriter: Deno.Writer = {
|
||||
async write(_: Uint8Array): Promise<number> {
|
||||
return new Promise(resolve => {
|
||||
timer = setTimeout(() => resolve(0), 1000);
|
||||
});
|
||||
}
|
||||
};
|
||||
const conn = dummyConn(buf, lazyWriter);
|
||||
const sock = createWebSocket({ conn });
|
||||
const onError = (e: unknown): unknown => e;
|
||||
const p = Promise.all([
|
||||
sock.send("hello").catch(onError),
|
||||
sock.send(new Uint8Array([1, 2])).catch(onError),
|
||||
sock.ping().catch(onError)
|
||||
]);
|
||||
sock.closeForce();
|
||||
assertEquals(sock.isClosed, true);
|
||||
const [a, b, c] = await p;
|
||||
assert(a instanceof Deno.errors.ConnectionReset);
|
||||
assert(b instanceof Deno.errors.ConnectionReset);
|
||||
assert(c instanceof Deno.errors.ConnectionReset);
|
||||
clearTimeout(timer);
|
||||
test({
|
||||
name:
|
||||
"[ws] WebSocket should reject sending promise when connection reset forcely",
|
||||
fn: async () => {
|
||||
const buf = new Buffer();
|
||||
let timer: number | undefined;
|
||||
const lazyWriter: Deno.Writer = {
|
||||
async write(_: Uint8Array): Promise<number> {
|
||||
return new Promise(resolve => {
|
||||
timer = setTimeout(() => resolve(0), 1000);
|
||||
});
|
||||
}
|
||||
};
|
||||
const conn = dummyConn(buf, lazyWriter);
|
||||
const sock = createWebSocket({ conn });
|
||||
const onError = (e: unknown): unknown => e;
|
||||
const p = Promise.all([
|
||||
sock.send("hello").catch(onError),
|
||||
sock.send(new Uint8Array([1, 2])).catch(onError),
|
||||
sock.ping().catch(onError)
|
||||
]);
|
||||
sock.closeForce();
|
||||
assertEquals(sock.isClosed, true);
|
||||
const [a, b, c] = await p;
|
||||
assert(a instanceof Deno.errors.ConnectionReset);
|
||||
assert(b instanceof Deno.errors.ConnectionReset);
|
||||
assert(c instanceof Deno.errors.ConnectionReset);
|
||||
clearTimeout(timer);
|
||||
// Wait for another event loop turn for `timeout` op promise
|
||||
// to resolve, otherwise we'll get "op leak".
|
||||
await delay(10);
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue