deno.land / std@0.224.0 / async / tee_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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { tee } from "./tee.ts";import { assertEquals } from "../assert/mod.ts";
/** An example async generator */const gen = async function* iter() { yield 1; yield 2; yield 3;};
Deno.test("tee() handles 2 branches", async () => { const iter = gen(); const [res0, res1] = tee(iter).map(async (src) => await Array.fromAsync(src)); assertEquals( await Promise.all([res0, res1]), [ [1, 2, 3], [1, 2, 3], ], );});
Deno.test("tee() handles 3 branches with immediate consumption", async () => { const iter = gen(); const [res0, res1, res2] = tee(iter, 3).map(async (src) => await Array.fromAsync(src) ); assertEquals( await Promise.all([res0, res1, res2]), [ [1, 2, 3], [1, 2, 3], [1, 2, 3], ], );});
Deno.test("tee() handles 3 branches with delayed consumption", async () => { const iter = gen(); const iters = tee(iter, 3);
await new Promise<void>((resolve) => { setTimeout(() => resolve(), 20); });
assertEquals( await Promise.all(iters.map(async (src) => await Array.fromAsync(src))), [ [1, 2, 3], [1, 2, 3], [1, 2, 3], ], );});
Deno.test("tee() handles concurrent next() calls", async () => { const [left] = tee(gen()); const l = left[Symbol.asyncIterator](); assertEquals(await Promise.all([l.next(), l.next(), l.next(), l.next()]), [{ value: 1, done: false, }, { value: 2, done: false, }, { value: 3, done: false, }, { value: undefined, done: true, }]);});
std

Version Info

Tagged at
3 weeks ago