summaryrefslogtreecommitdiff
path: root/cli/tests/unit
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests/unit')
-rw-r--r--cli/tests/unit/net_test.ts10
-rw-r--r--cli/tests/unit/performance_test.ts4
-rw-r--r--cli/tests/unit/signal_test.ts6
-rw-r--r--cli/tests/unit/test_util.ts19
-rw-r--r--cli/tests/unit/timers_test.ts20
-rw-r--r--cli/tests/unit/tls_test.ts4
6 files changed, 23 insertions, 40 deletions
diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts
index 6165e77f6..b1f4f87ca 100644
--- a/cli/tests/unit/net_test.ts
+++ b/cli/tests/unit/net_test.ts
@@ -5,7 +5,7 @@ import {
assertNotEquals,
assertThrows,
assertThrowsAsync,
- createResolvable,
+ deferred,
unitTest,
} from "./test_util.ts";
@@ -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;
@@ -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;
@@ -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();
@@ -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);
diff --git a/cli/tests/unit/performance_test.ts b/cli/tests/unit/performance_test.ts
index 122938b36..2b8bc620d 100644
--- a/cli/tests/unit/performance_test.ts
+++ b/cli/tests/unit/performance_test.ts
@@ -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();
diff --git a/cli/tests/unit/signal_test.ts b/cli/tests/unit/signal_test.ts
index 2ea1e865f..4f97a82e4 100644
--- a/cli/tests/unit/signal_test.ts
+++ b/cli/tests/unit/signal_test.ts
@@ -3,7 +3,7 @@ import {
assert,
assertEquals,
assertThrows,
- createResolvable,
+ deferred,
unitTest,
} from "./test_util.ts";
@@ -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);
@@ -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);
diff --git a/cli/tests/unit/test_util.ts b/cli/tests/unit/test_util.ts
index 6163865de..39a1949c3 100644
--- a/cli/tests/unit/test_util.ts
+++ b/cli/tests/unit/test_util.ts
@@ -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";
@@ -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.
diff --git a/cli/tests/unit/timers_test.ts b/cli/tests/unit/timers_test.ts
index af876b742..e315d018a 100644
--- a/cli/tests/unit/timers_test.ts
+++ b/cli/tests/unit/timers_test.ts
@@ -3,7 +3,7 @@ import {
assert,
assertEquals,
assertNotEquals,
- createResolvable,
+ deferred,
unitTest,
} from "./test_util.ts";
@@ -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++;
@@ -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 => {
@@ -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++;
@@ -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++;
@@ -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);
@@ -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);
@@ -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++;
@@ -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";
@@ -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 = () => {
diff --git a/cli/tests/unit/tls_test.ts b/cli/tests/unit/tls_test.ts
index 058abf5d7..9e1c66161 100644
--- a/cli/tests/unit/tls_test.ts
+++ b/cli/tests/unit/tls_test.ts
@@ -4,7 +4,7 @@ import {
assertEquals,
assertThrows,
assertThrowsAsync,
- createResolvable,
+ deferred,
unitTest,
} from "./test_util.ts";
import { BufReader, BufWriter } from "../../../std/io/bufio.ts";
@@ -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;