From 9c66a06afafb0b7436a08db8917ff079a1050e96 Mon Sep 17 00:00:00 2001 From: Andreas Lien <30557582+DiFronzo@users.noreply.github.com> Date: Sun, 3 Oct 2021 13:40:48 +0200 Subject: [PATCH 1/4] refactor: migrate test-infra to testify for mainly window_func_test.go (#28536) --- executor/aggfuncs/func_cume_dist_test.go | 9 ++-- executor/aggfuncs/func_lead_lag_test.go | 15 ++++-- executor/aggfuncs/func_ntile_test.go | 10 ++-- executor/aggfuncs/func_percent_rank_test.go | 9 ++-- executor/aggfuncs/func_rank_test.go | 9 ++-- executor/aggfuncs/func_value_test.go | 9 ++-- executor/aggfuncs/row_number_test.go | 9 ++-- executor/aggfuncs/window_func_test.go | 55 ++++++++++++--------- 8 files changed, 77 insertions(+), 48 deletions(-) diff --git a/executor/aggfuncs/func_cume_dist_test.go b/executor/aggfuncs/func_cume_dist_test.go index d5a6c5dc2df85..7a0180b272fd1 100644 --- a/executor/aggfuncs/func_cume_dist_test.go +++ b/executor/aggfuncs/func_cume_dist_test.go @@ -15,13 +15,16 @@ package aggfuncs_test import ( - . "github.com/pingcap/check" + "testing" + "github.com/pingcap/parser/ast" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/executor/aggfuncs" ) -func (s *testSuite) TestMemCumeDist(c *C) { +func TestMemCumeDist(t *testing.T) { + t.Parallel() + tests := []windowMemTest{ buildWindowMemTester(ast.WindowFuncCumeDist, mysql.TypeLonglong, 0, 1, 1, aggfuncs.DefPartialResult4CumeDistSize, rowMemDeltaGens), @@ -31,6 +34,6 @@ func (s *testSuite) TestMemCumeDist(c *C) { aggfuncs.DefPartialResult4CumeDistSize, rowMemDeltaGens), } for _, test := range tests { - s.testWindowAggMemFunc(c, test) + testWindowAggMemFunc(t, test) } } diff --git a/executor/aggfuncs/func_lead_lag_test.go b/executor/aggfuncs/func_lead_lag_test.go index 9dc49c19208d6..7459b8082dca8 100644 --- a/executor/aggfuncs/func_lead_lag_test.go +++ b/executor/aggfuncs/func_lead_lag_test.go @@ -15,7 +15,8 @@ package aggfuncs_test import ( - . "github.com/pingcap/check" + "testing" + "github.com/pingcap/parser/ast" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/executor/aggfuncs" @@ -23,7 +24,9 @@ import ( "github.com/pingcap/tidb/types" ) -func (s *testSuite) TestLeadLag(c *C) { +func TestLeadLag(t *testing.T) { + t.Parallel() + zero := expression.NewZero() one := expression.NewOne() two := &expression.Constant{ @@ -111,12 +114,14 @@ func (s *testSuite) TestLeadLag(c *C) { []expression.Expression{million, defaultArg}, 0, numRows, 0, 1, 2), } for _, test := range tests { - s.testWindowFunc(c, test) + testWindowFunc(t, test) } } -func (s *testSuite) TestMemLeadLag(c *C) { +func TestMemLeadLag(t *testing.T) { + t.Parallel() + zero := expression.NewZero() one := expression.NewOne() two := &expression.Constant{ @@ -160,7 +165,7 @@ func (s *testSuite) TestMemLeadLag(c *C) { } for _, test := range tests { - s.testWindowAggMemFunc(c, test) + testWindowAggMemFunc(t, test) } } diff --git a/executor/aggfuncs/func_ntile_test.go b/executor/aggfuncs/func_ntile_test.go index da820a9d6c3bc..fdfcd9d08291f 100644 --- a/executor/aggfuncs/func_ntile_test.go +++ b/executor/aggfuncs/func_ntile_test.go @@ -15,14 +15,16 @@ package aggfuncs_test import ( - . "github.com/pingcap/check" + "testing" + "github.com/pingcap/parser/ast" "github.com/pingcap/parser/mysql" - "github.com/pingcap/tidb/executor/aggfuncs" ) -func (s *testSuite) TestMemNtile(c *C) { +func TestMemNtile(t *testing.T) { + t.Parallel() + tests := []windowMemTest{ buildWindowMemTester(ast.WindowFuncNtile, mysql.TypeLonglong, 1, 1, 1, aggfuncs.DefPartialResult4Ntile, defaultUpdateMemDeltaGens), @@ -32,6 +34,6 @@ func (s *testSuite) TestMemNtile(c *C) { aggfuncs.DefPartialResult4Ntile, defaultUpdateMemDeltaGens), } for _, test := range tests { - s.testWindowAggMemFunc(c, test) + testWindowAggMemFunc(t, test) } } diff --git a/executor/aggfuncs/func_percent_rank_test.go b/executor/aggfuncs/func_percent_rank_test.go index ce174a2342be8..5e04218e19ddd 100644 --- a/executor/aggfuncs/func_percent_rank_test.go +++ b/executor/aggfuncs/func_percent_rank_test.go @@ -15,13 +15,16 @@ package aggfuncs_test import ( - . "github.com/pingcap/check" + "testing" + "github.com/pingcap/parser/ast" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/executor/aggfuncs" ) -func (s *testSuite) TestMemPercentRank(c *C) { +func TestMemPercentRank(t *testing.T) { + t.Parallel() + tests := []windowMemTest{ buildWindowMemTester(ast.WindowFuncPercentRank, mysql.TypeLonglong, 0, 1, 1, aggfuncs.DefPartialResult4RankSize, rowMemDeltaGens), @@ -31,6 +34,6 @@ func (s *testSuite) TestMemPercentRank(c *C) { aggfuncs.DefPartialResult4RankSize, rowMemDeltaGens), } for _, test := range tests { - s.testWindowAggMemFunc(c, test) + testWindowAggMemFunc(t, test) } } diff --git a/executor/aggfuncs/func_rank_test.go b/executor/aggfuncs/func_rank_test.go index eeb5f13724dcf..211ade16583bd 100644 --- a/executor/aggfuncs/func_rank_test.go +++ b/executor/aggfuncs/func_rank_test.go @@ -15,13 +15,16 @@ package aggfuncs_test import ( - . "github.com/pingcap/check" + "testing" + "github.com/pingcap/parser/ast" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/executor/aggfuncs" ) -func (s *testSuite) TestMemRank(c *C) { +func TestMemRank(t *testing.T) { + t.Parallel() + tests := []windowMemTest{ buildWindowMemTester(ast.WindowFuncRank, mysql.TypeLonglong, 0, 1, 1, aggfuncs.DefPartialResult4RankSize, rowMemDeltaGens), @@ -31,6 +34,6 @@ func (s *testSuite) TestMemRank(c *C) { aggfuncs.DefPartialResult4RankSize, rowMemDeltaGens), } for _, test := range tests { - s.testWindowAggMemFunc(c, test) + testWindowAggMemFunc(t, test) } } diff --git a/executor/aggfuncs/func_value_test.go b/executor/aggfuncs/func_value_test.go index 97a03bee0fd03..e83b0e08ee677 100644 --- a/executor/aggfuncs/func_value_test.go +++ b/executor/aggfuncs/func_value_test.go @@ -15,7 +15,8 @@ package aggfuncs_test import ( - . "github.com/pingcap/check" + "testing" + "github.com/pingcap/parser/ast" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/executor/aggfuncs" @@ -59,7 +60,9 @@ func nthValueEvaluateRowUpdateMemDeltaGens(nth int) updateMemDeltaGens { } } -func (s *testSuite) TestMemValue(c *C) { +func TestMemValue(t *testing.T) { + t.Parallel() + firstMemDeltaGens := nthValueEvaluateRowUpdateMemDeltaGens(1) secondMemDeltaGens := nthValueEvaluateRowUpdateMemDeltaGens(2) fifthMemDeltaGens := nthValueEvaluateRowUpdateMemDeltaGens(5) @@ -96,6 +99,6 @@ func (s *testSuite) TestMemValue(c *C) { aggfuncs.DefPartialResult4NthValueSize+aggfuncs.DefValue4StringSize, fifthMemDeltaGens), } for _, test := range tests { - s.testWindowAggMemFunc(c, test) + testWindowAggMemFunc(t, test) } } diff --git a/executor/aggfuncs/row_number_test.go b/executor/aggfuncs/row_number_test.go index 3a76be76be719..d4c001fb10a5e 100644 --- a/executor/aggfuncs/row_number_test.go +++ b/executor/aggfuncs/row_number_test.go @@ -15,18 +15,21 @@ package aggfuncs_test import ( - . "github.com/pingcap/check" + "testing" + "github.com/pingcap/parser/ast" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/executor/aggfuncs" ) -func (s *testSuite) TestMemRowNumber(c *C) { +func TestMemRowNumber(t *testing.T) { + t.Parallel() + tests := []windowMemTest{ buildWindowMemTester(ast.WindowFuncRowNumber, mysql.TypeLonglong, 0, 0, 4, aggfuncs.DefPartialResult4RowNumberSize, defaultUpdateMemDeltaGens), } for _, test := range tests { - s.testWindowAggMemFunc(c, test) + testWindowAggMemFunc(t, test) } } diff --git a/executor/aggfuncs/window_func_test.go b/executor/aggfuncs/window_func_test.go index a6d2a9e75d333..8e2804c68025f 100644 --- a/executor/aggfuncs/window_func_test.go +++ b/executor/aggfuncs/window_func_test.go @@ -15,9 +15,9 @@ package aggfuncs_test import ( + "testing" "time" - . "github.com/pingcap/check" "github.com/pingcap/parser/ast" "github.com/pingcap/parser/mysql" @@ -27,6 +27,9 @@ import ( "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/types/json" "github.com/pingcap/tidb/util/chunk" + "github.com/pingcap/tidb/util/mock" + + "github.com/stretchr/testify/require" ) type windowTest struct { @@ -54,52 +57,54 @@ type windowMemTest struct { updateMemDeltaGens updateMemDeltaGens } -func (s *testSuite) testWindowFunc(c *C, p windowTest) { +func testWindowFunc(t *testing.T, p windowTest) { srcChk := p.genSrcChk() + ctx := mock.NewContext() - desc, err := aggregation.NewAggFuncDesc(s.ctx, p.funcName, p.args, false) - c.Assert(err, IsNil) - finalFunc := aggfuncs.BuildWindowFunctions(s.ctx, desc, 0, p.orderByCols) + desc, err := aggregation.NewAggFuncDesc(ctx, p.funcName, p.args, false) + require.NoError(t, err) + finalFunc := aggfuncs.BuildWindowFunctions(ctx, desc, 0, p.orderByCols) finalPr, _ := finalFunc.AllocPartialResult() resultChk := chunk.NewChunkWithCapacity([]*types.FieldType{desc.RetTp}, 1) iter := chunk.NewIterator4Chunk(srcChk) for row := iter.Begin(); row != iter.End(); row = iter.Next() { - _, err = finalFunc.UpdatePartialResult(s.ctx, []chunk.Row{row}, finalPr) - c.Assert(err, IsNil) + _, err = finalFunc.UpdatePartialResult(ctx, []chunk.Row{row}, finalPr) + require.NoError(t, err) } - c.Assert(p.numRows, Equals, len(p.results)) + require.Len(t, p.results, p.numRows) for i := 0; i < p.numRows; i++ { - err = finalFunc.AppendFinalResult2Chunk(s.ctx, finalPr, resultChk) - c.Assert(err, IsNil) + err = finalFunc.AppendFinalResult2Chunk(ctx, finalPr, resultChk) + require.NoError(t, err) dt := resultChk.GetRow(0).GetDatum(0, desc.RetTp) - result, err := dt.CompareDatum(s.ctx.GetSessionVars().StmtCtx, &p.results[i]) - c.Assert(err, IsNil) - c.Assert(result, Equals, 0) + result, err := dt.CompareDatum(ctx.GetSessionVars().StmtCtx, &p.results[i]) + require.NoError(t, err) + require.Equal(t, 0, result) resultChk.Reset() } finalFunc.ResetPartialResult(finalPr) } -func (s *testSuite) testWindowAggMemFunc(c *C, p windowMemTest) { +func testWindowAggMemFunc(t *testing.T, p windowMemTest) { srcChk := p.windowTest.genSrcChk() + ctx := mock.NewContext() - desc, err := aggregation.NewAggFuncDesc(s.ctx, p.windowTest.funcName, p.windowTest.args, false) - c.Assert(err, IsNil) - finalFunc := aggfuncs.BuildWindowFunctions(s.ctx, desc, 0, p.windowTest.orderByCols) + desc, err := aggregation.NewAggFuncDesc(ctx, p.windowTest.funcName, p.windowTest.args, false) + require.NoError(t, err) + finalFunc := aggfuncs.BuildWindowFunctions(ctx, desc, 0, p.windowTest.orderByCols) finalPr, memDelta := finalFunc.AllocPartialResult() - c.Assert(memDelta, Equals, p.allocMemDelta) + require.Equal(t, p.allocMemDelta, memDelta) updateMemDeltas, err := p.updateMemDeltaGens(srcChk, p.windowTest.dataType) - c.Assert(err, IsNil) + require.NoError(t, err) i := 0 iter := chunk.NewIterator4Chunk(srcChk) for row := iter.Begin(); row != iter.End(); row = iter.Next() { - memDelta, err = finalFunc.UpdatePartialResult(s.ctx, []chunk.Row{row}, finalPr) - c.Assert(err, IsNil) - c.Assert(memDelta, Equals, updateMemDeltas[i]) + memDelta, err = finalFunc.UpdatePartialResult(ctx, []chunk.Row{row}, finalPr) + require.NoError(t, err) + require.Equal(t, updateMemDeltas[i], memDelta) i++ } } @@ -166,7 +171,9 @@ func buildWindowMemTesterWithArgs(funcName string, tp byte, args []expression.Ex return pt } -func (s *testSuite) TestWindowFunctions(c *C) { +func TestWindowFunctions(t *testing.T) { + t.Parallel() + tests := []windowTest{ buildWindowTester(ast.WindowFuncCumeDist, mysql.TypeLonglong, 0, 1, 1, 1), buildWindowTester(ast.WindowFuncCumeDist, mysql.TypeLonglong, 0, 0, 2, 1, 1), @@ -203,6 +210,6 @@ func (s *testSuite) TestWindowFunctions(c *C) { buildWindowTester(ast.WindowFuncRowNumber, mysql.TypeLonglong, 0, 0, 4, 1, 2, 3, 4), } for _, test := range tests { - s.testWindowFunc(c, test) + testWindowFunc(t, test) } } From 9333c5d4167b3ac9d8a1e270fc79e42645d314fd Mon Sep 17 00:00:00 2001 From: Suriya Ganesh A Date: Mon, 4 Oct 2021 22:08:49 +0530 Subject: [PATCH 2/4] executor: migrate test infra to testify for adpater_test.go (#28539) --- executor/adapter_test.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/executor/adapter_test.go b/executor/adapter_test.go index ebc7fc60246d0..40ae6e168b1f0 100644 --- a/executor/adapter_test.go +++ b/executor/adapter_test.go @@ -15,24 +15,29 @@ package executor_test import ( + "testing" "time" - . "github.com/pingcap/check" - "github.com/pingcap/tidb/util/testkit" + "github.com/pingcap/tidb/testkit" + "github.com/stretchr/testify/require" ) -func (s *testSuiteP2) TestQueryTime(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestQueryTime(t *testing.T) { + t.Parallel() + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec("use test") - costTime := time.Since(tk.Se.GetSessionVars().StartTime) - c.Assert(costTime < 1*time.Second, IsTrue) + costTime := time.Since(tk.Session().GetSessionVars().StartTime) + require.Less(t, costTime, time.Second) tk.MustExec("drop table if exists t") tk.MustExec("create table t(a int)") tk.MustExec("insert into t values(1), (1), (1), (1), (1)") tk.MustExec("select * from t t1 join t t2 on t1.a = t2.a") - costTime = time.Since(tk.Se.GetSessionVars().StartTime) - c.Assert(costTime < 1*time.Second, IsTrue) + costTime = time.Since(tk.Session().GetSessionVars().StartTime) + require.Less(t, costTime, time.Second) } From 476027077e43902f69b6d4b4608ebd06fd831d12 Mon Sep 17 00:00:00 2001 From: goroutine Date: Wed, 6 Oct 2021 02:30:50 +0800 Subject: [PATCH 3/4] *: update a few links in readme (#26874) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ec1f533775e24..8d14579d316e6 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ For support, please contact [PingCAP](http://bit.ly/contact_us_via_github). ### To start using TiDB -See [Quick Start Guide](https://pingcap.com/docs/stable/quick-start-with-tidb/). +See [Quick Start Guide](https://docs.pingcap.com/tidb/stable/quick-start-with-tidb). ### To start developing TiDB From 7fd60012336c46158df46c30d095a364fcc103f3 Mon Sep 17 00:00:00 2001 From: unconsolable Date: Wed, 6 Oct 2021 22:36:50 +0800 Subject: [PATCH 4/4] br/pkg/pdutil: migrate test-infra to testify (#28315) --- br/pkg/pdutil/main_test.go | 31 +++++ br/pkg/pdutil/pd.go | 18 ++- .../pdutil/{pd_test.go => pd_serial_test.go} | 110 +++++++++--------- 3 files changed, 102 insertions(+), 57 deletions(-) create mode 100644 br/pkg/pdutil/main_test.go rename br/pkg/pdutil/{pd_test.go => pd_serial_test.go} (72%) diff --git a/br/pkg/pdutil/main_test.go b/br/pkg/pdutil/main_test.go new file mode 100644 index 0000000000000..861c3921a3eb3 --- /dev/null +++ b/br/pkg/pdutil/main_test.go @@ -0,0 +1,31 @@ +// 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 pdutil + +import ( + "testing" + + "github.com/pingcap/tidb/util/testbridge" + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + testbridge.WorkaroundGoCheckFlags() + opts := []goleak.Option{ + goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"), + goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), + } + goleak.VerifyTestMain(m, opts...) +} diff --git a/br/pkg/pdutil/pd.go b/br/pkg/pdutil/pd.go index 5610578c0f766..2f898d9c062ef 100644 --- a/br/pkg/pdutil/pd.go +++ b/br/pkg/pdutil/pd.go @@ -156,14 +156,17 @@ func pdRequest( if count > pdRequestRetryTime || resp.StatusCode < 500 { break } - resp.Body.Close() - time.Sleep(time.Second) + _ = resp.Body.Close() + time.Sleep(pdRequestRetryInterval()) resp, err = cli.Do(req) if err != nil { return nil, errors.Trace(err) } } - defer resp.Body.Close() + defer func() { + _ = resp.Body.Close() + }() + if resp.StatusCode != http.StatusOK { res, _ := io.ReadAll(resp.Body) return nil, errors.Annotatef(berrors.ErrPDInvalidResponse, "[%d] %s %s", resp.StatusCode, res, reqURL) @@ -176,6 +179,15 @@ func pdRequest( return r, nil } +func pdRequestRetryInterval() time.Duration { + failpoint.Inject("FastRetry", func(v failpoint.Value) { + if v.(bool) { + failpoint.Return(0) + } + }) + return time.Second +} + // PdController manage get/update config from pd. type PdController struct { addrs []string diff --git a/br/pkg/pdutil/pd_test.go b/br/pkg/pdutil/pd_serial_test.go similarity index 72% rename from br/pkg/pdutil/pd_test.go rename to br/pkg/pdutil/pd_serial_test.go index e4e82d412171c..2dde535cd54b9 100644 --- a/br/pkg/pdutil/pd_test.go +++ b/br/pkg/pdutil/pd_serial_test.go @@ -15,26 +15,19 @@ import ( "testing" "github.com/coreos/go-semver/semver" - . "github.com/pingcap/check" + "github.com/pingcap/failpoint" "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/tidb/util/codec" + "github.com/stretchr/testify/require" "github.com/tikv/pd/pkg/typeutil" "github.com/tikv/pd/server/api" "github.com/tikv/pd/server/core" "github.com/tikv/pd/server/statistics" ) -func TestT(t *testing.T) { - TestingT(t) -} - -type testPDControllerSuite struct { -} - -var _ = Suite(&testPDControllerSuite{}) - -func (s *testPDControllerSuite) TestScheduler(c *C) { - ctx := context.Background() +func TestScheduler(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() scheduler := "balance-leader-scheduler" mock := func(context.Context, string, string, *http.Client, string, io.Reader) ([]byte, error) { @@ -44,13 +37,13 @@ func (s *testPDControllerSuite) TestScheduler(c *C) { pdController := &PdController{addrs: []string{"", ""}, schedulerPauseCh: schedulerPauseCh} _, err := pdController.pauseSchedulersAndConfigWith(ctx, []string{scheduler}, nil, mock) - c.Assert(err, ErrorMatches, "failed") + require.EqualError(t, err, "failed") go func() { <-schedulerPauseCh }() err = pdController.resumeSchedulerWith(ctx, []string{scheduler}, mock) - c.Assert(err, IsNil) + require.NoError(t, err) cfg := map[string]interface{}{ "max-merge-region-keys": 0, @@ -59,34 +52,37 @@ func (s *testPDControllerSuite) TestScheduler(c *C) { "max-pending-peer-count": uint64(16), } _, err = pdController.pauseSchedulersAndConfigWith(ctx, []string{}, cfg, mock) - c.Assert(err, ErrorMatches, "failed to update PD.*") + require.Error(t, err) + require.Regexp(t, "^failed to update PD.*", err.Error()) go func() { <-schedulerPauseCh }() + err = pdController.resumeSchedulerWith(ctx, []string{scheduler}, mock) + require.NoError(t, err) _, err = pdController.listSchedulersWith(ctx, mock) - c.Assert(err, ErrorMatches, "failed") + require.EqualError(t, err, "failed") mock = func(context.Context, string, string, *http.Client, string, io.Reader) ([]byte, error) { return []byte(`["` + scheduler + `"]`), nil } _, err = pdController.pauseSchedulersAndConfigWith(ctx, []string{scheduler}, cfg, mock) - c.Assert(err, IsNil) + require.NoError(t, err) go func() { <-schedulerPauseCh }() err = pdController.resumeSchedulerWith(ctx, []string{scheduler}, mock) - c.Assert(err, IsNil) + require.NoError(t, err) schedulers, err := pdController.listSchedulersWith(ctx, mock) - c.Assert(err, IsNil) - c.Assert(schedulers, HasLen, 1) - c.Assert(schedulers[0], Equals, scheduler) + require.NoError(t, err) + require.Len(t, schedulers, 1) + require.Equal(t, scheduler, schedulers[0]) } -func (s *testPDControllerSuite) TestGetClusterVersion(c *C) { +func TestGetClusterVersion(t *testing.T) { pdController := &PdController{addrs: []string{"", ""}} // two endpoints counter := 0 mock := func(context.Context, string, string, *http.Client, string, io.Reader) ([]byte, error) { @@ -99,17 +95,17 @@ func (s *testPDControllerSuite) TestGetClusterVersion(c *C) { ctx := context.Background() respString, err := pdController.getClusterVersionWith(ctx, mock) - c.Assert(err, IsNil) - c.Assert(respString, Equals, "test") + require.NoError(t, err) + require.Equal(t, "test", respString) mock = func(context.Context, string, string, *http.Client, string, io.Reader) ([]byte, error) { return nil, errors.New("mock error") } _, err = pdController.getClusterVersionWith(ctx, mock) - c.Assert(err, NotNil) + require.Error(t, err) } -func (s *testPDControllerSuite) TestRegionCount(c *C) { +func TestRegionCount(t *testing.T) { regions := core.NewRegionsInfo() regions.SetRegion(core.NewRegionInfo(&metapb.Region{ Id: 1, @@ -129,55 +125,61 @@ func (s *testPDControllerSuite) TestRegionCount(c *C) { EndKey: codec.EncodeBytes(nil, []byte{3, 4}), RegionEpoch: &metapb.RegionEpoch{}, }, nil)) - c.Assert(regions.Len(), Equals, 3) + require.Equal(t, 3, regions.Len()) mock := func( _ context.Context, addr string, prefix string, _ *http.Client, _ string, _ io.Reader, ) ([]byte, error) { query := fmt.Sprintf("%s/%s", addr, prefix) u, e := url.Parse(query) - c.Assert(e, IsNil, Commentf("%s", query)) + require.NoError(t, e, query) start := u.Query().Get("start_key") end := u.Query().Get("end_key") - c.Log(hex.EncodeToString([]byte(start))) - c.Log(hex.EncodeToString([]byte(end))) + t.Log(hex.EncodeToString([]byte(start))) + t.Log(hex.EncodeToString([]byte(end))) scanRegions := regions.ScanRange([]byte(start), []byte(end), 0) stats := statistics.RegionStats{Count: len(scanRegions)} ret, err := json.Marshal(stats) - c.Assert(err, IsNil) + require.NoError(t, err) return ret, nil } pdController := &PdController{addrs: []string{"http://mock"}} ctx := context.Background() resp, err := pdController.getRegionCountWith(ctx, mock, []byte{}, []byte{}) - c.Assert(err, IsNil) - c.Assert(resp, Equals, 3) + require.NoError(t, err) + require.Equal(t, 3, resp) resp, err = pdController.getRegionCountWith(ctx, mock, []byte{0}, []byte{0xff}) - c.Assert(err, IsNil) - c.Assert(resp, Equals, 3) + require.NoError(t, err) + require.Equal(t, 3, resp) resp, err = pdController.getRegionCountWith(ctx, mock, []byte{1, 2}, []byte{1, 4}) - c.Assert(err, IsNil) - c.Assert(resp, Equals, 2) + require.NoError(t, err) + require.Equal(t, 2, resp) } -func (s *testPDControllerSuite) TestPDVersion(c *C) { +func TestPDVersion(t *testing.T) { v := []byte("\"v4.1.0-alpha1\"\n") r := parseVersion(v) expectV := semver.New("4.1.0-alpha1") - c.Assert(r.Major, Equals, expectV.Major) - c.Assert(r.Minor, Equals, expectV.Minor) - c.Assert(r.PreRelease, Equals, expectV.PreRelease) + require.Equal(t, expectV.Major, r.Major) + require.Equal(t, expectV.Minor, r.Minor) + require.Equal(t, expectV.PreRelease, r.PreRelease) } -func (s *testPDControllerSuite) TestPDRequestRetry(c *C) { +func TestPDRequestRetry(t *testing.T) { ctx := context.Background() + + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/br/pkg/pdutil/FastRetry", "return(true)")) + defer func() { + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/br/pkg/pdutil/FastRetry")) + }() + count := 0 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { count++ - if count <= 5 { + if count <= pdRequestRetryTime-1 { w.WriteHeader(http.StatusGatewayTimeout) return } @@ -186,12 +188,12 @@ func (s *testPDControllerSuite) TestPDRequestRetry(c *C) { cli := http.DefaultClient taddr := ts.URL _, reqErr := pdRequest(ctx, taddr, "", cli, http.MethodGet, nil) - c.Assert(reqErr, IsNil) + require.NoError(t, reqErr) ts.Close() count = 0 ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { count++ - if count <= 11 { + if count <= pdRequestRetryTime+1 { w.WriteHeader(http.StatusGatewayTimeout) return } @@ -200,10 +202,10 @@ func (s *testPDControllerSuite) TestPDRequestRetry(c *C) { defer ts.Close() taddr = ts.URL _, reqErr = pdRequest(ctx, taddr, "", cli, http.MethodGet, nil) - c.Assert(reqErr, NotNil) + require.Error(t, reqErr) } -func (s *testPDControllerSuite) TestStoreInfo(c *C) { +func TestStoreInfo(t *testing.T) { storeInfo := api.StoreInfo{ Status: &api.StoreStatus{ Capacity: typeutil.ByteSize(1024), @@ -217,18 +219,18 @@ func (s *testPDControllerSuite) TestStoreInfo(c *C) { _ context.Context, addr string, prefix string, _ *http.Client, _ string, _ io.Reader, ) ([]byte, error) { query := fmt.Sprintf("%s/%s", addr, prefix) - c.Assert(query, Equals, "http://mock/pd/api/v1/store/1") + require.Equal(t, "http://mock/pd/api/v1/store/1", query) ret, err := json.Marshal(storeInfo) - c.Assert(err, IsNil) + require.NoError(t, err) return ret, nil } pdController := &PdController{addrs: []string{"http://mock"}} ctx := context.Background() resp, err := pdController.getStoreInfoWith(ctx, mock, 1) - c.Assert(err, IsNil) - c.Assert(resp, NotNil) - c.Assert(resp.Status, NotNil) - c.Assert(resp.Store.StateName, Equals, "Tombstone") - c.Assert(uint64(resp.Status.Available), Equals, uint64(1024)) + require.NoError(t, err) + require.NotNil(t, resp) + require.NotNil(t, resp.Status) + require.Equal(t, "Tombstone", resp.Store.StateName) + require.Equal(t, uint64(1024), uint64(resp.Status.Available)) }