forked from Code-Hex/go-generics-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_internal_test.go
153 lines (118 loc) · 3.03 KB
/
cache_internal_test.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package cache
import (
"context"
"testing"
"time"
)
func TestDeletedCache(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nc := NewContext[string, int](ctx)
key := "key"
nc.Set(key, 1, WithExpiration(-time.Second))
_, ok := nc.cache.Get(key)
if !ok {
t.Fatal("want true")
}
nc.DeleteExpired()
_, ok = nc.cache.Get(key)
if ok {
t.Fatal("want false")
}
}
func TestDeleteExpired(t *testing.T) {
now := time.Now()
restore := func() {
nowFunc = time.Now
}
t.Run("normal", func(t *testing.T) {
defer restore()
c := New[string, int]()
c.Set("0", 0)
c.Set("1", 10, WithExpiration(10*time.Millisecond))
c.Set("2", 20, WithExpiration(20*time.Millisecond))
c.Set("3", 30, WithExpiration(30*time.Millisecond))
c.Set("4", 40, WithExpiration(40*time.Millisecond))
c.Set("5", 50)
maxItems := c.Len()
expItems := 2
for i := 0; i <= maxItems; i++ {
nowFunc = func() time.Time {
// Advance time to expire some items
advanced := time.Duration(i * 10)
return now.Add(advanced * time.Millisecond).Add(time.Millisecond)
}
c.DeleteExpired()
got := c.Len()
want := max(maxItems-i, expItems)
if want != got {
t.Errorf("want %d items but got %d", want, got)
}
}
})
t.Run("with remove", func(t *testing.T) {
defer restore()
c := New[string, int]()
c.Set("0", 0)
c.Set("1", 10, WithExpiration(10*time.Millisecond))
c.Set("2", 20, WithExpiration(20*time.Millisecond))
c.Delete("1")
nowFunc = func() time.Time {
return now.Add(30 * time.Millisecond).Add(time.Millisecond)
}
c.DeleteExpired()
keys := c.Keys()
want := 1
if want != len(keys) {
t.Errorf("want %d items but got %d", want, len(keys))
}
})
t.Run("with update", func(t *testing.T) {
defer restore()
c := New[string, int]()
c.Set("0", 0)
c.Set("1", 10, WithExpiration(10*time.Millisecond))
c.Set("2", 20, WithExpiration(20*time.Millisecond))
c.Set("1", 30, WithExpiration(30*time.Millisecond)) // update
maxItems := c.Len()
nowFunc = func() time.Time {
return now.Add(10 * time.Millisecond).Add(time.Millisecond)
}
c.DeleteExpired()
got1 := c.Len()
want1 := maxItems
if want1 != got1 {
t.Errorf("want1 %d items but got1 %d", want1, got1)
}
nowFunc = func() time.Time {
return now.Add(30 * time.Millisecond).Add(time.Millisecond)
}
c.DeleteExpired()
got2 := c.Len()
want2 := 1
if want2 != got2 {
t.Errorf("want2 %d items but got2 %d", want2, got2)
}
})
t.Run("issue #51", func(t *testing.T) {
defer restore()
c := New[string, int]()
c.Set("1", 10, WithExpiration(10*time.Millisecond))
c.Set("2", 20, WithExpiration(20*time.Millisecond))
c.Set("1", 30, WithExpiration(100*time.Millisecond)) // expected do not expired key "1"
nowFunc = func() time.Time {
return now.Add(30 * time.Millisecond).Add(time.Millisecond)
}
c.DeleteExpired()
got := c.Len()
if want := 1; want != got {
t.Errorf("want %d items but got %d", want, got)
}
})
}
func max(x, y int) int {
if x < y {
return y
}
return x
}