2022-02-02 14:21:39 +00:00
|
|
|
// Copyright 2018-2022 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,
|
|
|
|
} from "../testing/asserts.ts";
|
2020-07-29 00:44:34 +00:00
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("[async] pooledMap", async function () {
|
2020-07-29 00:44:34 +00:00
|
|
|
const start = new Date();
|
|
|
|
const results = pooledMap(
|
|
|
|
2,
|
|
|
|
[1, 2, 3],
|
|
|
|
(i) => new Promise((r) => setTimeout(() => r(i), 1000)),
|
|
|
|
);
|
2022-05-22 14:18:10 +00:00
|
|
|
const array = [];
|
2020-07-29 00:44:34 +00:00
|
|
|
for await (const value of results) {
|
2022-05-22 14:18:10 +00:00
|
|
|
array.push(value);
|
2020-07-29 00:44:34 +00:00
|
|
|
}
|
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);
|
|
|
|
});
|
|
|
|
|
2022-05-19 05:05:49 +00:00
|
|
|
Deno.test("[async] pooledMap 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[] = [];
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(async () => {
|
2021-09-15 15:35:21 +00:00
|
|
|
for await (const m of pooledMap(3, [1, 2, 3, 4], mapNumber)) {
|
|
|
|
mappedNumbers.push(m);
|
2021-01-22 11:45:29 +00:00
|
|
|
}
|
2021-09-15 15:35:21 +00:00
|
|
|
}, (error: Error) => {
|
|
|
|
assert(error instanceof AggregateError);
|
2022-05-19 05:05:49 +00:00
|
|
|
assert(error.message === ERROR_WHILE_MAPPING_MESSAGE);
|
2021-09-15 15:35:21 +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
|
|
|
|
|
|
|
Deno.test("pooledMap returns ordered items", async () => {
|
|
|
|
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) =>
|
|
|
|
new Promise((r) => setTimeout(() => r(i), getRandomInt(5, 20) * 100)),
|
|
|
|
);
|
|
|
|
|
|
|
|
const returned = [];
|
|
|
|
for await (const value of results) {
|
|
|
|
returned.push(value);
|
|
|
|
}
|
|
|
|
assertEquals(returned, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
|
|
|
});
|