Skip to content

Commit

Permalink
pkg, api, tests: migrate the server/api tests to testify (#5204)
Browse files Browse the repository at this point in the history
close #5199

Migrate the server/api tests to testify.

Signed-off-by: JmPotato <ghzpotato@gmail.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
  • Loading branch information
JmPotato and ti-chi-bot authored Jun 22, 2022
1 parent 18fbc4a commit 3b3ff69
Show file tree
Hide file tree
Showing 45 changed files with 2,300 additions and 2,198 deletions.
2 changes: 1 addition & 1 deletion client/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestDynamicOptionChange(t *testing.T) {
expectBool := true
o.setEnableTSOFollowerProxy(expectBool)
// Check the value changing notification.
testutil.WaitUntil(t, func() bool {
testutil.Eventually(re, func() bool {
<-o.enableTSOFollowerProxyCh
return true
})
Expand Down
42 changes: 20 additions & 22 deletions client/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,47 @@
package testutil

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

const (
waitMaxRetry = 200
waitRetrySleep = time.Millisecond * 100
defaultWaitFor = time.Second * 20
defaultSleepInterval = time.Millisecond * 100
)

// WaitOp represents available options when execute WaitUntil
// WaitOp represents available options when execute Eventually.
type WaitOp struct {
retryTimes int
waitFor time.Duration
sleepInterval time.Duration
}

// 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 }
}

// WaitUntil repeatedly evaluates f() for a period of time, util it returns true.
func WaitUntil(t *testing.T, f func() bool, opts ...WaitOption) {
t.Log("wait start")
// WithWaitFor specify the max wait for duration
func WithWaitFor(waitFor time.Duration) WaitOption {
return func(op *WaitOp) { op.waitFor = waitFor }
}

// 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{
retryTimes: waitMaxRetry,
sleepInterval: waitRetrySleep,
waitFor: defaultWaitFor,
sleepInterval: defaultSleepInterval,
}
for _, opt := range opts {
opt(option)
}
for i := 0; i < option.retryTimes; i++ {
if f() {
return
}
time.Sleep(option.sleepInterval)
}
t.Fatal("wait timeout")
re.Eventually(
condition,
option.waitFor,
option.sleepInterval,
)
}
36 changes: 17 additions & 19 deletions pkg/testutil/api_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,67 @@ import (
"encoding/json"
"io"
"net/http"
"strings"

"github.com/pingcap/check"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/apiutil"
)

// Status is used to check whether http response code is equal given code
func Status(c *check.C, code int) func([]byte, int) {
func Status(re *require.Assertions, code int) func([]byte, int) {
return func(_ []byte, i int) {
c.Assert(i, check.Equals, code)
re.Equal(code, i)
}
}

// StatusOK is used to check whether http response code is equal http.StatusOK
func StatusOK(c *check.C) func([]byte, int) {
return Status(c, http.StatusOK)
func StatusOK(re *require.Assertions) func([]byte, int) {
return Status(re, http.StatusOK)
}

// StatusNotOK is used to check whether http response code is not equal http.StatusOK
func StatusNotOK(c *check.C) func([]byte, int) {
func StatusNotOK(re *require.Assertions) func([]byte, int) {
return func(_ []byte, i int) {
c.Assert(i == http.StatusOK, check.IsFalse)
re.NotEqual(http.StatusOK, i)
}
}

// ExtractJSON is used to check whether given data can be extracted successfully
func ExtractJSON(c *check.C, data interface{}) func([]byte, int) {
func ExtractJSON(re *require.Assertions, data interface{}) func([]byte, int) {
return func(res []byte, _ int) {
err := json.Unmarshal(res, data)
c.Assert(err, check.IsNil)
re.NoError(json.Unmarshal(res, data))
}
}

// StringContain is used to check whether response context contains given string
func StringContain(c *check.C, sub string) func([]byte, int) {
func StringContain(re *require.Assertions, sub string) func([]byte, int) {
return func(res []byte, _ int) {
c.Assert(strings.Contains(string(res), sub), check.IsTrue)
re.Contains(string(res), sub)
}
}

// StringEqual is used to check whether response context equal given string
func StringEqual(c *check.C, str string) func([]byte, int) {
func StringEqual(re *require.Assertions, str string) func([]byte, int) {
return func(res []byte, _ int) {
c.Assert(strings.Contains(string(res), str), check.IsTrue)
re.Contains(string(res), str)
}
}

// ReadGetJSON is used to do get request and check whether given data can be extracted successfully
func ReadGetJSON(c *check.C, client *http.Client, url string, data interface{}) error {
func ReadGetJSON(re *require.Assertions, client *http.Client, url string, data interface{}) error {
resp, err := apiutil.GetJSON(client, url, nil)
if err != nil {
return err
}
return checkResp(resp, StatusOK(c), ExtractJSON(c, data))
return checkResp(resp, StatusOK(re), ExtractJSON(re, data))
}

// ReadGetJSONWithBody is used to do get request with input and check whether given data can be extracted successfully
func ReadGetJSONWithBody(c *check.C, client *http.Client, url string, input []byte, data interface{}) error {
func ReadGetJSONWithBody(re *require.Assertions, client *http.Client, url string, input []byte, data interface{}) error {
resp, err := apiutil.GetJSON(client, url, input)
if err != nil {
return err
}
return checkResp(resp, StatusOK(c), ExtractJSON(c, data))
return checkResp(resp, StatusOK(re), ExtractJSON(re, data))
}

// CheckPostJSON is used to do post request and do check options
Expand Down
11 changes: 1 addition & 10 deletions pkg/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,7 @@ func NewRequestHeader(clusterID uint64) *pdpb.RequestHeader {
}

// MustNewGrpcClient must create a new grpc client.
func MustNewGrpcClient(c *check.C, addr string) pdpb.PDClient {
conn, err := grpc.Dial(strings.TrimPrefix(addr, "http://"), grpc.WithInsecure())

c.Assert(err, check.IsNil)
return pdpb.NewPDClient(conn)
}

// MustNewGrpcClientWithTestify must create a new grpc client.
// NOTICE: this is a temporary function that we will be used to replace `MustNewGrpcClient` later.
func MustNewGrpcClientWithTestify(re *require.Assertions, addr string) pdpb.PDClient {
func MustNewGrpcClient(re *require.Assertions, addr string) pdpb.PDClient {
conn, err := grpc.Dial(strings.TrimPrefix(addr, "http://"), grpc.WithInsecure())

re.NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/typeutil/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDurationJSON(t *testing.T) {
example := &example{}

text := []byte(`{"interval":"1h1m1s"}`)
re.Nil(json.Unmarshal(text, example))
re.NoError(json.Unmarshal(text, example))
re.Equal(float64(60*60+60+1), example.Interval.Seconds())

b, err := json.Marshal(example)
Expand Down
Loading

0 comments on commit 3b3ff69

Please sign in to comment.