@@ -9,11 +9,17 @@ import (
9
9
func TestDeletedCache (t * testing.T ) {
10
10
ctx , cancel := context .WithCancel (context .Background ())
11
11
defer cancel ()
12
+ restore := func () {
13
+ nowFunc = time .Now
14
+ }
15
+ defer restore ()
12
16
13
17
nc := NewContext [string , int ](ctx )
14
18
key := "key"
15
- nc .Set (key , 1 , WithExpiration (- time .Second ))
16
-
19
+ nc .Set (key , 1 , WithExpiration (1 * time .Second ))
20
+ nowFunc = func () time.Time {
21
+ return time .Now ().Add (2 * time .Second )
22
+ }
17
23
_ , ok := nc .cache .Get (key )
18
24
if ! ok {
19
25
t .Fatal ("want true" )
@@ -143,6 +149,30 @@ func TestDeleteExpired(t *testing.T) {
143
149
t .Errorf ("want %d items but got %d" , want , got )
144
150
}
145
151
})
152
+
153
+ t .Run ("issue #64" , func (t * testing.T ) {
154
+ defer restore ()
155
+ c := New [string , int ]()
156
+ c .Set ("1" , 4 , WithExpiration (0 )) // These should not be expired
157
+ c .Set ("2" , 5 , WithExpiration (- 1 )) // These should not be expired
158
+ c .Set ("3" , 6 , WithExpiration (1 * time .Hour ))
159
+
160
+ want := true
161
+ _ , ok := c .Get ("1" )
162
+ if ok != want {
163
+ t .Errorf ("want %t but got %t" , want , ok )
164
+ }
165
+
166
+ _ , ok = c .Get ("2" )
167
+ if ok != want {
168
+ t .Errorf ("want %t but got %t" , want , ok )
169
+ }
170
+ _ , ok = c .Get ("3" )
171
+ if ok != want {
172
+ t .Errorf ("want %t but got %t" , want , ok )
173
+ }
174
+
175
+ })
146
176
}
147
177
148
178
func max (x , y int ) int {
0 commit comments