2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2024-06-20 02:58:45 +00:00
|
|
|
import { assertEquals, assertRejects } from "@std/assert";
|
2022-11-29 13:55:38 +00:00
|
|
|
import { zipReadableStreams } from "./zip_readable_streams.ts";
|
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("zipReadableStreams()", async () => {
|
2023-11-10 03:57:52 +00:00
|
|
|
const textStream = ReadableStream.from([
|
|
|
|
"qwertzuiopasd",
|
|
|
|
"mnbvcxylkjhgfds",
|
|
|
|
"apoiuztrewq0987321",
|
|
|
|
]);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-11-10 03:57:52 +00:00
|
|
|
const textStream2 = ReadableStream.from([
|
|
|
|
"mnbvcxylkjhgfdsewr",
|
|
|
|
"apoiuztrewq0987654321",
|
|
|
|
"qwertzuiopasq123d",
|
|
|
|
]);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
const buf = await Array.fromAsync(
|
|
|
|
zipReadableStreams(textStream, textStream2),
|
|
|
|
);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
|
|
|
assertEquals(buf, [
|
|
|
|
"qwertzuiopasd",
|
|
|
|
"mnbvcxylkjhgfdsewr",
|
|
|
|
"mnbvcxylkjhgfds",
|
|
|
|
"apoiuztrewq0987654321",
|
|
|
|
"apoiuztrewq0987321",
|
|
|
|
"qwertzuiopasq123d",
|
|
|
|
]);
|
|
|
|
});
|
2024-06-20 02:58:45 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
);
|
|
|
|
});
|