Skip to content

Commit

Permalink
test(streams): improve test coverage (#5078)
Browse files Browse the repository at this point in the history
* tests(streams): improve test coverage

* tweak

* work

* work

* work

* work

* revert

* revert
  • Loading branch information
iuioiua authored Jun 20, 2024
1 parent 1e12f50 commit 572a537
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
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",
);
});

0 comments on commit 572a537

Please sign in to comment.