Skip to content

Commit

Permalink
avoid introducing global variables for test
Browse files Browse the repository at this point in the history
  • Loading branch information
glorv committed Nov 17, 2022
1 parent f1fa607 commit 3bc3d83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
19 changes: 13 additions & 6 deletions domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package domain
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net"
"runtime"
"testing"
Expand Down Expand Up @@ -247,6 +249,12 @@ func TestClosestReplicaReadChecker(t *testing.T) {
}
dom.sysVarCache.Unlock()

makeFailpointRes := func(v interface{}) string {
bytes, err := json.Marshal(v)
require.NoError(t, err)
return fmt.Sprintf("return(`%s`)", string(bytes))
}

mockedAllServerInfos := map[string]*infosync.ServerInfo{
"s1": {
ID: "s1",
Expand All @@ -261,10 +269,9 @@ func TestClosestReplicaReadChecker(t *testing.T) {
},
},
}
infosync.SetAllServerInfo4Test(mockedAllServerInfos)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/infosync/mockGetAllServerInfo", `return("")`))
infosync.SetServerInfo4Test(mockedAllServerInfos["s2"])
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/infosync/mockGetServerInfo", `return("")`))

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/infosync/mockGetAllServerInfo", makeFailpointRes(mockedAllServerInfos)))
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/infosync/mockGetServerInfo", makeFailpointRes(mockedAllServerInfos["s2"])))

stores := []*metapb.Store{
{
Expand Down Expand Up @@ -355,7 +362,7 @@ func TestClosestReplicaReadChecker(t *testing.T) {
},
}
pdClient.stores = stores
infosync.SetAllServerInfo4Test(mockedAllServerInfos)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/infosync/mockGetAllServerInfo", makeFailpointRes(mockedAllServerInfos)))
cases := []struct {
id string
matches bool
Expand All @@ -382,7 +389,7 @@ func TestClosestReplicaReadChecker(t *testing.T) {
},
}
for _, c := range cases {
infosync.SetServerInfo4Test(mockedAllServerInfos[c.id])
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/infosync/mockGetServerInfo", makeFailpointRes(mockedAllServerInfos[c.id])))
variable.SetEnableAdaptiveReplicaRead(!c.matches)
err = dom.checkReplicaRead(ctx, pdClient)
require.Nil(t, err)
Expand Down
28 changes: 8 additions & 20 deletions domain/infosync/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,10 @@ func SetMockTiFlash(tiflash *MockTiFlash) {

// GetServerInfo gets self server static information.
func GetServerInfo() (*ServerInfo, error) {
failpoint.Inject("mockGetServerInfo", func() {
failpoint.Return(serverInfo4Test, nil)
failpoint.Inject("mockGetServerInfo", func(v failpoint.Value) {
var res ServerInfo
err := json.Unmarshal([]byte(v.(string)), &res)
failpoint.Return(&res, err)
})
is, err := getGlobalInfoSyncer()
if err != nil {
Expand Down Expand Up @@ -317,26 +319,12 @@ func (is *InfoSyncer) getServerInfoByID(ctx context.Context, id string) (*Server
return info, nil
}

// global variables only used in failpoint tests.
var (
allServerInfo4Test map[string]*ServerInfo
serverInfo4Test *ServerInfo
)

// SetAllServerInfo4Test set the value of `allServerInfo4Test` used in unit test.
func SetAllServerInfo4Test(infos map[string]*ServerInfo) {
allServerInfo4Test = infos
}

// SetServerInfo4Test set the value of `serverInfo4Test` used in unit test.
func SetServerInfo4Test(info *ServerInfo) {
serverInfo4Test = info
}

// GetAllServerInfo gets all servers static information from etcd.
func GetAllServerInfo(ctx context.Context) (map[string]*ServerInfo, error) {
failpoint.Inject("mockGetAllServerInfo", func() {
failpoint.Return(allServerInfo4Test, nil)
failpoint.Inject("mockGetAllServerInfo", func(val failpoint.Value) {
res := make(map[string]*ServerInfo)
err := json.Unmarshal([]byte(val.(string)), &res)
failpoint.Return(res, err)
})
is, err := getGlobalInfoSyncer()
if err != nil {
Expand Down

0 comments on commit 3bc3d83

Please sign in to comment.