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 { earlyZipReadableStreams } from "./early_zip_readable_streams.ts";
|
2024-06-20 02:58:45 +00:00
|
|
|
import { assertEquals, assertRejects } from "@std/assert";
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("earlyZipReadableStreams() handles short first", async () => {
|
2023-11-10 03:57:52 +00:00
|
|
|
const textStream = ReadableStream.from(["1", "2", "3"]);
|
|
|
|
const textStream2 = ReadableStream.from(["a", "b", "c", "d", "e"]);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
const buf = await Array.fromAsync(
|
|
|
|
earlyZipReadableStreams(textStream, textStream2),
|
|
|
|
);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
|
|
|
assertEquals(buf, [
|
|
|
|
"1",
|
|
|
|
"a",
|
|
|
|
"2",
|
|
|
|
"b",
|
|
|
|
"3",
|
|
|
|
"c",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2024-02-27 20:12:47 +00:00
|
|
|
Deno.test("earlyZipReadableStreams() handles long first", async () => {
|
2023-11-10 03:57:52 +00:00
|
|
|
const textStream = ReadableStream.from(["a", "b", "c", "d", "e"]);
|
|
|
|
const textStream2 = ReadableStream.from(["1", "2", "3"]);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
const buf = await Array.fromAsync(
|
|
|
|
earlyZipReadableStreams(textStream, textStream2),
|
|
|
|
);
|
2022-11-29 13:55:38 +00:00
|
|
|
|
|
|
|
assertEquals(buf, [
|
|
|
|
"a",
|
|
|
|
"1",
|
|
|
|
"b",
|
|
|
|
"2",
|
|
|
|
"c",
|
|
|
|
"3",
|
|
|
|
"d",
|
|
|
|
]);
|
|
|
|
});
|
2024-05-28 01:27:40 +00:00
|
|
|
|
|
|
|
Deno.test("earlyZipReadableStreams() can zip three streams", async () => {
|
|
|
|
const textStream = ReadableStream.from(["a", "b", "c", "d", "e"]);
|
|
|
|
const textStream2 = ReadableStream.from(["1", "2", "3"]);
|
|
|
|
const textStream3 = ReadableStream.from(["x", "y"]);
|
|
|
|
|
|
|
|
const buf = await Array.fromAsync(
|
|
|
|
earlyZipReadableStreams(textStream, textStream2, textStream3),
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(buf, [
|
|
|
|
"a",
|
|
|
|
"1",
|
|
|
|
"x",
|
|
|
|
"b",
|
|
|
|
"2",
|
|
|
|
"y",
|
|
|
|
"c",
|
|
|
|
"3",
|
|
|
|
]);
|
|
|
|
});
|
2024-06-20 02:58:45 +00:00
|
|
|
|
2024-06-20 05:36:58 +00:00
|
|
|
Deno.test("earlyZipReadableStreams() forwards cancel()", async () => {
|
|
|
|
const num = 10;
|
|
|
|
let cancelled = 0;
|
|
|
|
const streams = new Array(num).fill(false).map(() =>
|
|
|
|
new ReadableStream(
|
|
|
|
{
|
|
|
|
pull(controller) {
|
|
|
|
controller.enqueue("chunk");
|
|
|
|
},
|
|
|
|
cancel(reason) {
|
|
|
|
cancelled++;
|
|
|
|
assertEquals(reason, "I was cancelled!");
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
await earlyZipReadableStreams(...streams).cancel("I was cancelled!");
|
|
|
|
assertEquals(cancelled, num);
|
|
|
|
});
|
|
|
|
|
2024-06-20 02:58:45 +00:00
|
|
|
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);
|
|
|
|
});
|