@@ -26,6 +26,7 @@ func TestPDFetcher(t *testing.T) {
2626 {
2727 infos : map [string ]* infosync.TiDBTopologyInfo {
2828 "1.1.1.1:4000" : {
29+ Addr : "1.1.1.1:4000" ,
2930 Labels : map [string ]string {"k1" : "v1" },
3031 IP : "1.1.1.1" ,
3132 StatusPort : 10080 ,
@@ -34,6 +35,7 @@ func TestPDFetcher(t *testing.T) {
3435 check : func (m map [string ]* BackendInfo ) {
3536 require .Len (t , m , 1 )
3637 require .NotNil (t , m ["1.1.1.1:4000" ])
38+ require .Equal (t , "1.1.1.1:4000" , m ["1.1.1.1:4000" ].Addr )
3739 require .Equal (t , "1.1.1.1" , m ["1.1.1.1:4000" ].IP )
3840 require .Equal (t , uint (10080 ), m ["1.1.1.1:4000" ].StatusPort )
3941 require .Equal (t , map [string ]string {"k1" : "v1" }, m ["1.1.1.1:4000" ].Labels )
@@ -42,24 +44,43 @@ func TestPDFetcher(t *testing.T) {
4244 {
4345 infos : map [string ]* infosync.TiDBTopologyInfo {
4446 "1.1.1.1:4000" : {
47+ Addr : "1.1.1.1:4000" ,
4548 IP : "1.1.1.1" ,
4649 StatusPort : 10080 ,
4750 },
4851 "2.2.2.2:4000" : {
52+ Addr : "2.2.2.2:4000" ,
4953 IP : "2.2.2.2" ,
5054 StatusPort : 10080 ,
5155 },
5256 },
5357 check : func (m map [string ]* BackendInfo ) {
5458 require .Len (t , m , 2 )
5559 require .NotNil (t , m ["1.1.1.1:4000" ])
60+ require .Equal (t , "1.1.1.1:4000" , m ["1.1.1.1:4000" ].Addr )
5661 require .Equal (t , "1.1.1.1" , m ["1.1.1.1:4000" ].IP )
5762 require .Equal (t , uint (10080 ), m ["1.1.1.1:4000" ].StatusPort )
5863 require .NotNil (t , m ["2.2.2.2:4000" ])
64+ require .Equal (t , "2.2.2.2:4000" , m ["2.2.2.2:4000" ].Addr )
5965 require .Equal (t , "2.2.2.2" , m ["2.2.2.2:4000" ].IP )
6066 require .Equal (t , uint (10080 ), m ["2.2.2.2:4000" ].StatusPort )
6167 },
6268 },
69+ {
70+ infos : map [string ]* infosync.TiDBTopologyInfo {
71+ "cluster-a/shared.tidb:4000" : {
72+ Addr : "shared.tidb:4000" ,
73+ IP : "10.0.0.1" ,
74+ StatusPort : 10080 ,
75+ },
76+ },
77+ check : func (m map [string ]* BackendInfo ) {
78+ require .Len (t , m , 1 )
79+ require .NotNil (t , m ["cluster-a/shared.tidb:4000" ])
80+ require .Equal (t , "shared.tidb:4000" , m ["cluster-a/shared.tidb:4000" ].Addr )
81+ require .Equal (t , "10.0.0.1" , m ["cluster-a/shared.tidb:4000" ].IP )
82+ },
83+ },
6384 {
6485 ctx : func () context.Context {
6586 ctx , cancel := context .WithCancel (context .Background ())
@@ -74,9 +95,10 @@ func TestPDFetcher(t *testing.T) {
7495
7596 tpFetcher := newMockTpFetcher (t )
7697 lg , _ := logger .CreateLoggerForTest (t )
77- pf := NewPDFetcher (tpFetcher , lg , newHealthCheckConfigForTest ())
98+ pf := NewPDFetcher (tpFetcher , nil , lg , newHealthCheckConfigForTest ())
7899 for _ , test := range tests {
79100 tpFetcher .infos = test .infos
101+ tpFetcher .hasClusters = true
80102 if test .ctx == nil {
81103 test .ctx = context .Background ()
82104 }
@@ -85,3 +107,27 @@ func TestPDFetcher(t *testing.T) {
85107 require .NoError (t , err )
86108 }
87109}
110+
111+ func TestPDFetcherFallbackToStaticWithoutBackendClusters (t * testing.T ) {
112+ tpFetcher := newMockTpFetcher (t )
113+ lg , _ := logger .CreateLoggerForTest (t )
114+ fetcher := NewPDFetcher (tpFetcher , []string {"127.0.0.1:4000" }, lg , newHealthCheckConfigForTest ())
115+
116+ backends , err := fetcher .GetBackendList (context .Background ())
117+ require .NoError (t , err )
118+ require .Len (t , backends , 1 )
119+ require .Contains (t , backends , "127.0.0.1:4000" )
120+
121+ tpFetcher .hasClusters = true
122+ tpFetcher .infos = map [string ]* infosync.TiDBTopologyInfo {
123+ "cluster-a/10.0.0.1:4000" : {
124+ Addr : "10.0.0.1:4000" ,
125+ ClusterName : "cluster-a" ,
126+ },
127+ }
128+ backends , err = fetcher .GetBackendList (context .Background ())
129+ require .NoError (t , err )
130+ require .Len (t , backends , 1 )
131+ require .Equal (t , "10.0.0.1:4000" , backends ["cluster-a/10.0.0.1:4000" ].Addr )
132+ require .Equal (t , "cluster-a" , backends ["cluster-a/10.0.0.1:4000" ].ClusterName )
133+ }
0 commit comments