tests

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2025 License: Apache-2.0 Imports: 52 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Initial int32 = iota
	Running
	Stop
	Destroy
)

TestServer states.

Variables

View Source
var (
	// WaitLeaderReturnDelay represents the time interval of WaitLeader sleep before returning.
	WaitLeaderReturnDelay = 20 * time.Millisecond
	// WaitLeaderCheckInterval represents the time interval of WaitLeader running check.
	WaitLeaderCheckInterval = 500 * time.Millisecond
	// WaitLeaderRetryTimes represents the maximum number of loops of WaitLeader.
	WaitLeaderRetryTimes = 100
)
View Source
var (
	// TestDialClient is a http client for test.
	TestDialClient = &http.Client{
		Transport: &http.Transport{
			DisableKeepAlives: true,
		},
	}
)

Functions

func InitLogger

func InitLogger(logConfig log.Config, logger *zap.Logger, logProps *log.ZapProperties, redactInfoLog logutil.RedactInfoLogType) (err error)

InitLogger initializes the logger for test.

func InitRegions

func InitRegions(regionLen int) []*core.RegionInfo

InitRegions is used for test purpose.

func MustPutRegion

func MustPutRegion(re *require.Assertions, cluster *TestCluster, regionID, storeID uint64, start, end []byte, opts ...core.RegionCreateOption) *core.RegionInfo

MustPutRegion is used for test purpose.

func MustPutRegionInfo

func MustPutRegionInfo(re *require.Assertions, cluster *TestCluster, regionInfo *core.RegionInfo)

MustPutRegionInfo is used for test purpose.

func MustPutStore

func MustPutStore(re *require.Assertions, cluster *TestCluster, store *metapb.Store)

MustPutStore is used for test purpose.

func MustReportBuckets

func MustReportBuckets(re *require.Assertions, cluster *TestCluster, regionID uint64, start, end []byte, stats *metapb.BucketStats) *metapb.Buckets

MustReportBuckets is used for test purpose.

func NewSchedulingTestServer

func NewSchedulingTestServer(ctx context.Context, cfg *sc.Config) (*scheduling.Server, testutil.CleanupFunc, error)

NewSchedulingTestServer creates a scheduling server with given config for testing.

func NewTSOTestServer

func NewTSOTestServer(ctx context.Context, cfg *tso.Config) (*tso.Server, testutil.CleanupFunc, error)

NewTSOTestServer creates a tso server with given config for testing.

func RunServer

func RunServer(server *TestServer) <-chan error

RunServer starts to run TestServer.

func RunServers

func RunServers(servers []*TestServer) error

RunServers starts to run multiple TestServer.

func SetRangePort

func SetRangePort(start, end int)

SetRangePort sets the range of ports for test.

func StartSingleResourceManagerTestServer

func StartSingleResourceManagerTestServer(ctx context.Context, re *require.Assertions, backendEndpoints, listenAddrs string) (*rm.Server, func())

StartSingleResourceManagerTestServer creates and starts a resource manager server with default config for testing.

func StartSingleSchedulingTestServer

func StartSingleSchedulingTestServer(ctx context.Context, re *require.Assertions, backendEndpoints, listenAddrs string) (*scheduling.Server, func())

StartSingleSchedulingTestServer creates and starts a scheduling server with default config for testing.

func StartSingleTSOTestServer

func StartSingleTSOTestServer(ctx context.Context, re *require.Assertions, backendEndpoints, listenAddrs string) (*tso.Server, func())

StartSingleTSOTestServer creates and starts a tso server with default config for testing.

func StartSingleTSOTestServerWithoutCheck

func StartSingleTSOTestServerWithoutCheck(ctx context.Context, re *require.Assertions, backendEndpoints, listenAddrs string) (*tso.Server, func(), error)

StartSingleTSOTestServerWithoutCheck creates and starts a tso server with default config for testing.

func WaitForPrimaryServing

