Skip to content

Commit 196c51c

Browse files
authored
remove time.Sleep from tests (#1305)
1 parent 77feef5 commit 196c51c

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

internal/common/cache/lru.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type lru struct {
4141
ttl time.Duration
4242
pin bool
4343
rmFunc RemovedFunc
44+
// We use this instead of time.Now() in order to make testing easier
45+
now func() time.Time
4446
}
4547

4648
// New creates a new cache with the given options
@@ -56,6 +58,7 @@ func New(maxSize int, opts *Options) Cache {
5658
maxSize: maxSize,
5759
pin: opts.Pin,
5860
rmFunc: opts.RemovedFunc,
61+
now: time.Now,
5962
}
6063
}
6164

@@ -97,7 +100,7 @@ func (c *lru) Get(key string) interface{} {
97100
cacheEntry.refCount++
98101
}
99102

100-
if cacheEntry.refCount == 0 && !cacheEntry.expiration.IsZero() && time.Now().After(cacheEntry.expiration) {
103+
if cacheEntry.refCount == 0 && !cacheEntry.expiration.IsZero() && c.now().After(cacheEntry.expiration) {
101104
// Entry has expired
102105
if c.rmFunc != nil {
103106
go c.rmFunc(cacheEntry.value)
@@ -182,7 +185,7 @@ func (c *lru) putInternal(key string, value interface{}, allowUpdate bool) (inte
182185
entry.value = value
183186
}
184187
if c.ttl != 0 {
185-
entry.expiration = time.Now().Add(c.ttl)
188+
entry.expiration = c.now().Add(c.ttl)
186189
}
187190
c.byAccess.MoveToFront(elt)
188191
if c.pin {
@@ -201,7 +204,7 @@ func (c *lru) putInternal(key string, value interface{}, allowUpdate bool) (inte
201204
}
202205

203206
if c.ttl != 0 {
204-
entry.expiration = time.Now().Add(c.ttl)
207+
entry.expiration = c.now().Add(c.ttl)
205208
}
206209

207210
c.byKey[key] = c.byAccess.PushFront(entry)

internal/common/cache/lru_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,17 @@ func TestLRU(t *testing.T) {
6565
func TestLRUWithTTL(t *testing.T) {
6666
cache := New(5, &Options{
6767
TTL: time.Millisecond * 100,
68-
})
68+
}).(*lru)
69+
70+
// We will capture this in the caches now function, and advance time as needed
71+
currentTime := time.UnixMilli(0)
72+
cache.now = func() time.Time { return currentTime }
73+
6974
cache.Put("A", "foo")
7075
assert.Equal(t, "foo", cache.Get("A"))
71-
time.Sleep(time.Millisecond * 300)
76+
77+
currentTime = currentTime.Add(time.Millisecond * 300)
78+
7279
assert.Nil(t, cache.Get("A"))
7380
assert.Equal(t, 0, cache.Size())
7481
}
@@ -139,18 +146,23 @@ func TestRemovedFuncWithTTL(t *testing.T) {
139146
assert.True(t, ok)
140147
ch <- true
141148
},
142-
})
149+
}).(*lru)
150+
151+
// We will capture this in the caches now function, and advance time as needed
152+
currentTime := time.UnixMilli(0)
153+
cache.now = func() time.Time { return currentTime }
143154

144155
cache.Put("A", t)
145156
assert.Equal(t, t, cache.Get("A"))
146-
time.Sleep(time.Millisecond * 100)
157+
158+
currentTime = currentTime.Add(time.Millisecond * 100)
159+
147160
assert.Nil(t, cache.Get("A"))
148161

149-
timeout := time.NewTimer(time.Millisecond * 300)
150162
select {
151163
case b := <-ch:
152164
assert.True(t, b)
153-
case <-timeout.C:
165+
case <-time.After(100 * time.Millisecond):
154166
t.Error("RemovedFunc did not send true on channel ch")
155167
}
156168
}

0 commit comments

Comments
 (0)