2021-01-11 02:59:07 +00:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-25 12:57:31 +00:00
|
|
|
import { delay } from "./delay.ts";
|
2021-08-17 05:04:21 +00:00
|
|
|
import { assert, assertRejects } from "../testing/asserts.ts";
|
2020-09-25 12:57:31 +00:00
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("[async] delay", async function () {
|
2020-09-25 12:57:31 +00:00
|
|
|
const start = new Date();
|
|
|
|
const delayedPromise = delay(100);
|
|
|
|
const result = await delayedPromise;
|
|
|
|
const diff = new Date().getTime() - start.getTime();
|
|
|
|
assert(result === undefined);
|
|
|
|
assert(diff >= 100);
|
|
|
|
});
|
2021-08-17 05:04:21 +00:00
|
|
|
|
|
|
|
Deno.test("[async] delay with abort", async function () {
|
|
|
|
const start = new Date();
|
|
|
|
const abort = new AbortController();
|
|
|
|
const { signal } = abort;
|
|
|
|
const delayedPromise = delay(100, { signal });
|
|
|
|
setTimeout(() => abort.abort(), 0);
|
2021-09-15 15:35:21 +00:00
|
|
|
await assertRejects(
|
2021-08-17 05:04:21 +00:00
|
|
|
() => delayedPromise,
|
|
|
|
DOMException,
|
|
|
|
"Delay was aborted",
|
|
|
|
);
|
|
|
|
|
|
|
|
const diff = new Date().getTime() - start.getTime();
|
|
|
|
assert(diff < 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[async] delay with non-aborted signal", async function () {
|
|
|
|
const start = new Date();
|
|
|
|
const abort = new AbortController();
|
|
|
|
const { signal } = abort;
|
|
|
|
const delayedPromise = delay(100, { signal });
|
|
|
|
// abort.abort()
|
|
|
|
const result = await delayedPromise;
|
|
|
|
const diff = new Date().getTime() - start.getTime();
|
|
|
|
assert(result === undefined);
|
|
|
|
assert(diff >= 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[async] delay with signal aborted after delay", async function () {
|
|
|
|
const start = new Date();
|
|
|
|
const abort = new AbortController();
|
|
|
|
const { signal } = abort;
|
|
|
|
const delayedPromise = delay(100, { signal });
|
|
|
|
const result = await delayedPromise;
|
|
|
|
abort.abort();
|
|
|
|
const diff = new Date().getTime() - start.getTime();
|
|
|
|
assert(result === undefined);
|
|
|
|
assert(diff >= 100);
|
|
|
|
});
|