func WaitForPrimaryServing(re *require.Assertions, serverMap map[string]bs.Server) string

WaitForPrimaryServing waits for one of servers being elected to be the primary/leader

Types

type ConfigOption

type ConfigOption func(conf *config.Config, serverName string)

ConfigOption is used to define customize settings in test. You can use serverName to customize a config for a certain server. Usually, the server name will be like `pd1`, `pd2` and so on, which determined by the number of servers you set.

type SchedulerMode

type SchedulerMode int

SchedulerMode is used for test purpose.

const (
	// Both represents both PD mode and API mode.
	Both SchedulerMode = iota
	// PDMode represents PD mode.
	PDMode
	// APIMode represents API mode.
	APIMode
)

type SchedulingTestEnvironment

type SchedulingTestEnvironment struct {
	RunMode SchedulerMode
	// contains filtered or unexported fields
}

SchedulingTestEnvironment is used for test purpose.

func NewSchedulingTestEnvironment

func NewSchedulingTestEnvironment(t *testing.T, opts ...ConfigOption) *SchedulingTestEnvironment

NewSchedulingTestEnvironment is to create a new SchedulingTestEnvironment.

func (*SchedulingTestEnvironment) Cleanup

func (s *SchedulingTestEnvironment) Cleanup()

Cleanup is to cleanup the environment.

func (*SchedulingTestEnvironment) RunTestBasedOnMode

func (s *SchedulingTestEnvironment) RunTestBasedOnMode(test func(*TestCluster))

RunTestBasedOnMode runs test based on mode. If mode not set, it will run test in both PD mode and API mode.

func (*SchedulingTestEnvironment) RunTestInAPIMode

func (s *SchedulingTestEnvironment) RunTestInAPIMode(test func(*TestCluster))

RunTestInAPIMode is to run test in api mode.

func (*SchedulingTestEnvironment) RunTestInPDMode

func (s *SchedulingTestEnvironment) RunTestInPDMode(test func(*TestCluster))

RunTestInPDMode is to run test in pd mode.

type TestCluster

type TestCluster struct {
	// contains filtered or unexported fields
}

TestCluster is only for test.

func NewTestAPICluster

func NewTestAPICluster(ctx context.Context, initialServerCount int, opts ...ConfigOption) (*TestCluster, error)

NewTestAPICluster creates a new TestCluster with API service.

func NewTestCluster

func NewTestCluster(ctx context.Context, initialServerCount int, opts ...ConfigOption) (*TestCluster, error)

NewTestCluster creates a new TestCluster.

func RestartTestAPICluster

func RestartTestAPICluster(ctx context.Context, cluster *TestCluster) (*TestCluster, error)

RestartTestAPICluster restarts the API test cluster.

func (*TestCluster) CheckClusterDCLocation

func (c *TestCluster) CheckClusterDCLocation()

CheckClusterDCLocation will force the cluster to do the dc-location check in order to speed up the test.

func (*TestCluster) CheckTSOUnique

func (c *TestCluster) CheckTSOUnique(ts uint64) bool

CheckTSOUnique will check whether the TSO is unique among the cluster in the past and present.

func (*TestCluster) DeleteServer

func (c *TestCluster) DeleteServer(name string)

DeleteServer is used to delete a server.

func (*TestCluster) Destroy

func (c *TestCluster) Destroy()

Destroy is used to destroy a TestCluster.

func (*TestCluster) GetCluster

func (c *TestCluster) GetCluster() *metapb.Cluster

GetCluster returns PD cluster.

func (*TestCluster) GetClusterStatus

func (c *TestCluster) GetClusterStatus() (*cluster.Status, error)

GetClusterStatus returns raft cluster status.

func (*TestCluster) GetConfig

func (c *TestCluster) GetConfig() *clusterConfig

GetConfig returns the current TestCluster's configuration.

func (*TestCluster) GetEtcdClient

