std/streams/concat_readable_streams_test.ts
Doctor 39c2a4c076
feat(streams): concatReadableStreams() (#4747)
* feat(streams): `new ConcatStreams()`

* refactor(streams): new ConcatStream to be a ReadableStream instead

- Converted ConcatStream from a TransformStream into a ReadableStream, also now with proper cleaning up if the `.cancel()` method is called.

* adjust(streams): ConcatStreams class into function

* Adjust(streams): based off comments

* adjust(streams): Remove redundant locking

* adjust(streams): based off comments

* tweaks

* fix

* tweak

* add Leo as co-author

Co-authored-by: crowlKats <crowlkats@toaxl.com>

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
Co-authored-by: crowlKats <crowlkats@toaxl.com>
2024-05-20 22:22:01 +00:00

96 lines
2.0 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects } from "../assert/mod.ts";
import { concatReadableStreams } from "./concat_readable_streams.ts";
Deno.test("concatStreams()", async () => {
const readable1 = ReadableStream.from([1, 2, 3]);
const readable2 = ReadableStream.from([4, 5, 6]);
const readable3 = ReadableStream.from([7, 8, 9]);
assertEquals(
await Array.fromAsync(
concatReadableStreams(readable1, readable2, readable3),
),
[
1,
2,
3,
4,
5,
6,
7,
8,
9,
],
);
});
Deno.test("concatStreams() with empty streams", async () => {
const readable1 = ReadableStream.from([]);
const readable2 = ReadableStream.from([]);
const readable3 = ReadableStream.from([]);
assertEquals(
await Array.fromAsync(
concatReadableStreams(readable1, readable2, readable3),
),
[],
);
});
Deno.test("concatStreams() with one empty stream", async () => {
const readable1 = ReadableStream.from([1, 2, 3]);
const readable2 = ReadableStream.from([]);
const readable3 = ReadableStream.from([7, 8, 9]);
assertEquals(
await Array.fromAsync(
concatReadableStreams(readable1, readable2, readable3),
),
[
1,
2,
3,
7,
8,
9,
],
);
});
Deno.test("concatStreams() handles errors", async () => {
const readable1 = ReadableStream.from([1, 2, 3]);
const readable2 = ReadableStream.from(async function* () {
yield 4;
yield 5;
yield 6;
throw new TypeError("I am an error!");
}());
const readable3 = ReadableStream.from([7, 8, 9]);
const results: number[] = [];
await assertRejects(
async () => {
for await (
const value of concatReadableStreams(readable1, readable2, readable3)
) {
results.push(value);
}
},
TypeError,
"I am an error!",
);
assertEquals(
results,
[
1,
2,
3,
4,
5,
6,
],
);
});