2020-05-09 08:34:47 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-15 12:03:07 -04:00
|
|
|
import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
|
2020-05-09 08:34:47 -04: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 12:03:07 -04:00
|
|
|
async function* genThrows(): AsyncIterableIterator<number> {
|
|
|
|
yield 7;
|
|
|
|
throw new Error("something went wrong");
|
|
|
|
}
|
|
|
|
|
2020-05-09 08:34:47 -04:00
|
|
|
Deno.test("[async] MuxAsyncIterator", async function (): Promise<void> {
|
|
|
|
const mux = new MuxAsyncIterator<number>();
|
|
|
|
mux.add(gen123());
|
|
|
|
mux.add(gen456());
|
|
|
|
const results = new Set();
|
|
|
|
for await (const value of mux) {
|
|
|
|
results.add(value);
|
|
|
|
}
|
|
|
|
assertEquals(results.size, 6);
|
|
|
|
});
|
2020-06-15 12:03:07 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "[async] MuxAsyncIterator throws",
|
|
|
|
async fn() {
|
|
|
|
const mux = new MuxAsyncIterator<number>();
|
|
|
|
mux.add(gen123());
|
|
|
|
mux.add(genThrows());
|
|
|
|
const results = new Set();
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async () => {
|
|
|
|
for await (const value of mux) {
|
|
|
|
results.add(value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"something went wrong",
|
2020-06-15 12:03:07 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|