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

mux_async_iterator_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
87
88
89
90
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertRejects } from "../assert/mod.ts";import { MuxAsyncIterator } from "./mux_async_iterator.ts";
async function* gen123(): AsyncIterableIterator<number> { yield 1; yield 2; yield 3;}
async function* gen456(): AsyncIterableIterator<number> { yield 4; yield 5; yield 6;}
async function* genThrows(): AsyncIterableIterator<number> { yield 7; throw new Error("something went wrong");}
class CustomAsyncIterable { [Symbol.asyncIterator]() { return gen123(); }}
Deno.test("MuxAsyncIterator()", async () => { const mux = new MuxAsyncIterator<number>(); mux.add(gen123()); mux.add(gen456()); const results = new Set(await Array.fromAsync(mux)); assertEquals(results.size, 6); assertEquals(results, new Set([1, 2, 3, 4, 5, 6]));});
Deno.test("MuxAsyncIterator() works with no iterables", async () => { const mux = new MuxAsyncIterator<number>(); const results = new Set(await Array.fromAsync(mux)); assertEquals(results.size, 0); assertEquals(results, new Set([]));});
Deno.test("MuxAsyncIterator() clears iterables after successful iteration", async () => { const mux = new MuxAsyncIterator<number>(); mux.add(gen123()); mux.add(gen456()); const results = new Set(await Array.fromAsync(mux)); assertEquals(results.size, 6); assertEquals(results, new Set([1, 2, 3, 4, 5, 6])); mux.add(gen123()); const results2 = new Set(await Array.fromAsync(mux)); assertEquals(results2.size, 3); assertEquals(results2, new Set([1, 2, 3]));});
Deno.test("MuxAsyncIterator() takes async iterable as source", async () => { const mux = new MuxAsyncIterator<number>(); mux.add(new CustomAsyncIterable()); const results = new Set(await Array.fromAsync(mux)); assertEquals(results.size, 3); assertEquals(results, new Set([1, 2, 3]));});
Deno.test("MuxAsyncIterator() throws when the source throws", async () => { const mux = new MuxAsyncIterator<number>(); mux.add(gen123()); mux.add(genThrows()); await assertRejects( async () => await Array.fromAsync(mux), Error, "something went wrong", );});
Deno.test("MuxAsyncIterator() doesn't clear iterables after throwing", async () => { const mux = new MuxAsyncIterator<number>(); mux.add(genThrows()); await assertRejects( async () => await Array.fromAsync(mux), Error, "something went wrong", ); await assertRejects( async () => await Array.fromAsync(mux), Error, "something went wrong", );});
std

Version Info

Tagged at
6 months ago