Skip to content

Commit

Permalink
kv: migrate test-infra to testify (pingcap#26101)
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun authored Jul 12, 2021
1 parent c8bcc3b commit b7ca7ca
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 247 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions kv/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
14 changes: 8 additions & 6 deletions kv/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}
64 changes: 36 additions & 28 deletions kv/fault_injection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,81 @@ 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)
cfg.SetCommitError(err1)

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))
}
Loading

0 comments on commit b7ca7ca

Please sign in to comment.