mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
572a53704b
* tests(streams): improve test coverage * tweak * work * work * work * work * revert * revert
117 lines
2.6 KiB
TypeScript
117 lines
2.6 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,
|
|
],
|
|
);
|
|
});
|
|
|
|
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"]);
|
|
});
|