Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions engine-bridge/src/__tests__/chain-state-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@

function makeRpc(): RpcClient {
const rpc = new RpcClient(["http://test"]);
rpc.call = async (fn: any) => {

Check warning on line 6 in engine-bridge/src/__tests__/chain-state-cache.test.ts

View workflow job for this annotation

GitHub Actions / engine-bridge

Unexpected any. Specify a different type
return fn({});
};
return rpc;
}

function deferred<T>(): {
promise: Promise<T>;
resolve: (value: T) => void;
} {
let resolve!: (value: T) => void;
const promise = new Promise<T>(resolvePromise => {
resolve = resolvePromise;
});
return { promise, resolve };
}

describe("ChainStateCache", () => {
describe("bounded cache (maxEntries)", () => {
it("caps cache size at maxEntries when more distinct keys are inserted", async () => {
Expand All @@ -21,7 +32,7 @@
await cache.getSwr("c", fetcher);
await cache.getSwr("d", fetcher);

expect((cache as any).cache.size).toBe(3);

Check warning on line 35 in engine-bridge/src/__tests__/chain-state-cache.test.ts

View workflow job for this annotation

GitHub Actions / engine-bridge

Unexpected any. Specify a different type
});

it("evicts the least recently used entry", async () => {
Expand All @@ -36,9 +47,9 @@

await cache.getSwr("c", fetcher);

expect((cache as any).cache.has("a")).toBe(true);

Check warning on line 50 in engine-bridge/src/__tests__/chain-state-cache.test.ts

View workflow job for this annotation

GitHub Actions / engine-bridge

Unexpected any. Specify a different type
expect((cache as any).cache.has("b")).toBe(false);

Check warning on line 51 in engine-bridge/src/__tests__/chain-state-cache.test.ts

View workflow job for this annotation

GitHub Actions / engine-bridge

Unexpected any. Specify a different type
expect((cache as any).cache.has("c")).toBe(true);

Check warning on line 52 in engine-bridge/src/__tests__/chain-state-cache.test.ts

View workflow job for this annotation

GitHub Actions / engine-bridge

Unexpected any. Specify a different type
});

it("does not evict when under maxEntries", async () => {
Expand All @@ -50,8 +61,43 @@
await cache.getSwr("b", fetcher);
await cache.getSwr("c", fetcher);

expect((cache as any).cache.size).toBe(3);

Check warning on line 64 in engine-bridge/src/__tests__/chain-state-cache.test.ts

View workflow job for this annotation

GitHub Actions / engine-bridge

Unexpected any. Specify a different type
});

it("reapplies the LRU bound when revalidation resurrects an evicted entry", async () => {
const rpc = makeRpc();
const cache = new ChainStateCache(rpc, -1, 2);
const initialFetcher = async () => ({ value: "initial" });

await cache.getSwr("a", initialFetcher);
await cache.getSwr("b", initialFetcher);

const refreshStarted = deferred<void>();
const refreshResult = deferred<{ value: string }>();
const stale = await cache.getSwr("a", async () => {
refreshStarted.resolve(undefined);
return refreshResult.promise;
});
await refreshStarted.promise;
expect(stale).toEqual({ value: "initial" });

await cache.getSwr("c", initialFetcher);
await cache.getSwr("d", initialFetcher);

const cacheEntries = (
cache as unknown as {
cache: Map<string, { data: { value: string } }>;
}
).cache;
expect([...cacheEntries.keys()]).toEqual(["c", "d"]);

refreshResult.resolve({ value: "refreshed" });
await new Promise<void>(resolve => setImmediate(resolve));

expect(cacheEntries.size).toBe(2);
expect([...cacheEntries.keys()]).toEqual(["d", "a"]);
expect(cacheEntries.get("a")?.data).toEqual({ value: "refreshed" });
});
});

describe("SWR behavior", () => {
Expand Down
1 change: 1 addition & 0 deletions engine-bridge/src/chain-state-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class ChainStateCache {
try {
const data = await fetcher(this.rpc);
this.cache.set(key, { data, updatedAt: Date.now(), isRevalidating: false });
this.evictIfNeeded();
} catch (error) {
const item = this.cache.get(key);
if (item) {
Expand Down
Loading