2023-01-03 10:47:44 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { assertEquals, assertRejects } from "../assert/mod.ts";
|
2022-02-21 04:51:39 +00:00
|
|
|
import { abortable } from "./abortable.ts";
|
|
|
|
|
|
|
|
Deno.test("[async] abortable (Promise)", async () => {
|
|
|
|
const c = new AbortController();
|
2023-11-10 03:31:16 +00:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
|
|
|
const t = setTimeout(() => resolve("Hello"), 100);
|
|
|
|
const result = await abortable(promise, c.signal);
|
2022-02-21 04:51:39 +00:00
|
|
|
assertEquals(result, "Hello");
|
|
|
|
clearTimeout(t);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[async] abortable (Promise) with signal aborted after delay", async () => {
|
|
|
|
const c = new AbortController();
|
2023-11-10 03:31:16 +00:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
|
|
|
const t = setTimeout(() => resolve("Hello"), 100);
|
2022-02-21 04:51:39 +00:00
|
|
|
setTimeout(() => c.abort(), 50);
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
2023-11-10 03:31:16 +00:00
|
|
|
await abortable(promise, c.signal);
|
2022-02-21 04:51:39 +00:00
|
|
|
},
|
|
|
|
DOMException,
|
|
|
|
"AbortError",
|
|
|
|
);
|
|
|
|
clearTimeout(t);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[async] abortable (Promise) with already aborted signal", async () => {
|
|
|
|
const c = new AbortController();
|
2023-11-10 03:31:16 +00:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
|
|
|
const t = setTimeout(() => resolve("Hello"), 100);
|
2022-02-21 04:51:39 +00:00
|
|
|
c.abort();
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
2023-11-10 03:31:16 +00:00
|
|
|
await abortable(promise, c.signal);
|
2022-02-21 04:51:39 +00:00
|
|
|
},
|
|
|
|
DOMException,
|
|
|
|
"AbortError",
|
|
|
|
);
|
|
|
|
clearTimeout(t);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[async] abortable (AsyncIterable)", async () => {
|
|
|
|
const c = new AbortController();
|
2023-11-10 03:31:16 +00:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
|
|
|
const t = setTimeout(() => resolve("Hello"), 100);
|
2022-02-21 04:51:39 +00:00
|
|
|
const a = async function* () {
|
|
|
|
yield "Hello";
|
2023-11-10 03:31:16 +00:00
|
|
|
await promise;
|
2022-02-21 04:51:39 +00:00
|
|
|
yield "World";
|
|
|
|
};
|
2023-11-10 19:00:28 +00:00
|
|
|
const items = await Array.fromAsync(abortable(a(), c.signal));
|
2022-02-21 04:51:39 +00:00
|
|
|
assertEquals(items, ["Hello", "World"]);
|
|
|
|
clearTimeout(t);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[async] abortable (AsyncIterable) with signal aborted after delay", async () => {
|
|
|
|
const c = new AbortController();
|
2023-11-10 03:31:16 +00:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
|
|
|
const t = setTimeout(() => resolve("Hello"), 100);
|
2022-02-21 04:51:39 +00:00
|
|
|
const a = async function* () {
|
|
|
|
yield "Hello";
|
2023-11-10 03:31:16 +00:00
|
|
|
await promise;
|
2022-02-21 04:51:39 +00:00
|
|
|
yield "World";
|
|
|
|
};
|
|
|
|
setTimeout(() => c.abort(), 50);
|
|
|
|
const items: string[] = [];
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
for await (const item of abortable(a(), c.signal)) {
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DOMException,
|
|
|
|
"AbortError",
|
|
|
|
);
|
|
|
|
assertEquals(items, ["Hello"]);
|
|
|
|
clearTimeout(t);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[async] abortable (AsyncIterable) with already aborted signal", async () => {
|
|
|
|
const c = new AbortController();
|
2023-11-10 03:31:16 +00:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
|
|
|
const t = setTimeout(() => resolve("Hello"), 100);
|
2022-02-21 04:51:39 +00:00
|
|
|
const a = async function* () {
|
|
|
|
yield "Hello";
|
2023-11-10 03:31:16 +00:00
|
|
|
await promise;
|
2022-02-21 04:51:39 +00:00
|
|
|
yield "World";
|
|
|
|
};
|
|
|
|
c.abort();
|
|
|
|
const items: string[] = [];
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
for await (const item of abortable(a(), c.signal)) {
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DOMException,
|
|
|
|
"AbortError",
|
|
|
|
);
|
|
|
|
assertEquals(items, []);
|
|
|
|
clearTimeout(t);
|
|
|
|
});
|