Skip to content

Commit

Permalink
tests: make TestExpireRegionCache stable (tikv#7010)
Browse files Browse the repository at this point in the history
close tikv#6363

Signed-off-by: lhy1024 <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
lhy1024 and ti-chi-bot[bot] authored Aug 31, 2023
1 parent 934c9df commit 13dd27b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/testutil"
)

func TestExpireRegionCache(t *testing.T) {
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestExpireRegionCache(t *testing.T) {

re.Equal(sortIDs(cache.GetAllID()), []uint64{1, 2, 3})

// after 20ms, the key 1 will be expired
time.Sleep(20 * time.Millisecond)

value, ok = cache.Get(1)
Expand All @@ -92,7 +94,10 @@ func TestExpireRegionCache(t *testing.T) {
re.True(ok)
re.Equal(3.0, value)

re.Equal(2, cache.Len())
testutil.Eventually(re, func() bool {
// we can't ensure whether gc is executed, so we check the length of cache in a loop.
return cache.Len() == 2
}, testutil.WithWaitFor(50*time.Millisecond), testutil.WithTickInterval(time.Millisecond))
re.Equal(sortIDs(cache.GetAllID()), []uint64{2, 3})

cache.Remove(2)
Expand Down

0 comments on commit 13dd27b

Please sign in to comment.