Repository navigation
[repo-assist] test: add coverage for in-memory cache (Caching.fs) #493
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
sergey-tihon
merged 2 commits into
master
from
repo-assist/test-caching-coverage-c1a8c64f16c96479
Sep 9, 2026
Merged
Changes from 1 commit
Commits
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,111 @@ | ||
| namespace SwaggerProvider.Tests.CachingTests | ||
|
|
||
| open System | ||
| open System.Threading | ||
| open Xunit | ||
| open FsUnitTyped | ||
| open SwaggerProvider.Caching | ||
|
|
||
| /// Unit tests for the in-memory ICache implementation used by Provider.OpenApiClient.fs | ||
| /// to cache generated provided types keyed by schema/parameter combination. | ||
| module InMemoryCacheTests = | ||
|
|
||
| [<Fact>] | ||
| let ``TryRetrieve returns None for a key that was never set``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) | ||
| cache.TryRetrieve("missing") |> shouldEqual None | ||
|
|
||
| [<Fact>] | ||
| let ``Set followed by TryRetrieve returns the stored value``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) | ||
| cache.Set("key1", 42) | ||
| cache.TryRetrieve("key1") |> shouldEqual(Some 42) | ||
|
|
||
| [<Fact>] | ||
| let ``Set overwrites a previously stored value for the same key``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) | ||
| cache.Set("key1", "first") | ||
| cache.Set("key1", "second") | ||
| cache.TryRetrieve("key1") |> shouldEqual(Some "second") | ||
|
|
||
| [<Fact>] | ||
| let ``Remove deletes a stored value so TryRetrieve returns None``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) | ||
| cache.Set("key1", 1) | ||
| cache.Remove("key1") | ||
| cache.TryRetrieve("key1") |> shouldEqual None | ||
|
|
||
| [<Fact>] | ||
| let ``Remove on a missing key does not throw``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) | ||
| // Should be a no-op, not an exception. | ||
| cache.Remove("never-set") | ||
|
|
||
| [<Fact>] | ||
| let ``GetOrAdd calls the factory once and returns its result on first access``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) | ||
| let mutable calls = 0 | ||
|
|
||
| let result = | ||
| cache.GetOrAdd( | ||
| "key1", | ||
| fun () -> | ||
| calls <- calls + 1 | ||
| "computed" | ||
| ) | ||
|
|
||
| result |> shouldEqual "computed" | ||
| calls |> shouldEqual 1 | ||
|
|
||
| [<Fact>] | ||
| let ``GetOrAdd does not call the factory again once the value is cached``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) | ||
| let mutable calls = 0 | ||
|
|
||
| let factory() = | ||
| calls <- calls + 1 | ||
| calls | ||
|
|
||
| cache.GetOrAdd("key1", factory) |> shouldEqual 1 | ||
| // Second call should return the same cached value (1), not invoke the factory again (which would return 2). | ||
| cache.GetOrAdd("key1", factory) |> shouldEqual 1 | ||
| calls |> shouldEqual 1 | ||
|
|
||
| [<Fact>] | ||
| let ``GetOrAdd with different keys caches values independently``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) | ||
| cache.GetOrAdd("a", fun () -> "value-a") |> shouldEqual "value-a" | ||
| cache.GetOrAdd("b", fun () -> "value-b") |> shouldEqual "value-b" | ||
| cache.TryRetrieve("a") |> shouldEqual(Some "value-a") | ||
| cache.TryRetrieve("b") |> shouldEqual(Some "value-b") | ||
|
|
||
| [<Fact>] | ||
| let ``TryRetrieve returns None once the expiration window has elapsed``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMilliseconds 20.0) | ||
| cache.Set("key1", "value") | ||
| Thread.Sleep(200) | ||
| cache.TryRetrieve("key1") |> shouldEqual None | ||
|
|
||
| [<Fact>] | ||
| let ``TryRetrieve with extendCacheExpiration=true keeps the entry alive past the original expiration``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMilliseconds 150.0) | ||
| cache.Set("key1", "value") | ||
| // Read partway through the window and extend the expiration. | ||
| Thread.Sleep(80) | ||
|
|
||
| cache.TryRetrieve("key1", extendCacheExpiration = true) | ||
| |> shouldEqual(Some "value") | ||
| // Total elapsed time (80 + 100 = 180ms) exceeds the original 150ms window, | ||
| // but the extension at 80ms should have reset the clock, so it should still be present. | ||
| Thread.Sleep(100) | ||
| cache.TryRetrieve("key1") |> shouldEqual(Some "value") | ||
|
|
||
| [<Fact>] | ||
| let ``TryRetrieve without extendCacheExpiration does not reset expiration``() = | ||
| let cache = createInMemoryCache(TimeSpan.FromMilliseconds 100.0) | ||
| cache.Set("key1", "value") | ||
| Thread.Sleep(60) | ||
| cache.TryRetrieve("key1") |> shouldEqual(Some "value") | ||
| Thread.Sleep(80) | ||
| // Original 100ms window has elapsed (60 + 80 = 140ms) without extension. | ||
| cache.TryRetrieve("key1") |> shouldEqual None | ||
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.