|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import * as Deferred from "effect/Deferred"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Exit from "effect/Exit"; |
| 5 | +import * as Fiber from "effect/Fiber"; |
| 6 | +import * as Ref from "effect/Ref"; |
| 7 | +import * as Scope from "effect/Scope"; |
| 8 | +import * as TestClock from "effect/testing/TestClock"; |
| 9 | + |
| 10 | +import { makeStaleWhileRevalidate } from "./staleWhileRevalidate.ts"; |
| 11 | + |
| 12 | +it.effect("computes inline once, then answers from memory until the ttl lapses", () => |
| 13 | + Effect.gen(function* () { |
| 14 | + const calls = yield* Ref.make(0); |
| 15 | + const read = yield* makeStaleWhileRevalidate( |
| 16 | + Ref.updateAndGet(calls, (count) => count + 1), |
| 17 | + "60 seconds", |
| 18 | + ); |
| 19 | + |
| 20 | + assert.equal(yield* read, 1); |
| 21 | + yield* TestClock.adjust("59 seconds"); |
| 22 | + assert.equal(yield* read, 1); |
| 23 | + assert.equal(yield* Ref.get(calls), 1); |
| 24 | + }).pipe(Effect.scoped), |
| 25 | +); |
| 26 | + |
| 27 | +it.effect("serves the stale value while one background refresh replaces it", () => |
| 28 | + Effect.gen(function* () { |
| 29 | + const calls = yield* Ref.make(0); |
| 30 | + const release = yield* Deferred.make<void>(); |
| 31 | + const read = yield* makeStaleWhileRevalidate( |
| 32 | + Effect.gen(function* () { |
| 33 | + const call = yield* Ref.updateAndGet(calls, (count) => count + 1); |
| 34 | + if (call > 1) yield* Deferred.await(release); |
| 35 | + return call; |
| 36 | + }), |
| 37 | + "60 seconds", |
| 38 | + ); |
| 39 | + |
| 40 | + assert.equal(yield* read, 1); |
| 41 | + yield* TestClock.adjust("61 seconds"); |
| 42 | + // The refresh is blocked, yet callers are answered immediately and only |
| 43 | + // one refresh starts however many callers arrive. |
| 44 | + assert.equal(yield* read, 1); |
| 45 | + assert.equal(yield* read, 1); |
| 46 | + yield* TestClock.adjust("1 second"); |
| 47 | + assert.equal(yield* Ref.get(calls), 2); |
| 48 | + |
| 49 | + yield* Deferred.succeed(release, undefined); |
| 50 | + yield* TestClock.adjust("1 second"); |
| 51 | + assert.equal(yield* read, 2); |
| 52 | + }), |
| 53 | +); |
| 54 | + |
| 55 | +it.effect("does not cache an interrupted first computation", () => |
| 56 | + Effect.gen(function* () { |
| 57 | + const calls = yield* Ref.make(0); |
| 58 | + const read = yield* makeStaleWhileRevalidate( |
| 59 | + Ref.updateAndGet(calls, (count) => count + 1).pipe( |
| 60 | + Effect.flatMap((call) => (call === 1 ? Effect.never : Effect.succeed(call))), |
| 61 | + ), |
| 62 | + "60 seconds", |
| 63 | + ); |
| 64 | + |
| 65 | + const first = yield* read.pipe(Effect.forkChild); |
| 66 | + yield* Effect.yieldNow; |
| 67 | + yield* Fiber.interrupt(first); |
| 68 | + |
| 69 | + assert.equal(yield* read, 2); |
| 70 | + }), |
| 71 | +); |
| 72 | + |
| 73 | +it.effect("abandons a hung refresh so a later call can start another", () => |
| 74 | + Effect.gen(function* () { |
| 75 | + const calls = yield* Ref.make(0); |
| 76 | + const read = yield* makeStaleWhileRevalidate( |
| 77 | + Ref.updateAndGet(calls, (count) => count + 1).pipe( |
| 78 | + Effect.flatMap((call) => (call === 2 ? Effect.never : Effect.succeed(call))), |
| 79 | + ), |
| 80 | + "60 seconds", |
| 81 | + ); |
| 82 | + |
| 83 | + assert.equal(yield* read, 1); |
| 84 | + yield* TestClock.adjust("61 seconds"); |
| 85 | + assert.equal(yield* read, 1); |
| 86 | + yield* TestClock.adjust("1 second"); |
| 87 | + assert.equal(yield* Ref.get(calls), 2); |
| 88 | + |
| 89 | + // The hung refresh times out, releasing the claim for the next caller. |
| 90 | + yield* TestClock.adjust("30 seconds"); |
| 91 | + assert.equal(yield* read, 1); |
| 92 | + yield* TestClock.adjust("1 second"); |
| 93 | + assert.equal(yield* Ref.get(calls), 3); |
| 94 | + assert.equal(yield* read, 3); |
| 95 | + }).pipe(Effect.scoped), |
| 96 | +); |
| 97 | + |
| 98 | +it.effect("interrupts a background refresh when the owning scope closes", () => |
| 99 | + Effect.gen(function* () { |
| 100 | + const interrupted = yield* Deferred.make<void>(); |
| 101 | + const scope = yield* Scope.make(); |
| 102 | + const calls = yield* Ref.make(0); |
| 103 | + const read = yield* makeStaleWhileRevalidate( |
| 104 | + Ref.updateAndGet(calls, (count) => count + 1).pipe( |
| 105 | + Effect.flatMap((call) => |
| 106 | + call === 1 |
| 107 | + ? Effect.succeed(call) |
| 108 | + : Effect.never.pipe(Effect.onInterrupt(() => Deferred.succeed(interrupted, undefined))), |
| 109 | + ), |
| 110 | + ), |
| 111 | + "60 seconds", |
| 112 | + ).pipe(Scope.provide(scope)); |
| 113 | + |
| 114 | + yield* read; |
| 115 | + yield* TestClock.adjust("61 seconds"); |
| 116 | + yield* read; |
| 117 | + yield* TestClock.adjust("1 second"); |
| 118 | + yield* Scope.close(scope, Exit.void); |
| 119 | + yield* Deferred.await(interrupted); |
| 120 | + }), |
| 121 | +); |
0 commit comments