forked from RhysSullivan/executor
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(sdk): refresh cache writes in LRU order #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import { Effect } from "effect"; | ||
|
|
||
| import { createExecutor, type Executor } from "./executor"; | ||
| import { Tenant } from "./ids"; | ||
|
|
||
| // Keep in sync with the unexported fallback cache defaults in executor.ts. | ||
| const MEMORY_CACHE_CAPACITY = 2_048; | ||
| const MEMORY_CACHE_TTL_MS = 10 * 60 * 1000; | ||
|
|
||
| const makeExecutor = Effect.acquireRelease( | ||
| createExecutor({ | ||
| tenant: Tenant.make("test-tenant"), | ||
| onElicitation: "accept-all", | ||
| }), | ||
| (executor) => executor.close().pipe(Effect.ignore), | ||
| ); | ||
|
|
||
| const withFakeNow = <A, E, R>( | ||
| initialNow: number, | ||
| run: (clock: { readonly advance: (ms: number) => void }) => Effect.Effect<A, E, R>, | ||
| ): Effect.Effect<A, E, R> => | ||
| Effect.acquireUseRelease( | ||
| Effect.sync(() => { | ||
| const originalNow = Date.now; | ||
| let now = initialNow; | ||
| Date.now = () => now; | ||
| return { | ||
| advance: (ms: number) => { | ||
| now += ms; | ||
| }, | ||
| restore: () => { | ||
| Date.now = originalNow; | ||
| }, | ||
| }; | ||
| }), | ||
| (clock) => run({ advance: clock.advance }), | ||
| (clock) => Effect.sync(clock.restore), | ||
| ); | ||
|
|
||
| describe("executor cache", () => { | ||
| it.effect("uses an in-memory fallback when no cache is configured", () => | ||
| Effect.scoped( | ||
| Effect.gen(function* () { | ||
| const executor = yield* makeExecutor; | ||
|
|
||
| yield* executor.cache.set("a", "value"); | ||
| expect(yield* executor.cache.get("a")).toBe("value"); | ||
|
|
||
| yield* executor.cache.remove("a"); | ||
| expect(yield* executor.cache.get("a")).toBeUndefined(); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect("expires fallback entries by TTL on get and size", () => | ||
| withFakeNow(1_000, ({ advance }) => | ||
| Effect.scoped( | ||
| Effect.gen(function* () { | ||
| const executor = yield* makeExecutor; | ||
|
|
||
| yield* executor.cache.set("a", "value"); | ||
| expect(yield* executor.cache.size).toBe(1); | ||
|
|
||
| advance(MEMORY_CACHE_TTL_MS); | ||
|
|
||
| expect(yield* executor.cache.get("a")).toBeUndefined(); | ||
| expect(yield* executor.cache.size).toBe(0); | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect("refreshes fallback LRU position when an existing key is written", () => | ||
| Effect.scoped( | ||
| Effect.gen(function* () { | ||
| const executor: Executor = yield* makeExecutor; | ||
|
|
||
| yield* executor.cache.set("a", "old"); | ||
| for (let index = 0; index < MEMORY_CACHE_CAPACITY - 1; index += 1) { | ||
| yield* executor.cache.set(`key-${index}`, String(index)); | ||
| } | ||
|
|
||
| yield* executor.cache.set("a", "new"); | ||
| yield* executor.cache.set("overflow", "value"); | ||
|
|
||
| expect(yield* executor.cache.get("a")).toBe("new"); | ||
| expect(yield* executor.cache.get("key-0")).toBeUndefined(); | ||
| expect(yield* executor.cache.get("key-1")).toBe("1"); | ||
| }), | ||
| ), | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.