Skip to content

Commit

Permalink
br/pkg/utils: migrate tests to testify (#30032)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepymole committed Nov 24, 2021
1 parent d04a128 commit 791f59d
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 305 deletions.
67 changes: 29 additions & 38 deletions br/pkg/utils/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,20 @@ package utils_test

import (
"context"
"testing"
"time"

. "github.com/pingcap/check"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/mock"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/util/testleak"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var _ = Suite(&testBackofferSuite{})
func TestBackoffWithSuccess(t *testing.T) {
t.Parallel()

type testBackofferSuite struct {
mock *mock.Cluster
}

func (s *testBackofferSuite) SetUpSuite(c *C) {
var err error
s.mock, err = mock.NewCluster()
c.Assert(err, IsNil)
}

func (s *testBackofferSuite) TearDownSuite(c *C) {
testleak.AfterTest(c)()
}

func (s *testBackofferSuite) TestBackoffWithSuccess(c *C) {
var counter int
backoffer := utils.NewBackoffer(10, time.Nanosecond, time.Nanosecond)
err := utils.WithRetry(context.Background(), func() error {
Expand All @@ -47,11 +32,13 @@ func (s *testBackofferSuite) TestBackoffWithSuccess(c *C) {
}
return nil
}, backoffer)
c.Assert(counter, Equals, 3)
c.Assert(err, IsNil)
require.Equal(t, 3, counter)
require.NoError(t, err)
}

func (s *testBackofferSuite) TestBackoffWithFatalError(c *C) {
func TestBackoffWithFatalError(t *testing.T) {
t.Parallel()

var counter int
backoffer := utils.NewBackoffer(10, time.Nanosecond, time.Nanosecond)
gRPCError := status.Error(codes.Unavailable, "transport is closing")
Expand All @@ -69,38 +56,40 @@ func (s *testBackofferSuite) TestBackoffWithFatalError(c *C) {
}
return nil
}, backoffer)
c.Assert(counter, Equals, 4)
c.Assert(multierr.Errors(err), DeepEquals, []error{
require.Equal(t, 4, counter)
require.Equal(t, []error{
gRPCError,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVDownloadFailed,
berrors.ErrKVRangeIsEmpty,
})
}, multierr.Errors(err))
}

func (s *testBackofferSuite) TestBackoffWithFatalRawGRPCError(c *C) {
func TestBackoffWithFatalRawGRPCError(t *testing.T) {
t.Parallel()

var counter int
canceledError := status.Error(codes.Canceled, "context canceled")
backoffer := utils.NewBackoffer(10, time.Nanosecond, time.Nanosecond)
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
return canceledError // nolint:wrapcheck
}, backoffer)
c.Assert(counter, Equals, 1)
c.Assert(multierr.Errors(err), DeepEquals, []error{
canceledError,
})
require.Equal(t, 1, counter)
require.Equal(t, []error{canceledError}, multierr.Errors(err))
}

func (s *testBackofferSuite) TestBackoffWithRetryableError(c *C) {
func TestBackoffWithRetryableError(t *testing.T) {
t.Parallel()

var counter int
backoffer := utils.NewBackoffer(10, time.Nanosecond, time.Nanosecond)
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
return berrors.ErrKVEpochNotMatch
}, backoffer)
c.Assert(counter, Equals, 10)
c.Assert(multierr.Errors(err), DeepEquals, []error{
require.Equal(t, 10, counter)
require.Equal(t, []error{
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
Expand All @@ -111,19 +100,21 @@ func (s *testBackofferSuite) TestBackoffWithRetryableError(c *C) {
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
berrors.ErrKVEpochNotMatch,
})
}, multierr.Errors(err))
}

func (s *testBackofferSuite) TestPdBackoffWithRetryableError(c *C) {
func TestPdBackoffWithRetryableError(t *testing.T) {
t.Parallel()

var counter int
backoffer := utils.NewPDReqBackoffer()
gRPCError := status.Error(codes.Unavailable, "transport is closing")
err := utils.WithRetry(context.Background(), func() error {
defer func() { counter++ }()
return gRPCError
}, backoffer)
c.Assert(counter, Equals, 16)
c.Assert(multierr.Errors(err), DeepEquals, []error{
require.Equal(t, 16, counter)
require.Equal(t, []error{
gRPCError,
gRPCError,
gRPCError,
Expand All @@ -140,5 +131,5 @@ func (s *testBackofferSuite) TestPdBackoffWithRetryableError(c *C) {
gRPCError,
gRPCError,
gRPCError,
})
}, multierr.Errors(err))
}
19 changes: 9 additions & 10 deletions br/pkg/utils/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ package utils

import (
"os"
"testing"

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

type envSuit struct{}
func TestProxyFields(t *testing.T) {
t.Parallel()

var _ = check.Suite(&envSuit{})

func (s *envSuit) TestProxyFields(c *check.C) {
revIndex := map[string]int{
"http_proxy": 0,
"https_proxy": 1,
Expand All @@ -25,20 +24,20 @@ func (s *envSuit) TestProxyFields(c *check.C) {
// Each bit of the mask decided whether this index of `envs` would be set.
for mask := 0; mask <= 0b111; mask++ {
for _, env := range envs {
c.Assert(os.Unsetenv(env), check.IsNil)
require.NoError(t, os.Unsetenv(env))
}

for i := 0; i < 3; i++ {
if (1<<i)&mask != 0 {
c.Assert(os.Setenv(envs[i], envPreset[i]), check.IsNil)
require.NoError(t, os.Setenv(envs[i], envPreset[i]))
}
}

for _, field := range proxyFields() {
idx, ok := revIndex[field.Key]
c.Assert(ok, check.IsTrue)
c.Assert((1<<idx)&mask, check.Not(check.Equals), 0)
c.Assert(field.String, check.Equals, envPreset[idx])
require.True(t, ok)
require.NotZero(t, (1<<idx)&mask)
require.Equal(t, envPreset[idx], field.String)
}
}
}
46 changes: 7 additions & 39 deletions br/pkg/utils/json_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package utils

import (
"encoding/json"
"fmt"
"reflect"
"testing"

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

type testJSONSuite struct{}

var _ = Suite(&testJSONSuite{})

var testMetaJSONs = [][]byte{
[]byte(`{
"files": [
Expand Down Expand Up @@ -212,40 +206,14 @@ var testMetaJSONs = [][]byte{
}`),
}

type jsonEquals struct{}

func (j jsonEquals) Info() *CheckerInfo {
return &CheckerInfo{
Name: "JSONEquals",
Params: []string{"lhs", "rhs"},
}
}

func (j jsonEquals) Check(params []interface{}, _ []string) (result bool, error string) {
lhs := params[0].([]byte)
rhs := params[1].([]byte)

lhsMap := map[string]interface{}{}
rhsMap := map[string]interface{}{}

if err := json.Unmarshal(lhs, &lhsMap); err != nil {
return false, fmt.Sprintf("failed to unmarshal lhs: %s", error)
}
if err := json.Unmarshal(rhs, &rhsMap); err != nil {
return false, fmt.Sprintf("failed to unmarshal rhs: %s", error)
}
if !reflect.DeepEqual(lhsMap, rhsMap) {
return false, fmt.Sprintf("lhs %s not equals rhs %s", lhsMap, rhsMap)
}
return true, ""
}
func TestEncodeAndDecode(t *testing.T) {
t.Parallel()

func (testJSONSuite) TestEncodeAndDecode(c *C) {
for _, testMetaJSON := range testMetaJSONs {
meta, err := UnmarshalBackupMeta(testMetaJSON)
c.Assert(err, IsNil)
require.NoError(t, err)
metaJSON, err := MarshalBackupMeta(meta)
c.Assert(err, IsNil)
c.Assert(metaJSON, jsonEquals{}, testMetaJSON)
require.NoError(t, err)
require.JSONEq(t, string(testMetaJSON), string(metaJSON))
}
}
30 changes: 16 additions & 14 deletions br/pkg/utils/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ package utils

import (
"encoding/hex"
"testing"

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

type testKeySuite struct{}
func TestParseKey(t *testing.T) {
t.Parallel()

var _ = Suite(&testKeySuite{})

func (r *testKeySuite) TestParseKey(c *C) {
// test rawKey
testRawKey := []struct {
rawKey string
Expand All @@ -28,8 +27,8 @@ func (r *testKeySuite) TestParseKey(c *C) {

for _, tt := range testRawKey {
parsedKey, err := ParseKey("raw", tt.rawKey)
c.Assert(err, IsNil)
c.Assert(parsedKey, BytesEquals, tt.ans)
require.NoError(t, err)
require.Equal(t, tt.ans, parsedKey)
}

// test EscapedKey
Expand All @@ -46,8 +45,8 @@ func (r *testKeySuite) TestParseKey(c *C) {

for _, tt := range testEscapedKey {
parsedKey, err := ParseKey("escaped", tt.EscapedKey)
c.Assert(err, IsNil)
c.Assert(parsedKey, BytesEquals, tt.ans)
require.NoError(t, err)
require.Equal(t, tt.ans, parsedKey)
}

// test hexKey
Expand All @@ -68,8 +67,8 @@ func (r *testKeySuite) TestParseKey(c *C) {
for _, tt := range testHexKey {
key := hex.EncodeToString([]byte(tt.hexKey))
parsedKey, err := ParseKey("hex", key)
c.Assert(err, IsNil)
c.Assert(parsedKey, BytesEquals, tt.ans)
require.NoError(t, err)
require.Equal(t, tt.ans, parsedKey)
}

// test other
Expand All @@ -89,11 +88,14 @@ func (r *testKeySuite) TestParseKey(c *C) {

for _, tt := range testNotSupportKey {
_, err := ParseKey("notSupport", tt.any)
c.Assert(err, ErrorMatches, "unknown format.*")
require.Error(t, err)
require.Regexp(t, "unknown format.*", err.Error())
}
}

func (r *testKeySuite) TestCompareEndKey(c *C) {
func TestCompareEndKey(t *testing.T) {
t.Parallel()

// test endKey
testCase := []struct {
key1 []byte
Expand All @@ -110,6 +112,6 @@ func (r *testKeySuite) TestCompareEndKey(c *C) {

for _, tt := range testCase {
res := CompareEndKey(tt.key1, tt.key2)
c.Assert(res, Equals, tt.ans)
require.Equal(t, tt.ans, res)
}
}
30 changes: 30 additions & 0 deletions br/pkg/utils/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import (
"testing"

"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
testbridge.WorkaroundGoCheckFlags()
goleak.VerifyTestMain(m, opts...)
}

0 comments on commit 791f59d

Please sign in to comment.