Skip to content

Commit

Permalink
tests: testify some server tests (tikv#5186)
Browse files Browse the repository at this point in the history
ref tikv#4813

Testify some server tests.

Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato authored and CabinfeverB committed Jul 14, 2022
1 parent c0a6b11 commit c8ce97c
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 440 deletions.
111 changes: 52 additions & 59 deletions tests/server/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,37 @@ import (
"sync"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/testutil"
"github.com/tikv/pd/tests"
"go.uber.org/goleak"
)

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

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

const allocStep = uint64(1000)

var _ = Suite(&testAllocIDSuite{})

type testAllocIDSuite struct {
ctx context.Context
cancel context.CancelFunc
}

func (s *testAllocIDSuite) SetUpSuite(c *C) {
s.ctx, s.cancel = context.WithCancel(context.Background())
}

func (s *testAllocIDSuite) TearDownSuite(c *C) {
s.cancel()
}

func (s *testAllocIDSuite) TestID(c *C) {
cluster, err := tests.NewTestCluster(s.ctx, 1)
c.Assert(err, IsNil)
func TestID(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 1)
re.NoError(err)
defer cluster.Destroy()

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)
cluster.WaitLeader()

leaderServer := cluster.GetServer(cluster.GetLeader())
var last uint64
for i := uint64(0); i < allocStep; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last)
re.NoError(err)
re.Greater(id, last)
last = id
}

Expand All @@ -81,111 +65,120 @@ func (s *testAllocIDSuite) TestID(c *C) {

for i := 0; i < 200; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
re.NoError(err)
m.Lock()
_, ok := ids[id]
ids[id] = struct{}{}
m.Unlock()
c.Assert(ok, IsFalse)
re.False(ok)
}
}()
}

wg.Wait()
}

func (s *testAllocIDSuite) TestCommand(c *C) {
cluster, err := tests.NewTestCluster(s.ctx, 1)
c.Assert(err, IsNil)
func TestCommand(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 1)
re.NoError(err)
defer cluster.Destroy()

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)
cluster.WaitLeader()

leaderServer := cluster.GetServer(cluster.GetLeader())
req := &pdpb.AllocIDRequest{Header: testutil.NewRequestHeader(leaderServer.GetClusterID())}

grpcPDClient := testutil.MustNewGrpcClient(c, leaderServer.GetAddr())
grpcPDClient := testutil.MustNewGrpcClientWithTestify(re, leaderServer.GetAddr())
var last uint64
for i := uint64(0); i < 2*allocStep; i++ {
resp, err := grpcPDClient.AllocID(context.Background(), req)
c.Assert(err, IsNil)
c.Assert(resp.GetId(), Greater, last)
re.NoError(err)
re.Greater(resp.GetId(), last)
last = resp.GetId()
}
}

func (s *testAllocIDSuite) TestMonotonicID(c *C) {
cluster, err := tests.NewTestCluster(s.ctx, 2)
c.Assert(err, IsNil)
func TestMonotonicID(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 2)
re.NoError(err)
defer cluster.Destroy()

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)
cluster.WaitLeader()

leaderServer := cluster.GetServer(cluster.GetLeader())
var last1 uint64
for i := uint64(0); i < 10; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last1)
re.NoError(err)
re.Greater(id, last1)
last1 = id
}
err = cluster.ResignLeader()
c.Assert(err, IsNil)
re.NoError(err)
cluster.WaitLeader()
leaderServer = cluster.GetServer(cluster.GetLeader())
var last2 uint64
for i := uint64(0); i < 10; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last2)
re.NoError(err)
re.Greater(id, last2)
last2 = id
}
err = cluster.ResignLeader()
c.Assert(err, IsNil)
re.NoError(err)
cluster.WaitLeader()
leaderServer = cluster.GetServer(cluster.GetLeader())
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last2)
re.NoError(err)
re.Greater(id, last2)
var last3 uint64
for i := uint64(0); i < 1000; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last3)
re.NoError(err)
re.Greater(id, last3)
last3 = id
}
}

func (s *testAllocIDSuite) TestPDRestart(c *C) {
cluster, err := tests.NewTestCluster(s.ctx, 1)
c.Assert(err, IsNil)
func TestPDRestart(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 1)
re.NoError(err)
defer cluster.Destroy()

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)
cluster.WaitLeader()
leaderServer := cluster.GetServer(cluster.GetLeader())

var last uint64
for i := uint64(0); i < 10; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last)
re.NoError(err)
re.Greater(id, last)
last = id
}

c.Assert(leaderServer.Stop(), IsNil)
c.Assert(leaderServer.Run(), IsNil)
re.NoError(leaderServer.Stop())
re.NoError(leaderServer.Run())
cluster.WaitLeader()

for i := uint64(0); i < 10; i++ {
id, err := leaderServer.GetAllocator().Alloc()
c.Assert(err, IsNil)
c.Assert(id, Greater, last)
re.NoError(err)
re.Greater(id, last)
last = id
}
}
32 changes: 9 additions & 23 deletions tests/server/join/join_fail/join_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,29 @@ package join_fail_test

import (
"context"
"strings"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/tikv/pd/pkg/testutil"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/tests"
"go.uber.org/goleak"
)

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

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

var _ = Suite(&joinTestSuite{})

type joinTestSuite struct{}

func (s *joinTestSuite) TestFailedPDJoinInStep1(c *C) {
func TestFailedPDJoinInStep1(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 1)
defer cluster.Destroy()
c.Assert(err, IsNil)
re.NoError(err)

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)
cluster.WaitLeader()

// Join the second PD.
c.Assert(failpoint.Enable("github.com/tikv/pd/server/join/add-member-failed", `return`), IsNil)
re.NoError(failpoint.Enable("github.com/tikv/pd/server/join/add-member-failed", `return`))
_, err = cluster.Join(ctx)
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "join failed"), IsTrue)
c.Assert(failpoint.Disable("github.com/tikv/pd/server/join/add-member-failed"), IsNil)
re.Error(err)
re.Contains(err.Error(), "join failed")
re.NoError(failpoint.Disable("github.com/tikv/pd/server/join/add-member-failed"))
}
Loading

0 comments on commit c8ce97c

Please sign in to comment.