Files
plainleaf/lib/async.test.ts
T
MaksandGitHub 8a3782e946 improve dependency management (#770)
* use import_map.json instead of per-file import

* remove test_deps.ts in favor of import_map

* remove common/deps.ts in favor of import_map

* use import_map instead of server/deps.ts

* make all plug-api files usable without an import_map

* make all lib files usable without an import_map

* remove web/deps.ts in favor of import_map

* fix plug build

* remove plug-api/deps.ts in favor of lib/deps.ts

* fix server deps bundled in client deps
2024-03-16 15:29:24 +01:00

43 lines
1.3 KiB
TypeScript

import { assert, assertEquals } from "$std/testing/asserts.ts";
import { batchRequests, PromiseQueue, sleep } from "./async.ts";
Deno.test("PromiseQueue test", async () => {
const q = new PromiseQueue();
let r1RanFirst = false;
const r1 = q.runInQueue(async () => {
await sleep(10);
r1RanFirst = true;
// console.log("1");
return 1;
});
const r2 = q.runInQueue(async () => {
// console.log("2");
await sleep(4);
return 2;
});
assertEquals(await Promise.all([r1, r2]), [1, 2]);
assertEquals(r1RanFirst, true);
let wasRun = false;
await q.runInQueue(async () => {
await sleep(4);
wasRun = true;
});
assertEquals(wasRun, true);
});
Deno.test("Batch test", async () => {
// Generate an array with numbers up to 100
const elements = Array.from(Array(100).keys());
const multiplied = await batchRequests(elements, async (batch) => {
await sleep(2);
// Batches should be 9 or smaller (last batch will be smaller)
assert(batch.length <= 9);
return batch.map((e) => e * 2);
}, 9);
assertEquals(multiplied, elements.map((e) => e * 2));
const multiplied2 = await batchRequests(elements, async (batch) => {
return batch.map((e) => e * 2);
}, 10000);
assertEquals(multiplied2, elements.map((e) => e * 2));
});