Skip to content

Commit 572a537

Browse files
authored
test(streams): improve test coverage (denoland#5078)
* tests(streams): improve test coverage * tweak * work * work * work * work * revert * revert
1 parent 1e12f50 commit 572a537

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

streams/concat_readable_streams_test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,24 @@ Deno.test("concatStreams() handles errors", async () => {
9393
],
9494
);
9595
});
96+
97+
Deno.test("concatReadableStreams cancels all streams when concatenated stream is cancelled", async () => {
98+
const reasons: string[] = [];
99+
const createMockStream = () =>
100+
new ReadableStream({
101+
start(controller) {
102+
controller.enqueue("data");
103+
},
104+
cancel(error) {
105+
reasons.push(error);
106+
},
107+
});
108+
109+
const stream1 = createMockStream();
110+
const stream2 = createMockStream();
111+
const concatenatedStream = concatReadableStreams(stream1, stream2);
112+
113+
await concatenatedStream.cancel("Test cancel");
114+
115+
assertEquals(reasons, ["Test cancel", "Test cancel"]);
116+
});

streams/early_zip_readable_streams_test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

33
import { earlyZipReadableStreams } from "./early_zip_readable_streams.ts";
4-
import { assertEquals } from "@std/assert";
4+
import { assertEquals, assertRejects } from "@std/assert";
55

66
Deno.test("earlyZipReadableStreams() handles short first", async () => {
77
const textStream = ReadableStream.from(["1", "2", "3"]);
@@ -60,3 +60,24 @@ Deno.test("earlyZipReadableStreams() can zip three streams", async () => {
6060
"3",
6161
]);
6262
});
63+
64+
Deno.test("earlyZipReadableStreams() controller error", async () => {
65+
const errorMsg = "Test error";
66+
const stream = new ReadableStream({
67+
start(controller) {
68+
controller.enqueue("This will succeed");
69+
},
70+
pull() {
71+
throw new Error(errorMsg);
72+
},
73+
});
74+
75+
const zippedStream = earlyZipReadableStreams(stream);
76+
const reader = zippedStream.getReader();
77+
78+
assertEquals(await reader.read(), {
79+
value: "This will succeed",
80+
done: false,
81+
});
82+
await assertRejects(async () => await reader.read(), Error, errorMsg);
83+
});

streams/text_delimiter_stream.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class TextDelimiterStream extends TransformStream<string, string> {
8686
*/
8787
constructor(
8888
delimiter: string,
89-
options: DelimiterStreamOptions = { disposition: "discard" },
89+
options?: DelimiterStreamOptions,
9090
) {
9191
super({
9292
transform: (chunk, controller) => {
@@ -99,7 +99,7 @@ export class TextDelimiterStream extends TransformStream<string, string> {
9999

100100
this.#delimiter = delimiter;
101101
this.#delimLPS = createLPS(new TextEncoder().encode(delimiter));
102-
this.#disp = options.disposition ?? "discard";
102+
this.#disp = options?.disposition ?? "discard";
103103
}
104104

105105
#handle(

streams/zip_readable_streams_test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

3-
import { assertEquals } from "@std/assert";
3+
import { assertEquals, assertRejects } from "@std/assert";
44
import { zipReadableStreams } from "./zip_readable_streams.ts";
55

66
Deno.test("zipReadableStreams()", async () => {
@@ -29,3 +29,25 @@ Deno.test("zipReadableStreams()", async () => {
2929
"qwertzuiopasq123d",
3030
]);
3131
});
32+
33+
Deno.test("zipReadableStreams handles errors by closing the stream with an error", async () => {
34+
const errorStream = new ReadableStream({
35+
start(controller) {
36+
controller.enqueue("Initial data");
37+
},
38+
pull() {
39+
throw new Error("Test error during read");
40+
},
41+
});
42+
const normalStream = ReadableStream.from(["Normal data"]);
43+
const zippedStream = zipReadableStreams(errorStream, normalStream);
44+
const reader = zippedStream.getReader();
45+
46+
assertEquals(await reader.read(), { value: "Initial data", done: false });
47+
assertEquals(await reader.read(), { value: "Normal data", done: false });
48+
await assertRejects(
49+
async () => await reader.read(),
50+
Error,
51+
"Test error during read",
52+
);
53+
});

0 commit comments

Comments
 (0)