mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-17 04:37:48 +00:00
Weekend fun with further optimisations.
This commit is contained in:
+37
-10
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user