Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Deferred from "effect/Deferred";
3import * as Effect from "effect/Effect";
4import * as Fiber from "effect/Fiber";
5import * as Ref from "effect/Ref";
6
7import * as ResetCreditCoordinator from "./resetCreditCoordinator.ts";
8
9describe("ResetCreditCoordinator", () => {
10 it.effect("re-sends the same idempotency key after a failed attempt, then clears it", () =>
11 Effect.gen(function* () {
12 const { redeem } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
13 const keys = yield* Ref.make<ReadonlyArray<string>>([]);
14 const attempts = yield* Ref.make(0);
15 const consume = (key: string) =>
16 Effect.gen(function* () {
17 yield* Ref.update(keys, (seen) => [...seen, key]);
18 const attempt = yield* Ref.updateAndGet(attempts, (n) => n + 1);
19 if (attempt === 1) return yield* Effect.fail("timed out" as const);
20 return "reset" as const;
21 });
22
23 const first = yield* redeem("acct", consume).pipe(Effect.result);
24 assert.isTrue(first._tag === "Failure");
25 const second = yield* redeem("acct", consume);
26 assert.strictEqual(second, "reset");
27 yield* redeem("acct", consume);
28
29 const seen = yield* Ref.get(keys);
30 assert.strictEqual(seen.length, 3);
31 assert.strictEqual(seen[0], seen[1]);
32 assert.notStrictEqual(seen[1], seen[2]);
33 }).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
34 );
35
36 it.effect("starts a fresh attempt after a settled failure", () =>
37 Effect.gen(function* () {
38 const { redeem } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
39 const keys = yield* Ref.make<ReadonlyArray<string>>([]);
40 const consume = (key: string) =>
41 Ref.update(keys, (seen) => [...seen, key]).pipe(
42 Effect.andThen(Effect.fail("cooldown" as const)),
43 );
44 const isSettled = (error: "cooldown") => error === "cooldown";
45
46 yield* redeem("acct", consume, isSettled).pipe(Effect.result);
47 yield* redeem("acct", consume, isSettled).pipe(Effect.result);
48
49 const seen = yield* Ref.get(keys);
50 assert.strictEqual(seen.length, 2);
51 assert.notStrictEqual(seen[0], seen[1]);
52 }).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
53 );
54
55 it.effect("serialises concurrent redemptions on the same account, not per caller", () =>
56 Effect.gen(function* () {
57 const { redeem } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
58 const release = yield* Deferred.make<void>();
59 const inFlight = yield* Ref.make(0);
60 const peak = yield* Ref.make(0);
61 const consume = () =>
62 Effect.gen(function* () {
63 const now = yield* Ref.updateAndGet(inFlight, (n) => n + 1);
64 yield* Ref.update(peak, (p) => Math.max(p, now));
65 yield* Deferred.await(release);
66 yield* Ref.update(inFlight, (n) => n - 1);
67 return "reset" as const;
68 });
69
70 const a = yield* redeem("acct", consume).pipe(Effect.forkChild);
71 const b = yield* redeem("acct", consume).pipe(Effect.forkChild);
72 yield* Effect.yieldNow;
73 yield* Deferred.succeed(release, undefined);
74 yield* Fiber.join(a);
75 yield* Fiber.join(b);
76
77 assert.strictEqual(yield* Ref.get(peak), 1);
78 }).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
79 );
80
81 it.effect("keeps different accounts independent", () =>
82 Effect.gen(function* () {
83 const { redeem } = yield* ResetCreditCoordinator.ResetCreditCoordinator;
84 const release = yield* Deferred.make<void>();
85 const peak = yield* Ref.make(0);
86 const inFlight = yield* Ref.make(0);
87 const consume = () =>
88 Effect.gen(function* () {
89 const now = yield* Ref.updateAndGet(inFlight, (n) => n + 1);
90 yield* Ref.update(peak, (p) => Math.max(p, now));
91 yield* Deferred.await(release);
92 return "reset" as const;
93 });
94 const a = yield* redeem("acct-a", consume).pipe(Effect.forkChild);
95 const b = yield* redeem("acct-b", consume).pipe(Effect.forkChild);
96 yield* Effect.yieldNow;
97 yield* Deferred.succeed(release, undefined);
98 yield* Fiber.join(a);
99 yield* Fiber.join(b);
100 assert.strictEqual(yield* Ref.get(peak), 2);
101 }).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
102 );
103});