mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-14 04:07:46 +00:00
Further pursue of perfection.
This commit is contained in:
+27
-28
@@ -7,45 +7,36 @@ import (
|
||||
|
||||
func TestCache_Cleanup(t *testing.T) {
|
||||
c := NewCache()
|
||||
defer c.Close() // Stop the auto-cleanup goroutine
|
||||
|
||||
// Add some items with different expiration times
|
||||
now := time.Now()
|
||||
pastTime := now.Add(-1 * time.Hour) // Already expired
|
||||
futureTime := now.Add(1 * time.Hour) // Not expired
|
||||
|
||||
// Create test items
|
||||
c.items["expired"] = CacheItem{
|
||||
Value: "expired-value",
|
||||
ExpiresAt: pastTime,
|
||||
}
|
||||
|
||||
c.items["valid"] = CacheItem{
|
||||
Value: "valid-value",
|
||||
ExpiresAt: futureTime,
|
||||
}
|
||||
|
||||
// Store original elements in the order list to match items
|
||||
c.elems["expired"] = c.order.PushBack(lruEntry{key: "expired"})
|
||||
c.elems["valid"] = c.order.PushBack(lruEntry{key: "valid"})
|
||||
// Use the Set method to properly add items with synchronization
|
||||
c.Set("expired", "expired-value", pastTime.Sub(now))
|
||||
c.Set("valid", "valid-value", futureTime.Sub(now))
|
||||
|
||||
// Call cleanup, which should only remove expired items
|
||||
c.Cleanup()
|
||||
|
||||
// Check that only the expired item was removed
|
||||
if _, exists := c.items["expired"]; exists {
|
||||
if _, exists := c.Get("expired"); exists {
|
||||
t.Error("Expired item was not removed by Cleanup()")
|
||||
}
|
||||
|
||||
if _, exists := c.items["valid"]; !exists {
|
||||
if _, exists := c.Get("valid"); !exists {
|
||||
t.Error("Valid item was incorrectly removed by Cleanup()")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache_SetMaxSize(t *testing.T) {
|
||||
c := NewCache()
|
||||
defer c.Close() // Stop the auto-cleanup goroutine
|
||||
|
||||
// Set a lower max size
|
||||
originalMaxSize := c.maxSize
|
||||
originalMaxSize := 10
|
||||
newMaxSize := 3
|
||||
|
||||
// Add more items than the new max size
|
||||
@@ -54,25 +45,33 @@ func TestCache_SetMaxSize(t *testing.T) {
|
||||
c.Set(key, i, 1*time.Hour)
|
||||
}
|
||||
|
||||
// Verify items were added
|
||||
if len(c.items) != originalMaxSize {
|
||||
t.Errorf("Expected %d items before SetMaxSize, got %d", originalMaxSize, len(c.items))
|
||||
// Verify items were added by checking a few keys
|
||||
for i := 0; i < originalMaxSize; i++ {
|
||||
key := "key" + string(rune('A'+i))
|
||||
if _, exists := c.Get(key); !exists {
|
||||
t.Errorf("Expected key %s to exist before SetMaxSize", key)
|
||||
}
|
||||
}
|
||||
|
||||
// Change the max size to a smaller value
|
||||
c.SetMaxSize(newMaxSize)
|
||||
|
||||
// Count remaining items
|
||||
count := 0
|
||||
for i := 0; i < originalMaxSize; i++ {
|
||||
key := "key" + string(rune('A'+i))
|
||||
if _, exists := c.Get(key); exists {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
// Check that the cache was reduced to the new max size
|
||||
if len(c.items) > newMaxSize {
|
||||
t.Errorf("Cache size %d exceeds new max size %d after SetMaxSize", len(c.items), newMaxSize)
|
||||
if count > newMaxSize {
|
||||
t.Errorf("Cache size %d exceeds new max size %d after SetMaxSize", count, newMaxSize)
|
||||
}
|
||||
|
||||
if c.maxSize != newMaxSize {
|
||||
t.Errorf("Cache maxSize not updated, expected %d, got %d", newMaxSize, c.maxSize)
|
||||
}
|
||||
|
||||
// Check that the oldest items were evicted (should keep "keyC", "keyD", "keyE", etc.)
|
||||
if _, exists := c.items["keyA"]; exists {
|
||||
// Check that the oldest items were evicted (keyA should be evicted)
|
||||
if _, exists := c.Get("keyA"); exists {
|
||||
t.Error("Expected oldest item 'keyA' to be evicted, but it still exists")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user