Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/streams.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575/// <reference lib="esnext" />/** * @fileoverview * This file provides a suite of tests for the `splitStream` function. The goal is to ensure that the splitting * behavior is correct under various conditions, including normal operation, empty sources, error scenarios, * and cancellation/cleanup behaviors. * * ### Key Testing Strategies: * 1. **Data-Driven (Test Tables)**: * We use parameterized test cases defined in arrays, providing input streams, predicates, and expected outputs. * This ensures comprehensive coverage of scenarios without excessive boilerplate. * * 2. **Isolation and Determinism**: * Each test creates its own source stream to avoid interference. We use deterministic input data * and avoid fixed delays. Instead, we rely on naturally completing streams and checking conditions. * * 3. **State Tracking**: * We test the stream's behavior when: * - The source ends normally. * - The predicate routes chunks to both streams. * - One or both resulting streams are canceled early. * - Errors occur while reading from the source. * - Attempting to read when the source is done or queues are empty. * * 4. **Logging and Assertions**: * For debugging, tests may log progress. Assertions ensure stable expectations: even in tricky scenarios, * the output and state of the streams are as intended. * * ### Tools: * - `@libs/testing` for test runner & assertions. * - `splitStream` function (imported from the stream splitting module). * - Helper functions and stable test inputs to ensure reproducibility. */
/** * @fileoverview * Tests for the `splitStream` function to ensure reliable and predictable splitting of chunks * from a given source `ReadableStream` into two output streams based on a predicate. * * ### Test Strategies: * - **Data-Driven (Test Tables)**: * Using arrays of input/output scenarios to thoroughly validate even/odd splits, empty sources, * all-passing/all-failing predicates, and more complex object-based scenarios. * * - **Edge Cases & Error Handling**: * Test sources that end immediately (empty), sources that error out, * and behavior when canceling or disposing streams. * * - **Synchronization & No Flaky Delays**: * We avoid fixed delays. Instead, tests rely on deterministic inputs or * event-driven checks. The code should run consistently regardless of timing variances. * * - **Documentation & Reproducibility**: * Each test is documented, and scenarios are traceable to ensure maintainability and clarity. */
import { test, expect } from "@libs/testing";import { splitStream } from "./split.ts"; // Adjust path as neededimport { iterableFromStream } from "./utils.ts"; // Helper to read entire streams easily
/** * Helper to create a ReadableStream from an array of values. * Ensures a deterministic, static input source for testing. */function createArrayStream<T>(values: T[]): ReadableStream<T> { return new ReadableStream<T>({ start(controller) { for (const v of values) { controller.enqueue(v); } controller.close(); }, });}
/** * Helper to simulate an error in the source stream. * Once it has enqueued half of the provided values, it throws an error. */function createErrorStream<T>(values: T[]): ReadableStream<T> { return new ReadableStream<T>({ start(controller) { let index = 0; for (const v of values) { if (index === Math.floor(values.length / 2)) { // Simulate an error mid-way controller.error(new Error("Simulated source error")); return; } controller.enqueue(v); index++; } controller.close(); }, });}
/** * @test * Comprehensive Data-Driven Tests (Test Table): * Unified test cases supporting numeric and object-based inputs. * * Each entry includes: * - name: a descriptive test name * - input: array of input chunks (numbers or objects) * - predicate: function to split input * - expectedFirst: expected chunks in the first stream * - expectedSecond: expected chunks in the second stream * * Scenarios include: * - Decision table testing with objects * - Basic numeric splitting logic * - Edge cases like empty input * - Normal operations */type InputType = { type: "allowed" | "forbidden"; value: number; } | number;
const testCases = [ // Object-based decision table cases { name: "All allowed", input: [{type: "allowed", value: 1}, {type: "allowed", value: 2}] as Obj[], predicate: (obj: Obj) => obj.type === "allowed", expectedFirst: [{type: "allowed", value: 1}, {type: "allowed", value: 2}], expectedSecond: [] }, { name: "All forbidden", input: [{type: "forbidden", value: 1}, {type: "forbidden", value: 2}] as Obj[], predicate: (obj: Obj) => obj.type === "allowed", expectedFirst: [], expectedSecond: [{type: "forbidden", value: 1}, {type: "forbidden", value: 2}] }, { name: "Mixed types", input: [ {type: "allowed", value: 1}, {type: "forbidden", value: 2}, {type: "allowed", value: 3}, {type: "forbidden", value: 4}, ] as Obj[], predicate: (obj: Obj) => obj.type === "allowed", expectedFirst: [{type: "allowed", value: 1}, {type: "allowed", value: 3}], expectedSecond: [{type: "forbidden", value: 2}, {type: "forbidden", value: 4}], }, { name: "Mixed values", input: [ {type: "allowed", value: 10}, {type: "forbidden", value: 20}, {type: "allowed", value: 30}, {type: "forbidden", value: 40}, ] as Obj[], predicate: (obj: Obj) => obj.type === "allowed", expectedFirst: [{type: "allowed", value: 10}, {type: "allowed", value: 30}], expectedSecond: [{type: "forbidden", value: 20}, {type: "forbidden", value: 40}], },
// Numeric test cases { name: "Split even/odd numbers", input: [1, 2, 3, 4], predicate: (num: number) => num % 2 === 0, expectedFirst: [2, 4], expectedSecond: [1, 3] }, { name: "Empty source", input: [] as number[], predicate: (num: number) => num % 2 === 0, expectedFirst: [] as number[], expectedSecond: [] as number[] }, { name: "All chunks match predicate", input: [2, 4, 6], predicate: (num: number) => num % 2 === 0, expectedFirst: [2, 4, 6], expectedSecond: [] }, { name: "No chunks match predicate", input: [1, 3, 5], predicate: (num: number) => num % 2 === 0, expectedFirst: [], expectedSecond: [1, 3, 5] }];
/** * @testtable * Validate splitStream using various inputs, objects, and predicates. */for (const { name, input, predicate, expectedFirst, expectedSecond } of testCases) { test(`splitStream - ${name}`, async () => { const source = createArrayStream(input as InputType[]); const [firstStream, secondStream] = splitStream(source, predicate as ((input: InputType) => boolean));
const [resultFirst, resultSecond] = await Promise.all([ Array.fromAsync(iterableFromStream(firstStream)), Array.fromAsync(iterableFromStream(secondStream)), ]);
expect(resultFirst).toEqual(expectedFirst); expect(resultSecond).toEqual(expectedSecond); });}
/** * @test * Tests the behavior when the source encounters an error during reading. * The expected behavior: Both resulting streams should abort once the error is encountered. */test("splitStream - error in source", async () => { const source = createErrorStream([1,2,3,4,5,6]); const [streamA, streamB] = splitStream(source, (num: number) => num > 3);
const resultsA: number[] = []; const resultsB: number[] = [];
let errorCaught = false;
// Attempt to read both streams concurrently await Promise.allSettled([ (async () => { try { for await (const val of streamA) { resultsA.push(val); } } catch (err) { errorCaught = true; } })(), (async () => { try { for await (const val of streamB) { resultsB.push(val); } } catch (err) { errorCaught = true; } })() ]);
// Since the source errors mid-way, we only get a subset of values. // The first half is enqueued, the error occurs, and both streams end abruptly. // Depending on when error is raised, we might have some chunks or none after the error point. expect(errorCaught).toBe(true); expect(resultsA.length + resultsB.length).toBeLessThanOrEqual(3); // Only got up to half before error});
/** * @test * Tests cancellation behavior: cancel one of the resulting streams early and ensure the other can still be read. */test("splitStream - cancel one resulting stream early", async () => { const source = createArrayStream([1,2,3,4,5]); const [evenStream, oddStream] = splitStream(source, (num: number) => num % 2 === 0);
// Cancel the evenStream immediately const evenReader = evenStream.getReader(); await evenReader.cancel("No need for even numbers");
// The oddStream should still provide odd values const oddValues = await Array.fromAsync(iterableFromStream(oddStream)); expect(oddValues).toEqual([1,3,5]);});
/** * @test * Ensures that if we try to read from one of the streams after the source is already done, it simply closes. */test("splitStream - read after source done", async () => { const source = createArrayStream([10,11]); const [streamTrue, streamFalse] = splitStream(source, (num: number) => num > 10);
// Read all from true stream first const trueValues = await Array.fromAsync(iterableFromStream(streamTrue)); expect(trueValues).toEqual([11]);
// Source is done now. If we read from false stream afterward: const falseValues = await Array.fromAsync(iterableFromStream(streamFalse)); expect(falseValues).toEqual([10]);});
/** * @test * Attempt multiple reads in parallel on the same resulting stream to check stability. * We don't want to cause deadlocks or race conditions. */test("splitStream - parallel reads on the same resulting stream", async () => { const source = createArrayStream([0,1,2,3,4,5]); await using streams = splitStream(source, (num: number) => num % 2 === 0); const [evenStream] = streams;
// Attempt concurrent reads: const readPromise1 = iterableFromStream(evenStream); const readPromise2 = iterableFromStream(evenStream);
// Both attempts will read the same stream. The second will start reading after the first finishes or simultaneously. // Depending on the implementation, one reader might lock the stream and the other might get no data. const [res1, res2] = await Promise.all([ Array.fromAsync(readPromise1), Array.fromAsync(readPromise2), ]);
// At least one should have the data. Another might see an empty array due to locking rules. // The even numbers: [0,2,4] // We can accept that one gets the data and the other is empty or that both share results depending on implementation. // For a properly implemented splitStream, each returned stream is independent and can be read fully. // Since we only returned evenStream, there's only one resulting stream. If multiple readers are attempted, // typically only one can read the data (locked by first reader). // Check that at least one got the correct data. const combined = [...res1, ...res2]; expect(combined.sort()).toEqual([0,2,4]);});
/** * @test * Test disposing of both resulting streams using Symbol.asyncDispose and ensuring cleanup. */test("splitStream - async disposal of resulting streams", async () => { const source = createArrayStream([5,6,7,8]); const streams = splitStream(source, (n: number) => n < 7); const [streamA, streamB] = streams;
// Dispose both using Symbol.asyncDispose: await streams[Symbol.asyncDispose]();
// Attempt reading after disposal should fail await Promise.all([ expect(async () => { await Array.fromAsync(iterableFromStream(streamA)); }).rejects.toThrow(),
expect(async () => { await Array.fromAsync(iterableFromStream(streamB)); }).rejects.toThrow(), ]);});
/** * @test * Test table with complex decision logic (decision table testing): * * Conditions: * - Input: a series of objects with a property `type`. * - Predicate: checks if `type` is "allowed". * - Expected result: allowed objects in one stream, disallowed in the other. * * We can represent multiple conditions in a decision table: */interface Obj { type: "allowed" | "forbidden"; value: number; }
const decisionTestCases = [ { name: "All allowed", input: [{type: "allowed", value: 1}, {type: "allowed", value: 2}] as Obj[], predicate: (obj: Obj) => obj.type === "allowed", expectedFirst: [{type: "allowed", value: 1}, {type: "allowed", value: 2}], expectedSecond: [] }, { name: "All forbidden", input: [{type: "forbidden", value: 1}, {type: "forbidden", value: 2}] as Obj[], predicate: (obj: Obj) => obj.type === "allowed", expectedFirst: [], expectedSecond: [{type: "forbidden", value: 1}, {type: "forbidden", value: 2}] }, { name: "Mixed types", input: [ {type: "allowed", value: 1}, {type: "forbidden", value: 2}, {type: "allowed", value: 3}, {type: "forbidden", value: 4}, ] as Obj[], predicate: (obj: Obj) => obj.type === "allowed", expectedFirst: [{type: "allowed", value: 1}, {type: "allowed", value: 3}], expectedSecond: [{type: "forbidden", value: 2}, {type: "forbidden", value: 4}], }, { name: "Mixed", input: [ {type: "allowed", value: 10}, {type: "forbidden", value: 20}, {type: "allowed", value: 30}, {type: "forbidden", value: 40}, ] as Obj[], predicate: (obj: Obj) => obj.type === "allowed", expectedFirst: [{type: "allowed", value: 10}, {type: "allowed", value: 30}], expectedSecond: [{type: "forbidden", value: 20}, {type: "forbidden", value: 40}], },];
for (const {name, input, predicate, expectedFirst, expectedSecond} of decisionTestCases) { test(`splitStream - decision table: ${name}`, async () => { const source = createArrayStream(input); const [allowedStream, forbiddenStream] = splitStream(source, predicate);
const resAllowed = await Array.fromAsync(iterableFromStream(allowedStream)); const resForbidden = await Array.fromAsync(iterableFromStream(forbiddenStream));
expect(resAllowed).toEqual(expectedFirst); expect(resForbidden).toEqual(expectedSecond); });}
/** * @test * Testing synchronization and concurrency: * Attempt reading from one resulting stream with dynamic checks (no fixed delays) and ensure no flaky behavior. * We'll do this by waiting for the first chunk to appear instead of sleeping. */test("splitStream - dynamic wait (no fixed delay)", async () => { const source = new ReadableStream<number>({ start(controller) { // Enqueue asynchronously setTimeout(() => controller.enqueue(42), 10); queueMicrotask(() => { controller.enqueue(54); }); setTimeout(() => controller.close(), 20); } });
const [matches, others] = splitStream(source, (x) => x === 42);
// Check the 'matches' stream dynamically: const matchesReader = matches.getReader(); const otherReader = others.getReader();
// This read will wait until chunk is available (dynamic wait) const matchesRes = await matchesReader.read(); const otherRes = await otherReader.read();
expect(matchesRes.value).toBe(42); expect(matchesRes.done).toBe(false);
expect(otherRes.value).toBe(54); expect(matchesRes.done).toBe(false);
const secondMatchRead = await matchesReader.read(); const secondOtherRead = await otherReader.read();
expect(secondMatchRead.done).toBe(true); expect(secondOtherRead.done).toBe(true);});
/** * @test * Test behavior when the source errors out midway. * Both streams should encounter this error and stop providing new data. */test("splitStream - error in source stream", async () => { const source = createErrorStream([1,2,3,4,5,6]); const [aStream, bStream] = splitStream(source, (num: number) => num > 3);
const resultsA: number[] = []; const resultsB: number[] = []; let errorCaught = false;
await Promise.allSettled([ (async () => { try { for await (const v of aStream) { resultsA.push(v); } } catch (_) { errorCaught = true; } })(), (async () => { try { for await (const v of bStream) { resultsB.push(v); } } catch (_) { errorCaught = true; } })() ]);
expect(errorCaught).toBe(true); // We only read up until half before error occurred, so combined we should have fewer than full set expect(resultsA.length + resultsB.length).toBeLessThanOrEqual(3);});
/** * @test * Cancelling one resulting stream should not prevent reading from the other. * Ensure that when we cancel the first stream, the second can still be read fully. */test("splitStream - cancel one stream early", async () => { const source = createArrayStream([10,11,12,13,14]); const [gt12Stream, ltEqual12Stream] = splitStream(source, (num: number) => num > 12);
// Cancel the gt12Stream immediately const gt12Reader = gt12Stream.getReader(); await gt12Reader.cancel("No need for values > 12");
const remainValues = await Array.fromAsync(iterableFromStream(ltEqual12Stream)); // Values ≤ 12: [10,11,12] expect(remainValues).toEqual([10,11,12]);});
/** * @test * Read after the source is done should just close the streams gracefully. */test("splitStream - read after source done", async () => { const source = createArrayStream([1,2]); const [evenStream, oddStream] = splitStream(source, (num) => num % 2 === 0);
// Read even first const evenVals = await Array.fromAsync(iterableFromStream(evenStream)); expect(evenVals).toEqual([2]);
// Now read odd - source done, but odd should yield [1] const oddVals = await Array.fromAsync(iterableFromStream(oddStream)); expect(oddVals).toEqual([1]);});
/** * @test * Ensuring no flakes: dynamically wait for data without fixed delays. * Source asynchronously enqueues a single chunk, no fixed sleeps. */test("splitStream - dynamic wait (no fixed delays)", async () => { const source = new ReadableStream<number>({ start(controller) { // enqueue after a short async tick queueMicrotask(() => { controller.enqueue(42); controller.close(); }); } });
const [matches, others] = splitStream(source, (x) => x === 42);
const matchesRes = await Array.fromAsync(iterableFromStream(matches)); expect(matchesRes).toEqual([42]);
const othersRes = await Array.fromAsync(iterableFromStream(others)); expect(othersRes).toEqual([]);});
/** * @test * Attempt reading from one stream after another stream is fully drained and ensure no conflicts or deadlocks. */test("splitStream - sequential reading of both streams", async () => { const source = createArrayStream([10,11,12,13]); const [ge12Stream, lt12Stream] = splitStream(source, (n) => n >= 12);
// Read ge12 first const ge12Vals = await Array.fromAsync(iterableFromStream(ge12Stream)); expect(ge12Vals).toEqual([12,13]);
// Now read lt12 const lt12Vals = await Array.fromAsync(iterableFromStream(lt12Stream)); expect(lt12Vals).toEqual([10,11]);});
/** * These tests collectively ensure: * - Correct splitting of data under normal and edge conditions. * - Stable behavior on source completion, errors, and cancellations. * - Proper synchronization and no flaky, timing-dependent failures. * - Comprehensive coverage via test tables (basic and decision table testing). */