-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.test.ts
82 lines (70 loc) · 1.95 KB
/
sort.test.ts
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
import { delay } from "./delay.ts";
import type { Result } from "./types.ts";
import { sort, sortSettled } from "./sort.ts";
import { assertEquals } from "./deps_test.ts";
Deno.test("sort()", async (t) => {
await t.step("return in order of settled", async () => {
const results: number[] = [];
for await (
const waited of sort(
[1000, 2000, 500, 1500].map((n, index) => delay(n).then(() => index)),
)
) {
results.push(waited);
}
assertEquals(results, [2, 0, 3, 1]);
});
await t.step("ignore errors", async () => {
const results: number[] = [];
for await (
const waited of sort(
[1000, 2000, 1500, 500].map(async (n, index) => {
if (index < 2) throw Error(`Error: ${index}`);
await delay(n);
return index;
}),
)
) {
results.push(waited);
}
assertEquals(results, [3, 2]);
});
});
Deno.test("sortSettled()", async (t) => {
await t.step("return in order of settled", async () => {
const results: Result<number>[] = [];
for await (
const waited of sortSettled(
[1000, 2000, 500, 1500].map((n, index) => delay(n).then(() => index)),
)
) {
results.push(waited);
}
assertEquals(results, [
{ success: true, value: 2 },
{ success: true, value: 0 },
{ success: true, value: 3 },
{ success: true, value: 1 },
]);
});
await t.step("catch errors", async () => {
const results: Result<number>[] = [];
for await (
const waited of sortSettled(
[1000, 2000, 1500, 500].map(async (n, index) => {
if (index < 2) throw `Error: ${index}`;
await delay(n);
return index;
}),
)
) {
results.push(waited);
}
assertEquals(results, [
{ success: false, reason: "Error: 0" },
{ success: false, reason: "Error: 1" },
{ success: true, value: 3 },
{ success: true, value: 2 },
]);
});
});