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
|
|
|
|
|
|
|
import { mergeReadableStreams } from "./merge_readable_streams.ts";
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("mergeReadableStreams()", async () => {
|
2023-11-10 03:57:52 +00:00
|
|
|
const textStream = ReadableStream.from([
|
|
|
|
"qwertzuiopasd",
|
|
|
|
"mnbvcxylkjhgfds",
|
|
|
|
"apoiuztrewq0987654321",
|
|
|
|
]);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-11-10 03:57:52 +00:00
|
|
|
const textStream2 = ReadableStream.from([
|
|
|
|
"mnbvcxylkjhgfds",
|
|
|
|
"apoiuztrewq0987654321",
|
|
|
|
"qwertzuiopasd",
|
|
|
|
]);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
const buf = await Array.fromAsync(
|
|
|
|
mergeReadableStreams(textStream, textStream2),
|
|
|
|
);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
|
|
|
assertEquals(buf.sort(), [
|
|
|
|
"apoiuztrewq0987654321",
|
|
|
|
"apoiuztrewq0987654321",
|
|
|
|
"mnbvcxylkjhgfds",
|
|
|
|
"mnbvcxylkjhgfds",
|
|
|
|
"qwertzuiopasd",
|
|
|
|
"qwertzuiopasd",
|
|
|
|
]);
|
|
|
|
});
|
2023-05-30 10:46:57 +00:00
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("mergeReadableStreams() handles errors", async () => {
|
2023-11-10 03:57:52 +00:00
|
|
|
const textStream = ReadableStream.from(["1", "3"]);
|
2023-05-30 10:46:57 +00:00
|
|
|
|
2023-11-10 03:57:52 +00:00
|
|
|
const textStream2 = ReadableStream.from(["2", "4"]);
|
2023-05-30 10:46:57 +00:00
|
|
|
|
|
|
|
const buf = [];
|
|
|
|
try {
|
|
|
|
for await (const s of mergeReadableStreams(textStream, textStream2)) {
|
|
|
|
buf.push(s);
|
|
|
|
if (s === "2") {
|
|
|
|
throw new Error("error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error("should not be here");
|
|
|
|
} catch (error) {
|
|
|
|
assertEquals((error as Error).message, "error");
|
|
|
|
assertEquals(buf, ["1", "2"]);
|
|
|
|
}
|
|
|
|
});
|