func (c *TestCluster) GetEtcdClient() *clientv3.Client

GetEtcdClient returns the builtin etcd client.

func (*TestCluster) GetFollower

func (c *TestCluster) GetFollower() string

GetFollower returns an follower of all servers

func (*TestCluster) GetHTTPClient

func (c *TestCluster) GetHTTPClient() *http.Client

GetHTTPClient returns the builtin http client.

func (*TestCluster) GetLeader

func (c *TestCluster) GetLeader() string

GetLeader returns the leader of all servers

func (*TestCluster) GetLeaderServer

func (c *TestCluster) GetLeaderServer() *TestServer

GetLeaderServer returns the leader server of all servers

func (*TestCluster) GetSchedulingPrimaryServer

func (c *TestCluster) GetSchedulingPrimaryServer() *scheduling.Server

GetSchedulingPrimaryServer returns the scheduling primary server.

func (*TestCluster) GetServer

func (c *TestCluster) GetServer(name string) *TestServer

GetServer returns a server with a given name.

func (*TestCluster) GetServers

func (c *TestCluster) GetServers() map[string]*TestServer

GetServers returns all servers.

func (*TestCluster) HandleRegionHeartbeat

func (c *TestCluster) HandleRegionHeartbeat(region *core.RegionInfo) error

HandleRegionHeartbeat processes RegionInfo reports from the client.

func (*TestCluster) HandleReportBuckets

func (c *TestCluster) HandleReportBuckets(b *metapb.Buckets) error

HandleReportBuckets processes BucketInfo reports from the client.

func (*TestCluster) Join

func (c *TestCluster) Join(ctx context.Context, opts ...ConfigOption) (*TestServer, error)

Join is used to add a new TestServer into the cluster.

func (*TestCluster) JoinAPIServer

func (c *TestCluster) JoinAPIServer(ctx context.Context, opts ...ConfigOption) (*TestServer, error)

JoinAPIServer is used to add a new TestAPIServer into the cluster.

func (*TestCluster) ResignLeader

func (c *TestCluster) ResignLeader() error

ResignLeader resigns the leader of the cluster.

func (*TestCluster) RunInitialServers

func (c *TestCluster) RunInitialServers() error

RunInitialServers starts to run servers in InitialServers.

func (*TestCluster) SetSchedulingCluster

func (c *TestCluster) SetSchedulingCluster(cluster *TestSchedulingCluster)

SetSchedulingCluster sets the scheduling cluster.

func (*TestCluster) StopAll

func (c *TestCluster) StopAll() error

StopAll is used to stop all servers.

func (*TestCluster) WaitAllLeaders

func (c *TestCluster) WaitAllLeaders(re *require.Assertions, dcLocations map[string]string)

WaitAllLeaders will block and wait for the election of PD leader and all Local TSO Allocator leaders.

func (*TestCluster) WaitAllocatorLeader

func (c *TestCluster) WaitAllocatorLeader(dcLocation string, ops ...WaitOption) string

WaitAllocatorLeader is used to get the Local TSO Allocator leader. If it exceeds the maximum number of loops, it will return an empty string.

func (*TestCluster) WaitLeader

func (c *TestCluster) WaitLeader(ops ...WaitOption) string

WaitLeader is used to get leader. If it exceeds the maximum number of loops, it will return an empty string.

func (*TestCluster) WaitRegionSyncerClientsReady

func (c *TestCluster) WaitRegionSyncerClientsReady(n int) bool

WaitRegionSyncerClientsReady is used to wait the region syncer clients establish the connection. n means wait n clients.

type TestSchedulingCluster

type TestSchedulingCluster struct {
	// contains filtered or unexported fields
}

TestSchedulingCluster is a test cluster for scheduling.

func NewTestSchedulingCluster

func NewTestSchedulingCluster(ctx context.Context, initialServerCount int, backendEndpoints string) (tc *TestSchedulingCluster, err error)

