|
| 1 | +import { test } from "@cross/test"; |
| 2 | +import { delay } from "@std/async/delay"; |
| 3 | +import { assertEquals } from "@std/assert"; |
| 4 | +import { repeatable } from "./repeatable.ts"; |
| 5 | + |
| 6 | +async function* delayedGenerator(sideEffect?: () => void) { |
| 7 | + yield 1; |
| 8 | + await delay(100); |
| 9 | + yield 2; |
| 10 | + await delay(100); |
| 11 | + yield 3; |
| 12 | + sideEffect?.(); |
| 13 | +} |
| 14 | + |
| 15 | +await test("repeatable should return the same sequence on multiple iterations", async () => { |
| 16 | + const input = delayedGenerator(); |
| 17 | + const it = repeatable(input); |
| 18 | + |
| 19 | + const result1 = await Array.fromAsync(it); |
| 20 | + const result2 = await Array.fromAsync(it); |
| 21 | + |
| 22 | + assertEquals(result1, [1, 2, 3], "First iteration"); |
| 23 | + assertEquals(result2, [1, 2, 3], "First iteration"); |
| 24 | +}); |
| 25 | + |
| 26 | +await test("repeatable should call internal iterator only once", async () => { |
| 27 | + let called = 0; |
| 28 | + const input = delayedGenerator(() => called++); |
| 29 | + const it = repeatable(input); |
| 30 | + |
| 31 | + const result1 = await Array.fromAsync(it); |
| 32 | + const result2 = await Array.fromAsync(it); |
| 33 | + |
| 34 | + assertEquals(result1, [1, 2, 3], "First iteration"); |
| 35 | + assertEquals(result2, [1, 2, 3], "First iteration"); |
| 36 | + assertEquals(called, 1, "Internal iterator called only once"); |
| 37 | +}); |
| 38 | + |
| 39 | +await test("repeatable should work correctly when consumed partially and then fully", async () => { |
| 40 | + const input = delayedGenerator(); |
| 41 | + const it = repeatable(input); |
| 42 | + |
| 43 | + const result1: number[] = []; |
| 44 | + const firstIter = it[Symbol.asyncIterator](); |
| 45 | + |
| 46 | + result1.push((await firstIter.next()).value); // 1 |
| 47 | + |
| 48 | + const result2 = await Array.fromAsync(it); |
| 49 | + |
| 50 | + result1.push((await firstIter.next()).value); // 2 |
| 51 | + result1.push((await firstIter.next()).value); // 3 |
| 52 | + |
| 53 | + assertEquals(result1, [1, 2, 3], "First iteration"); |
| 54 | + assertEquals(result2, [1, 2, 3], "First iteration"); |
| 55 | +}); |
| 56 | + |
| 57 | +await test("repeatable should cache values and return them immediately on subsequent iterations", async () => { |
| 58 | + const input = delayedGenerator(); |
| 59 | + const it = repeatable(input); |
| 60 | + |
| 61 | + const start = performance.now(); |
| 62 | + const result1 = await Array.fromAsync(it); |
| 63 | + const end1 = performance.now(); |
| 64 | + const timeTaken1 = end1 - start; |
| 65 | + |
| 66 | + const start2 = performance.now(); |
| 67 | + const result2 = await Array.fromAsync(it); |
| 68 | + const end2 = performance.now(); |
| 69 | + const timeTaken2 = end2 - start2; |
| 70 | + |
| 71 | + assertEquals(result1, [1, 2, 3], "First iteration"); |
| 72 | + assertEquals(result2, [1, 2, 3], "Second iteration"); |
| 73 | + |
| 74 | + console.debug("Time taken for first consume:", timeTaken1); |
| 75 | + console.debug("Time taken for second consume (with cache):", timeTaken2); |
| 76 | + |
| 77 | + if (timeTaken2 > timeTaken1 / 10) { |
| 78 | + throw new Error( |
| 79 | + "Second consume took too long, cache might not be working.", |
| 80 | + ); |
| 81 | + } |
| 82 | +}); |
0 commit comments