2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertRejects } from "@std/assert";
|
2020-05-09 12:34:47 +00:00
|
|
|
import { MuxAsyncIterator } from "./mux_async_iterator.ts";
|
|
|
|
|
|
|
|
async function* gen123(): AsyncIterableIterator<number> {
|
|
|
|
yield 1;
|
|
|
|
yield 2;
|
|
|
|
yield 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function* gen456(): AsyncIterableIterator<number> {
|
|
|
|
yield 4;
|
|
|
|
yield 5;
|
|
|
|
yield 6;
|
|
|
|
}
|
|
|
|
|
2020-06-15 16:03:07 +00:00
|
|
|
async function* genThrows(): AsyncIterableIterator<number> {
|
|
|
|
yield 7;
|
|
|
|
throw new Error("something went wrong");
|
|
|
|
}
|
|
|
|
|
2021-05-18 13:38:53 +00:00
|
|
|
class CustomAsyncIterable {
|
|
|
|
[Symbol.asyncIterator]() {
|
|
|
|
return gen123();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 21:23:54 +00:00
|
|
|
Deno.test("MuxAsyncIterator()", async () => {
|
2020-05-09 12:34:47 +00:00
|
|
|
const mux = new MuxAsyncIterator<number>();
|
|
|
|
mux.add(gen123());
|
|
|
|
mux.add(gen456());
|
2023-11-10 19:00:28 +00:00
|
|
|
const results = new Set(await Array.fromAsync(mux));
|
2020-05-09 12:34:47 +00:00
|
|
|
assertEquals(results.size, 6);
|
2021-05-18 13:38:53 +00:00
|
|
|
assertEquals(results, new Set([1, 2, 3, 4, 5, 6]));
|
|
|
|
});
|
|
|
|
|
2024-04-11 21:23:54 +00:00
|
|
|
Deno.test("MuxAsyncIterator() works with no iterables", async () => {
|
|
|
|
const mux = new MuxAsyncIterator<number>();
|
|
|
|
const results = new Set(await Array.fromAsync(mux));
|
|
|
|
assertEquals(results.size, 0);
|
|
|
|
assertEquals(results, new Set([]));
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("MuxAsyncIterator() clears iterables after successful iteration", async () => {
|
|
|
|
const mux = new MuxAsyncIterator<number>();
|
|
|
|
mux.add(gen123());
|
|
|
|
mux.add(gen456());
|
|
|
|
const results = new Set(await Array.fromAsync(mux));
|
|
|
|
assertEquals(results.size, 6);
|
|
|
|
assertEquals(results, new Set([1, 2, 3, 4, 5, 6]));
|
|
|
|
mux.add(gen123());
|
|
|
|
const results2 = new Set(await Array.fromAsync(mux));
|
|
|
|
assertEquals(results2.size, 3);
|
|
|
|
assertEquals(results2, new Set([1, 2, 3]));
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("MuxAsyncIterator() takes async iterable as source", async () => {
|
2021-05-18 13:38:53 +00:00
|
|
|
const mux = new MuxAsyncIterator<number>();
|
|
|
|
mux.add(new CustomAsyncIterable());
|
2023-11-10 19:00:28 +00:00
|
|
|
const results = new Set(await Array.fromAsync(mux));
|
2021-05-18 13:38:53 +00:00
|
|
|
assertEquals(results.size, 3);
|
|
|
|
assertEquals(results, new Set([1, 2, 3]));
|
2020-05-09 12:34:47 +00:00
|
|
|
});
|
2020-06-15 16:03:07 +00:00
|
|
|
|
2024-04-11 21:23:54 +00:00
|
|
|
Deno.test("MuxAsyncIterator() throws when the source throws", async () => {
|
|
|
|
const mux = new MuxAsyncIterator<number>();
|
|
|
|
mux.add(gen123());
|
|
|
|
mux.add(genThrows());
|
|
|
|
await assertRejects(
|
|
|
|
async () => await Array.fromAsync(mux),
|
|
|
|
Error,
|
|
|
|
"something went wrong",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("MuxAsyncIterator() doesn't clear iterables after throwing", async () => {
|
|
|
|
const mux = new MuxAsyncIterator<number>();
|
|
|
|
mux.add(genThrows());
|
|
|
|
await assertRejects(
|
|
|
|
async () => await Array.fromAsync(mux),
|
|
|
|
Error,
|
|
|
|
"something went wrong",
|
|
|
|
);
|
|
|
|
await assertRejects(
|
|
|
|
async () => await Array.fromAsync(mux),
|
|
|
|
Error,
|
|
|
|
"something went wrong",
|
|
|
|
);
|
2020-06-15 16:03:07 +00:00
|
|
|
});
|