Skip to content

Commit

Permalink
Clean up the surrounding code of check pkg
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato committed Jun 24, 2022
1 parent 37cb7e4 commit 02de74f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 88 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,
)
}
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(time.Second*15), 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 tests/client/client_test.go
Original file line number Diff line number Diff line change
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(1*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(1*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(time.Second*90), 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(time.Second*90), 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(1*time.Second))
return
}
}

0 comments on commit 02de74f

Please sign in to comment.