|
| 1 | +package ttlmap_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/packaged/ttlmap" |
| 9 | +) |
| 10 | + |
| 11 | +func TestHasAndExpiry(t *testing.T) { |
| 12 | + cache := ttlmap.New(ttlmap.WithDefaultTTL(50*time.Millisecond), ttlmap.WithCleanupDuration(10*time.Millisecond)) |
| 13 | + if cache.Has("missing") { |
| 14 | + t.Fatalf("expected Has to be false for missing key") |
| 15 | + } |
| 16 | + cache.Set("a", 1, nil) |
| 17 | + if !cache.Has("a") { |
| 18 | + t.Fatalf("expected Has to be true right after Set") |
| 19 | + } |
| 20 | + time.Sleep(70 * time.Millisecond) |
| 21 | + if cache.Has("a") { |
| 22 | + t.Fatalf("expected Has to be false after TTL expiry") |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestGetExpiryAndGetItemCopy(t *testing.T) { |
| 27 | + ttl := 80 * time.Millisecond |
| 28 | + cache := ttlmap.New(ttlmap.WithDefaultTTL(ttl), ttlmap.WithCleanupDuration(10*time.Millisecond)) |
| 29 | + cache.Set("k", "v", &ttl) |
| 30 | + |
| 31 | + exp1 := cache.GetExpiry("k") |
| 32 | + if exp1 == nil { |
| 33 | + t.Fatalf("expected expiry for existing key") |
| 34 | + } |
| 35 | + // Missing key should return nil |
| 36 | + if exp := cache.GetExpiry("missing"); exp != nil { |
| 37 | + t.Fatalf("expected nil expiry for missing key") |
| 38 | + } |
| 39 | + |
| 40 | + // Get a copy and Touch it; the underlying stored item should not change expiry |
| 41 | + itm, ok := cache.GetItem("k") |
| 42 | + if !ok || itm == nil { |
| 43 | + t.Fatalf("expected GetItem to succeed") |
| 44 | + } |
| 45 | + // Touch the returned copy |
| 46 | + itm.Touch() |
| 47 | + exp2 := cache.GetExpiry("k") |
| 48 | + if exp2 == nil || !exp2.Equal(*exp1) { |
| 49 | + t.Fatalf("expected stored item expiry to remain unchanged after touching copy") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestFlush(t *testing.T) { |
| 54 | + cache := ttlmap.New(ttlmap.WithDefaultTTL(500 * time.Millisecond)) |
| 55 | + cache.Set("x", 1, nil) |
| 56 | + cache.Set("y", 2, nil) |
| 57 | + if !cache.Has("x") || !cache.Has("y") { |
| 58 | + t.Fatalf("expected keys to exist before Flush") |
| 59 | + } |
| 60 | + cache.Flush() |
| 61 | + if cache.Has("x") || cache.Has("y") { |
| 62 | + t.Fatalf("expected keys to be removed after Flush") |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestCloseIdempotent(t *testing.T) { |
| 67 | + cache := ttlmap.New(ttlmap.WithCleanupDuration(5 * time.Millisecond)) |
| 68 | + // Should not panic on repeated Close |
| 69 | + cache.Close() |
| 70 | + cache.Close() |
| 71 | +} |
| 72 | + |
| 73 | +func TestBackgroundUpdateSingleFlight(t *testing.T) { |
| 74 | + cache := ttlmap.New(ttlmap.WithDefaultTTL(20 * time.Millisecond)) |
| 75 | + key := "sf" |
| 76 | + cache.Set(key, 1, nil) |
| 77 | + |
| 78 | + var calls int |
| 79 | + var mu sync.Mutex |
| 80 | + start := make(chan struct{}) |
| 81 | + var wg sync.WaitGroup |
| 82 | + // Launch many contenders; only one updater should run at a time for the key |
| 83 | + n := 50 |
| 84 | + wg.Add(n) |
| 85 | + for i := 0; i < n; i++ { |
| 86 | + go func() { |
| 87 | + defer wg.Done() |
| 88 | + <-start |
| 89 | + cache.BackgroundUpdate(key, func() (interface{}, error) { |
| 90 | + mu.Lock() |
| 91 | + calls++ |
| 92 | + mu.Unlock() |
| 93 | + time.Sleep(5 * time.Millisecond) |
| 94 | + return 2, nil |
| 95 | + }) |
| 96 | + }() |
| 97 | + } |
| 98 | + close(start) |
| 99 | + wg.Wait() |
| 100 | + |
| 101 | + // Only one should have executed; the rest return immediately because the key is locked |
| 102 | + if calls != 1 { |
| 103 | + t.Fatalf("expected exactly one updater call, got %d", calls) |
| 104 | + } |
| 105 | +} |
0 commit comments