Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(streams): improve test coverage #5078

Merged
merged 8 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions streams/concat_readable_streams_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,24 @@ Deno.test("concatStreams() handles errors", async () => {
],
);
});

Deno.test("concatReadableStreams cancels all streams when concatenated stream is cancelled", async () => {
const reasons: string[] = [];
const createMockStream = () =>
new ReadableStream({
start(controller) {
controller.enqueue("data");
},
cancel(error) {
reasons.push(error);
},
});

const stream1 = createMockStream();
const stream2 = createMockStream();
const concatenatedStream = concatReadableStreams(stream1, stream2);

await concatenatedStream.cancel("Test cancel");

assertEquals(reasons, ["Test cancel", "Test cancel"]);
});
23 changes: 22 additions & 1 deletion streams/early_zip_readable_streams_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { earlyZipReadableStreams } from "./early_zip_readable_streams.ts";
import { assertEquals } from "@std/assert";
import { assertEquals, assertRejects } from "@std/assert";

Deno.test("earlyZipReadableStreams() handles short first", async () => {
const textStream = ReadableStream.from(["1", "2", "3"]);
Expand Down Expand Up @@ -60,3 +60,24 @@ Deno.test("earlyZipReadableStreams() can zip three streams", async () => {
"3",
]);
});

Deno.test("earlyZipReadableStreams() controller error", async () => {
const errorMsg = "Test error";
const stream = new ReadableStream({
start(controller) {
controller.enqueue("This will succeed");
},
pull() {
throw new Error(errorMsg);
},
});

const zippedStream = earlyZipReadableStreams(stream);
const reader = zippedStream.getReader();

assertEquals(await reader.read(), {
value: "This will succeed",
done: false,
});
await assertRejects(async () => await reader.read(), Error, errorMsg);
});
4 changes: 2 additions & 2 deletions streams/text_delimiter_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class TextDelimiterStream extends TransformStream<string, string> {
*/
constructor(
delimiter: string,
options: DelimiterStreamOptions = { disposition: "discard" },
options?: DelimiterStreamOptions,
) {
super({
transform: (chunk, controller) => {
Expand All @@ -99,7 +99,7 @@ export class TextDelimiterStream extends TransformStream<string, string> {

this.#delimiter = delimiter;
this.#delimLPS = createLPS(new TextEncoder().encode(delimiter));
this.#disp = options.disposition ?? "discard";
this.#disp = options?.disposition ?? "discard";
}

#handle(
Expand Down
24 changes: 23 additions & 1 deletion streams/zip_readable_streams_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "@std/assert";
import { assertEquals, assertRejects } from "@std/assert";
import { zipReadableStreams } from "./zip_readable_streams.ts";

Deno.test("zipReadableStreams()", async () => {
Expand Down Expand Up @@ -29,3 +29,25 @@ Deno.test("zipReadableStreams()", async () => {
"qwertzuiopasq123d",
]);
});

Deno.test("zipReadableStreams handles errors by closing the stream with an error", async () => {
const errorStream = new ReadableStream({
start(controller) {
controller.enqueue("Initial data");
},
pull() {
throw new Error("Test error during read");
},
});
const normalStream = ReadableStream.from(["Normal data"]);
const zippedStream = zipReadableStreams(errorStream, normalStream);
const reader = zippedStream.getReader();

assertEquals(await reader.read(), { value: "Initial data", done: false });
assertEquals(await reader.read(), { value: "Normal data", done: false });
await assertRejects(
async () => await reader.read(),
Error,
"Test error during read",
);
});