Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/future.
Something went wrong. Try again.
future scope_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668/** * Comprehensive tests for the scope() function * * This test suite validates: * - Sequential execution of futures * - Error propagation through scope * - Resource disposal within scope * - Iteration behavior * - Edge cases */
import { describe, it } from "@std/testing/bdd";import { expect } from "@std/expect";
import { scope } from "./scope.ts";import { Future } from "./future.ts";import { from } from "./from.ts";
describe("scope()", () => { describe("Basic Functionality", () => { it("should execute futures sequentially", async () => { const executionOrder: number[] = [];
const futures = [ from(async function* () { executionOrder.push(1); yield 1; return 10; }), from(async function* () { executionOrder.push(2); yield 2; return 20; }), from(async function* () { executionOrder.push(3); yield 3; return 30; }), ];
await scope(futures).toPromise();
// Should execute in order: 1, 2, 3 expect(executionOrder).toEqual([1, 2, 3]); });
it("should return array of all results", async () => { const futures = [ from(async function* () { yield 1; return 10; }), from(async function* () { yield 2; return 20; }), from(async function* () { yield 3; return 30; }), ];
const results = await scope(futures).toPromise();
expect(results).toEqual([10, 20, 30]); });
it("should yield intermediate values from each future", async () => { const futures = [ from(async function* () { yield 1; yield 2; return 10; }), from(async function* () { yield 3; yield 4; return 20; }), ];
const scopeFuture = scope(futures); const values: number[] = [];
for await (const value of scopeFuture) { values.push(value); }
expect(values).toEqual([1, 2, 3, 4]); });
it("should handle empty array of futures", async () => { const results = await scope([]).toPromise(); expect(results).toEqual([]); });
it("should handle single future", async () => { const futures = [ from(async function* () { yield 1; return 10; }), ];
const results = await scope(futures).toPromise(); expect(results).toEqual([10]); }); });
describe("Sequential Execution Timing", () => { it("should execute sequentially, not concurrently", async () => { let active = 0; let maxActive = 0;
const createFuture = (value: number) => from(async function* () { active++; maxActive = Math.max(maxActive, active);
yield value; await new Promise(resolve => setTimeout(resolve, 50));
active--; return value; });
const futures = [ createFuture(1), createFuture(2), createFuture(3), ];
await scope(futures).toPromise();
// Should never have more than 1 active expect(maxActive).toBe(1); });
it("should take sequential time, not parallel time", async () => { const delay = 50; const count = 3;
const startTime = Date.now();
const futures = Array.from({ length: count }, (_, i) => from(async function* () { await new Promise(resolve => setTimeout(resolve, delay)); return i; }) );
await scope(futures).toPromise();
const duration = Date.now() - startTime;
// Should take ~delay*count (sequential), not ~delay (parallel) expect(duration).toBeGreaterThan(delay * count * 0.8); }); });
describe("Error Handling", () => { it("should propagate errors from futures", async () => { const futures = [ from(async function* () { yield 1; return 10; }), from(async function* () { yield 2; throw new Error("Scope error"); }), from(async function* () { yield 3; return 30; }), ];
await expect(scope(futures).toPromise()).rejects.toThrow("Scope error"); });
it("should stop execution on error", async () => { const executionOrder: number[] = [];
const futures = [ from(async function* () { executionOrder.push(1); yield 1; return 10; }), from(async function* () { executionOrder.push(2); throw new Error("Early error"); }), from(async function* () { executionOrder.push(3); yield 3; return 30; }), ];
try { await scope(futures).toPromise(); } catch { // Expected }
// Third future should not execute expect(executionOrder).toEqual([1, 2]); expect(executionOrder).not.toContain(3); });
it("should handle error in first future", async () => { const futures = [ from(async function* () { throw new Error("First error"); }), from(async function* () { yield 2; return 20; }), ];
await expect(scope(futures).toPromise()).rejects.toThrow("First error"); });
it("should handle error in last future", async () => { const futures = [ from(async function* () { yield 1; return 10; }), from(async function* () { throw new Error("Last error"); }), ];
await expect(scope(futures).toPromise()).rejects.toThrow("Last error"); }); });
describe("Resource Management and Disposal", () => { it("should dispose futures in scope", async () => { let disposed1 = false; let disposed2 = false;
const futures = [ from(async function* (_, stack) { stack.defer(() => { disposed1 = true; }); yield 1; return 10; }), from(async function* (_, stack) { stack.defer(() => { disposed2 = true; }); yield 2; return 20; }), ];
const scopeFuture = scope(futures); await scopeFuture.toPromise(); await scopeFuture.dispose();
expect(disposed1).toBe(true); expect(disposed2).toBe(true); });
it("should cleanup on error", async () => { let disposed1 = false; let disposed2 = false;
const futures = [ from(async function* (_, stack) { stack.defer(() => { disposed1 = true; }); yield 1; return 10; }), from(async function* (_, stack) { stack.defer(() => { disposed2 = true; }); throw new Error("Cleanup error"); }), ];
const scopeFuture = scope(futures);
try { await scopeFuture.toPromise(); } catch { // Expected }
await scopeFuture.dispose();
// Both should be disposed expect(disposed1).toBe(true); expect(disposed2).toBe(true); });
it("should support using syntax for automatic disposal", async () => { let disposed = false;
{ await using scopeFuture = scope([ from(async function* (_, stack) { stack.defer(() => { disposed = true; }); yield 1; return 10; }), ]);
await scopeFuture.toPromise(); }
// Should be automatically disposed expect(disposed).toBe(true); }); });
describe("Iteration and Yielding", () => { it("should yield values in order from all futures", async () => { const futures = [ from(async function* () { yield 1; yield 2; return 10; }), from(async function* () { yield 3; yield 4; return 20; }), from(async function* () { yield 5; yield 6; return 30; }), ];
const scopeFuture = scope(futures); const values: number[] = [];
for await (const value of scopeFuture) { values.push(value); }
// Should get all yields in sequential order expect(values).toEqual([1, 2, 3, 4, 5, 6]); });
it("should handle futures with no yields", async () => { const futures = [ from(async function* () { return 10; }), from(async function* () { return 20; }), ];
const scopeFuture = scope(futures); const values: number[] = [];
for await (const value of scopeFuture) { values.push(value); }
expect(values).toEqual([]);
const results = await scopeFuture.toPromise(); expect(results).toEqual([10, 20]); });
it("should handle futures with only yields (no return)", async () => { const futures = [ from(async function* () { yield 1; yield 2; }), from(async function* () { yield 3; yield 4; }), ];
const scopeFuture = scope(futures); const values: number[] = [];
for await (const value of scopeFuture) { values.push(value); }
expect(values).toEqual([1, 2, 3, 4]); }); });
describe("Edge Cases", () => { it("should handle very long chains", async () => { const count = 100; const futures = Array.from({ length: count }, (_, i) => from(async function* () { yield i; return i * 10; }) );
const results = await scope(futures).toPromise();
expect(results).toHaveLength(count); expect(results[99]).toBe(990); });
it("should handle futures returning different types", async () => { const futures = [ from(async function* () { yield 1; return 10; }), from(async function* () { yield "two"; return "twenty"; }), from(async function* () { yield true; return false; }), ];
const results = await scope(futures).toPromise();
expect(results).toEqual([10, "twenty", false]); });
it("should handle async operations within futures", async () => { const futures = [ from(async function* () { await new Promise(resolve => setTimeout(resolve, 10)); yield 1; return 10; }), from(async function* () { await new Promise(resolve => setTimeout(resolve, 20)); yield 2; return 20; }), ];
const results = await scope(futures).toPromise();
expect(results).toEqual([10, 20]); });
it("should handle null and undefined returns", async () => { const futures = [ from(async function* () { yield 1; return null; }), from(async function* () { yield 2; return undefined; }), from(async function* () { yield 3; return 0; }), ];
const results = await scope(futures).toPromise();
expect(results).toEqual([null, undefined, 0]); });
it("should handle nested scopes", async () => { const innerScope1 = scope([ from(async function* () { yield 1; return 10; }), from(async function* () { yield 2; return 20; }), ]);
const innerScope2 = scope([ from(async function* () { yield 3; return 30; }), from(async function* () { yield 4; return 40; }), ]);
const outerScope = scope([innerScope1, innerScope2]); const results = await outerScope.toPromise();
expect(results).toEqual([[10, 20], [30, 40]]); }); });
describe("Cancellation and Control", () => { it("should support cancellation", async () => { const futures = [ from(async function* (abort) { yield 1; await new Promise(resolve => setTimeout(resolve, 50)); abort.signal.throwIfAborted(); return 10; }), from(async function* () { yield 2; return 20; }), ];
const scopeFuture = scope(futures); const promise = scopeFuture.toPromise();
setTimeout(() => scopeFuture.cancel(), 30);
await expect(promise).rejects.toThrow(); });
it("should support pause and resume", async () => { const futures = [ from(async function* () { yield 1; return 10; }), from(async function* () { yield 2; return 20; }), ];
const scopeFuture = scope(futures);
// Start iteration const iterator = scopeFuture[Symbol.asyncIterator](); const result1 = await iterator.next(); expect(result1.value).toBe(1);
// Pause scopeFuture.pause();
// Resume setTimeout(() => scopeFuture.resume(), 50);
const result2 = await iterator.next(); expect(result2.value).toBe(2); });
it("should propagate cancellation to inner futures", async () => { let future1Completed = false; let future2Started = false;
const futures = [ from(async function* (abort) { yield 1; await new Promise(resolve => setTimeout(resolve, 50)); abort.signal.throwIfAborted(); future1Completed = true; return 10; }), from(async function* () { future2Started = true; yield 2; return 20; }), ];
const scopeFuture = scope(futures); const promise = scopeFuture.toPromise();
setTimeout(() => scopeFuture.cancel(), 30);
try { await promise; } catch { // Expected }
// First future should not complete, second should not start expect(future1Completed).toBe(false); expect(future2Started).toBe(false); }); });
describe("Type Validation", () => { it("should throw error for non-iterable input", () => { // @ts-expect-error - Testing runtime error expect(() => scope(42 as any)).toThrow(); });
it("should throw error for null input", () => { // @ts-expect-error - Testing runtime error expect(() => scope(null as any)).toThrow(); });
it("should throw error for undefined input", () => { // @ts-expect-error - Testing runtime error expect(() => scope(undefined as any)).toThrow(); }); });
describe("Integration with Other Functions", () => { it("should work with cloned futures", async () => { const originalFuture = from(async function* () { yield 1; return 10; });
const futures = [ originalFuture, originalFuture.clone(), originalFuture.clone(), ];
const results = await scope(futures).toPromise();
expect(results).toEqual([10, 10, 10]); });
it("should work with reset futures", async () => { const future1 = from(async function* () { yield 1; return 10; });
// Execute once await future1.toPromise();
// Reset future1.reset();
const futures = [future1]; const results = await scope(futures).toPromise();
expect(results).toEqual([10]); });
it("should handle mix of new and completed futures", async () => { const future1 = from(async function* () { yield 1; return 10; });
const future2 = from(async function* () { yield 2; return 20; });
// Complete first future await future1.toPromise();
// Reset it future1.reset();
// Now use both in scope const results = await scope([future1, future2]).toPromise();
expect(results).toEqual([10, 20]); }); });});