Skip to content

Commit

Permalink
add test for syncer
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Dec 5, 2024
1 parent 3cfd66f commit 6cd1b08
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 54 deletions.
77 changes: 77 additions & 0 deletions pkg/mock/mockserver/mockserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mockserver

import (
"context"

"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/storage"
"github.com/tikv/pd/pkg/utils/grpcutil"
)

type MockServer struct {

Check failure on line 26 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported type MockServer should have comment or be unexported (revive)
ctx context.Context
member, leader *pdpb.Member
storage storage.Storage
bc *core.BasicCluster
}

func NewMockServer(ctx context.Context, member, leader *pdpb.Member, storage storage.Storage, bc *core.BasicCluster) *MockServer {

Check failure on line 33 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported function NewMockServer should have comment or be unexported (revive)
return &MockServer{
ctx: ctx,
member: member,
leader: leader,
storage: storage,
bc: bc,
}
}

func (s *MockServer) LoopContext() context.Context {

Check failure on line 43 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported method MockServer.LoopContext should have comment or be unexported (revive)
return s.ctx
}

func (*MockServer) ClusterID() uint64 {

Check failure on line 47 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported method MockServer.ClusterID should have comment or be unexported (revive)
return 1
}

func (s *MockServer) GetMemberInfo() *pdpb.Member {

Check failure on line 51 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported method MockServer.GetMemberInfo should have comment or be unexported (revive)
return s.member
}

func (s *MockServer) GetLeader() *pdpb.Member {

Check failure on line 55 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported method MockServer.GetLeader should have comment or be unexported (revive)
return s.leader
}

func (s *MockServer) GetStorage() storage.Storage {

Check failure on line 59 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported method MockServer.GetStorage should have comment or be unexported (revive)
return s.storage
}

func (*MockServer) Name() string {

Check failure on line 63 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported method MockServer.Name should have comment or be unexported (revive)
return "mock-server"
}

func (s *MockServer) GetRegions() []*core.RegionInfo {

Check failure on line 67 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported method MockServer.GetRegions should have comment or be unexported (revive)
return s.bc.GetRegions()
}

func (*MockServer) GetTLSConfig() *grpcutil.TLSConfig {

Check failure on line 71 in pkg/mock/mockserver/mockserver.go

View workflow job for this annotation

GitHub Actions / statics

exported: exported method MockServer.GetTLSConfig should have comment or be unexported (revive)
return &grpcutil.TLSConfig{}
}

