@@ -106,25 +106,90 @@ func TestTTLCache_Reset(t *testing.T) {
106
106
require .Equal (t , "qux" , value )
107
107
}
108
108
109
- func TestTTLCache_RemoveExpiredItems (t * testing.T ) {
109
+ func TestTTLCache_EvictExpired (t * testing.T ) {
110
110
c := NewTTLCache [string , string ](time .Minute )
111
111
clock := quartz .NewMock (t )
112
112
c .clock = clock
113
113
c .Set ("foo" , "bar" )
114
114
_ , ok := c .items ["foo" ]
115
115
require .True (t , ok )
116
- // Advance the clock and update foo, it should not be removed.
116
+ // Advance the clock and update foo, it should not be evicted as its
117
+ // expiration time should be refreshed.
117
118
clock .Advance (time .Minute )
118
119
c .Set ("foo" , "bar" )
119
120
_ , ok = c .items ["foo" ]
120
121
require .True (t , ok )
121
- // Advance the clock again but this time set bar, foo should be removed.
122
- clock .Advance (time .Minute )
122
+ eviction1 := clock .Now ()
123
+ require .Equal (t , eviction1 , c .lastEvictedAt )
124
+ // Advance the clock 15 seconds. Since 15 seconds is less than half
125
+ // the TTL (30 seconds) since the last eviction, no eviction should
126
+ // be run.
127
+ clock .Advance (15 * time .Second )
123
128
c .Set ("bar" , "baz" )
124
129
_ , ok = c .items ["foo" ]
130
+ require .True (t , ok )
131
+ _ , ok = c .items ["bar" ]
132
+ require .True (t , ok )
133
+ require .Equal (t , eviction1 , c .lastEvictedAt )
134
+ // Advance the clock 16 seconds. Since 31 seconds is more than half the TTL
135
+ // since the last eviction, an eviction should be run, but no items should
136
+ // be evicted.
137
+ clock .Advance (16 * time .Second )
138
+ c .Set ("baz" , "qux" )
139
+ _ , ok = c .items ["foo" ]
140
+ require .True (t , ok )
141
+ _ , ok = c .items ["bar" ]
142
+ require .True (t , ok )
143
+ _ , ok = c .items ["baz" ]
144
+ require .True (t , ok )
145
+ eviction2 := clock .Now ()
146
+ require .Equal (t , eviction2 , c .lastEvictedAt )
147
+ // Advance the clock another 15 seconds. Again, since 15 seconds is less
148
+ // than half the TTL (seconds) since the last eviction, no eviction should
149
+ // be run.
150
+ clock .Advance (15 * time .Second )
151
+ c .Set ("qux" , "corge" )
152
+ _ , ok = c .items ["foo" ]
153
+ require .True (t , ok )
154
+ _ , ok = c .items ["bar" ]
155
+ require .True (t , ok )
156
+ _ , ok = c .items ["baz" ]
157
+ require .True (t , ok )
158
+ _ , ok = c .items ["qux" ]
159
+ require .True (t , ok )
160
+ require .Equal (t , eviction2 , c .lastEvictedAt )
161
+ // Advance the clock another 16 seconds. Since 31 seconds is more than
162
+ // half the TTL since the last eviction, an eviction should be run and
163
+ // this time foo should be evicted as it has expired.
164
+ clock .Advance (16 * time .Second )
165
+ c .Set ("corge" , "jorge" )
166
+ _ , ok = c .items ["foo" ]
125
167
require .False (t , ok )
126
168
_ , ok = c .items ["bar" ]
127
169
require .True (t , ok )
170
+ _ , ok = c .items ["baz" ]
171
+ require .True (t , ok )
172
+ _ , ok = c .items ["qux" ]
173
+ require .True (t , ok )
174
+ _ , ok = c .items ["corge" ]
175
+ require .True (t , ok )
176
+ eviction3 := clock .Now ()
177
+ require .Equal (t , eviction3 , c .lastEvictedAt )
178
+ // Advance the clock one whole minute. All items should be expired.
179
+ clock .Advance (time .Minute )
180
+ c .Set ("foo" , "bar" )
181
+ _ , ok = c .items ["foo" ]
182
+ require .True (t , ok )
183
+ _ , ok = c .items ["bar" ]
184
+ require .False (t , ok )
185
+ _ , ok = c .items ["baz" ]
186
+ require .False (t , ok )
187
+ _ , ok = c .items ["qux" ]
188
+ require .False (t , ok )
189
+ _ , ok = c .items ["qux" ]
190
+ require .False (t , ok )
191
+ eviction4 := clock .Now ()
192
+ require .Equal (t , eviction4 , c .lastEvictedAt )
128
193
}
129
194
130
195
func TestNopCache (t * testing.T ) {
0 commit comments