Open History to see the last commit
1import * as NodeV8 from "node:v8";
2import * as NodeVM from "node:vm";
3import { assert, it } from "@effect/vitest";
4import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import * as Layer from "effect/Layer";
7import { PullRequestOperationError } from "@t3tools/contracts";
8import * as Deferred from "effect/Deferred";
9import * as Duration from "effect/Duration";
10import * as Effect from "effect/Effect";
11import * as Fiber from "effect/Fiber";
12import * as FileSystem from "effect/FileSystem";
13import * as Option from "effect/Option";
14import * as Path from "effect/Path";
15import * as KeyValueStore from "effect/persistence/KeyValueStore";
16import * as TestClock from "effect/testing/TestClock";
17import * as PullRequestReadCache from "./PullRequestReadCache.ts";
18
19NodeV8.setFlagsFromString("--expose-gc");
20const collectGarbage = NodeVM.runInNewContext("gc") as () => void;
21
22const cacheLayer = (directory: string) =>
23 PullRequestReadCache.make.pipe(Effect.provide(KeyValueStore.layerFileSystem(directory)));
24
25it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PR filesystem cache", (it) => {
26 it.effect("clears in-flight reads before a new service can reuse them", () =>
27 Effect.gen(function* () {
28 const fs = yield* FileSystem.FileSystem;
29 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
30 const started = yield* Deferred.make<void>();
31 const release = yield* Deferred.make<void>();
32 const cache = yield* cacheLayer(directory);
33 const read = yield* cache
34 .get(
35 "summary",
36 Deferred.succeed(started, undefined).pipe(
37 Effect.andThen(Deferred.await(release)),
38 Effect.as("old"),
39 ),
40 ["pr"],
41 )
42 .pipe(Effect.forkChild);
43 yield* Deferred.await(started);
44 const invalidate = yield* cache
45 .invalidate("pr")
46 .pipe(Effect.forkChild({ startImmediately: true }));
47 // Another PR's invalidation does not wait on this read; this one's does.
48 yield* cache.invalidate("other-pr");
49 assert.isUndefined(invalidate.pollUnsafe());
50 yield* Deferred.succeed(release, undefined);
51 yield* Fiber.join(read);
52 yield* Fiber.join(invalidate);
53 const restarted = yield* cacheLayer(directory);
54 assert.strictEqual(yield* restarted.get("summary", Effect.succeed("new"), ["pr"]), "new");
55 }),
56 );
57
58 it.effect("prunes expired entry files the store wrote", () =>
59 Effect.gen(function* () {
60 const fs = yield* FileSystem.FileSystem;
61 const path = yield* Path.Path;
62 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
63 const cache = yield* cacheLayer(directory);
64 yield* cache.get("summary", Effect.succeed("value"));
65 const [entry] = yield* fs.readDirectory(directory);
66 assert.isDefined(entry);
67 const info = yield* fs.stat(path.join(directory, entry));
68 yield* TestClock.setTime(
69 Option.getOrThrow(info.mtime).getTime() + Duration.toMillis(Duration.days(2)),
70 );
71 yield* PullRequestReadCache.pruneExpiredEntryFiles(directory);
72 assert.deepStrictEqual(yield* fs.readDirectory(directory), []);
73 }),
74 );
75
76 it.effect("invalidates only the changed scope across restarts and coalesces its next reads", () =>
77 Effect.gen(function* () {
78 const fs = yield* FileSystem.FileSystem;
79 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
80 let reads = 0;
81 const lookup = Effect.sync(() => String(++reads));
82 const cache = yield* cacheLayer(directory);
83 yield* cache.get("first", lookup, ["project", "pr-1"]);
84 yield* cache.get("second", lookup, ["project", "pr-2"]);
85 yield* cache.get("third", lookup, ["other-project", "pr-3"]);
86 yield* cache.invalidate("pr-1");
87 const restarted = yield* cacheLayer(directory);
88 const answers = yield* Effect.all(
89 Array.from({ length: 10 }, () => restarted.get("first", lookup, ["project", "pr-1"])),
90 { concurrency: 10 },
91 );
92 assert.deepStrictEqual(answers, Array(10).fill("4"));
93 assert.strictEqual(yield* restarted.get("second", lookup, ["project", "pr-2"]), "2");
94 yield* restarted.invalidate("project");
95 const again = yield* cacheLayer(directory);
96 assert.strictEqual(yield* again.get("second", lookup, ["project", "pr-2"]), "5");
97 assert.strictEqual(yield* again.get("third", lookup, ["other-project", "pr-3"]), "3");
98 assert.strictEqual(reads, 5);
99 const files = (yield* fs.readDirectory(directory)).length;
100 for (let index = 0; index < 3; index++) {
101 yield* again.invalidate("pr-1");
102 yield* again.get("first", lookup, ["project", "pr-1"]);
103 }
104 assert.strictEqual((yield* fs.readDirectory(directory)).length, files);
105 }),
106 );
107
108 it.effect("shares a pending refresh without blocking an unrelated cached PR", () =>
109 Effect.gen(function* () {
110 const cache = yield* PullRequestReadCache.make;
111 yield* cache.get("first", Effect.succeed("old"), ["pr-1"]);
112 yield* cache.get("second", Effect.succeed("warm"), ["pr-2"]);
113 yield* cache.invalidate("pr-1");
114 const started = yield* Deferred.make<void>();
115 const release = yield* Deferred.make<void>();
116 let reads = 0;
117 const refresh = cache.get(
118 "first",
119 Effect.gen(function* () {
120 reads++;
121 yield* Deferred.succeed(started, undefined);
122 yield* Deferred.await(release);
123 return "fresh";
124 }),
125 ["pr-1"],
126 );
127 const pending = yield* Effect.all(
128 Array.from({ length: 10 }, () => refresh),
129 {
130 concurrency: 10,
131 },
132 ).pipe(Effect.forkChild);
133 yield* Deferred.await(started);
134 assert.strictEqual(yield* cache.get("second", Effect.die("cache miss"), ["pr-2"]), "warm");
135 yield* Deferred.succeed(release, undefined);
136 assert.deepStrictEqual(yield* Fiber.join(pending), Array(10).fill("fresh"));
137 assert.strictEqual(reads, 1);
138 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
139 );
140
141 it.effect("does not persist failed GitHub reads", () =>
142 Effect.gen(function* () {
143 const fs = yield* FileSystem.FileSystem;
144 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
145 const cache = yield* cacheLayer(directory);
146 const error = new PullRequestOperationError({ operation: "summary", detail: "unavailable" });
147 yield* cache.get("summary", Effect.fail(error)).pipe(Effect.flip);
148 const restarted = yield* cacheLayer(directory);
149 assert.strictEqual(yield* restarted.get("summary", Effect.succeed("recovered")), "recovered");
150 }),
151 );
152
153 it.effect.each(["", "{", '{"pr":{"revision":42}}'])(
154 "recovers from malformed scope revisions without reusing stale entries: %j",
155 (corrupt) =>
156 Effect.gen(function* () {
157 const fs = yield* FileSystem.FileSystem;
158 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" });
159 const original = yield* cacheLayer(directory);
160 yield* original.get("summary", Effect.succeed("stale"), ["pr"]);
161 yield* fs.writeFileString(directory + "/revisions", corrupt);
162 const recovered = yield* cacheLayer(directory);
163 let reads = 0;
164 const lookup = Effect.sync(() => String(++reads));
165 assert.strictEqual(yield* recovered.get("summary", lookup, ["pr"]), "1");
166 assert.strictEqual(yield* recovered.get("summary", lookup, ["pr"]), "1");
167 yield* recovered.invalidate("pr");
168 assert.strictEqual(yield* recovered.get("summary", lookup, ["pr"]), "2");
169 const restarted = yield* cacheLayer(directory);
170 assert.strictEqual(yield* restarted.get("summary", lookup, ["pr"]), "2");
171 assert.strictEqual(reads, 2);
172 }),
173 );
174 it.effect("resumes caching after a failed scope read", () =>
175 Effect.gen(function* () {
176 const backing = yield* KeyValueStore.KeyValueStore;
177 let fail = true;
178 let reads = 0;
179 const cache = yield* PullRequestReadCache.make.pipe(
180 Effect.provideService(KeyValueStore.KeyValueStore, {
181 ...backing,
182 get: (key) =>
183 Effect.suspend(() => {
184 if (!fail) return backing.get(key);
185 fail = false;
186 return Effect.fail(
187 new KeyValueStore.KeyValueStoreError({ method: "get", message: "unavailable" }),
188 );
189 }),
190 }),
191 );
192 const read = cache.get(
193 "summary",
194 Effect.sync(() => String(++reads)),
195 ["pr"],
196 );
197 assert.strictEqual(yield* read, "1");
198 assert.strictEqual(yield* read, "2");
199 assert.strictEqual(yield* read, "2");
200 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
201 );
202
203 it.effect("does not keep a caller's data alive after its reads and invalidations", () =>
204 Effect.gen(function* () {
205 const cache = yield* PullRequestReadCache.make;
206 const sweeps: Array<WeakRef<object>> = [];
207 // Like a sync sweep: an Effect.fn made per run that closes over that run's data.
208 const sweep = Effect.fn("sweep")(function* (index: number) {
209 const groups = new Map<string, number>(
210 Array.from({ length: 1_000 }, (_, id) => [`group-${id}`, id]),
211 );
212 sweeps.push(new WeakRef(groups));
213 const read = Effect.fn("read")(function* (pr: string) {
214 const size = groups.size;
215 yield* cache.invalidate(pr);
216 const answer = yield* cache.get(`${pr}-${index}`, Effect.succeed("answer"), [pr]);
217 return size + answer.length;
218 });
219 yield* Effect.forEach(["pr-1", "pr-2"], read, { discard: true });
220 });
221 for (let index = 0; index < 5; index++) yield* sweep(index);
222 // WeakRef targets stay alive until the current job ends, so yield before collecting.
223 yield* Effect.yieldNow;
224 collectGarbage();
225 assert.strictEqual(sweeps.filter((sweepData) => sweepData.deref() !== undefined).length, 0);
226 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
227 );
228
229 it.effect("cancels abandoned reads without blocking invalidation", () =>
230 Effect.gen(function* () {
231 const cache = yield* PullRequestReadCache.make;
232 const started = yield* Deferred.make<void>();
233 const read = yield* cache
234 .get("summary", Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)), [
235 "pr",
236 ])
237 .pipe(Effect.forkChild);
238 yield* Deferred.await(started);
239 yield* Fiber.interrupt(read);
240 yield* cache.invalidate("pr");
241 assert.strictEqual(yield* cache.get("summary", Effect.succeed("fresh"), ["pr"]), "fresh");
242 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
243 );
244
245 it.effect(
246 "finishes the in-memory revision update when invalidation is canceled after writing",
247 () =>
248 Effect.gen(function* () {
249 const backing = yield* KeyValueStore.KeyValueStore;
250 const written = yield* Deferred.make<void>();
251 const release = yield* Deferred.make<void>();
252 const cache = yield* PullRequestReadCache.make.pipe(
253 Effect.provideService(KeyValueStore.KeyValueStore, {
254 ...backing,
255 set: (key, value) =>
256 backing
257 .set(key, value)
258 .pipe(
259 Effect.andThen(
260 key === "revisions"
261 ? Deferred.succeed(written, undefined).pipe(
262 Effect.andThen(Deferred.await(release)),
263 )
264 : Effect.void,
265 ),
266 ),
267 }),
268 );
269 yield* cache.get("summary", Effect.succeed("old"), ["pr"]);
270 const invalidation = yield* cache.invalidate("pr").pipe(Effect.forkChild);
271 yield* Deferred.await(written);
272 const interrupt = yield* Fiber.interrupt(invalidation).pipe(
273 Effect.forkChild({ startImmediately: true }),
274 );
275 yield* Deferred.succeed(release, undefined);
276 yield* Fiber.join(interrupt);
277 assert.strictEqual(yield* cache.get("summary", Effect.succeed("fresh"), ["pr"]), "fresh");
278 }).pipe(Effect.provide(KeyValueStore.layerMemory)),
279 );
280});