deno.land / std@0.224.0 / streams / to_transform_stream_test.ts

to_transform_stream_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects } from "../assert/mod.ts";import { toTransformStream } from "./to_transform_stream.ts";
Deno.test({ name: "toTransformStream()", async fn() { const readable = ReadableStream.from([0, 1, 2]) .pipeThrough(toTransformStream(async function* (src) { for await (const i of src) { yield i * 100; } }));
const res = await Array.fromAsync(readable); assertEquals(res, [0, 100, 200]); },});
Deno.test({ name: "toTransformStream() handles iterable instead of asyncIterable", async fn() { const readable = ReadableStream.from([0, 1, 2]) .pipeThrough(toTransformStream(function* (_src) { yield 0; yield 100; yield 200; }));
const res = await Array.fromAsync(readable); assertEquals(res, [0, 100, 200]); },});
Deno.test({ name: "toTransformStream() propagates the error from readable 1", async fn(t) { // When data is pipelined in the order of readable1 → generator → readable2, // Propagate the error that occurred in readable1 to generator and readable2. const expectedError = new Error("Error from readable1"); await t.step({ name: "to readable 2", async fn() { // Propagate the error that occurred in readable1 to readable2. let actualError = null;
const readable1 = new ReadableStream({ start(controller) { controller.error(expectedError); // error from readable1 }, }); const readable2 = readable1.pipeThrough( toTransformStream(async function* (src) { for await (const i of src) { yield i; } }), );
try { await readable2.getReader().read(); } catch (error) { actualError = error; // catch error in readable2 }
assertEquals(actualError, expectedError); }, }); await t.step({ name: "to generator", async fn() { // Propagate the error that occurred in readable1 to generator. let actualError = null;
const readable1 = new ReadableStream({ start(controller) { controller.error(expectedError); // error from readable1 }, }); const readable2 = readable1.pipeThrough( toTransformStream(async function* (src) { try { await src.getReader().read(); } catch (error) { actualError = error; // catch error in generator } yield 0; }), );
await readable2.getReader().read(); assertEquals(actualError, expectedError); }, }); },});
Deno.test({ name: "toTransformStream() propagates the error from generator", async fn(t) { // When data is pipelined in the order of readable1 → generator → readable2, // Propagate the error that occurred in generator to readable2 and readable1. const expectedError = new Error("Error from generator"); let actualError1: unknown = null; let actualError2: unknown = null;
const readable1 = new ReadableStream({ cancel(reason) { actualError1 = reason; // catch error in readable1 }, }); const readable2 = readable1.pipeThrough( // deno-lint-ignore require-yield toTransformStream(function* () { throw expectedError; // error from generator }), );
try { await readable2.getReader().read(); } catch (error) { actualError2 = error; // catch error in readable2 }
await t.step({ name: "to readable 1", fn() { assertEquals(actualError1, expectedError); }, }); await t.step({ name: "to readable 2", fn() { assertEquals(actualError2, expectedError); }, }); },});
Deno.test({ name: "toTransformStream() propagates cancellation from readable 2", async fn(t) { // When data is pipelined in the order of readable1 → generator → readable2, // Propagate the cancellation that occurred in readable2 to readable1 and generator. const expectedError = new Error("Error from readable2"); await t.step({ name: "to readable 1", async fn() { let actualError = null;
const readable1 = new ReadableStream({ cancel(reason) { actualError = reason; // catch error in readable1 }, }); const readable2 = readable1.pipeThrough( toTransformStream(function* () { yield 0; }), );
await readable2.cancel(expectedError); // cancellation from readable2 assertEquals(actualError, expectedError); }, }); await t.step({ name: "to readable 2", async fn() { let actualError = null;
const readable1 = new ReadableStream(); const readable2 = readable1.pipeThrough( toTransformStream(function* () { try { yield 0; } catch (error) { actualError = error; // catch error in generator } }), );
const reader = readable2.getReader(); await reader.read(); await reader.cancel(expectedError); // cancellation from readable2 assertEquals(actualError, expectedError); }, }); },});
Deno.test({ name: "toTransformStream() handles streams cancel with the correct error message", async fn() { const src = ReadableStream.from([0, 1, 2]); // deno-lint-ignore require-yield const transform = toTransformStream(function* (src) { src.getReader(); // lock the source stream to cause error at cancel throw new Error("foo"); });
await assertRejects( async () => await Array.fromAsync(src.pipeThrough(transform)), Error, "foo", ); },});
std

Version Info

Tagged at
6 months ago