Skip to content

Commit

Permalink
using race
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jun 27, 2024
1 parent 9a9d39d commit 1bfec95
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 113 deletions.
2 changes: 1 addition & 1 deletion pkg/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ func BenchmarkRandomSetRegion(b *testing.B) {

func TestGetRegionSizeByRange(t *testing.T) {
regions := NewRegionsInfo()
nums := 1000010
nums := 100001
for i := 0; i < nums; i++ {
peer := &metapb.Peer{StoreId: 1, Id: uint64(i + 1)}
endKey := []byte(fmt.Sprintf("%20d", i+1))
Expand Down
18 changes: 11 additions & 7 deletions scripts/ci-subtask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@ integrations_dir=$(pwd)/tests/integrations

case $1 in
1)
# unit tests ignore `tests`
./bin/pd-ut run --race --ignore tests --coverprofile $ROOT_PATH_COV || exit 1
# unit tests ignore `tests` and `server`
./bin/pd-ut run --ignore tests,server --race --coverprofile $ROOT_PATH_COV || exit 1
;;
2)
# unit tests only in `server`
./bin/pd-ut run server --race --coverprofile $ROOT_PATH_COV || exit 1
;;
3)
# unit tests only in `tests`
./bin/pd-ut run tests --race --coverprofile $ROOT_PATH_COV || exit 1
;;
3)
4)
# tools tests
cd ./tools && make ci-test-job && cat covprofile >> $ROOT_PATH_COV || exit 1
;;
4)
5)
# integration test client
./bin/pd-ut it run client --race --coverprofile $ROOT_PATH_COV || exit 1
# client tests
cd ./client && make ci-test-job && cat covprofile >> $ROOT_PATH_COV || exit 1
;;
5)
6)
# integration test tso
./bin/pd-ut it run tso --race --coverprofile $ROOT_PATH_COV || exit 1
./bin/pd-ut it run tso --race --ignore mcs/tso --coverprofile $ROOT_PATH_COV || exit 1
;;
6)
7)
# integration test mcs
./bin/pd-ut it run mcs --race --coverprofile $ROOT_PATH_COV || exit 1
;;
Expand Down
2 changes: 1 addition & 1 deletion server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestRegionsWithKillRequest(t *testing.T) {
url := fmt.Sprintf("%s%s/api/v1/regions", addr, apiPrefix)
mustBootstrapCluster(re, svr)

regionCount := 100000
regionCount := 10000
tu.GenerateTestDataConcurrently(regionCount, func(i int) {
r := core.NewTestRegionInfo(uint64(i+2), 1,
[]byte(fmt.Sprintf("%09d", i)),
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestLeaderTransferAndMoveCluster(t *testing.T) {
}()

// Transfer leader.
for i := 0; i < 5; i++ {
for i := 0; i < 3; i++ {
oldLeaderName := cluster.WaitLeader()
err := cluster.GetServer(oldLeaderName).ResignLeader()
re.NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/tso/keyspace_group_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (suite *tsoKeyspaceGroupManagerTestSuite) allocID() uint32 {
return uint32(id)
}

func TestTSOKeyspaceGroupManager(t *testing.T) {
func TestTSOKeyspaceGroupManagerSuite(t *testing.T) {
suite.Run(t, &tsoKeyspaceGroupManagerTestSuite{})
}

Expand Down
141 changes: 75 additions & 66 deletions tests/integrations/mcs/tso/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ package tso

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -260,8 +257,8 @@ func TestWaitAPIServiceReady(t *testing.T) {
}
}

type APIServerForwardTestSuite struct {
suite.Suite
type APIServerForward struct {
re *require.Assertions
ctx context.Context
cancel context.CancelFunc
cluster *tests.TestCluster
Expand All @@ -270,13 +267,11 @@ type APIServerForwardTestSuite struct {
pdClient pd.Client
}

func TestAPIServerForwardTestSuite(t *testing.T) {
suite.Run(t, new(APIServerForwardTestSuite))
}

func (suite *APIServerForwardTestSuite) SetupTest() {
func NewAPIServerForward(re *require.Assertions) APIServerForward {
suite := APIServerForward{
re: re,
}
var err error
re := suite.Require()
suite.ctx, suite.cancel = context.WithCancel(context.Background())
suite.cluster, err = tests.NewTestAPICluster(suite.ctx, 3)
re.NoError(err)
Expand All @@ -294,11 +289,12 @@ func (suite *APIServerForwardTestSuite) SetupTest() {
suite.pdClient, err = pd.NewClientWithContext(context.Background(),
[]string{suite.backendEndpoints}, pd.SecurityOption{}, pd.WithMaxErrorRetry(1))
re.NoError(err)
return suite
}

func (suite *APIServerForwardTestSuite) TearDownTest() {
re := suite.Require()
func (suite *APIServerForward) ShutDown() {
suite.pdClient.Close()
re := suite.re

etcdClient := suite.pdLeader.GetEtcdClient()
clusterID := strconv.FormatUint(suite.pdLeader.GetClusterID(), 10)
Expand All @@ -314,8 +310,10 @@ func (suite *APIServerForwardTestSuite) TearDownTest() {
re.NoError(failpoint.Disable("github.com/tikv/pd/client/usePDServiceMode"))
}

func (suite *APIServerForwardTestSuite) TestForwardTSORelated() {
re := suite.Require()
func TestForwardTSORelated(t *testing.T) {
re := require.New(t)
suite := NewAPIServerForward(re)
defer suite.ShutDown()
// Unable to use the tso-related interface without tso server
suite.checkUnavailableTSO(re)
tc, err := tests.NewTestTSOCluster(suite.ctx, 1, suite.backendEndpoints)
Expand All @@ -325,8 +323,10 @@ func (suite *APIServerForwardTestSuite) TestForwardTSORelated() {
suite.checkAvailableTSO(re)
}

func (suite *APIServerForwardTestSuite) TestForwardTSOWhenPrimaryChanged() {
re := suite.Require()
func TestForwardTSOWhenPrimaryChanged(t *testing.T) {
re := require.New(t)
suite := NewAPIServerForward(re)
defer suite.ShutDown()

tc, err := tests.NewTestTSOCluster(suite.ctx, 2, suite.backendEndpoints)
re.NoError(err)
Expand Down Expand Up @@ -363,10 +363,11 @@ func (suite *APIServerForwardTestSuite) TestForwardTSOWhenPrimaryChanged() {
suite.checkAvailableTSO(re)
}

func (suite *APIServerForwardTestSuite) TestResignTSOPrimaryForward() {
func TestResignTSOPrimaryForward(t *testing.T) {
re := require.New(t)
suite := NewAPIServerForward(re)
defer suite.ShutDown()
// TODO: test random kill primary with 3 nodes
re := suite.Require()

tc, err := tests.NewTestTSOCluster(suite.ctx, 2, suite.backendEndpoints)
re.NoError(err)
defer tc.Destroy()
Expand All @@ -388,8 +389,10 @@ func (suite *APIServerForwardTestSuite) TestResignTSOPrimaryForward() {
}
}

func (suite *APIServerForwardTestSuite) TestResignAPIPrimaryForward() {
re := suite.Require()
func TestResignAPIPrimaryForward(t *testing.T) {
re := require.New(t)
suite := NewAPIServerForward(re)
defer suite.ShutDown()

tc, err := tests.NewTestTSOCluster(suite.ctx, 2, suite.backendEndpoints)
re.NoError(err)
Expand All @@ -410,8 +413,10 @@ func (suite *APIServerForwardTestSuite) TestResignAPIPrimaryForward() {
}
}

func (suite *APIServerForwardTestSuite) TestForwardTSOUnexpectedToFollower1() {
re := suite.Require()
func TestForwardTSOUnexpectedToFollower1(t *testing.T) {
re := require.New(t)
suite := NewAPIServerForward(re)
defer suite.ShutDown()
suite.checkForwardTSOUnexpectedToFollower(func() {
// unary call will retry internally
// try to update gc safe point
Expand All @@ -421,8 +426,10 @@ func (suite *APIServerForwardTestSuite) TestForwardTSOUnexpectedToFollower1() {
})
}

func (suite *APIServerForwardTestSuite) TestForwardTSOUnexpectedToFollower2() {
re := suite.Require()
func TestForwardTSOUnexpectedToFollower2(t *testing.T) {
re := require.New(t)
suite := NewAPIServerForward(re)
defer suite.ShutDown()
suite.checkForwardTSOUnexpectedToFollower(func() {
// unary call will retry internally
// try to set external ts
Expand All @@ -433,16 +440,18 @@ func (suite *APIServerForwardTestSuite) TestForwardTSOUnexpectedToFollower2() {
})
}

func (suite *APIServerForwardTestSuite) TestForwardTSOUnexpectedToFollower3() {
re := suite.Require()
func TestForwardTSOUnexpectedToFollower3(t *testing.T) {
re := require.New(t)
suite := NewAPIServerForward(re)
defer suite.ShutDown()
suite.checkForwardTSOUnexpectedToFollower(func() {
_, _, err := suite.pdClient.GetTS(suite.ctx)
re.Error(err)
})
}

func (suite *APIServerForwardTestSuite) checkForwardTSOUnexpectedToFollower(checkTSO func()) {
re := suite.Require()
func (suite *APIServerForward) checkForwardTSOUnexpectedToFollower(checkTSO func()) {
re := suite.re
tc, err := tests.NewTestTSOCluster(suite.ctx, 2, suite.backendEndpoints)
re.NoError(err)
tc.WaitForDefaultPrimaryServing(re)
Expand Down Expand Up @@ -477,7 +486,7 @@ func (suite *APIServerForwardTestSuite) checkForwardTSOUnexpectedToFollower(chec
tc.Destroy()
}

func (suite *APIServerForwardTestSuite) addRegions() {
func (suite *APIServerForward) addRegions() {
leader := suite.cluster.GetServer(suite.cluster.WaitLeader())
rc := leader.GetServer().GetRaftCluster()
for i := 0; i < 3; i++ {
Expand All @@ -491,7 +500,7 @@ func (suite *APIServerForwardTestSuite) addRegions() {
}
}

func (suite *APIServerForwardTestSuite) checkUnavailableTSO(re *require.Assertions) {
func (suite *APIServerForward) checkUnavailableTSO(re *require.Assertions) {
_, _, err := suite.pdClient.GetTS(suite.ctx)
re.Error(err)
// try to update gc safe point
Expand All @@ -502,7 +511,7 @@ func (suite *APIServerForwardTestSuite) checkUnavailableTSO(re *require.Assertio
re.Error(err)
}

func (suite *APIServerForwardTestSuite) checkAvailableTSO(re *require.Assertions) {
func (suite *APIServerForward) checkAvailableTSO(re *require.Assertions) {
mcs.WaitForTSOServiceAvailable(suite.ctx, re, suite.pdClient)
// try to get ts
_, _, err := suite.pdClient.GetTS(suite.ctx)
Expand Down Expand Up @@ -578,36 +587,36 @@ func (suite *CommonTestSuite) TestAdvertiseAddr() {
re.Equal(conf.GetListenAddr(), conf.GetAdvertiseListenAddr())
}

func (suite *CommonTestSuite) TestBootstrapDefaultKeyspaceGroup() {
re := suite.Require()

// check the default keyspace group
check := func() {
resp, err := tests.TestDialClient.Get(suite.pdLeader.GetServer().GetConfig().AdvertiseClientUrls + "/pd/api/v2/tso/keyspace-groups")
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
respString, err := io.ReadAll(resp.Body)
re.NoError(err)
var kgs []*endpoint.KeyspaceGroup
re.NoError(json.Unmarshal(respString, &kgs))
re.Len(kgs, 1)
re.Equal(utils.DefaultKeyspaceGroupID, kgs[0].ID)
re.Equal(endpoint.Basic.String(), kgs[0].UserKind)
re.Empty(kgs[0].SplitState)
re.Empty(kgs[0].Members)
re.Empty(kgs[0].KeyspaceLookupTable)
}
check()

s, err := suite.cluster.JoinAPIServer(suite.ctx)
re.NoError(err)
re.NoError(s.Run())

// transfer leader to the new server
suite.pdLeader.ResignLeader()
suite.pdLeader = suite.cluster.GetServer(suite.cluster.WaitLeader())
check()
suite.pdLeader.ResignLeader()
suite.pdLeader = suite.cluster.GetServer(suite.cluster.WaitLeader())
}
// func (suite *CommonTestSuite) TestBootstrapDefaultKeyspaceGroup() {
// re := suite.Require()

// // check the default keyspace group
// check := func() {
// resp, err := tests.TestDialClient.Get(suite.pdLeader.GetServer().GetConfig().AdvertiseClientUrls + "/pd/api/v2/tso/keyspace-groups")
// re.NoError(err)
// defer resp.Body.Close()
// re.Equal(http.StatusOK, resp.StatusCode)
// respString, err := io.ReadAll(resp.Body)
// re.NoError(err)
// var kgs []*endpoint.KeyspaceGroup
// re.NoError(json.Unmarshal(respString, &kgs))
// re.Len(kgs, 1)
// re.Equal(utils.DefaultKeyspaceGroupID, kgs[0].ID)
// re.Equal(endpoint.Basic.String(), kgs[0].UserKind)
// re.Empty(kgs[0].SplitState)
// re.Empty(kgs[0].Members)
// re.Empty(kgs[0].KeyspaceLookupTable)
// }
// check()

// s, err := suite.cluster.JoinAPIServer(suite.ctx)
// re.NoError(err)
// re.NoError(s.Run())

// // transfer leader to the new server
// suite.pdLeader.ResignLeader()
// suite.pdLeader = suite.cluster.GetServer(suite.cluster.WaitLeader())
// check()
// suite.pdLeader.ResignLeader()
// suite.pdLeader = suite.cluster.GetServer(suite.cluster.WaitLeader())
// }
4 changes: 2 additions & 2 deletions tests/integrations/tso/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func (suite *tsoClientTestSuite) getBackendEndpoints() []string {
return strings.Split(suite.backendEndpoints, ",")
}

func TestLegacyTSOClient(t *testing.T) {
func TestLegacyTSOClientSuite(t *testing.T) {
suite.Run(t, &tsoClientTestSuite{
legacy: true,
})
}

func TestMicroserviceTSOClient(t *testing.T) {
func TestMicroserviceTSOClientSuite(t *testing.T) {
suite.Run(t, &tsoClientTestSuite{
legacy: false,
})
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/tso/consistency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ type tsoConsistencyTestSuite struct {
tsoClient tsopb.TSOClient
}

func TestLegacyTSOConsistency(t *testing.T) {
func TestLegacyTSOConsistencySuite(t *testing.T) {
suite.Run(t, &tsoConsistencyTestSuite{
legacy: true,
})
}

func TestMicroserviceTSOConsistency(t *testing.T) {
func TestMicroserviceTSOConsistencySuite(t *testing.T) {
suite.Run(t, &tsoConsistencyTestSuite{
legacy: false,
})
Expand Down
16 changes: 13 additions & 3 deletions tests/server/api/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,28 @@ const apiPrefix = "/pd"

type scheduleTestSuite struct {
suite.Suite
env *tests.SchedulingTestEnvironment
env *tests.SchedulingTestEnvironment
runMode tests.SchedulerMode
}

func TestScheduleTestSuite(t *testing.T) {
suite.Run(t, new(scheduleTestSuite))
func TestPDSchedulingTestSuite(t *testing.T) {
suite.Run(t, &scheduleTestSuite{
runMode: tests.PDMode,
})
}

func TestAPISchedulingTestSuite(t *testing.T) {
suite.Run(t, &scheduleTestSuite{
runMode: tests.APIMode,
})
}

func (suite *scheduleTestSuite) SetupSuite() {
re := suite.Require()
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/schedule/changeCoordinatorTicker", `return(true)`))
re.NoError(failpoint.Enable("github.com/tikv/pd/server/cluster/skipStoreConfigSync", `return(true)`))
suite.env = tests.NewSchedulingTestEnvironment(suite.T())
suite.env.RunMode = suite.runMode
}

func (suite *scheduleTestSuite) TearDownSuite() {
Expand Down
Loading

0 comments on commit 1bfec95

Please sign in to comment.