func (s *MockServer) GetBasicCluster() *core.BasicCluster {
return s.bc
}
69 changes: 15 additions & 54 deletions pkg/syncer/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (

"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/mock/mockserver"
"github.com/tikv/pd/pkg/storage"
"github.com/tikv/pd/pkg/utils/grpcutil"
"google.golang.org/grpc/codes"
Expand All @@ -37,11 +37,13 @@ func TestLoadRegion(t *testing.T) {
rs, err := storage.NewRegionStorageWithLevelDBBackend(context.Background(), tempDir, nil)
re.NoError(err)

server := &mockServer{
ctx: context.Background(),
storage: storage.NewCoreStorage(storage.NewStorageWithMemoryBackend(), rs),
bc: core.NewBasicCluster(),
}
server := mockserver.NewMockServer(
context.Background(),
nil,
nil,
storage.NewCoreStorage(storage.NewStorageWithMemoryBackend(), rs),
core.NewBasicCluster(),
)
for i := range 30 {
rs.SaveRegion(&metapb.Region{Id: uint64(i) + 1})
}
Expand All @@ -64,11 +66,13 @@ func TestErrorCode(t *testing.T) {
tempDir := t.TempDir()
rs, err := storage.NewRegionStorageWithLevelDBBackend(context.Background(), tempDir, nil)
re.NoError(err)
server := &mockServer{
ctx: context.Background(),
storage: storage.NewCoreStorage(storage.NewStorageWithMemoryBackend(), rs),
bc: core.NewBasicCluster(),
}
server := mockserver.NewMockServer(
context.Background(),
nil,
nil,
storage.NewCoreStorage(storage.NewStorageWithMemoryBackend(), rs),
core.NewBasicCluster(),
)
ctx, cancel := context.WithCancel(context.TODO())
rc := NewRegionSyncer(server)
conn, err := grpcutil.GetClientConn(ctx, "http://127.0.0.1", nil)
Expand All @@ -79,46 +83,3 @@ func TestErrorCode(t *testing.T) {
re.True(ok)
re.Equal(codes.Canceled, ev.Code())
}

type mockServer struct {
ctx context.Context
member, leader *pdpb.Member
storage storage.Storage
bc *core.BasicCluster
}

func (s *mockServer) LoopContext() context.Context {
return s.ctx
}

func (*mockServer) ClusterID() uint64 {
return 1
}

func (s *mockServer) GetMemberInfo() *pdpb.Member {
return s.member
}

func (s *mockServer) GetLeader() *pdpb.Member {
return s.leader
}

func (s *mockServer) GetStorage() storage.Storage {
return s.storage
}

func (*mockServer) Name() string {
return "mock-server"
}

func (s *mockServer) GetRegions() []*core.RegionInfo {
return s.bc.GetRegions()
}

func (*mockServer) GetTLSConfig() *grpcutil.TLSConfig {
return &grpcutil.TLSConfig{}
}

func (s *mockServer) GetBasicCluster() *core.BasicCluster {
return s.bc
}
44 changes: 44 additions & 0 deletions tests/server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/tikv/pd/pkg/dashboard"
"github.com/tikv/pd/pkg/id"
"github.com/tikv/pd/pkg/mock/mockid"
"github.com/tikv/pd/pkg/mock/mockserver"
sc "github.com/tikv/pd/pkg/schedule/config"
"github.com/tikv/pd/pkg/schedule/operator"
"github.com/tikv/pd/pkg/schedule/schedulers"
Expand All @@ -45,6 +46,7 @@ import (
"github.com/tikv/pd/pkg/storage"
"github.com/tikv/pd/pkg/syncer"
"github.com/tikv/pd/pkg/tso"
"github.com/tikv/pd/pkg/utils/tempurl"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/pkg/utils/tsoutil"
"github.com/tikv/pd/pkg/utils/typeutil"
Expand Down Expand Up @@ -1887,3 +1889,45 @@ func checkLog(re *require.Assertions, fname, expect string) {
})
os.Truncate(fname, 0)
}

func TestFollowerExitSyncTime(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tc, err := tests.NewTestCluster(ctx, 1)
defer tc.Destroy()
re.NoError(err)
err = tc.RunInitialServers()
re.NoError(err)
tc.WaitLeader()
leaderServer := tc.GetLeaderServer()
re.NoError(leaderServer.BootstrapCluster())

tempDir := t.TempDir()
rs, err := storage.NewRegionStorageWithLevelDBBackend(context.Background(), tempDir, nil)
re.NoError(err)

server := mockserver.NewMockServer(
context.Background(),
&pdpb.Member{MemberId: 1, Name: "test", ClientUrls: []string{tempurl.Alloc()}},
nil,
storage.NewCoreStorage(storage.NewStorageWithMemoryBackend(), rs),
core.NewBasicCluster(),
)
s := syncer.NewRegionSyncer(server)
s.StartSyncWithLeader(leaderServer.GetAddr())
time.Sleep(time.Second)

// Record the time when exiting sync
startTime := time.Now()

// Simulate leader change scenario
// Directly call StopSyncWithLeader to simulate exit
s.StopSyncWithLeader()

// Calculate time difference
elapsedTime := time.Since(startTime)

// Assert that the sync exit time is within expected range
re.Less(elapsedTime, time.Second)
}

0 comments on commit 6cd1b08

Please sign in to comment.