@@ -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 ())
@@ -85,3 +106,31 @@ func TestPDFetcher(t *testing.T) {
85106 require .NoError (t , err )
86107 }
87108}
109+
110+ func TestFallbackFetcher (t * testing.T ) {
111+ tpFetcher := newMockTpFetcher (t )
112+ lg , _ := logger .CreateLoggerForTest (t )
113+ fetcher := NewFallbackFetcher (
114+ tpFetcher ,
115+ NewPDFetcher (tpFetcher , lg , newHealthCheckConfigForTest ()),
116+ NewStaticFetcher ([]string {"127.0.0.1:4000" }),
117+ )
118+
119+ backends , err := fetcher .GetBackendList (context .Background ())
120+ require .NoError (t , err )
121+ require .Len (t , backends , 1 )
122+ require .Contains (t , backends , "127.0.0.1:4000" )
123+
124+ tpFetcher .hasClusters = true
125+ tpFetcher .infos = map [string ]* infosync.TiDBTopologyInfo {
126+ "cluster-a/10.0.0.1:4000" : {
127+ Addr : "10.0.0.1:4000" ,
128+ ClusterName : "cluster-a" ,
129+ },
130+ }
131+ backends , err = fetcher .GetBackendList (context .Background ())
132+ require .NoError (t , err )
133+ require .Len (t , backends , 1 )
134+ require .Equal (t , "10.0.0.1:4000" , backends ["cluster-a/10.0.0.1:4000" ].Addr )
135+ require .Equal (t , "cluster-a" , backends ["cluster-a/10.0.0.1:4000" ].ClusterName )
136+ }
0 commit comments