@@ -127,7 +127,7 @@ func TestTTLAdd(t *testing.T) {
127
127
require .Equal (4 , oldest )
128
128
}
129
129
130
- // TestTTLLength tests that elements are not evicted on Length
130
+ // TestTTLLength tests that elements are evicted on Length
131
131
func TestTTLLength (t * testing.T ) {
132
132
require := require .New (t )
133
133
@@ -156,10 +156,10 @@ func TestTTLLength(t *testing.T) {
156
156
clock .Set (start .Add (testTTL + time .Second ))
157
157
158
158
// No more elements should be present in the window.
159
- require .Equal (3 , window .Length ())
159
+ require .Equal (0 , window .Length ())
160
160
}
161
161
162
- // TestTTLOldest tests that stale elements are not evicted on calling Oldest
162
+ // TestTTLOldest tests that stale elements are evicted on calling Oldest
163
163
func TestTTLOldest (t * testing.T ) {
164
164
require := require .New (t )
165
165
@@ -188,15 +188,25 @@ func TestTTLOldest(t *testing.T) {
188
188
require .Equal (1 , oldest )
189
189
require .Equal (3 , window .elements .Len ())
190
190
191
- // Now we're one second past the ttl of 10 seconds as defined in testTTL,
191
+ // Now we're one second before the ttl of 10 seconds as defined in testTTL,
192
192
// so all existing elements shoud still exist.
193
- clock .Set (start .Add (testTTL + time .Second ))
193
+ // Add 4 to the window to make it:
194
+ // [1, 2, 3, 4]
195
+ clock .Set (start .Add (testTTL - time .Second ))
196
+ window .Add (4 )
194
197
195
- // Now there should be three elements in the window
196
198
oldest , ok = window .Oldest ()
197
199
require .True (ok )
198
200
require .Equal (1 , oldest )
199
- require .Equal (3 , window .elements .Len ())
201
+ require .Equal (4 , window .elements .Len ())
202
+
203
+ // Now we're one second past the ttl of the initial 3 elements
204
+ // call to oldest should now evict 1,2,3 and return 4.
205
+ clock .Set (start .Add (testTTL + time .Second ))
206
+ oldest , ok = window .Oldest ()
207
+ require .True (ok )
208
+ require .Equal (4 , oldest )
209
+ require .Equal (1 , window .elements .Len ())
200
210
}
201
211
202
212
// Tests that we bound the amount of elements in the window
0 commit comments