Skip to content

Commit

Permalink
refactor(cli/tests): replace createResolvable with deferred (denoland…
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Nov 26, 2020
1 parent 2031418 commit e6685f0
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 114 deletions.
10 changes: 5 additions & 5 deletions cli/tests/unit/net_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
assertNotEquals,
assertThrows,
assertThrowsAsync,
createResolvable,
deferred,
unitTest,
} from "./test_util.ts";

Expand Down Expand Up @@ -306,7 +306,7 @@ unitTest(
unitTest(
{ perms: { net: true } },
async function netTcpListenIteratorBreakClosesResource(): Promise<void> {
const promise = createResolvable();
const promise = deferred();

async function iterate(listener: Deno.Listener): Promise<void> {
let i = 0;
Expand Down Expand Up @@ -440,7 +440,7 @@ unitTest(
async function netCloseWriteSuccess() {
const addr = { hostname: "127.0.0.1", port: 3500 };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
const closeDeferred = deferred();
listener.accept().then(async (conn) => {
await conn.write(new Uint8Array([1, 2, 3]));
await closeDeferred;
Expand Down Expand Up @@ -474,7 +474,7 @@ unitTest(
async function netDoubleCloseWrite() {
const addr = { hostname: "127.0.0.1", port: 3500 };
const listener = Deno.listen(addr);
const closeDeferred = createResolvable();
const closeDeferred = deferred();
listener.accept().then(async (conn) => {
await closeDeferred;
conn.close();
Expand All @@ -497,7 +497,7 @@ unitTest(
},
async function netHangsOnClose() {
let acceptedConn: Deno.Conn;
const resolvable = createResolvable();
const resolvable = deferred();

async function iteratorReq(listener: Deno.Listener): Promise<void> {
const p = new Uint8Array(10);
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/unit/performance_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
assert,
assertEquals,
assertThrows,
createResolvable,
deferred,
unitTest,
} from "./test_util.ts";

unitTest({ perms: { hrtime: false } }, async function performanceNow(): Promise<
void
> {
const resolvable = createResolvable();
const resolvable = deferred();
const start = performance.now();
setTimeout((): void => {
const end = performance.now();
Expand Down
6 changes: 3 additions & 3 deletions cli/tests/unit/signal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
assert,
assertEquals,
assertThrows,
createResolvable,
deferred,
unitTest,
} from "./test_util.ts";

Expand Down Expand Up @@ -106,7 +106,7 @@ unitTest(
unitTest(
{ ignore: Deno.build.os === "windows", perms: { run: true, net: true } },
async function signalStreamTest(): Promise<void> {
const resolvable = createResolvable();
const resolvable = deferred();
// This prevents the program from exiting.
const t = setInterval(() => {}, 1000);

Expand Down Expand Up @@ -137,7 +137,7 @@ unitTest(
unitTest(
{ ignore: Deno.build.os === "windows", perms: { run: true } },
async function signalPromiseTest(): Promise<void> {
const resolvable = createResolvable();
const resolvable = deferred();
// This prevents the program from exiting.
const t = setInterval(() => {}, 1000);

Expand Down
19 changes: 1 addition & 18 deletions cli/tests/unit/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
fail,
unreachable,
} from "../../../std/testing/asserts.ts";
export { deferred } from "../../../std/async/deferred.ts";
export { readLines } from "../../../std/io/bufio.ts";
export { parse as parseArgs } from "../../../std/flags/mod.ts";

Expand Down Expand Up @@ -187,24 +188,6 @@ export function unitTest(
REGISTERED_UNIT_TESTS.push(unitTestDefinition);
}

export interface ResolvableMethods<T> {
resolve: (value: T | PromiseLike<T>) => void;
// deno-lint-ignore no-explicit-any
reject: (reason?: any) => void;
}

export type Resolvable<T> = Promise<T> & ResolvableMethods<T>;

export function createResolvable<T = void>(): Resolvable<T> {
let methods: ResolvableMethods<T>;
const promise = new Promise<T>((resolve, reject): void => {
methods = { resolve, reject };
});
// TypeScript doesn't know that the Promise callback occurs synchronously
// therefore use of not null assertion (`!`)
return Object.assign(promise, methods!) as Resolvable<T>;
}

const encoder = new TextEncoder();

// Replace functions with null, errors with their stack strings, and JSONify.
Expand Down
20 changes: 10 additions & 10 deletions cli/tests/unit/timers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
assert,
assertEquals,
assertNotEquals,
createResolvable,
deferred,
unitTest,
} from "./test_util.ts";

Expand All @@ -12,7 +12,7 @@ function waitForMs(ms: number): Promise<number> {
}

unitTest(async function timeoutSuccess(): Promise<void> {
const promise = createResolvable();
const promise = deferred();
let count = 0;
setTimeout((): void => {
count++;
Expand All @@ -24,7 +24,7 @@ unitTest(async function timeoutSuccess(): Promise<void> {
});

unitTest(async function timeoutArgs(): Promise<void> {
const promise = createResolvable();
const promise = deferred();
const arg = 1;
setTimeout(
(a, b, c): void => {
Expand Down Expand Up @@ -79,7 +79,7 @@ unitTest(async function timeoutCancelMultiple(): Promise<void> {

unitTest(async function timeoutCancelInvalidSilentFail(): Promise<void> {
// Expect no panic
const promise = createResolvable();
const promise = deferred();
let count = 0;
const id = setTimeout((): void => {
count++;
Expand All @@ -95,7 +95,7 @@ unitTest(async function timeoutCancelInvalidSilentFail(): Promise<void> {
});

unitTest(async function intervalSuccess(): Promise<void> {
const promise = createResolvable();
const promise = deferred();
let count = 0;
const id = setInterval((): void => {
count++;
Expand Down Expand Up @@ -155,7 +155,7 @@ unitTest(async function fireCallbackImmediatelyWhenDelayOverMaxValue(): Promise<
});

unitTest(async function timeoutCallbackThis(): Promise<void> {
const promise = createResolvable();
const promise = deferred();
const obj = {
foo(): void {
assertEquals(this, window);
Expand All @@ -182,7 +182,7 @@ unitTest(async function timeoutBindThis(): Promise<void> {
];

for (const thisArg of thisCheckPassed) {
const resolvable = createResolvable();
const resolvable = deferred();
let hasThrown = 0;
try {
setTimeout.call(thisArg, () => resolvable.resolve(), 1);
Expand Down Expand Up @@ -286,7 +286,7 @@ unitTest(async function timerMaxCpuBug(): Promise<void> {
unitTest(async function timerBasicMicrotaskOrdering(): Promise<void> {
let s = "";
let count = 0;
const promise = createResolvable();
const promise = deferred();
setTimeout(() => {
Promise.resolve().then(() => {
count++;
Expand All @@ -309,7 +309,7 @@ unitTest(async function timerBasicMicrotaskOrdering(): Promise<void> {

unitTest(async function timerNestedMicrotaskOrdering(): Promise<void> {
let s = "";
const promise = createResolvable();
const promise = deferred();
s += "0";
setTimeout(() => {
s += "4";
Expand Down Expand Up @@ -349,7 +349,7 @@ unitTest(function testQueueMicrotask() {

unitTest(async function timerIgnoresDateOverride(): Promise<void> {
const OriginalDate = Date;
const promise = createResolvable();
const promise = deferred();
let hasThrown = 0;
try {
const overrideCalled: () => number = () => {
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/unit/tls_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
assertEquals,
assertThrows,
assertThrowsAsync,
createResolvable,
deferred,
unitTest,
} from "./test_util.ts";
import { BufReader, BufWriter } from "../../../std/io/bufio.ts";
Expand Down Expand Up @@ -121,7 +121,7 @@ unitTest(
unitTest(
{ perms: { read: true, net: true } },
async function dialAndListenTLS(): Promise<void> {
const resolvable = createResolvable();
const resolvable = deferred();
const hostname = "localhost";
const port = 3500;

Expand Down
40 changes: 20 additions & 20 deletions cli/tests/websocket_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
assert,
assertEquals,
assertThrows,
createResolvable,
fail,
} from "./unit/test_util.ts";
} from "../../std/testing/asserts.ts";
import { deferred } from "../../std/async/deferred.ts";

Deno.test("invalid scheme", () => {
assertThrows(() => new WebSocket("foo:https://localhost:4242"));
Expand All @@ -21,7 +21,7 @@ Deno.test("duplicate protocols", () => {
});

Deno.test("invalid server", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:2121");
let err = false;
ws.onerror = (): void => {
Expand All @@ -39,7 +39,7 @@ Deno.test("invalid server", async () => {
});

Deno.test("connect & close", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.onerror = (): void => fail();
ws.onopen = (): void => {
Expand All @@ -52,7 +52,7 @@ Deno.test("connect & close", async () => {
});

Deno.test("connect & abort", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.close();
let err = false;
Expand All @@ -71,7 +71,7 @@ Deno.test("connect & abort", async () => {
});

Deno.test("connect & close custom valid code", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.onerror = (): void => fail();
ws.onopen = (): void => ws.close(1000);
Expand All @@ -82,7 +82,7 @@ Deno.test("connect & close custom valid code", async () => {
});

Deno.test("connect & close custom invalid code", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.onerror = (): void => fail();
ws.onopen = (): void => {
Expand All @@ -96,7 +96,7 @@ Deno.test("connect & close custom invalid code", async () => {
});

Deno.test("connect & close custom valid reason", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.onerror = (): void => fail();
ws.onopen = (): void => ws.close(1000, "foo");
Expand All @@ -107,7 +107,7 @@ Deno.test("connect & close custom valid reason", async () => {
});

Deno.test("connect & close custom invalid reason", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.onerror = (): void => fail();
ws.onopen = (): void => {
Expand All @@ -121,7 +121,7 @@ Deno.test("connect & close custom invalid reason", async () => {
});

Deno.test("echo string", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.onerror = (): void => fail();
ws.onopen = (): void => ws.send("foo");
Expand All @@ -136,8 +136,8 @@ Deno.test("echo string", async () => {
});

Deno.test("echo string tls", async () => {
const promise1 = createResolvable();
const promise2 = createResolvable();
const promise1 = deferred();
const promise2 = deferred();
const ws = new WebSocket("wss:https://localhost:4243");
ws.onerror = (): void => fail();
ws.onopen = (): void => ws.send("foo");
Expand All @@ -154,7 +154,7 @@ Deno.test("echo string tls", async () => {
});

Deno.test("websocket error", async () => {
const promise1 = createResolvable();
const promise1 = deferred();
const ws = new WebSocket("wss:https://localhost:4242");
ws.onopen = () => fail();
ws.onerror = (err): void => {
Expand All @@ -166,7 +166,7 @@ Deno.test("websocket error", async () => {
});

Deno.test("echo blob with binaryType blob", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
const blob = new Blob(["foo"]);
ws.onerror = (): void => fail();
Expand All @@ -186,7 +186,7 @@ Deno.test("echo blob with binaryType blob", async () => {
});

Deno.test("echo blob with binaryType arraybuffer", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.binaryType = "arraybuffer";
const blob = new Blob(["foo"]);
Expand All @@ -205,7 +205,7 @@ Deno.test("echo blob with binaryType arraybuffer", async () => {
});

Deno.test("echo uint8array with binaryType blob", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
const uint = new Uint8Array([102, 111, 111]);
ws.onerror = (): void => fail();
Expand All @@ -223,7 +223,7 @@ Deno.test("echo uint8array with binaryType blob", async () => {
});

Deno.test("echo uint8array with binaryType arraybuffer", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.binaryType = "arraybuffer";
const uint = new Uint8Array([102, 111, 111]);
Expand All @@ -240,7 +240,7 @@ Deno.test("echo uint8array with binaryType arraybuffer", async () => {
});

Deno.test("echo arraybuffer with binaryType blob", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
const buffer = new ArrayBuffer(3);
ws.onerror = (): void => fail();
Expand All @@ -258,7 +258,7 @@ Deno.test("echo arraybuffer with binaryType blob", async () => {
});

Deno.test("echo arraybuffer with binaryType arraybuffer", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
ws.binaryType = "arraybuffer";
const buffer = new ArrayBuffer(3);
Expand All @@ -275,7 +275,7 @@ Deno.test("echo arraybuffer with binaryType arraybuffer", async () => {
});

Deno.test("Event Handlers order", async () => {
const promise = createResolvable();
const promise = deferred();
const ws = new WebSocket("ws:https://localhost:4242");
const arr: number[] = [];
ws.onerror = (): void => fail();
Expand Down
Loading

0 comments on commit e6685f0

Please sign in to comment.