Debian Codegraph @

cache/cache_test.go

raw
1 // Copyright 2025 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package cache
5
6 import (
7 "testing"
8
9 "github.com/pb33f/libopenapi/datamodel/high/base"
10 "github.com/santhosh-tekuri/jsonschema/v6"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 )
14
15 func TestNewDefaultCache(t *testing.T) {
16 cache := NewDefaultCache()
17 assert.NotNil(t, cache)
18 assert.NotNil(t, cache.m)
19 }
20
21 func TestDefaultCache_StoreAndLoad(t *testing.T) {
22 cache := NewDefaultCache()
23
24 // Create a test schema cache entry
25 testSchema := &SchemaCacheEntry{
26 Schema: &base.Schema{},
27 RenderedInline: []byte("rendered"),
28 RenderedJSON: []byte(`{"type":"object"}`),
29 CompiledSchema: &jsonschema.Schema{},
30 }
31
32 // Create a test key (uint64 hash)
33 key := uint64(0x123456789abcdef0)
34
35 // Store the schema
36 cache.Store(key, testSchema)
37
38 // Load the schema back
39 loaded, ok := cache.Load(key)
40 assert.True(t, ok, "Should find the cached schema")
41 require.NotNil(t, loaded)
42 assert.Equal(t, testSchema.RenderedInline, loaded.RenderedInline)
43 assert.Equal(t, testSchema.RenderedJSON, loaded.RenderedJSON)
44 assert.NotNil(t, loaded.CompiledSchema)
45 }
46
47 func TestDefaultCache_LoadMissing(t *testing.T) {
48 cache := NewDefaultCache()
49
50 // Try to load a key that doesn't exist
51 key := uint64(0xdeadbeef)
52
53 loaded, ok := cache.Load(key)
54 assert.False(t, ok, "Should not find non-existent key")
55 assert.Nil(t, loaded)
56 }
57
58 func TestDefaultCache_LoadNilCache(t *testing.T) {
59 var cache *DefaultCache
60
61 key := uint64(0)
62 loaded, ok := cache.Load(key)
63
64 assert.False(t, ok)
65 assert.Nil(t, loaded)
66 }
67
68 func TestDefaultCache_StoreNilCache(t *testing.T) {
69 var cache *DefaultCache
70
71 // Should not panic
72 key := uint64(0)
73 cache.Store(key, &SchemaCacheEntry{})
74
75 // Verify nothing was stored (cache is nil)
76 assert.Nil(t, cache)
77 }
78
79 func TestDefaultCache_Range(t *testing.T) {
80 cache := NewDefaultCache()
81
82 // Store multiple entries
83 entries := make(map[uint64]*SchemaCacheEntry)
84 for i := 0; i < 5; i++ {
85 key := uint64(i)
86
87 entry := &SchemaCacheEntry{
88 RenderedInline: []byte{byte(i)},
89 RenderedJSON: []byte{byte(i)},
90 }
91 entries[key] = entry
92 cache.Store(key, entry)
93 }
94
95 // Range over all entries
96 count := 0
97 foundKeys := make(map[uint64]bool)
98 cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
99 count++
100 foundKeys[key] = true
101
102 // Verify the value matches what we stored
103 expected, exists := entries[key]
104 assert.True(t, exists, "Key should exist in original entries")
105 assert.Equal(t, expected.RenderedInline, value.RenderedInline)
106 return true
107 })
108
109 assert.Equal(t, 5, count, "Should iterate over all 5 entries")
110 assert.Equal(t, 5, len(foundKeys), "Should find all 5 unique keys")
111 }
112
113 func TestDefaultCache_RangeEarlyTermination(t *testing.T) {
114 cache := NewDefaultCache()
115
116 // Store multiple entries
117 for i := 0; i < 10; i++ {
118 key := uint64(i)
119 cache.Store(key, &SchemaCacheEntry{})
120 }
121
122 // Range but stop after 3 iterations
123 count := 0
124 cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
125 count++
126 return count < 3 // Stop after 3
127 })
128
129 assert.Equal(t, 3, count, "Should stop after 3 iterations")
130 }
131
132 func TestDefaultCache_RangeNilCache(t *testing.T) {
133 var cache *DefaultCache
134
135 // Should not panic
136 called := false
137 cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
138 called = true
139 return true
140 })
141
142 assert.False(t, called, "Callback should not be called on nil cache")
143 }
144
145 func TestDefaultCache_RangeEmpty(t *testing.T) {
146 cache := NewDefaultCache()
147
148 // Range over empty cache
149 count := 0
150 cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
151 count++
152 return true
153 })
154
155 assert.Equal(t, 0, count, "Should not iterate over empty cache")
156 }
157
158 func TestDefaultCache_Overwrite(t *testing.T) {
159 cache := NewDefaultCache()
160
161 key := uint64(0x12345678)
162
163 // Store first value
164 first := &SchemaCacheEntry{
165 RenderedInline: []byte("first"),
166 }
167 cache.Store(key, first)
168
169 // Store second value with same key
170 second := &SchemaCacheEntry{
171 RenderedInline: []byte("second"),
172 }
173 cache.Store(key, second)
174
175 // Load should return the second value
176 loaded, ok := cache.Load(key)
177 assert.True(t, ok)
178 require.NotNil(t, loaded)
179 assert.Equal(t, []byte("second"), loaded.RenderedInline)
180 }
181
182 func TestDefaultCache_MultipleKeys(t *testing.T) {
183 cache := NewDefaultCache()
184
185 // Store with different keys
186 key1 := uint64(1)
187 key2 := uint64(2)
188 key3 := uint64(3)
189
190 cache.Store(key1, &SchemaCacheEntry{RenderedInline: []byte("value1")})
191 cache.Store(key2, &SchemaCacheEntry{RenderedInline: []byte("value2")})
192 cache.Store(key3, &SchemaCacheEntry{RenderedInline: []byte("value3")})
193
194 // Load each one
195 val1, ok1 := cache.Load(key1)
196 val2, ok2 := cache.Load(key2)
197 val3, ok3 := cache.Load(key3)
198
199 assert.True(t, ok1)
200 assert.True(t, ok2)
201 assert.True(t, ok3)
202
203 assert.Equal(t, []byte("value1"), val1.RenderedInline)
204 assert.Equal(t, []byte("value2"), val2.RenderedInline)
205 assert.Equal(t, []byte("value3"), val3.RenderedInline)
206 }
207
208 func TestDefaultCache_ThreadSafety(t *testing.T) {
209 cache := NewDefaultCache()
210
211 // Concurrent writes
212 done := make(chan bool, 10)
213 for i := 0; i < 10; i++ {
214 go func(val int) {
215 key := uint64(val)
216 cache.Store(key, &SchemaCacheEntry{
217 RenderedInline: []byte{byte(val)},
218 })
219 done <- true
220 }(i)
221 }
222
223 // Wait for all writes
224 for i := 0; i < 10; i++ {
225 <-done
226 }
227
228 // Concurrent reads
229 for i := 0; i < 10; i++ {
230 go func(val int) {
231 key := uint64(val)
232 loaded, ok := cache.Load(key)
233 assert.True(t, ok)
234 assert.NotNil(t, loaded)
235 done <- true
236 }(i)
237 }
238
239 // Wait for all reads
240 for i := 0; i < 10; i++ {
241 <-done
242 }
243
244 // Verify all entries exist
245 count := 0
246 cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
247 count++
248 return true
249 })
250 assert.Equal(t, 10, count, "All entries should be present")
251 }
252
253 func TestSchemaCache_Fields(t *testing.T) {
254 // Test that SchemaCache properly holds all fields
255 schema := &base.Schema{}
256 compiled := &jsonschema.Schema{}
257
258 sc := &SchemaCacheEntry{
259 Schema: schema,
260 RenderedInline: []byte("rendered"),
261 RenderedJSON: []byte(`{"type":"object"}`),
262 CompiledSchema: compiled,
263 }
264
265 assert.Equal(t, schema, sc.Schema)
266 assert.Equal(t, []byte("rendered"), sc.RenderedInline)
267 assert.Equal(t, []byte(`{"type":"object"}`), sc.RenderedJSON)
268 assert.Equal(t, compiled, sc.CompiledSchema)
269 }
270
271 func TestDefaultCache_RangeWithInvalidTypes(t *testing.T) {
272 cache := NewDefaultCache()
273
274 // Manually insert invalid types into the underlying sync.Map to test defensive programming
275 // Store an entry with wrong key type
276 cache.m.Store("invalid-key-type", &SchemaCacheEntry{})
277
278 // Store an entry with wrong value type
279 validKey := uint64(1)
280 cache.m.Store(validKey, "invalid-value-type")
281
282 // Store a valid entry
283 validKey2 := uint64(2)
284 validEntry := &SchemaCacheEntry{RenderedInline: []byte("valid")}
285 cache.Store(validKey2, validEntry)
286
287 // Range should skip invalid entries and only process valid ones
288 count := 0
289 var seenEntry *SchemaCacheEntry
290 cache.Range(func(key uint64, value *SchemaCacheEntry) bool {
291 count++
292 seenEntry = value
293 return true
294 })
295
296 assert.Equal(t, 1, count, "Should only process valid entry")
297 assert.Equal(t, validEntry, seenEntry, "Should see the valid entry")
298 }