|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright (c) 2016 Bazinga Technologies Inc |
| 3 | + |
| 4 | +module FSharp.Data.GraphQL.Tests.ValidationCacheTests |
| 5 | + |
| 6 | +open Xunit |
| 7 | +open FSharp.Data.GraphQL |
| 8 | +open FSharp.Data.GraphQL.Validation |
| 9 | +open System.Threading |
| 10 | + |
| 11 | +[<Fact>] |
| 12 | +let ``MemoryValidationResultCache caches results for same key`` () = |
| 13 | + let cache = MemoryValidationResultCache() :> IValidationResultCache |
| 14 | + let mutable callCount = 0 |
| 15 | + let producer () = |
| 16 | + Interlocked.Increment(&callCount) |> ignore |
| 17 | + Success |
| 18 | + |
| 19 | + let key = { DocumentId = "doc1"; SchemaId = "schema1" } |
| 20 | + |
| 21 | + // First call should invoke producer |
| 22 | + let result1 = cache.GetOrAdd producer key |
| 23 | + equals 1 callCount |
| 24 | + equals Success result1 |
| 25 | + |
| 26 | + // Second call with same key should NOT invoke producer (cached) |
| 27 | + let result2 = cache.GetOrAdd producer key |
| 28 | + equals 1 callCount // Still 1, not 2 |
| 29 | + equals Success result2 |
| 30 | + |
| 31 | +[<Fact>] |
| 32 | +let ``MemoryValidationResultCache uses different cache entries for different DocumentIds`` () = |
| 33 | + let cache = MemoryValidationResultCache() :> IValidationResultCache |
| 34 | + let mutable callCount = 0 |
| 35 | + let producer () = |
| 36 | + Interlocked.Increment(&callCount) |> ignore |
| 37 | + Success |
| 38 | + |
| 39 | + let key1 = { DocumentId = "doc1"; SchemaId = "schema1" } |
| 40 | + let key2 = { DocumentId = "doc2"; SchemaId = "schema1" } |
| 41 | + |
| 42 | + // First call |
| 43 | + let result1 = cache.GetOrAdd producer key1 |
| 44 | + equals 1 callCount |
| 45 | + |
| 46 | + // Second call with different DocumentId should invoke producer again |
| 47 | + let result2 = cache.GetOrAdd producer key2 |
| 48 | + equals 2 callCount // Should be 2 now |
| 49 | + |
| 50 | +[<Fact>] |
| 51 | +let ``MemoryValidationResultCache uses different cache entries for different SchemaIds`` () = |
| 52 | + let cache = MemoryValidationResultCache() :> IValidationResultCache |
| 53 | + let mutable callCount = 0 |
| 54 | + let producer () = |
| 55 | + Interlocked.Increment(&callCount) |> ignore |
| 56 | + Success |
| 57 | + |
| 58 | + let key1 = { DocumentId = "doc1"; SchemaId = "schema1" } |
| 59 | + let key2 = { DocumentId = "doc1"; SchemaId = "schema2" } |
| 60 | + |
| 61 | + // First call |
| 62 | + let result1 = cache.GetOrAdd producer key1 |
| 63 | + equals 1 callCount |
| 64 | + |
| 65 | + // Second call with different SchemaId should invoke producer again |
| 66 | + let result2 = cache.GetOrAdd producer key2 |
| 67 | + equals 2 callCount // Should be 2 now |
| 68 | + |
| 69 | +[<Fact>] |
| 70 | +let ``MemoryValidationResultCache distinguishes keys with same hash code`` () = |
| 71 | + let cache = MemoryValidationResultCache() :> IValidationResultCache |
| 72 | + |
| 73 | + // Create two different keys that might have hash collisions |
| 74 | + // Using very similar but different strings |
| 75 | + let key1 = { DocumentId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; SchemaId = "schema1" } |
| 76 | + let key2 = { DocumentId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; SchemaId = "schema1" } |
| 77 | + |
| 78 | + let mutable callCount = 0 |
| 79 | + let producer () = |
| 80 | + Interlocked.Increment(&callCount) |> ignore |
| 81 | + Success |
| 82 | + |
| 83 | + // First call |
| 84 | + let result1 = cache.GetOrAdd producer key1 |
| 85 | + equals 1 callCount |
| 86 | + |
| 87 | + // Second call with different key should invoke producer even if hash codes collide |
| 88 | + let result2 = cache.GetOrAdd producer key2 |
| 89 | + equals 2 callCount // Should be 2, proving we use full key not just hash |
| 90 | + |
| 91 | +[<Fact>] |
| 92 | +let ``MemoryValidationResultCache caches error results`` () = |
| 93 | + let cache = MemoryValidationResultCache() :> IValidationResultCache |
| 94 | + let mutable callCount = 0 |
| 95 | + let error = GQLProblemDetails.Create("Test error") |
| 96 | + let producer () = |
| 97 | + Interlocked.Increment(&callCount) |> ignore |
| 98 | + ValidationError [error] |
| 99 | + |
| 100 | + let key = { DocumentId = "doc1"; SchemaId = "schema1" } |
| 101 | + |
| 102 | + // First call should invoke producer |
| 103 | + let result1 = cache.GetOrAdd producer key |
| 104 | + equals 1 callCount |
| 105 | + match result1 with |
| 106 | + | ValidationError errors -> equals 1 (Seq.length errors) |
| 107 | + | Success -> fail "Expected ValidationError" |
| 108 | + |
| 109 | + // Second call with same key should NOT invoke producer (cached) |
| 110 | + let result2 = cache.GetOrAdd producer key |
| 111 | + equals 1 callCount // Still 1, not 2 |
| 112 | + match result2 with |
| 113 | + | ValidationError errors -> equals 1 (Seq.length errors) |
| 114 | + | Success -> fail "Expected ValidationError" |
| 115 | + |
| 116 | +[<Fact>] |
| 117 | +let ``MemoryValidationResultCache handles concurrent access`` () = |
| 118 | + let cache = MemoryValidationResultCache() :> IValidationResultCache |
| 119 | + let mutable callCount = 0 |
| 120 | + let producer () = |
| 121 | + Interlocked.Increment(&callCount) |> ignore |
| 122 | + Thread.Sleep(10) // Simulate some work |
| 123 | + Success |
| 124 | + |
| 125 | + let key = { DocumentId = "doc1"; SchemaId = "schema1" } |
| 126 | + |
| 127 | + // Call cache from multiple threads simultaneously |
| 128 | + let tasks = |
| 129 | + [1..10] |
| 130 | + |> List.map (fun _ -> |
| 131 | + async { |
| 132 | + return cache.GetOrAdd producer key |
| 133 | + }) |
| 134 | + |
| 135 | + let results = tasks |> Async.Parallel |> Async.RunSynchronously |
| 136 | + |
| 137 | + // All results should be Success |
| 138 | + results |> Array.iter (fun r -> equals Success r) |
| 139 | + |
| 140 | + // Producer should be called at least once, but possibly more due to race conditions |
| 141 | + // The important thing is it's not called 10 times |
| 142 | + Assert.True(callCount >= 1 && callCount < 10, $"Expected callCount between 1 and 9, got {callCount}") |
0 commit comments