NewTestSchedulingCluster creates a new scheduling test cluster.

func (*TestSchedulingCluster) AddServer

func (tc *TestSchedulingCluster) AddServer(addr string) error

AddServer adds a new scheduling server to the test cluster.

func (*TestSchedulingCluster) Destroy

func (tc *TestSchedulingCluster) Destroy()

Destroy stops and destroy the test cluster.

func (*TestSchedulingCluster) DestroyServer

func (tc *TestSchedulingCluster) DestroyServer(addr string)

DestroyServer stops and destroy the test server by the given address.

func (*TestSchedulingCluster) GetAddrs

func (tc *TestSchedulingCluster) GetAddrs() []string

GetAddrs returns all scheduling server addresses.

func (*TestSchedulingCluster) GetPrimaryServer

func (tc *TestSchedulingCluster) GetPrimaryServer() *scheduling.Server

GetPrimaryServer returns the primary scheduling server.

func (*TestSchedulingCluster) GetServer

func (tc *TestSchedulingCluster) GetServer(addr string) *scheduling.Server

GetServer returns the scheduling server by the given address.

func (*TestSchedulingCluster) GetServers

func (tc *TestSchedulingCluster) GetServers() map[string]*scheduling.Server

GetServers returns all scheduling servers.

func (*TestSchedulingCluster) WaitForPrimaryServing

func (tc *TestSchedulingCluster) WaitForPrimaryServing(re *require.Assertions) *scheduling.Server

WaitForPrimaryServing waits for one of servers being elected to be the primary/leader of the given keyspace.

type TestServer

type TestServer struct {
	syncutil.RWMutex
	// contains filtered or unexported fields
}

TestServer is only for test.

func NewTestAPIServer

func NewTestAPIServer(ctx context.Context, cfg *config.Config) (*TestServer, error)

NewTestAPIServer creates a new TestServer.

func NewTestServer

func NewTestServer(ctx context.Context, cfg *config.Config) (*TestServer, error)

NewTestServer creates a new TestServer.

func (*TestServer) BootstrapCluster

func (s *TestServer) BootstrapCluster() error

BootstrapCluster is used to bootstrap the cluster.

func (*TestServer) Destroy

func (s *TestServer) Destroy() error

Destroy is used to destroy a TestServer.

func (*TestServer) GetAddr

func (s *TestServer) GetAddr() string

GetAddr returns the address of TestCluster.

func (*TestServer) GetAdjacentRegions

func (s *TestServer) GetAdjacentRegions(region *core.RegionInfo) []*core.RegionInfo

GetAdjacentRegions returns regions' information that are adjacent with the specific region ID.

func (*TestServer) GetAllocator

func (s *TestServer) GetAllocator() id.Allocator

GetAllocator returns the current TestServer's ID allocator.

func (*TestServer) GetAllocatorLeader

func (s *TestServer) GetAllocatorLeader(dcLocation string) *pdpb.Member

GetAllocatorLeader returns current allocator leader of PD cluster for given dc-location.

func (*TestServer) GetCluster

func (s *TestServer) GetCluster() *metapb.Cluster

GetCluster returns PD cluster.

func (*TestServer) GetClusterID

func (*TestServer) GetClusterID() uint64

GetClusterID returns the cluster ID.

func (*TestServer) GetClusterVersion

func (s *TestServer) GetClusterVersion() semver.Version

GetClusterVersion returns PD cluster version.

func (*TestServer) GetConfig

func (s *TestServer) GetConfig() *config.Config

GetConfig returns the current TestServer's configuration.

func (*TestServer) GetEtcdClient

func (s *TestServer) GetEtcdClient() *clientv3.Client

GetEtcdClient returns the builtin etcd client.

func (*TestServer) GetEtcdLeader

func (s *TestServer) GetEtcdLeader() (string, error)

GetEtcdLeader returns the builtin etcd leader.

func (*TestServer) GetEtcdLeaderID

