Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tidb-binlog/pump_client: migrate test-infra to testify #33475

Merged
merged 1 commit into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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