Skip to content

Commit b6d58c3

Browse files
committed
test: add accessor methods and improve config change tests
1 parent a5edcaf commit b6d58c3

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

pkg/remoteresolution/resolver/framework/cache/cache.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ var _ resolutionframework.ConfigWatcher = (*resolverCache)(nil)
3434
// resolverCache is a wrapper around utilcache.LRUExpireCache that provides
3535
// type-safe methods for caching resolver results.
3636
type resolverCache struct {
37-
cache *utilcache.LRUExpireCache
38-
logger *zap.SugaredLogger
39-
ttl time.Duration
40-
clock Clock
37+
cache *utilcache.LRUExpireCache
38+
logger *zap.SugaredLogger
39+
ttl time.Duration
40+
maxSize int
41+
clock Clock
4142
}
4243

4344
func newResolverCache(maxSize int, ttl time.Duration) *resolverCache {
4445
return &resolverCache{
45-
cache: utilcache.NewLRUExpireCache(maxSize),
46-
ttl: ttl,
47-
clock: realClock{},
46+
cache: utilcache.NewLRUExpireCache(maxSize),
47+
ttl: ttl,
48+
maxSize: maxSize,
49+
clock: realClock{},
4850
}
4951
}
5052

@@ -56,7 +58,17 @@ func (c *resolverCache) GetConfigName(_ context.Context) string {
5658
// withLogger returns a new ResolverCache instance with the provided logger.
5759
// This prevents state leak by not storing logger in the global singleton.
5860
func (c *resolverCache) withLogger(logger *zap.SugaredLogger) *resolverCache {
59-
return &resolverCache{logger: logger, cache: c.cache, ttl: c.ttl, clock: c.clock}
61+
return &resolverCache{logger: logger, cache: c.cache, ttl: c.ttl, maxSize: c.maxSize, clock: c.clock}
62+
}
63+
64+
// TTL returns the time-to-live duration for cache entries.
65+
func (c *resolverCache) TTL() time.Duration {
66+
return c.ttl
67+
}
68+
69+
// MaxSize returns the maximum number of entries the cache can hold.
70+
func (c *resolverCache) MaxSize() int {
71+
return c.maxSize
6072
}
6173

6274
// Get retrieves a cached resource by resolver type and parameters, returning

pkg/remoteresolution/resolver/framework/cache/configstore_test.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func TestParseCacheConfigMap(t *testing.T) {
198198
}
199199

200200
func TestOnCacheConfigChanged(t *testing.T) {
201-
// Test that onCacheConfigChanged updates the shared cache
201+
// Test that onCacheConfigChanged updates the shared cache with new config values
202202
config := &cacheConfig{
203203
maxSize: 500,
204204
ttl: 10 * time.Minute,
@@ -207,25 +207,56 @@ func TestOnCacheConfigChanged(t *testing.T) {
207207
// Call onCacheConfigChanged to update the shared cache
208208
onCacheConfigChanged("test-config", config)
209209

210-
// Verify the shared cache was updated by checking we can still get it
211-
// We can't directly verify the size/ttl without accessing the internal cache,
212-
// but we can verify it doesn't panic and returns a valid cache
210+
// Verify the shared cache was updated with the correct config values
213211
ctx := logtesting.TestContextWithLogger(t)
214212
cache := Get(ctx)
215213
if cache == nil {
216-
t.Error("Expected cache after config change but got nil")
214+
t.Fatal("Expected cache after config change but got nil")
215+
}
216+
217+
// Verify TTL was applied
218+
if cache.TTL() != config.ttl {
219+
t.Errorf("Expected TTL to be %v, got %v", config.ttl, cache.TTL())
220+
}
221+
222+
// Verify MaxSize was applied
223+
if cache.MaxSize() != config.maxSize {
224+
t.Errorf("Expected MaxSize to be %d, got %d", config.maxSize, cache.MaxSize())
217225
}
218226
}
219227

220228
func TestOnCacheConfigChangedWithInvalidType(t *testing.T) {
229+
// First, set up a known good config
230+
goodConfig := &cacheConfig{
231+
maxSize: defaultCacheSize,
232+
ttl: defaultExpiration,
233+
}
234+
onCacheConfigChanged("test-config", goodConfig)
235+
236+
ctx := logtesting.TestContextWithLogger(t)
237+
cacheBefore := Get(ctx)
238+
if cacheBefore == nil {
239+
t.Fatal("Expected cache before invalid config change")
240+
}
241+
ttlBefore := cacheBefore.TTL()
242+
maxSizeBefore := cacheBefore.MaxSize()
243+
221244
// Test that onCacheConfigChanged handles invalid types gracefully
222-
// This should not panic
245+
// This should not panic and should preserve the existing cache
223246
onCacheConfigChanged("test-config", "invalid-type")
224247

225-
// Verify we can still get the cache
226-
ctx := logtesting.TestContextWithLogger(t)
227-
cache := Get(ctx)
228-
if cache == nil {
229-
t.Error("Expected cache after invalid config change but got nil")
248+
// Verify we can still get the cache and it wasn't modified
249+
cacheAfter := Get(ctx)
250+
if cacheAfter == nil {
251+
t.Fatal("Expected cache after invalid config change but got nil")
252+
}
253+
254+
// Verify cache config wasn't changed by invalid input
255+
if cacheAfter.TTL() != ttlBefore {
256+
t.Errorf("Expected TTL to remain %v after invalid config, got %v", ttlBefore, cacheAfter.TTL())
257+
}
258+
259+
if cacheAfter.MaxSize() != maxSizeBefore {
260+
t.Errorf("Expected MaxSize to remain %d after invalid config, got %d", maxSizeBefore, cacheAfter.MaxSize())
230261
}
231262
}

0 commit comments

Comments
 (0)