Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/future.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510import { test, expect } from "@libs/testing";import { enhanceReadableStream, ReadableStreamReader } from "./_enhanced_readable_stream.ts";import { timeout } from "./disposal.ts";
function createInfiniteStream(delay = 10) { let intervalId: ReturnType<typeof setInterval>; return new ReadableStream({ start(controller) { // Infinite stream for testing let count = 0; intervalId = setInterval(() => { controller.enqueue(count++); }, delay); }, cancel() { console.log("Stream canceled"); // Clean up when stream is canceled clearInterval(intervalId); }, });}
// Test Case ERS1: Basic reading functionalitytest("enhanceReadableStream - basic reading functionality", async () => { // Create a simple ReadableStream emitting [1, 2, 3] const stream = new ReadableStream({ start(controller) { [1, 2, 3].forEach((value) => controller.enqueue(value)); controller.close(); }, });
// Enhance the stream const enhancedStream = enhanceReadableStream(stream);
const values = []; for await (const value of enhancedStream) { values.push(value); }
expect(values).toEqual([1, 2, 3]);});
// Test Case ERS2: Disposal using Symbol.asyncDisposetest("enhanceReadableStream - disposal using Symbol.asyncDispose", async () => { const stream = new ReadableStream({ start(controller) { [1, 2, 3].forEach((value) => controller.enqueue(value)); // Do not close the stream to simulate an ongoing stream }, });
const enhancedStream = enhanceReadableStream(stream);
// Dispose the stream before reading enhancedStream[Symbol.asyncDispose]();
const values = []; try { for await (const value of enhancedStream) { values.push(value); } } catch (error) { // Expected to throw an error because the stream is canceled expect(error).toBeDefined(); }
expect(values.length).toBe(0);});
// Test Case ERS5: Disposing a locked streamtest("enhanceReadableStream - disposing a locked stream", async () => { const stream = new ReadableStream({ start(controller) { [1, 2, 3].forEach((value) => controller.enqueue(value)); // Do not close the stream to keep it open }, });
const enhancedStream = enhanceReadableStream(stream);
// Get the reader, which locks the stream const reader = enhancedStream.getReader();
// Dispose the stream while it is locked enhancedStream[Symbol.asyncDispose]();
// Attempt to read from the reader try { const { value, done } = await reader.read(); expect(done).toBe(true); // Should be done because the stream was canceled } catch (error) { // Expected behavior; the reader should be canceled expect(error).toBeDefined(); }
// Ensure that the reader's lock is released expect(enhancedStream.locked).toBe(false);});
// Test Case ERS6: Read multiple streams simultaneouslytest("enhanceReadableStream - read multiple streams simultaneously", async () => { const createStream = (id: number) => new ReadableStream({ start(controller) { [1, 2, 3].forEach((value) => controller.enqueue(`${id}-${value}`)); controller.close(); }, });
const stream1 = enhanceReadableStream(createStream(1)); const stream2 = enhanceReadableStream(createStream(2));
const results1: string[] = []; const results2: string[] = [];
await Promise.all([ (async () => { for await (const value of stream1) { results1.push(value); } })(), (async () => { for await (const value of stream2) { results2.push(value); } })(), ]);
expect(results1).toEqual(["1-1", "1-2", "1-3"]); expect(results2).toEqual(["2-1", "2-2", "2-3"]);});
// Test Case ERS7: Consume stream using while loop with .read()test("enhanceReadableStream - consume using while loop with .read()", async () => { const stream = new ReadableStream({ start(controller) { [1, 2, 3].forEach((value) => controller.enqueue(value)); controller.close(); }, });
const enhancedStream = enhanceReadableStream(stream); const reader = enhancedStream.getReader();
const values = []; let result; while (!(result = await reader.read()).done) { values.push(result.value); }
expect(values).toEqual([1, 2, 3]);});
// Test Case ERS8: Attempt to get a second reader when the stream is already lockedtest("enhanceReadableStream - attempt to get a second reader when locked", async () => { const stream = createInfiniteStream();
const enhancedStream = enhanceReadableStream(stream);
const reader1 = enhancedStream.getReader(); const reader2 = enhancedStream.getReader();
// Attempt to get a second reader console.log({ value1: await reader1.read(), value2: await reader2.read(), reader1, reader2 }) expect(true).toBe(true);
await enhancedStream.cancel();});
// Test Case ERS9: Dispose the stream while it's being readtest("enhanceReadableStream - dispose the stream while it's being read", async () => { const stream = createInfiniteStream(50); const enhancedStream = enhanceReadableStream(stream); const values: number[] = [];
const readPromise = (async () => { for await (const value of enhancedStream) { values.push(value); if (value >= 3) { break; } } })();
await readPromise; await enhancedStream[Symbol.asyncDispose]();
// Ensure that only values up to 3 are read expect(values).toEqual([0, 1, 2, 3]);
// Ensure that the stream is properly disposed expect(enhancedStream.locked).toBe(false); expect(ReadableStreamReader.has(enhancedStream)).toBe(false);});
// Test Case ERS10: Attempt to have a second reader with parallel reads when the stream is already lockedtest("enhanceReadableStream - attempt to have a second reader with parallel reads when locked", async () => { const stream = createInfiniteStream(); const enhancedStream = enhanceReadableStream(stream);
await Promise.race([ (async () => { try { for await (const value of enhancedStream) { console.log("Reader 1:", value); } } catch (_) { console.warn(_) } })(), (async () => { try { for await (const value of enhancedStream) { console.log("Reader 2:", value); } } catch (_) { console.warn(_) } })(), timeout(1000, { reject: false }), ]);
await enhancedStream.cancel();
// await stream.cancel();});
// Test Case: Complex Stream Teeing and Disposaltest.only("enhanceReadableStream - complex teeing and disposal", async () => { // Create a source ReadableStream that emits numbers every 500ms const sourceStream = new ReadableStream<number>({ start(controller) { let count = 0; const intervalId = setInterval(() => { if (count > 10) { clearInterval(intervalId); controller.close(); return; } else { controller.enqueue(count++); } }, 500); }, });
// Split the source stream into two branches const enhacnedBranch = enhanceReadableStream(sourceStream);
// Enhance both branches const branch1 = enhacnedBranch.getReader().stream; const branch2 = enhacnedBranch.getReader().stream;
const enhancedBranch1 = enhanceReadableStream(branch1); const enhancedBranch2 = enhanceReadableStream(branch2);
const resultsBranch1: number[] = []; const resultsBranch2: number[] = []; const resultsSubBranch1: number[] = []; const resultsSubBranch2: number[] = [];
// Start reading from the parent branches const readParentBranches = Promise.all([ (async () => { for await (const value of enhancedBranch1) { resultsBranch1.push(value); if (value === 3) { // After reading some values, split branch1 into two sub-branches const subBranch1 = enhancedBranch1.getReader().stream; const subBranch2 = enhancedBranch1.getReader().stream; const enhancedSubBranch1 = enhanceReadableStream(subBranch1); const enhancedSubBranch2 = enhanceReadableStream(subBranch2);
// Start reading from the sub-branches after a delay setTimeout(() => { (async () => { try { for await (const subValue of enhancedSubBranch1) { resultsSubBranch1.push(subValue); } } catch (_) { console.warn(_); } })(); }, 2000); // Delay of 2 seconds
setTimeout(() => { (async () => { try { for await (const subValue of enhancedSubBranch2) { resultsSubBranch2.push(subValue); } } catch (_) { console.warn(_); } })(); }, 2000); // Delay of 2 seconds }
if (value === 5) { break; } } })(), (async () => { for await (const value of enhancedBranch2) { resultsBranch2.push(value); if (value === 5) { break; } } })(), ]);
// Wait for the parent branches to finish reading await readParentBranches;
// Dispose of the parent branch after reading value 5 // await enhancedBranch2[Symbol.asyncDispose]();
// Wait for the sub-branches to read remaining values await new Promise((resolve) => setTimeout(resolve, 6000)); // Wait longer to allow sub-branches to read all values
// Output the results // console.log("Parent Branch 1:", resultsBranch1); // console.log("Parent Branch 2:", resultsBranch2); // console.log("Sub Branch 1:", resultsSubBranch1); // console.log("Sub Branch 2:", resultsSubBranch2);
// // Assertions // expect(resultsBranch1).toEqual([0, 1, 2, 3, 4, 5]); // expect(resultsBranch2).toEqual([0, 1, 2, 3, 4, 5]);
// // The sub-branches should have started reading from value 3 onwards // expect(resultsSubBranch1[0]).toBe(3); // expect(resultsSubBranch2[0]).toBe(3);
// // The sub-branches should continue to read values even after parent branch is disposed // expect(resultsSubBranch1).toEqual([3, 4, 5, 6, 7, 8, 9, 10]); // expect(resultsSubBranch2).toEqual([3, 4, 5, 6, 7, 8, 9, 10]);});
// ====
// Test Case ERR1: Basic reading from enhanced readertest("enhanceReaderWithDisposal - basic reading functionality", async () => { const stream = new ReadableStream<number>({ start(controller) { [1, 2, 3].forEach((value) => controller.enqueue(value)); controller.close(); }, });
const enhancedReader = enhanceReadableStream(stream).getReader();
const values = []; let result: ReadableStreamReadResult<number>; do { result = await enhancedReader.read(); if (!result.done) { values.push(result.value); } } while (!result.done);
expect(values).toEqual([1, 2, 3]);});
// Test Case ERR2: Disposal using Symbol.asyncDisposetest("enhanceReaderWithDisposal - disposal using Symbol.asyncDispose", async () => { const stream = new ReadableStream<number>({ start(controller) { [1, 2, 3].forEach((value) => controller.enqueue(value)); // Do not close the stream }, });
const enhancedReader = enhanceReadableStream(stream).getReader();
// Dispose the reader await enhancedReader[Symbol.asyncDispose]();
// Attempt to read from the reader try { await enhancedReader.read(); // Should not reach here expect(true).toBe(false); } catch (error) { // Expected to throw an error expect(error).toBeDefined(); }});
// Test Case ERR3: Multiple readers from different streamstest("enhanceReaderWithDisposal - multiple readers from different streams", async () => { const createStream = (id: number) => new ReadableStream<string>({ start(controller) { [1, 2, 3].forEach((value) => controller.enqueue(`${id}-${value}`)); controller.close(); }, });
const stream1 = createStream(1); const stream2 = createStream(2);
const reader1 = enhanceReadableStream(stream1).getReader(); const reader2 = enhanceReadableStream(stream2).getReader();
const values2: string[] = []; const values1: string[] = [];
await Promise.all([ (async () => { let result; while (!(result = await reader1.read()).done) { values1.push(result.value); } })(), (async () => { let result; while (!(result = await reader2.read()).done) { values2.push(result.value); } })(), ]);
expect(values1).toEqual(["1-1", "1-2", "1-3"]); expect(values2).toEqual(["2-1", "2-2", "2-3"]);});
// Test Case ERR4: Reader cancellation during read operationstest("enhanceReaderWithDisposal - reader cancellation during read", async () => { const stream = new ReadableStream<number>({ start(controller) { let count = 0; function push() { controller.enqueue(count++); setTimeout(push, 10); } push(); }, });
const reader = enhanceReadableStream(stream).getReader();
const values: number[] = [];
const readPromise = (async () => { while (true) { const result = await reader.read(); if (result.done) break; values.push(result.value); if (result.value >= 5) { // Cancel the reader await reader.cancel("No longer needed"); break; } } })();
await readPromise;
expect(values).toEqual([0, 1, 2, 3, 4, 5]);
// Ensure the reader is released reader.releaseLock(); expect(stream.locked).toBe(false);});
// Test Case ERR5: Dispose reader while it's in the middle of readingtest("enhanceReaderWithDisposal - dispose reader during read", async () => { const stream = new ReadableStream<number>({ start(controller) { let count = 0; function push() { controller.enqueue(count++); setTimeout(push, 20); } push(); }, });
const reader = enhanceReadableStream(stream).getReader();
const values: number[] = [];
const readPromise = (async () => { while (true) { const result = await reader.read(); if (result.done) break; values.push(result.value); if (result.value >= 3) { // Dispose the reader reader[Symbol.asyncDispose](); break; } } })();
await readPromise;
expect(values).toEqual([0, 1, 2, 3]);
// Ensure the reader is released expect(stream.locked).toBe(false);});