Skip to content

Commit

Permalink
*: clean up the surrounding code of check pkg (tikv#5225)
Browse files Browse the repository at this point in the history
ref tikv#4813, close tikv#5105

Clean up the surrounding code of check pkg.

Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato authored and CabinfeverB committed Jul 14, 2022
1 parent 48ac48e commit 1c26652
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 94 deletions.
28 changes: 14 additions & 14 deletions client/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,41 @@ 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)
}
re.Eventually(
condition,
option.waitFor,
option.sleepInterval,
option.tickInterval,
)
}
4 changes: 2 additions & 2 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ 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)
re.Nil(k)
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)

Expand Down
58 changes: 13 additions & 45 deletions pkg/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,80 +19,48 @@ 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)
}
re.Eventually(
condition,
option.waitFor,
option.sleepInterval,
option.tickInterval,
)
}

Expand Down
23 changes: 0 additions & 23 deletions scripts/check-test.sh
Original file line number Diff line number Diff line change
@@ -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) \
Expand Down
2 changes: 1 addition & 1 deletion server/api/tso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions server/schedule/operator/status_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}}

Expand Down
8 changes: 4 additions & 4 deletions tests/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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`.
Expand All @@ -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))
}
2 changes: 1 addition & 1 deletion tests/server/member/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion tests/server/tso/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/server/tso/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 1c26652

Please sign in to comment.