diff --git a/client/testutil/testutil.go b/client/testutil/testutil.go index 79a3c9eb913d..de9821024e29 100644 --- a/client/testutil/testutil.go +++ b/client/testutil/testutil.go @@ -21,34 +21,34 @@ import ( ) const ( - defaultWaitFor = time.Second * 20 - defaultSleepInterval = time.Millisecond * 100 + defaultWaitFor = time.Second * 20 + defaultTickInterval = time.Millisecond * 100 ) // WaitOp represents available options when execute Eventually. type WaitOp struct { - waitFor time.Duration - sleepInterval time.Duration + waitFor time.Duration + tickInterval time.Duration } -// WaitOption configures WaitOp +// WaitOption configures WaitOp. type WaitOption func(op *WaitOp) -// WithSleepInterval specify the sleep duration -func WithSleepInterval(sleep time.Duration) WaitOption { - return func(op *WaitOp) { op.sleepInterval = sleep } -} - -// WithWaitFor specify the max wait for duration +// WithWaitFor specify the max wait duration. func WithWaitFor(waitFor time.Duration) WaitOption { return func(op *WaitOp) { op.waitFor = waitFor } } +// WithTickInterval specify the tick interval to check the condition. +func WithTickInterval(tickInterval time.Duration) WaitOption { + return func(op *WaitOp) { op.tickInterval = tickInterval } +} + // Eventually asserts that given condition will be met in a period of time. func Eventually(re *require.Assertions, condition func() bool, opts ...WaitOption) { option := &WaitOp{ - waitFor: defaultWaitFor, - sleepInterval: defaultSleepInterval, + waitFor: defaultWaitFor, + tickInterval: defaultTickInterval, } for _, opt := range opts { opt(option) @@ -56,6 +56,6 @@ func Eventually(re *require.Assertions, condition func() bool, opts ...WaitOptio re.Eventually( condition, option.waitFor, - option.sleepInterval, + option.tickInterval, ) } diff --git a/pkg/cache/cache_test.go b/pkg/cache/cache_test.go index f708f966401b..5bcff65f7045 100644 --- a/pkg/cache/cache_test.go +++ b/pkg/cache/cache_test.go @@ -50,7 +50,7 @@ func TestExpireRegionCache(t *testing.T) { re.True(ok) re.Equal(expV, v2.(string)) - cache.PutWithTTL(11, "11", 1*time.Second) + cache.PutWithTTL(11, "11", time.Second) time.Sleep(5 * time.Second) k, v, success = cache.pop() re.False(success) @@ -58,7 +58,7 @@ func TestExpireRegionCache(t *testing.T) { re.Nil(v) // Test Get - cache.PutWithTTL(1, 1, 1*time.Second) + cache.PutWithTTL(1, 1, time.Second) cache.PutWithTTL(2, "v2", 5*time.Second) cache.PutWithTTL(3, 3.0, 5*time.Second) diff --git a/pkg/testutil/testutil.go b/pkg/testutil/testutil.go index 236438ecfd17..abe714384a58 100644 --- a/pkg/testutil/testutil.go +++ b/pkg/testutil/testutil.go @@ -19,72 +19,40 @@ import ( "strings" "time" - "github.com/pingcap/check" "github.com/pingcap/kvproto/pkg/pdpb" "github.com/stretchr/testify/require" "google.golang.org/grpc" ) const ( - defaultWaitRetryTimes = 200 - defaultSleepInterval = time.Millisecond * 100 - defaultWaitFor = time.Second * 20 + defaultWaitFor = time.Second * 20 + defaultTickInterval = time.Millisecond * 100 ) -// CheckFunc is a condition checker that passed to WaitUntil. Its implementation -// may call c.Fatal() to abort the test, or c.Log() to add more information. -type CheckFunc func() bool - -// WaitOp represents available options when execute WaitUntil +// WaitOp represents available options when execute Eventually. type WaitOp struct { - retryTimes int - sleepInterval time.Duration - waitFor time.Duration + waitFor time.Duration + tickInterval time.Duration } -// WaitOption configures WaitOp +// WaitOption configures WaitOp. type WaitOption func(op *WaitOp) -// WithRetryTimes specify the retry times -func WithRetryTimes(retryTimes int) WaitOption { - return func(op *WaitOp) { op.retryTimes = retryTimes } -} - -// WithSleepInterval specify the sleep duration -func WithSleepInterval(sleep time.Duration) WaitOption { - return func(op *WaitOp) { op.sleepInterval = sleep } -} - -// WithWaitFor specify the max wait for duration +// WithWaitFor specify the max wait duration. func WithWaitFor(waitFor time.Duration) WaitOption { return func(op *WaitOp) { op.waitFor = waitFor } } -// WaitUntil repeatedly evaluates f() for a period of time, util it returns true. -// NOTICE: this function will be removed soon, please use `Eventually` instead. -func WaitUntil(c *check.C, f CheckFunc, opts ...WaitOption) { - c.Log("wait start") - option := &WaitOp{ - retryTimes: defaultWaitRetryTimes, - sleepInterval: defaultSleepInterval, - } - for _, opt := range opts { - opt(option) - } - for i := 0; i < option.retryTimes; i++ { - if f() { - return - } - time.Sleep(option.sleepInterval) - } - c.Fatal("wait timeout") +// WithTickInterval specify the tick interval to check the condition. +func WithTickInterval(tickInterval time.Duration) WaitOption { + return func(op *WaitOp) { op.tickInterval = tickInterval } } // Eventually asserts that given condition will be met in a period of time. func Eventually(re *require.Assertions, condition func() bool, opts ...WaitOption) { option := &WaitOp{ - waitFor: defaultWaitFor, - sleepInterval: defaultSleepInterval, + waitFor: defaultWaitFor, + tickInterval: defaultTickInterval, } for _, opt := range opts { opt(option) @@ -92,7 +60,7 @@ func Eventually(re *require.Assertions, condition func() bool, opts ...WaitOptio re.Eventually( condition, option.waitFor, - option.sleepInterval, + option.tickInterval, ) } diff --git a/scripts/check-test.sh b/scripts/check-test.sh index 867d234cce6b..c3168066e3d0 100755 --- a/scripts/check-test.sh +++ b/scripts/check-test.sh @@ -1,28 +1,5 @@ #!/bin/bash -# TODO: remove this script after migrating all tests to the new test framework. - -# Check if there are any packages foget to add `TestingT` when use "github.com/pingcap/check". - -res=$(diff <(grep -rl --include=\*_test.go "github.com/pingcap/check" . | xargs -L 1 dirname | sort -u) \ - <(grep -rl --include=\*_test.go -E "^\s*(check\.)?TestingT\(" . | xargs -L 1 dirname | sort -u)) - -if [ "$res" ]; then - echo "following packages may be lost TestingT:" - echo "$res" | awk '{if(NF>1){print $2}}' - exit 1 -fi - -# Check if there are duplicated `TestingT` in package. - -res=$(grep -r --include=\*_test.go "TestingT(t)" . | cut -f1 | xargs -L 1 dirname | sort | uniq -d) - -if [ "$res" ]; then - echo "following packages may have duplicated TestingT:" - echo "$res" - exit 1 -fi - # Check if there is any inefficient assert function usage in package. res=$(grep -rn --include=\*_test.go -E "(re|suite|require)\.(True|False)\((t, )?reflect\.DeepEqual\(" . | sort -u) \ diff --git a/server/api/tso_test.go b/server/api/tso_test.go index 2f59a2ecf07a..07770b130d81 100644 --- a/server/api/tso_test.go +++ b/server/api/tso_test.go @@ -58,7 +58,7 @@ func (suite *tsoTestSuite) TestTransferAllocator() { suite.svr.GetTSOAllocatorManager().ClusterDCLocationChecker() _, err := suite.svr.GetTSOAllocatorManager().GetAllocator("dc-1") return err == nil - }, tu.WithRetryTimes(5), tu.WithSleepInterval(3*time.Second)) + }, tu.WithWaitFor(15*time.Second), tu.WithTickInterval(3*time.Second)) addr := suite.urlPrefix + "/tso/allocator/transfer/pd1?dcLocation=dc-1" err := tu.CheckPostJSON(testDialClient, addr, nil, tu.StatusOK(re)) suite.NoError(err) diff --git a/server/schedule/operator/status_tracker_test.go b/server/schedule/operator/status_tracker_test.go index d4441b0e7b60..04eb16d6ecde 100644 --- a/server/schedule/operator/status_tracker_test.go +++ b/server/schedule/operator/status_tracker_test.go @@ -123,11 +123,11 @@ func TestCheckStepTimeout(t *testing.T) { status OpStatus }{{ step: AddLearner{}, - start: time.Now().Add(-(SlowOperatorWaitTime - 1*time.Second)), + start: time.Now().Add(-(SlowOperatorWaitTime - time.Second)), status: STARTED, }, { step: AddLearner{}, - start: time.Now().Add(-(SlowOperatorWaitTime + 1*time.Second)), + start: time.Now().Add(-(SlowOperatorWaitTime + time.Second)), status: TIMEOUT, }} diff --git a/tests/client/client_test.go b/tests/client/client_test.go index 8e67cfb49498..2709c7cbc24b 100644 --- a/tests/client/client_test.go +++ b/tests/client/client_test.go @@ -424,14 +424,14 @@ func TestCustomTimeout(t *testing.T) { defer cluster.Destroy() endpoints := runServer(re, cluster) - cli := setupCli(re, ctx, endpoints, pd.WithCustomTimeoutOption(1*time.Second)) + cli := setupCli(re, ctx, endpoints, pd.WithCustomTimeoutOption(time.Second)) start := time.Now() re.NoError(failpoint.Enable("github.com/tikv/pd/server/customTimeout", "return(true)")) _, err = cli.GetAllStores(context.TODO()) re.NoError(failpoint.Disable("github.com/tikv/pd/server/customTimeout")) re.Error(err) - re.GreaterOrEqual(time.Since(start), 1*time.Second) + re.GreaterOrEqual(time.Since(start), time.Second) re.Less(time.Since(start), 2*time.Second) } @@ -1306,7 +1306,7 @@ func (suite *clientTestSuite) TestScatterRegion() { return resp.GetRegionId() == regionID && string(resp.GetDesc()) == "scatter-region" && resp.GetStatus() == pdpb.OperatorStatus_RUNNING - }, testutil.WithSleepInterval(1*time.Second)) + }, testutil.WithTickInterval(time.Second)) // Test interface `ScatterRegion`. // TODO: Deprecate interface `ScatterRegion`. @@ -1323,5 +1323,5 @@ func (suite *clientTestSuite) TestScatterRegion() { return resp.GetRegionId() == regionID && string(resp.GetDesc()) == "scatter-region" && resp.GetStatus() == pdpb.OperatorStatus_RUNNING - }, testutil.WithSleepInterval(1*time.Second)) + }, testutil.WithTickInterval(time.Second)) } diff --git a/tests/server/member/member_test.go b/tests/server/member/member_test.go index 229b47560453..15819bb4bf44 100644 --- a/tests/server/member/member_test.go +++ b/tests/server/member/member_test.go @@ -199,7 +199,7 @@ func waitEtcdLeaderChange(re *require.Assertions, server *tests.TestServer, old return false } return leader != old - }, testutil.WithWaitFor(time.Second*90), testutil.WithSleepInterval(time.Second)) + }, testutil.WithWaitFor(90*time.Second), testutil.WithTickInterval(time.Second)) return leader } diff --git a/tests/server/tso/allocator_test.go b/tests/server/tso/allocator_test.go index 59cedea07833..4640a74239a7 100644 --- a/tests/server/tso/allocator_test.go +++ b/tests/server/tso/allocator_test.go @@ -166,7 +166,7 @@ func TestPriorityAndDifferentLocalTSO(t *testing.T) { defer wg.Done() testutil.Eventually(re, func() bool { return cluster.WaitAllocatorLeader(dc) == serName - }, testutil.WithWaitFor(time.Second*90), testutil.WithSleepInterval(time.Second)) + }, testutil.WithWaitFor(90*time.Second), testutil.WithTickInterval(time.Second)) }(serverName, dcLocation) } wg.Wait() diff --git a/tests/server/tso/manager_test.go b/tests/server/tso/manager_test.go index 00278544f55e..5d19b3edea3f 100644 --- a/tests/server/tso/manager_test.go +++ b/tests/server/tso/manager_test.go @@ -182,7 +182,7 @@ func TestNextLeaderKey(t *testing.T) { cluster.CheckClusterDCLocation() currName := cluster.WaitAllocatorLeader("dc-1") return currName == name - }, testutil.WithSleepInterval(1*time.Second)) + }, testutil.WithTickInterval(time.Second)) return } }