Weekend fun with further optimisations.

This commit is contained in:
2025-09-13 23:42:02 +01:00
parent a31fd64112
commit 7457a901a9
48 changed files with 8726 additions and 3268 deletions
+37 -10
View File
@@ -137,7 +137,7 @@ func TestCacheMemoryExhaustion(t *testing.T) {
// Create a cache with limited size
cache := NewTokenCache()
cache.cache.SetMaxSize(100) // Small cache size
// Note: The new universal cache has a default max size that handles eviction automatically
// Ensure cleanup when test finishes
defer cache.Close()
@@ -178,19 +178,46 @@ func TestCacheMemoryExhaustion(t *testing.T) {
// Note: We can't access internal cache structure with the interface
// Just test that eviction works by checking if old items are removed
// Verify LRU eviction works
// The first tokens should have been evicted
firstToken := tokens[0]
if _, exists := cache.Get(firstToken); exists {
t.Errorf("First token should have been evicted from cache")
// Verify LRU eviction works by checking cache size
// The TokenCache uses a singleton with MaxSize=5000 by default
// With 500 tokens, they should all fit, so we need to add more
// Add many more tokens to exceed the cache limit and trigger eviction
extraTokens := 5000 // Add enough to exceed cache size
for i := numTokens; i < numTokens+extraTokens; i++ {
token, err := createTestJWT(ts.rsaPrivateKey, "RS256", "test-key-id", map[string]interface{}{
"iss": "https://test-issuer.com",
"aud": "test-client-id",
"exp": float64(time.Now().Add(1 * time.Hour).Unix()),
"sub": "test-subject",
"jti": fmt.Sprintf("jti-%d", i),
})
if err != nil {
t.Fatalf("Failed to create token %d: %v", i, err)
}
cache.Set(token, map[string]interface{}{"jti": fmt.Sprintf("jti-%d", i)}, time.Hour)
}
// The last tokens should still be in cache
lastToken := tokens[numTokens-1]
if _, exists := cache.Get(lastToken); !exists {
t.Errorf("Last token should still be in cache")
// The early tokens should have been evicted due to LRU
evicted := 0
kept := 0
// Check first 100 tokens
for i := 0; i < 100; i++ {
if _, exists := cache.Get(tokens[i]); !exists {
evicted++
} else {
kept++
}
}
// At least some of the first tokens should be evicted
if evicted == 0 {
t.Errorf("Expected some of the first 100 tokens to be evicted, but all were kept")
}
t.Logf("After adding %d total tokens, %d of first 100 were evicted, %d kept",
numTokens+extraTokens, evicted, kept)
t.Logf("Cache memory exhaustion test passed with %d tokens", numTokens)
}