2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-01-22 11:45:29 +00:00
|
|
|
import { delay } from "./delay.ts";
|
2024-06-14 10:15:35 +00:00
|
|
|
import { pooledMap } from "./pool.ts";
|
2021-01-22 11:45:29 +00:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-08-09 05:59:52 +00:00
|
|
|
assertRejects,
|
2021-01-22 11:45:29 +00:00
|
|
|
assertStringIncludes,
|
2024-04-29 02:57:30 +00:00
|
|
|
} from "@std/assert";
|
2020-07-29 00:44:34 +00:00
|
|
|
|
2024-04-11 21:23:54 +00:00
|
|
|
Deno.test("pooledMap()", async () => {
|
2020-07-29 00:44:34 +00:00
|
|
|
const start = new Date();
|
|
|
|
const results = pooledMap(
|
|
|
|
2,
|
|
|
|
[1, 2, 3],
|
2024-08-02 03:32:55 +00:00
|
|
|
(i) => new Promise<number>((r) => setTimeout(() => r(i), 100)),
|
2020-07-29 00:44:34 +00:00
|
|
|
);
|
2023-11-10 19:00:28 +00:00
|
|
|
const array = await Array.fromAsync(results);
|
2022-05-22 14:18:10 +00:00
|
|
|
assertEquals(array, [1, 2, 3]);
|
2020-07-29 00:44:34 +00:00
|
|
|
const diff = new Date().getTime() - start.getTime();
|
2024-08-02 03:32:55 +00:00
|
|
|
assert(diff >= 200);
|
|
|
|
assert(diff < 300);
|
2020-07-29 00:44:34 +00:00
|
|
|
});
|
|
|
|
|
2023-12-18 22:29:13 +00:00
|
|
|
Deno.test("pooledMap() handles errors", async () => {
|
2021-01-22 11:45:29 +00:00
|
|
|
async function mapNumber(n: number): Promise<number> {
|
|
|
|
if (n <= 2) {
|
|
|
|
throw new Error(`Bad number: ${n}`);
|
|
|
|
}
|
|
|
|
await delay(100);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
const mappedNumbers: number[] = [];
|
2023-11-10 19:00:28 +00:00
|
|
|
const error = await assertRejects(
|
|
|
|
async () => {
|
|
|
|
for await (const m of pooledMap(3, [1, 2, 3, 4], mapNumber)) {
|
|
|
|
mappedNumbers.push(m);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
AggregateError,
|
2024-08-22 06:02:58 +00:00
|
|
|
"Cannot complete the mapping as an error was thrown from an item",
|
2023-11-10 19:00:28 +00:00
|
|
|
);
|
2022-05-30 10:21:28 +00:00
|
|
|
assertEquals(error.errors.length, 2);
|
|
|
|
assertStringIncludes(error.errors[0].stack, "Error: Bad number: 1");
|
|
|
|
assertStringIncludes(error.errors[1].stack, "Error: Bad number: 2");
|
2021-01-22 11:45:29 +00:00
|
|
|
assertEquals(mappedNumbers, [3]);
|
|
|
|
});
|
2022-05-19 05:05:49 +00:00
|
|
|
|
2023-12-18 22:29:13 +00:00
|
|
|
Deno.test("pooledMap() returns ordered items", async () => {
|
2022-05-19 05:05:49 +00:00
|
|
|
const results = pooledMap(
|
|
|
|
2,
|
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
2024-08-02 03:32:55 +00:00
|
|
|
(i) => new Promise<number>((r) => setTimeout(() => r(i), 100 / i)),
|
2022-05-19 05:05:49 +00:00
|
|
|
);
|
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
const returned = await Array.fromAsync(results);
|
2022-05-19 05:05:49 +00:00
|
|
|
assertEquals(returned, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
|
|
|
});
|
2023-05-02 05:48:33 +00:00
|
|
|
|
2024-04-11 21:23:54 +00:00
|
|
|
Deno.test("pooledMap() checks browser compat", async () => {
|
2023-05-02 05:48:33 +00:00
|
|
|
// Simulates the environment where Symbol.asyncIterator is not available
|
|
|
|
const asyncIterFunc = ReadableStream.prototype[Symbol.asyncIterator];
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
delete (ReadableStream.prototype as any)[Symbol.asyncIterator];
|
|
|
|
try {
|
|
|
|
const results = pooledMap(
|
|
|
|
2,
|
|
|
|
[1, 2, 3],
|
|
|
|
(i) => new Promise<number>((r) => setTimeout(() => r(i), 100)),
|
|
|
|
);
|
2023-11-10 19:00:28 +00:00
|
|
|
const array = await Array.fromAsync(results);
|
2023-05-02 05:48:33 +00:00
|
|
|
assertEquals(array, [1, 2, 3]);
|
|
|
|
} finally {
|
|
|
|
ReadableStream.prototype[Symbol.asyncIterator] = asyncIterFunc;
|
|
|
|
}
|
|
|
|
});
|