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";
|
2022-05-19 05:05:49 +00:00
|
|
|
import { ERROR_WHILE_MAPPING_MESSAGE, 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,
|
2023-07-13 07:04:30 +00:00
|
|
|
} from "../assert/mod.ts";
|
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],
|
2023-05-02 05:48:33 +00:00
|
|
|
(i) => new Promise<number>((r) => setTimeout(() => r(i), 1000)),
|
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();
|
|
|
|
assert(diff >= 2000);
|
|
|
|
assert(diff < 3000);
|
|
|
|
});
|
|
|
|
|
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,
|
|
|
|
ERROR_WHILE_MAPPING_MESSAGE,
|
|
|
|
);
|
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
|
|
|
function getRandomInt(min: number, max: number): number {
|
|
|
|
min = Math.ceil(min);
|
|
|
|
max = Math.floor(max);
|
|
|
|
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = pooledMap(
|
|
|
|
2,
|
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
|
|
|
(i) =>
|
2023-05-02 05:48:33 +00:00
|
|
|
new Promise<number>((r) =>
|
|
|
|
setTimeout(() => r(i), getRandomInt(5, 20) * 100)
|
|
|
|
),
|
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;
|
|
|
|
}
|
|
|
|
});
|