Skip to content

Commit

Permalink
tidb-binlog/pump_client: migrate test-infra to testify (#33475)
Browse files Browse the repository at this point in the history
close #33463
  • Loading branch information
zanpocc committed Mar 27, 2022
1 parent e5d3e22 commit e09bc29
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
11 changes: 3 additions & 8 deletions tidb-binlog/pump_client/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

pb "github.com/pingcap/tipb/go-binlog"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -57,21 +58,15 @@ func writeFakeBinlog(b *testing.B, pumpClient *PumpsClient, threadCount int, num
StartTs: int64(baseTS + j),
}
err := pumpClient.WriteBinlog(pBinlog)
if err != nil {
b.Error(err)
return
}
require.NoError(b, err)

cBinlog := &pb.Binlog{
Tp: pb.BinlogType_Commit,
StartTs: int64(baseTS + j),
CommitTs: int64(baseTS + j),
}
err = pumpClient.WriteBinlog(cBinlog)
if err != nil {
b.Error(err)
return
}
require.NoError(b, err)
}
wg.Done()
}(10000000 * i)
Expand Down
48 changes: 20 additions & 28 deletions tidb-binlog/pump_client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/tidb-binlog/node"
binlog "github.com/pingcap/tipb/go-binlog"
pb "github.com/pingcap/tipb/go-binlog"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)

Expand All @@ -35,29 +35,21 @@ var (
testRetryTime = 5
)

func TestClient(t *testing.T) {
TestingT(t)
}

type testCase struct {
binlogs []*pb.Binlog
choosePumps []*PumpStatus
setAvliable []bool
setNodeID []string
}

var _ = Suite(&testClientSuite{})

type testClientSuite struct{}

func (t *testClientSuite) TestSelector(c *C) {
func TestSelector(t *testing.T) {
strategys := []string{Hash, Range}
for _, strategy := range strategys {
t.testSelector(c, strategy)
testSelector(t, strategy)
}
}

func (*testClientSuite) testSelector(c *C, strategy string) {
func testSelector(t *testing.T, strategy string) {
pumpsClient := &PumpsClient{
Pumps: NewPumpInfos(),
Selector: NewSelector(strategy),
Expand Down Expand Up @@ -115,7 +107,7 @@ func (*testClientSuite) testSelector(c *C, strategy string) {
}
pump := pumpsClient.Selector.Select(tCase.binlogs[i], 0)
pumpsClient.Selector.Feedback(tCase.binlogs[i].StartTs, tCase.binlogs[i].Tp, pump)
c.Assert(pump, Equals, tCase.choosePumps[i])
require.Equal(t, pump, tCase.choosePumps[i])
}

for j := 0; j < 10; j++ {
Expand All @@ -138,29 +130,29 @@ func (*testClientSuite) testSelector(c *C, strategy string) {
pump2 := pumpsClient.Selector.Select(commitBinlog, 0)
pumpsClient.Selector.Feedback(commitBinlog.StartTs, commitBinlog.Tp, pump2)
// prewrite binlog and commit binlog with same start ts should choose same pump
c.Assert(pump1.NodeID, Equals, pump2.NodeID)
require.Equal(t, pump1.NodeID, pump2.NodeID)
pumpsClient.setPumpAvailable(pump1, true)

// after change strategy, prewrite binlog and commit binlog will choose same pump
pump1 = pumpsClient.Selector.Select(prewriteBinlog, 0)
pumpsClient.Selector.Feedback(prewriteBinlog.StartTs, prewriteBinlog.Tp, pump1)
if strategy == Range {
err := pumpsClient.SetSelectStrategy(Hash)
c.Assert(err, IsNil)
require.NoError(t, err)
} else {
err := pumpsClient.SetSelectStrategy(Range)
c.Assert(err, IsNil)
require.NoError(t, err)
}
pump2 = pumpsClient.Selector.Select(commitBinlog, 0)
c.Assert(pump1.NodeID, Equals, pump2.NodeID)
require.Equal(t, pump1.NodeID, pump2.NodeID)

// set back
err := pumpsClient.SetSelectStrategy(strategy)
c.Assert(err, IsNil)
require.NoError(t, err)
}
}

func (t *testClientSuite) TestWriteBinlog(c *C) {
func TestWriteBinlog(t *testing.T) {
pumpServerConfig := []struct {
addr string
serverMode string
Expand All @@ -180,14 +172,14 @@ func (t *testClientSuite) TestWriteBinlog(c *C) {

for _, cfg := range pumpServerConfig {
pumpServer, err := createMockPumpServer(cfg.addr, cfg.serverMode, true)
c.Assert(err, IsNil)
require.NoError(t, err)

opt := grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout(cfg.serverMode, addr, timeout)
})
clientCon, err := grpc.Dial(cfg.addr, opt, grpc.WithInsecure())
c.Assert(err, IsNil)
c.Assert(clientCon, NotNil)
require.NoError(t, err)
require.NotNil(t, clientCon)
pumpClient := mockPumpsClient(pb.NewPumpClient(clientCon), true)

// test binlog size bigger than grpc's MaxRecvMsgSize
Expand All @@ -196,7 +188,7 @@ func (t *testClientSuite) TestWriteBinlog(c *C) {
PrewriteValue: make([]byte, testMaxRecvMsgSize+1),
}
err = pumpClient.WriteBinlog(blog)
c.Assert(err, NotNil)
require.Error(t, err)

for i := 0; i < 10; i++ {
// test binlog size small than grpc's MaxRecvMsgSize
Expand All @@ -205,11 +197,11 @@ func (t *testClientSuite) TestWriteBinlog(c *C) {
PrewriteValue: make([]byte, 1),
}
err = pumpClient.WriteBinlog(blog)
c.Assert(err, IsNil)
require.NoError(t, err)
}

// after write some binlog, the pump without grpc client will move to unavailable list in pump client.
c.Assert(len(pumpClient.Pumps.UnAvaliablePumps), Equals, 1)
require.Len(t, pumpClient.Pumps.UnAvaliablePumps, 1)

// test write commit binlog, will not return error although write binlog failed.
preWriteBinlog := &pb.Binlog{
Expand All @@ -225,17 +217,17 @@ func (t *testClientSuite) TestWriteBinlog(c *C) {
}

err = pumpClient.WriteBinlog(preWriteBinlog)
c.Assert(err, IsNil)
require.NoError(t, err)

// test when pump is down
pumpServer.Close()

// write commit binlog failed will not return error
err = pumpClient.WriteBinlog(commitBinlog)
c.Assert(err, IsNil)
require.NoError(t, err)

err = pumpClient.WriteBinlog(blog)
c.Assert(err, NotNil)
require.Error(t, err)
}
}

Expand Down

0 comments on commit e09bc29

Please sign in to comment.