deno.land / std@0.224.0 / async / pool_test.ts

pool_test.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { delay } from "./delay.ts";import { ERROR_WHILE_MAPPING_MESSAGE, pooledMap } from "./pool.ts";import { assert, assertEquals, assertRejects, assertStringIncludes,} from "../assert/mod.ts";
Deno.test("pooledMap()", async () => { const start = new Date(); const results = pooledMap( 2, [1, 2, 3], (i) => new Promise<number>((r) => setTimeout(() => r(i), 1000)), ); const array = await Array.fromAsync(results); assertEquals(array, [1, 2, 3]); const diff = new Date().getTime() - start.getTime(); assert(diff >= 2000); assert(diff < 3000);});
Deno.test("pooledMap() handles errors", async () => { async function mapNumber(n: number): Promise<number> { if (n <= 2) { throw new Error(`Bad number: ${n}`); } await delay(100); return n; } const mappedNumbers: number[] = []; 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, ); assertEquals(error.errors.length, 2); assertStringIncludes(error.errors[0].stack, "Error: Bad number: 1"); assertStringIncludes(error.errors[1].stack, "Error: Bad number: 2"); assertEquals(mappedNumbers, [3]);});
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<number>((r) => setTimeout(() => r(i), getRandomInt(5, 20) * 100) ), );
const returned = await Array.fromAsync(results); assertEquals(returned, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);});
Deno.test("pooledMap() checks browser compat", async () => { // 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)), ); const array = await Array.fromAsync(results); assertEquals(array, [1, 2, 3]); } finally { ReadableStream.prototype[Symbol.asyncIterator] = asyncIterFunc; }});
std

Version Info

Tagged at
6 months ago