|
| 1 | +namespace SwaggerProvider.Tests.CachingTests |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Threading |
| 5 | +open Xunit |
| 6 | +open FsUnitTyped |
| 7 | +open SwaggerProvider.Caching |
| 8 | + |
| 9 | +/// Unit tests for the in-memory ICache implementation used by Provider.OpenApiClient.fs |
| 10 | +/// to cache generated provided types keyed by schema/parameter combination. |
| 11 | +module InMemoryCacheTests = |
| 12 | + |
| 13 | + [<Fact>] |
| 14 | + let ``TryRetrieve returns None for a key that was never set``() = |
| 15 | + let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) |
| 16 | + cache.TryRetrieve("missing") |> shouldEqual None |
| 17 | + |
| 18 | + [<Fact>] |
| 19 | + let ``Set followed by TryRetrieve returns the stored value``() = |
| 20 | + let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) |
| 21 | + cache.Set("key1", 42) |
| 22 | + cache.TryRetrieve("key1") |> shouldEqual(Some 42) |
| 23 | + |
| 24 | + [<Fact>] |
| 25 | + let ``Set overwrites a previously stored value for the same key``() = |
| 26 | + let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) |
| 27 | + cache.Set("key1", "first") |
| 28 | + cache.Set("key1", "second") |
| 29 | + cache.TryRetrieve("key1") |> shouldEqual(Some "second") |
| 30 | + |
| 31 | + [<Fact>] |
| 32 | + let ``Remove deletes a stored value so TryRetrieve returns None``() = |
| 33 | + let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) |
| 34 | + cache.Set("key1", 1) |
| 35 | + cache.Remove("key1") |
| 36 | + cache.TryRetrieve("key1") |> shouldEqual None |
| 37 | + |
| 38 | + [<Fact>] |
| 39 | + let ``Remove on a missing key does not throw``() = |
| 40 | + let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) |
| 41 | + // Should be a no-op, not an exception. |
| 42 | + cache.Remove("never-set") |
| 43 | + |
| 44 | + [<Fact>] |
| 45 | + let ``GetOrAdd calls the factory once and returns its result on first access``() = |
| 46 | + let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) |
| 47 | + let mutable calls = 0 |
| 48 | + |
| 49 | + let result = |
| 50 | + cache.GetOrAdd( |
| 51 | + "key1", |
| 52 | + fun () -> |
| 53 | + calls <- calls + 1 |
| 54 | + "computed" |
| 55 | + ) |
| 56 | + |
| 57 | + result |> shouldEqual "computed" |
| 58 | + calls |> shouldEqual 1 |
| 59 | + |
| 60 | + [<Fact>] |
| 61 | + let ``GetOrAdd does not call the factory again once the value is cached``() = |
| 62 | + let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) |
| 63 | + let mutable calls = 0 |
| 64 | + |
| 65 | + let factory() = |
| 66 | + calls <- calls + 1 |
| 67 | + calls |
| 68 | + |
| 69 | + cache.GetOrAdd("key1", factory) |> shouldEqual 1 |
| 70 | + // Second call should return the same cached value (1), not invoke the factory again (which would return 2). |
| 71 | + cache.GetOrAdd("key1", factory) |> shouldEqual 1 |
| 72 | + calls |> shouldEqual 1 |
| 73 | + |
| 74 | + [<Fact>] |
| 75 | + let ``GetOrAdd with different keys caches values independently``() = |
| 76 | + let cache = createInMemoryCache(TimeSpan.FromMinutes 5.0) |
| 77 | + cache.GetOrAdd("a", fun () -> "value-a") |> shouldEqual "value-a" |
| 78 | + cache.GetOrAdd("b", fun () -> "value-b") |> shouldEqual "value-b" |
| 79 | + cache.TryRetrieve("a") |> shouldEqual(Some "value-a") |
| 80 | + cache.TryRetrieve("b") |> shouldEqual(Some "value-b") |
| 81 | + |
| 82 | + [<Fact>] |
| 83 | + let ``TryRetrieve returns None once the expiration window has elapsed``() = |
| 84 | + let cache = createInMemoryCache(TimeSpan.FromMilliseconds 20.0) |
| 85 | + cache.Set("key1", "value") |
| 86 | + Thread.Sleep(200) |
| 87 | + cache.TryRetrieve("key1") |> shouldEqual None |
| 88 | + |
| 89 | + [<Fact>] |
| 90 | + let ``TryRetrieve with extendCacheExpiration=true keeps the entry alive past the original expiration``() = |
| 91 | + let cache = createInMemoryCache(TimeSpan.FromMilliseconds 500.0) |
| 92 | + cache.Set("key1", "value") |
| 93 | + // Read partway through the window and extend the expiration. |
| 94 | + Thread.Sleep(200) |
| 95 | + |
| 96 | + cache.TryRetrieve("key1", extendCacheExpiration = true) |
| 97 | + |> shouldEqual(Some "value") |
| 98 | + // Total elapsed time (200 + 350 = 550ms) exceeds the original 500ms window, |
| 99 | + // but the extension at 200ms should have reset the clock, so it should still be present. |
| 100 | + Thread.Sleep(350) |
| 101 | + cache.TryRetrieve("key1") |> shouldEqual(Some "value") |
| 102 | + |
| 103 | + [<Fact>] |
| 104 | + let ``TryRetrieve without extendCacheExpiration does not reset expiration``() = |
| 105 | + let cache = createInMemoryCache(TimeSpan.FromMilliseconds 100.0) |
| 106 | + cache.Set("key1", "value") |
| 107 | + Thread.Sleep(60) |
| 108 | + cache.TryRetrieve("key1") |> shouldEqual(Some "value") |
| 109 | + Thread.Sleep(80) |
| 110 | + // Original 100ms window has elapsed (60 + 80 = 140ms) without extension. |
| 111 | + cache.TryRetrieve("key1") |> shouldEqual None |
0 commit comments