This commit is contained in:
2023-05-21 20:19:43 +08:00
parent 1dd7c21bb6
commit 022c98dec7
8 changed files with 227 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
import { assertEquals } from "std/testing/asserts.ts";
import { check_running } from "./pid_check.ts";
import { promiseState, sleep } from "./utils.ts";
import { asyncFilter, promiseState, PromiseStatus, sleep } from "./utils.ts";
Deno.test("promiseState_test", async () => {
const p1 = new Promise((res) => setTimeout(() => res(100), 100));
@@ -26,3 +26,19 @@ Deno.test("Pid_Test", async () => {
assertEquals(await check_running(Deno.pid), true);
}
});
Deno.test("asyncFilter_test", async () => {
const t = [3, 5];
const r = await asyncFilter(t, async (t) => {
await sleep(10);
return t === 3;
});
assertEquals(r, [3]);
const e = [new Promise<number>((res) => setTimeout(() => res(100), 100))];
const v = await asyncFilter(e, async (t) => {
const s = await promiseState(t);
return s.status === PromiseStatus.Pending;
});
assertEquals(v, e);
await Promise.allSettled(e);
});