func (s *TestServer) GetEtcdLeaderID() (uint64, error)

GetEtcdLeaderID returns the builtin etcd leader ID.

func (*TestServer) GetHTTPClient

func (s *TestServer) GetHTTPClient() *http.Client

GetHTTPClient returns the builtin http client.

func (*TestServer) GetKeyspaceManager

func (s *TestServer) GetKeyspaceManager() *keyspace.Manager

GetKeyspaceManager returns the current TestServer's Keyspace Manager.

func (*TestServer) GetLeader

func (s *TestServer) GetLeader() *pdpb.Member

GetLeader returns current leader of PD cluster.

func (*TestServer) GetPersistOptions

func (s *TestServer) GetPersistOptions() *config.PersistOptions

GetPersistOptions returns the current TestServer's schedule option.

func (*TestServer) GetRaftCluster

func (s *TestServer) GetRaftCluster() *cluster.RaftCluster

GetRaftCluster returns Raft cluster. If cluster has not been bootstrapped, return nil.

func (*TestServer) GetRangeHoles

func (s *TestServer) GetRangeHoles() [][]string

GetRangeHoles returns all range holes, i.e the key ranges without any region info.

func (*TestServer) GetRegionInfoByID

func (s *TestServer) GetRegionInfoByID(regionID uint64) *core.RegionInfo

GetRegionInfoByID returns regionInfo by regionID from cluster.

func (*TestServer) GetRegions

func (s *TestServer) GetRegions() []*core.RegionInfo

GetRegions returns all regions' information in detail.

func (*TestServer) GetServer

func (s *TestServer) GetServer() *server.Server

GetServer returns the real server of TestServer.

func (*TestServer) GetServerID

func (s *TestServer) GetServerID() uint64

GetServerID returns the unique etcd ID for this server in etcd cluster.

func (*TestServer) GetServicePrimaryAddr

func (s *TestServer) GetServicePrimaryAddr(ctx context.Context, serviceName string) (string, bool)

GetServicePrimaryAddr returns the primary address of the service.

func (*TestServer) GetStore

func (s *TestServer) GetStore(storeID uint64) *core.StoreInfo

GetStore returns the store with a given store ID.

func (*TestServer) GetStoreRegions

func (s *TestServer) GetStoreRegions(storeID uint64) []*core.RegionInfo

GetStoreRegions returns all regions' information with a given storeID.

func (*TestServer) GetStores

func (s *TestServer) GetStores() []*metapb.Store

GetStores returns the stores of the cluster.

func (*TestServer) GetTSOAllocatorManager

func (s *TestServer) GetTSOAllocatorManager() *tso.AllocatorManager

GetTSOAllocatorManager returns the server's TSO Allocator Manager.

func (*TestServer) IsAllocatorLeader

func (s *TestServer) IsAllocatorLeader(dcLocation string) bool

IsAllocatorLeader returns whether the server is a TSO Allocator leader or not.

func (*TestServer) IsLeader

func (s *TestServer) IsLeader() bool

IsLeader returns whether the server is leader or not.

func (*TestServer) MoveEtcdLeader

func (s *TestServer) MoveEtcdLeader(old, new uint64) error

MoveEtcdLeader moves etcd leader from old to new.

func (*TestServer) ResetPDLeader

func (s *TestServer) ResetPDLeader()

ResetPDLeader resigns the leader of the server.

func (*TestServer) ResignLeader

func (s *TestServer) ResignLeader() error

ResignLeader resigns the leader of the server.

func (*TestServer) Run

func (s *TestServer) Run() error

Run starts to run a TestServer.

func (*TestServer) SetEnableLocalTSO

func (s *TestServer) SetEnableLocalTSO(enableLocalTSO bool)

SetEnableLocalTSO sets the enable-local-tso flag of the TestServer.

func (*TestServer) SetKeyspaceManager

func (s *TestServer) SetKeyspaceManager(km *keyspace.Manager)

