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