Skip to content

Commit

Permalink
remove time.Sleep from tests (cadence-workflow#5489)
Browse files Browse the repository at this point in the history
  • Loading branch information
3vilhamster authored Dec 19, 2023
1 parent b8b3bdc commit 14e45f9
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions common/cache/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,17 @@ func TestLRUWithTTL(t *testing.T) {
cache := New(&Options{
MaxCount: 5,
TTL: time.Millisecond * 100,
})
}).(*lru)

// We will capture this in the caches now function, and advance time as needed
currentTime := time.UnixMilli(0)
cache.now = func() time.Time { return currentTime }

cache.Put("A", "foo")
assert.Equal(t, "foo", cache.Get("A"))
time.Sleep(time.Millisecond * 300)

currentTime = currentTime.Add(time.Millisecond * 300)

assert.Nil(t, cache.Get("A"))
assert.Equal(t, 0, cache.Size())
}
Expand Down Expand Up @@ -189,18 +196,23 @@ func TestRemovedFuncWithTTL(t *testing.T) {
assert.True(t, ok)
ch <- true
},
})
}).(*lru)

// We will capture this in the caches now function, and advance time as needed
currentTime := time.UnixMilli(0)
cache.now = func() time.Time { return currentTime }

cache.Put("A", t)
assert.Equal(t, t, cache.Get("A"))
time.Sleep(time.Millisecond * 100)

currentTime = currentTime.Add(time.Millisecond * 100)

assert.Nil(t, cache.Get("A"))

timeout := time.NewTimer(time.Millisecond * 300)
select {
case b := <-ch:
assert.True(t, b)
case <-timeout.C:
case <-time.After(100 * time.Millisecond):
t.Error("RemovedFunc did not send true on channel ch")
}
}
Expand All @@ -216,24 +228,27 @@ func TestRemovedFuncWithTTL_Pin(t *testing.T) {
assert.True(t, ok)
ch <- true
},
})
}).(*lru)

// We will capture this in the caches now function, and advance time as needed
currentTime := time.UnixMilli(0)
cache.now = func() time.Time { return currentTime }

_, err := cache.PutIfNotExist("A", t)
assert.NoError(t, err)
assert.Equal(t, t, cache.Get("A"))
time.Sleep(time.Millisecond * 100)
currentTime = currentTime.Add(time.Millisecond * 100)
assert.Equal(t, t, cache.Get("A"))
// release 3 time since put if not exist also increase the counter
cache.Release("A")
cache.Release("A")
cache.Release("A")
assert.Nil(t, cache.Get("A"))

timeout := time.NewTimer(time.Millisecond * 300)
select {
case b := <-ch:
assert.True(t, b)
case <-timeout.C:
case <-time.After(300 * time.Millisecond):
t.Error("RemovedFunc did not send true on channel ch")
}
}
Expand Down

0 comments on commit 14e45f9

Please sign in to comment.