SetKeyspaceManager sets the current TestServer's Keyspace Manager.

func (*TestServer) State

func (s *TestServer) State() int32

State returns the current TestServer's state.

func (*TestServer) Stop

func (s *TestServer) Stop() error

Stop is used to stop a TestServer.

func (*TestServer) WaitLeader

func (s *TestServer) WaitLeader() bool

WaitLeader is used to get instant leader info in order to make a test know the PD leader has been elected as soon as possible. If it exceeds the maximum number of loops, it will return nil.

type TestTSOCluster

type TestTSOCluster struct {
	// contains filtered or unexported fields
}

TestTSOCluster is a test cluster for TSO.

func NewTestTSOCluster

func NewTestTSOCluster(ctx context.Context, initialServerCount int, backendEndpoints string) (tc *TestTSOCluster, err error)

NewTestTSOCluster creates a new TSO test cluster.

func RestartTestTSOCluster

func RestartTestTSOCluster(
	ctx context.Context, cluster *TestTSOCluster,
) (newCluster *TestTSOCluster, err error)

RestartTestTSOCluster restarts the TSO test cluster.

func (*TestTSOCluster) AddServer

func (tc *TestTSOCluster) AddServer(addr string) error

AddServer adds a new TSO server to the test cluster.

func (*TestTSOCluster) Destroy

func (tc *TestTSOCluster) Destroy()

Destroy stops and destroy the test cluster.

func (*TestTSOCluster) DestroyServer

func (tc *TestTSOCluster) DestroyServer(addr string)

DestroyServer stops and destroy the test server by the given address.

func (*TestTSOCluster) GetAddrs

func (tc *TestTSOCluster) GetAddrs() []string

GetAddrs returns all TSO server addresses.

func (*TestTSOCluster) GetKeyspaceGroupMember

func (tc *TestTSOCluster) GetKeyspaceGroupMember() (members []endpoint.KeyspaceGroupMember)

GetKeyspaceGroupMember converts the TSO servers to KeyspaceGroupMember and returns.

func (*TestTSOCluster) GetPrimaryServer

func (tc *TestTSOCluster) GetPrimaryServer(keyspaceID, keyspaceGroupID uint32) *tso.Server

GetPrimaryServer returns the primary TSO server of the given keyspace

func (*TestTSOCluster) GetServer

func (tc *TestTSOCluster) GetServer(addr string) *tso.Server

GetServer returns the TSO server by the given address.

func (*TestTSOCluster) GetServers

func (tc *TestTSOCluster) GetServers() map[string]*tso.Server

GetServers returns all TSO servers.

func (*TestTSOCluster) ResignPrimary

func (tc *TestTSOCluster) ResignPrimary(keyspaceID, keyspaceGroupID uint32) error

ResignPrimary resigns the primary TSO server.

func (*TestTSOCluster) WaitForDefaultPrimaryServing

func (tc *TestTSOCluster) WaitForDefaultPrimaryServing(re *require.Assertions) *tso.Server

WaitForDefaultPrimaryServing waits for one of servers being elected to be the primary/leader of the default keyspace.

func (*TestTSOCluster) WaitForPrimaryServing

func (tc *TestTSOCluster) WaitForPrimaryServing(re *require.Assertions, keyspaceID, keyspaceGroupID uint32) *tso.Server

WaitForPrimaryServing waits for one of servers being elected to be the primary/leader of the given keyspace.

type WaitOp

type WaitOp struct {
	// contains filtered or unexported fields
}

WaitOp represent the wait configuration

type WaitOption

type WaitOption func(*WaitOp)

WaitOption represent the wait configuration

func WithRetryTimes

func WithRetryTimes(r int) WaitOption

WithRetryTimes indicates the retry times

func WithWaitInterval

func WithWaitInterval(i time.Duration) WaitOption

WithWaitInterval indicates the wait interval

Directories

Path Synopsis
server
api

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL