From b7ca7ca8c5106ce05050db1da346cd38df04b175 Mon Sep 17 00:00:00 2001 From: tison Date: Mon, 12 Jul 2021 12:45:31 +0800 Subject: [PATCH] kv: migrate test-infra to testify (#26101) --- go.mod | 1 + kv/checker_test.go | 24 +++--- kv/error_test.go | 14 ++-- kv/fault_injection_test.go | 64 ++++++++------- kv/key_test.go | 156 +++++++++++++++++++++---------------- kv/main_test.go | 26 +++++++ kv/mock_test.go | 87 +++++++++------------ kv/txn_test.go | 41 ++++------ kv/utils_test.go | 94 +++++++++++----------- kv/version_test.go | 19 +++-- 10 files changed, 279 insertions(+), 247 deletions(-) create mode 100644 kv/main_test.go diff --git a/go.mod b/go.mod index 20ac7530e7037..91cfec8f4de4c 100644 --- a/go.mod +++ b/go.mod @@ -69,6 +69,7 @@ require ( go.etcd.io/etcd v0.5.0-alpha.5.0.20200824191128-ae9734ed278b go.uber.org/atomic v1.8.0 go.uber.org/automaxprocs v1.4.0 + go.uber.org/goleak v1.1.10 go.uber.org/zap v1.18.1 golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c diff --git a/kv/checker_test.go b/kv/checker_test.go index 16050fdbd3174..91a6bc92a5ca3 100644 --- a/kv/checker_test.go +++ b/kv/checker_test.go @@ -14,21 +14,21 @@ package kv_test import ( - . "github.com/pingcap/check" + "testing" + "github.com/pingcap/tidb/kv" + "github.com/stretchr/testify/assert" ) -type checkerSuite struct{} - -var _ = Suite(&checkerSuite{}) +func TestIsRequestTypeSupported(t *testing.T) { + t.Parallel() -func (s checkerSuite) TestIsRequestTypeSupported(c *C) { checker := kv.RequestTypeSupportedChecker{}.IsRequestTypeSupported - c.Assert(checker(kv.ReqTypeSelect, kv.ReqSubTypeGroupBy), IsTrue) - c.Assert(checker(kv.ReqTypeDAG, kv.ReqSubTypeSignature), IsTrue) - c.Assert(checker(kv.ReqTypeDAG, kv.ReqSubTypeDesc), IsTrue) - c.Assert(checker(kv.ReqTypeDAG, kv.ReqSubTypeSignature), IsTrue) - c.Assert(checker(kv.ReqTypeDAG, kv.ReqSubTypeAnalyzeIdx), IsFalse) - c.Assert(checker(kv.ReqTypeAnalyze, 0), IsTrue) - c.Assert(checker(kv.ReqTypeChecksum, 0), IsFalse) + assert.True(t, checker(kv.ReqTypeSelect, kv.ReqSubTypeGroupBy)) + assert.True(t, checker(kv.ReqTypeDAG, kv.ReqSubTypeSignature)) + assert.True(t, checker(kv.ReqTypeDAG, kv.ReqSubTypeDesc)) + assert.True(t, checker(kv.ReqTypeDAG, kv.ReqSubTypeSignature)) + assert.False(t, checker(kv.ReqTypeDAG, kv.ReqSubTypeAnalyzeIdx)) + assert.True(t, checker(kv.ReqTypeAnalyze, 0)) + assert.False(t, checker(kv.ReqTypeChecksum, 0)) } diff --git a/kv/error_test.go b/kv/error_test.go index 349015c3adddb..d58d3115b98a3 100644 --- a/kv/error_test.go +++ b/kv/error_test.go @@ -14,16 +14,16 @@ package kv import ( - . "github.com/pingcap/check" + "testing" + "github.com/pingcap/parser/mysql" "github.com/pingcap/parser/terror" + "github.com/stretchr/testify/assert" ) -type testErrorSuite struct{} - -var _ = Suite(testErrorSuite{}) +func TestError(t *testing.T) { + t.Parallel() -func (s testErrorSuite) TestError(c *C) { kvErrs := []*terror.Error{ ErrNotExist, ErrTxnRetryable, @@ -35,8 +35,10 @@ func (s testErrorSuite) TestError(c *C) { ErrWriteConflict, ErrWriteConflictInTiDB, } + for _, err := range kvErrs { code := terror.ToSQLError(err).Code - c.Assert(code != mysql.ErrUnknown && code == uint16(err.Code()), IsTrue, Commentf("err: %v", err)) + assert.NotEqual(t, mysql.ErrUnknown, code) + assert.Equal(t, uint16(err.Code()), code) } } diff --git a/kv/fault_injection_test.go b/kv/fault_injection_test.go index 2b50a28ef21e2..294d485f2c935 100644 --- a/kv/fault_injection_test.go +++ b/kv/fault_injection_test.go @@ -15,18 +15,18 @@ package kv import ( "context" + "testing" - . "github.com/pingcap/check" "github.com/pingcap/errors" "github.com/pingcap/parser/terror" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/tikv/client-go/v2/tikv" ) -type testFaultInjectionSuite struct{} +func TestFaultInjectionBasic(t *testing.T) { + t.Parallel() -var _ = Suite(testFaultInjectionSuite{}) - -func (s testFaultInjectionSuite) TestFaultInjectionBasic(c *C) { var cfg InjectionConfig err1 := errors.New("foo") cfg.SetGetError(err1) @@ -34,54 +34,62 @@ func (s testFaultInjectionSuite) TestFaultInjectionBasic(c *C) { storage := NewInjectedStore(newMockStorage(), &cfg) txn, err := storage.Begin() - c.Assert(err, IsNil) + require.Nil(t, err) + _, err = storage.BeginWithOption(tikv.DefaultStartTSOption().SetTxnScope(GlobalTxnScope).SetStartTS(0)) - c.Assert(err, IsNil) + require.Nil(t, err) + ver := Version{Ver: 1} snap := storage.GetSnapshot(ver) b, err := txn.Get(context.TODO(), []byte{'a'}) - c.Assert(err.Error(), Equals, err1.Error()) - c.Assert(b, IsNil) + assert.NotNil(t, err) + assert.Equal(t, err1.Error(), err.Error()) + assert.Nil(t, b) + b, err = snap.Get(context.TODO(), []byte{'a'}) - c.Assert(err.Error(), Equals, err1.Error()) - c.Assert(b, IsNil) + assert.NotNil(t, err) + assert.Equal(t, err1.Error(), err.Error()) + assert.Nil(t, b) bs, err := snap.BatchGet(context.Background(), nil) - c.Assert(err.Error(), Equals, err1.Error()) - c.Assert(bs, IsNil) + assert.NotNil(t, err) + assert.Equal(t, err1.Error(), err.Error()) + assert.Nil(t, bs) bs, err = txn.BatchGet(context.Background(), nil) - c.Assert(err.Error(), Equals, err1.Error()) - c.Assert(bs, IsNil) + assert.NotNil(t, err) + assert.Equal(t, err1.Error(), err.Error()) + assert.Nil(t, bs) err = txn.Commit(context.Background()) - c.Assert(err.Error(), Equals, err1.Error()) + assert.NotNil(t, err) + assert.Equal(t, err1.Error(), err.Error()) cfg.SetGetError(nil) cfg.SetCommitError(nil) storage = NewInjectedStore(newMockStorage(), &cfg) txn, err = storage.Begin() - c.Assert(err, IsNil) - snap = storage.GetSnapshot(ver) + assert.Nil(t, err) + snap = storage.GetSnapshot(ver) b, err = txn.Get(context.TODO(), []byte{'a'}) - c.Assert(err, IsNil) - c.Assert(b, IsNil) + assert.Nil(t, err) + assert.Nil(t, b) bs, err = txn.BatchGet(context.Background(), nil) - c.Assert(err, IsNil) - c.Assert(bs, IsNil) + assert.Nil(t, err) + assert.Nil(t, bs) b, err = snap.Get(context.TODO(), []byte{'a'}) - c.Assert(terror.ErrorEqual(ErrNotExist, err), IsTrue) - c.Assert(b, IsNil) + assert.True(t, terror.ErrorEqual(ErrNotExist, err)) + assert.Nil(t, b) bs, err = snap.BatchGet(context.Background(), []Key{[]byte("a")}) - c.Assert(err, IsNil) - c.Assert(len(bs), Equals, 0) + assert.Nil(t, err) + assert.Len(t, bs, 0) err = txn.Commit(context.Background()) - c.Assert(err, NotNil) - c.Assert(terror.ErrorEqual(err, ErrTxnRetryable), IsTrue) + assert.NotNil(t, err) + assert.True(t, terror.ErrorEqual(err, ErrTxnRetryable)) } diff --git a/kv/key_test.go b/kv/key_test.go index ef26b689c448c..1b911527f4697 100644 --- a/kv/key_test.go +++ b/kv/key_test.go @@ -19,45 +19,43 @@ import ( "testing" "time" - . "github.com/pingcap/check" "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/codec" - "github.com/pingcap/tidb/util/testleak" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -var _ = Suite(&testKeySuite{}) +func TestPartialNext(t *testing.T) { + t.Parallel() -type testKeySuite struct { -} - -func (s *testKeySuite) TestPartialNext(c *C) { - defer testleak.AfterTest(c)() sc := &stmtctx.StatementContext{TimeZone: time.Local} // keyA represents a multi column index. keyA, err := codec.EncodeValue(sc, nil, types.NewDatum("abc"), types.NewDatum("def")) - c.Check(err, IsNil) + require.Nil(t, err) keyB, err := codec.EncodeValue(sc, nil, types.NewDatum("abca"), types.NewDatum("def")) - c.Check(err, IsNil) + require.Nil(t, err) // We only use first column value to seek. seekKey, err := codec.EncodeValue(sc, nil, types.NewDatum("abc")) - c.Check(err, IsNil) + require.Nil(t, err) nextKey := Key(seekKey).Next() cmp := bytes.Compare(nextKey, keyA) - c.Assert(cmp, Equals, -1) + assert.Equal(t, -1, cmp) // Use next partial key, we can skip all index keys with first column value equal to "abc". nextPartialKey := Key(seekKey).PrefixNext() cmp = bytes.Compare(nextPartialKey, keyA) - c.Assert(cmp, Equals, 1) + assert.Equal(t, 1, cmp) cmp = bytes.Compare(nextPartialKey, keyB) - c.Assert(cmp, Equals, -1) + assert.Equal(t, -1, cmp) } -func (s *testKeySuite) TestIsPoint(c *C) { +func TestIsPoint(t *testing.T) { + t.Parallel() + tests := []struct { start []byte end []byte @@ -99,110 +97,136 @@ func (s *testKeySuite) TestIsPoint(c *C) { isPoint: false, }, } + for _, tt := range tests { kr := KeyRange{ StartKey: tt.start, EndKey: tt.end, } - c.Check(kr.IsPoint(), Equals, tt.isPoint) + assert.Equal(t, tt.isPoint, kr.IsPoint()) } } -func (s *testKeySuite) TestBasicFunc(c *C) { - c.Assert(IsTxnRetryableError(nil), IsFalse) - c.Assert(IsTxnRetryableError(ErrTxnRetryable), IsTrue) - c.Assert(IsTxnRetryableError(errors.New("test")), IsFalse) +func TestBasicFunc(t *testing.T) { + t.Parallel() + + assert.False(t, IsTxnRetryableError(nil)) + assert.True(t, IsTxnRetryableError(ErrTxnRetryable)) + assert.False(t, IsTxnRetryableError(errors.New("test"))) } -func (s *testKeySuite) TestHandle(c *C) { +func TestHandle(t *testing.T) { + t.Parallel() + ih := IntHandle(100) - c.Assert(ih.IsInt(), IsTrue) + assert.True(t, ih.IsInt()) + _, iv, _ := codec.DecodeInt(ih.Encoded()) - c.Assert(iv, Equals, ih.IntValue()) + assert.Equal(t, ih.IntValue(), iv) + ih2 := ih.Next() - c.Assert(ih2.IntValue(), Equals, int64(101)) - c.Assert(ih.Equal(ih2), IsFalse) - c.Assert(ih.Compare(ih2), Equals, -1) - c.Assert(ih.String(), Equals, "100") - ch := mustNewCommonHandle(c, 100, "abc") - c.Assert(ch.IsInt(), IsFalse) + assert.Equal(t, int64(101), ih2.IntValue()) + assert.False(t, ih.Equal(ih2)) + assert.Equal(t, -1, ih.Compare(ih2)) + assert.Equal(t, "100", ih.String()) + + ch := mustNewCommonHandle(t, 100, "abc") + assert.False(t, ch.IsInt()) + ch2 := ch.Next() - c.Assert(ch.Equal(ch2), IsFalse) - c.Assert(ch.Compare(ch2), Equals, -1) - c.Assert(ch2.Encoded(), HasLen, len(ch.Encoded())) - c.Assert(ch.NumCols(), Equals, 2) + assert.False(t, ch.Equal(ch2)) + assert.Equal(t, -1, ch.Compare(ch2)) + assert.Len(t, ch2.Encoded(), len(ch.Encoded())) + assert.Equal(t, 2, ch.NumCols()) + _, d, err := codec.DecodeOne(ch.EncodedCol(0)) - c.Assert(err, IsNil) - c.Assert(d.GetInt64(), Equals, int64(100)) + assert.Nil(t, err) + assert.Equal(t, int64(100), d.GetInt64()) + _, d, err = codec.DecodeOne(ch.EncodedCol(1)) - c.Assert(err, IsNil) - c.Assert(d.GetString(), Equals, "abc") - c.Assert(ch.String(), Equals, "{100, abc}") + assert.Nil(t, err) + assert.Equal(t, "abc", d.GetString()) + assert.Equal(t, "{100, abc}", ch.String()) } -func (s *testKeySuite) TestPaddingHandle(c *C) { +func TestPaddingHandle(t *testing.T) { + t.Parallel() + dec := types.NewDecFromInt(1) encoded, err := codec.EncodeKey(new(stmtctx.StatementContext), nil, types.NewDecimalDatum(dec)) - c.Assert(err, IsNil) - c.Assert(len(encoded), Less, 9) + assert.Nil(t, err) + assert.Less(t, len(encoded), 9) + handle, err := NewCommonHandle(encoded) - c.Assert(err, IsNil) - c.Assert(handle.Encoded(), HasLen, 9) - c.Assert(handle.EncodedCol(0), BytesEquals, encoded) + assert.Nil(t, err) + assert.Len(t, handle.Encoded(), 9) + assert.Equal(t, encoded, handle.EncodedCol(0)) + newHandle, err := NewCommonHandle(handle.Encoded()) - c.Assert(err, IsNil) - c.Assert(newHandle.EncodedCol(0), BytesEquals, handle.EncodedCol(0)) + assert.Nil(t, err) + assert.Equal(t, handle.EncodedCol(0), newHandle.EncodedCol(0)) } -func (s *testKeySuite) TestHandleMap(c *C) { +func TestHandleMap(t *testing.T) { + t.Parallel() + m := NewHandleMap() h := IntHandle(1) + m.Set(h, 1) v, ok := m.Get(h) - c.Assert(ok, IsTrue) - c.Assert(v, Equals, 1) + assert.True(t, ok) + assert.Equal(t, 1, v) + m.Delete(h) v, ok = m.Get(h) - c.Assert(ok, IsFalse) - c.Assert(v, IsNil) - ch := mustNewCommonHandle(c, 100, "abc") + assert.False(t, ok) + assert.Nil(t, v) + + ch := mustNewCommonHandle(t, 100, "abc") m.Set(ch, "a") v, ok = m.Get(ch) - c.Assert(ok, IsTrue) - c.Assert(v, Equals, "a") + assert.True(t, ok) + assert.Equal(t, "a", v) + m.Delete(ch) v, ok = m.Get(ch) - c.Assert(ok, IsFalse) - c.Assert(v, IsNil) + assert.False(t, ok) + assert.Nil(t, v) + m.Set(ch, "a") - ch2 := mustNewCommonHandle(c, 101, "abc") + ch2 := mustNewCommonHandle(t, 101, "abc") m.Set(ch2, "b") - ch3 := mustNewCommonHandle(c, 99, "def") + ch3 := mustNewCommonHandle(t, 99, "def") m.Set(ch3, "c") - c.Assert(m.Len(), Equals, 3) + assert.Equal(t, 3, m.Len()) + cnt := 0 m.Range(func(h Handle, val interface{}) bool { cnt++ if h.Equal(ch) { - c.Assert(val, Equals, "a") + assert.Equal(t, "a", val) } else if h.Equal(ch2) { - c.Assert(val, Equals, "b") + assert.Equal(t, "b", val) } else { - c.Assert(val, Equals, "c") + assert.Equal(t, "c", val) } if cnt == 2 { return false } return true }) - c.Assert(cnt, Equals, 2) + + assert.Equal(t, 2, cnt) } -func mustNewCommonHandle(c *C, values ...interface{}) *CommonHandle { +func mustNewCommonHandle(t *testing.T, values ...interface{}) *CommonHandle { encoded, err := codec.EncodeKey(new(stmtctx.StatementContext), nil, types.MakeDatums(values...)...) - c.Assert(err, IsNil) + require.Nil(t, err) + ch, err := NewCommonHandle(encoded) - c.Assert(err, IsNil) + require.Nil(t, err) + return ch } diff --git a/kv/main_test.go b/kv/main_test.go new file mode 100644 index 0000000000000..30f4d5ea377c2 --- /dev/null +++ b/kv/main_test.go @@ -0,0 +1,26 @@ +// 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, +// See the License for the specific language governing permissions and +// limitations under the License. + +package kv + +import ( + "testing" + + "github.com/pingcap/tidb/util/testbridge" + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + testbridge.WorkaroundGoCheckFlags() + goleak.VerifyTestMain(m) +} diff --git a/kv/mock_test.go b/kv/mock_test.go index 5efaa146920df..da51782511849 100644 --- a/kv/mock_test.go +++ b/kv/mock_test.go @@ -15,30 +15,32 @@ package kv import ( "context" + "testing" - . "github.com/pingcap/check" + "github.com/stretchr/testify/assert" ) -var _ = Suite(testMockSuite{}) +func TestInterface(t *testing.T) { + t.Parallel() -type testMockSuite struct { -} - -func (s testMockSuite) TestInterface(c *C) { storage := newMockStorage() storage.GetClient() storage.UUID() version, err := storage.CurrentVersion(GlobalTxnScope) - c.Check(err, IsNil) + assert.Nil(t, err) + snapshot := storage.GetSnapshot(version) _, err = snapshot.BatchGet(context.Background(), []Key{Key("abc"), Key("def")}) - c.Check(err, IsNil) - snapshot.SetOption(Priority, PriorityNormal) + assert.Nil(t, err) + snapshot.SetOption(Priority, PriorityNormal) transaction, err := storage.Begin() - c.Check(err, IsNil) + assert.Nil(t, err) + assert.NotNil(t, transaction) + err = transaction.LockKeys(context.Background(), new(LockCtx), Key("lock")) - c.Check(err, IsNil) + assert.Nil(t, err) + transaction.SetOption(23, struct{}{}) if mock, ok := transaction.(*mockTxn); ok { mock.GetOption(23) @@ -46,59 +48,42 @@ func (s testMockSuite) TestInterface(c *C) { transaction.StartTS() if transaction.IsReadOnly() { _, err = transaction.Get(context.TODO(), Key("lock")) - c.Check(err, IsNil) + assert.Nil(t, err) err = transaction.Set(Key("lock"), []byte{}) - c.Check(err, IsNil) + assert.Nil(t, err) _, err = transaction.Iter(Key("lock"), nil) - c.Check(err, IsNil) + assert.Nil(t, err) _, err = transaction.IterReverse(Key("lock")) - c.Check(err, IsNil) + assert.Nil(t, err) } - transaction.Commit(context.Background()) + _ = transaction.Commit(context.Background()) transaction, err = storage.Begin() - c.Check(err, IsNil) + assert.Nil(t, err) // Test for mockTxn interface. - c.Assert(transaction.String(), Equals, "") - c.Assert(transaction.Valid(), Equals, true) - c.Assert(transaction.Len(), Equals, 0) - c.Assert(transaction.Size(), Equals, 0) - c.Assert(transaction.GetMemBuffer(), IsNil) + assert.Equal(t, "", transaction.String()) + assert.True(t, transaction.Valid()) + assert.Equal(t, 0, transaction.Len()) + assert.Equal(t, 0, transaction.Size()) + assert.Nil(t, transaction.GetMemBuffer()) + transaction.Reset() err = transaction.Rollback() - c.Check(err, IsNil) - c.Assert(transaction.Valid(), Equals, false) - c.Assert(transaction.IsPessimistic(), Equals, false) - c.Assert(transaction.Delete(nil), IsNil) + assert.Nil(t, err) + assert.False(t, transaction.Valid()) + assert.False(t, transaction.IsPessimistic()) + assert.Nil(t, transaction.Delete(nil)) - // Test for mockStorage interface. - c.Assert(storage.GetOracle(), IsNil) - c.Assert(storage.Name(), Equals, "KVMockStorage") - c.Assert(storage.Describe(), Equals, "KVMockStorage is a mock Store implementation, only for unittests in KV package") - c.Assert(storage.SupportDeleteRange(), IsFalse) + assert.Nil(t, storage.GetOracle()) + assert.Equal(t, "KVMockStorage", storage.Name()) + assert.Equal(t, "KVMockStorage is a mock Store implementation, only for unittests in KV package", storage.Describe()) + assert.False(t, storage.SupportDeleteRange()) status, err := storage.ShowStatus(context.Background(), "") - c.Assert(status, IsNil) - c.Assert(err, IsNil) + assert.Nil(t, status) + assert.Nil(t, err) err = storage.Close() - c.Check(err, IsNil) -} - -func (s testMockSuite) TestIsPoint(c *C) { - kr := KeyRange{ - StartKey: Key("rowkey1"), - EndKey: Key("rowkey2"), - } - c.Check(kr.IsPoint(), IsTrue) - - kr.EndKey = Key("rowkey3") - c.Check(kr.IsPoint(), IsFalse) - - kr = KeyRange{ - StartKey: Key(""), - EndKey: []byte{0}, - } - c.Check(kr.IsPoint(), IsTrue) + assert.Nil(t, err) } diff --git a/kv/txn_test.go b/kv/txn_test.go index 475a956ffdb36..4a742951346b2 100644 --- a/kv/txn_test.go +++ b/kv/txn_test.go @@ -16,37 +16,26 @@ package kv import ( "context" "errors" + "testing" "time" - . "github.com/pingcap/check" - "github.com/pingcap/tidb/util/testleak" + "github.com/stretchr/testify/assert" ) -var _ = Suite(&testTxnSuite{}) - -type testTxnSuite struct { -} - -func (s *testTxnSuite) SetUpTest(c *C) { +func TestBackOff(t *testing.T) { + mustBackOff(t, 1, 2) + mustBackOff(t, 2, 4) + mustBackOff(t, 3, 8) + mustBackOff(t, 100000, 100) } -func (s *testTxnSuite) TearDownTest(c *C) { +func mustBackOff(t *testing.T, cnt uint, sleep int) { + assert.LessOrEqual(t, BackOff(cnt), sleep*int(time.Millisecond)) } -func (s *testTxnSuite) TestBackOff(c *C) { - defer testleak.AfterTest(c)() - mustBackOff(c, 1, 2) - mustBackOff(c, 2, 4) - mustBackOff(c, 3, 8) - mustBackOff(c, 100000, 100) -} - -func mustBackOff(c *C, cnt uint, sleep int) { - c.Assert(BackOff(cnt), LessEqual, sleep*int(time.Millisecond)) -} +func TestRetryExceedCountError(t *testing.T) { + t.Parallel() -func (s *testTxnSuite) TestRetryExceedCountError(c *C) { - defer testleak.AfterTest(c)() defer func(cnt uint) { maxRetryCnt = cnt }(maxRetryCnt) @@ -55,17 +44,17 @@ func (s *testTxnSuite) TestRetryExceedCountError(c *C) { err := RunInNewTxn(context.Background(), &mockStorage{}, true, func(ctx context.Context, txn Transaction) error { return nil }) - c.Assert(err, NotNil) + assert.NotNil(t, err) err = RunInNewTxn(context.Background(), &mockStorage{}, true, func(ctx context.Context, txn Transaction) error { return ErrTxnRetryable }) - c.Assert(err, NotNil) + assert.NotNil(t, err) err = RunInNewTxn(context.Background(), &mockStorage{}, true, func(ctx context.Context, txn Transaction) error { return errors.New("do not retry") }) - c.Assert(err, NotNil) + assert.NotNil(t, err) var cfg InjectionConfig err1 := errors.New("foo") @@ -75,5 +64,5 @@ func (s *testTxnSuite) TestRetryExceedCountError(c *C) { err = RunInNewTxn(context.Background(), storage, true, func(ctx context.Context, txn Transaction) error { return nil }) - c.Assert(err, NotNil) + assert.NotNil(t, err) } diff --git a/kv/utils_test.go b/kv/utils_test.go index d5c184a42074f..c2cb2ac62a4e0 100644 --- a/kv/utils_test.go +++ b/kv/utils_test.go @@ -18,17 +18,52 @@ import ( "strconv" "testing" - . "github.com/pingcap/check" + "github.com/stretchr/testify/assert" ) -func TestT(t *testing.T) { - CustomVerboseFlag = true - TestingT(t) +func TestIncInt64(t *testing.T) { + t.Parallel() + + mb := newMockMap() + key := Key("key") + v, err := IncInt64(mb, key, 1) + assert.Nil(t, err) + assert.Equal(t, int64(1), v) + + v, err = IncInt64(mb, key, 10) + assert.Nil(t, err) + assert.Equal(t, int64(11), v) + + err = mb.Set(key, []byte("not int")) + assert.Nil(t, err) + + _, err = IncInt64(mb, key, 1) + assert.NotNil(t, err) + + // test int overflow + maxUint32 := int64(^uint32(0)) + err = mb.Set(key, []byte(strconv.FormatInt(maxUint32, 10))) + assert.Nil(t, err) + + v, err = IncInt64(mb, key, 1) + assert.Nil(t, err) + assert.Equal(t, maxUint32+1, v) } -var _ = Suite(testUtilsSuite{}) +func TestGetInt64(t *testing.T) { + t.Parallel() -type testUtilsSuite struct { + mb := newMockMap() + key := Key("key") + v, err := GetInt64(context.TODO(), mb, key) + assert.Equal(t, int64(0), v) + assert.Nil(t, err) + + _, err = IncInt64(mb, key, 15) + assert.Nil(t, err) + v, err = GetInt64(context.TODO(), mb, key) + assert.Equal(t, int64(15), v) + assert.Nil(t, err) } type mockMap struct { @@ -36,6 +71,8 @@ type mockMap struct { value [][]byte } +var _ RetrieverMutator = &mockMap{} + func newMockMap() *mockMap { return &mockMap{ index: make([]Key, 0), @@ -43,14 +80,14 @@ func newMockMap() *mockMap { } } -func (s *mockMap) Iter(k Key, upperBound Key) (Iterator, error) { +func (s *mockMap) Iter(Key, Key) (Iterator, error) { return nil, nil } -func (s *mockMap) IterReverse(k Key) (Iterator, error) { +func (s *mockMap) IterReverse(Key) (Iterator, error) { return nil, nil } -func (s *mockMap) Get(ctx context.Context, k Key) ([]byte, error) { +func (s *mockMap) Get(_ context.Context, k Key) ([]byte, error) { for i, key := range s.index { if key.Cmp(k) == 0 { return s.value[i], nil @@ -81,42 +118,3 @@ func (s *mockMap) Delete(k Key) error { } return nil } - -func (s testUtilsSuite) TestIncInt64(c *C) { - mb := newMockMap() - key := Key("key") - v, err := IncInt64(mb, key, 1) - c.Check(err, IsNil) - c.Check(v, Equals, int64(1)) - v, err = IncInt64(mb, key, 10) - c.Check(err, IsNil) - c.Check(v, Equals, int64(11)) - - err = mb.Set(key, []byte("not int")) - c.Check(err, IsNil) - _, err = IncInt64(mb, key, 1) - c.Check(err, NotNil) - - // test int overflow - maxUint32 := int64(^uint32(0)) - err = mb.Set(key, []byte(strconv.FormatInt(maxUint32, 10))) - c.Check(err, IsNil) - v, err = IncInt64(mb, key, 1) - c.Check(err, IsNil) - c.Check(v, Equals, maxUint32+1) - -} - -func (s testUtilsSuite) TestGetInt64(c *C) { - mb := newMockMap() - key := Key("key") - v, err := GetInt64(context.TODO(), mb, key) - c.Check(v, Equals, int64(0)) - c.Check(err, IsNil) - - _, err = IncInt64(mb, key, 15) - c.Check(err, IsNil) - v, err = GetInt64(context.TODO(), mb, key) - c.Check(v, Equals, int64(15)) - c.Check(err, IsNil) -} diff --git a/kv/version_test.go b/kv/version_test.go index 56219f89c0808..cd4aeb7c50205 100644 --- a/kv/version_test.go +++ b/kv/version_test.go @@ -14,21 +14,20 @@ package kv import ( - . "github.com/pingcap/check" -) + "testing" -var _ = Suite(testVersionSuite{}) + "github.com/stretchr/testify/assert" +) -type testVersionSuite struct{} +func TestVersion(t *testing.T) { + t.Parallel() -func (s testVersionSuite) TestVersion(c *C) { le := NewVersion(42).Cmp(NewVersion(43)) gt := NewVersion(42).Cmp(NewVersion(41)) eq := NewVersion(42).Cmp(NewVersion(42)) - c.Assert(le < 0, IsTrue) - c.Assert(gt > 0, IsTrue) - c.Assert(eq == 0, IsTrue) - - c.Check(MinVersion.Cmp(MaxVersion) < 0, IsTrue) + assert.True(t, le < 0) + assert.True(t, gt > 0) + assert.True(t, eq == 0) + assert.True(t, MinVersion.Cmp(MaxVersion) < 0) }