@@ -107,7 +107,7 @@ public Task<IClient> CreateClient(ClientParameters clientParameters, Broker brok
107107 public bool ValidateDns { get ; set ; } = false ;
108108 }
109109
110- public class ReplicaRouting : IRouting
110+ public class LeaderRouting : IRouting
111111 {
112112 public Task < IClient > CreateClient ( ClientParameters clientParameters , Broker broker , ILogger logger = null )
113113 {
@@ -125,6 +125,25 @@ public Task<IClient> CreateClient(ClientParameters clientParameters, Broker brok
125125 public bool ValidateDns { get ; set ; } = false ;
126126 }
127127
128+ public class ReplicaseRouting : IRouting
129+ {
130+ public Task < IClient > CreateClient ( ClientParameters clientParameters , Broker broker , ILogger logger = null )
131+ {
132+ var fake = new FakeClient ( clientParameters )
133+ {
134+ ConnectionProperties = new Dictionary < string , string > ( )
135+ {
136+
137+ [ "advertised_port" ] = "5553" ,
138+ [ "advertised_host" ] = "replica2"
139+ }
140+ } ;
141+ return Task . FromResult < IClient > ( fake ) ;
142+ }
143+
144+ public bool ValidateDns { get ; set ; } = false ;
145+ }
146+
128147 // This class is only for unit tests
129148 public class UnitTests
130149 {
@@ -141,7 +160,7 @@ public async Task GiveProperExceptionWhenUnableToConnect()
141160 var metaDataInfo = new StreamInfo ( "stream" , ResponseCode . Ok , new Broker ( "localhost" , 3939 ) ,
142161 new List < Broker > ( ) ) ;
143162 await Assert . ThrowsAsync < AggregateException > ( ( ) =>
144- RoutingHelper < Routing > . LookupRandomConnection ( clientParameters , metaDataInfo ,
163+ RoutingHelper < Routing > . LookupLeaderOrRandomReplicasConnection ( clientParameters , metaDataInfo ,
145164 new ConnectionsPool ( 1 , 1 ) ) ) ;
146165 }
147166
@@ -214,14 +233,34 @@ public void RandomReplicaLeader()
214233 var metaDataInfo = new StreamInfo ( "stream" , ResponseCode . Ok , new Broker ( "leader" , 5552 ) ,
215234 new List < Broker > ( ) ) ;
216235 var client =
217- RoutingHelper < ReplicaRouting > . LookupRandomConnection ( clientParameters , metaDataInfo ,
236+ RoutingHelper < LeaderRouting > . LookupLeaderOrRandomReplicasConnection ( clientParameters , metaDataInfo ,
218237 new ConnectionsPool ( 1 , 1 ) ) ;
219238 Assert . Equal ( "5552" , client . Result . ConnectionProperties [ "advertised_port" ] ) ;
220239 var res = ( client . Result . ConnectionProperties [ "advertised_host" ] == "leader" ||
221240 client . Result . ConnectionProperties [ "advertised_host" ] == "replica" ) ;
222241 Assert . True ( res ) ;
223242 }
224243
244+ [ Fact ]
245+ public void RandomOnlyReplicaIfThereAre ( )
246+ {
247+ // this test is not completed yet should add also some replicas
248+ var addressResolver = new AddressResolver ( new IPEndPoint ( IPAddress . Parse ( "192.168.10.99" ) , 5552 ) ) ;
249+ var clientParameters = new ClientParameters ( ) { AddressResolver = addressResolver , } ;
250+ var metaDataInfo = new StreamInfo ( "stream" ,
251+ ResponseCode . Ok , new Broker ( "leader" , 5552 ) ,
252+ new List < Broker > ( )
253+ {
254+ new Broker ( "replica2" , 5553 ) ,
255+ } ) ;
256+ var client =
257+ RoutingHelper < ReplicaseRouting > . LookupLeaderOrRandomReplicasConnection ( clientParameters , metaDataInfo ,
258+ new ConnectionsPool ( 1 , 1 ) ) ;
259+ Assert . Equal ( "5553" , client . Result . ConnectionProperties [ "advertised_port" ] ) ;
260+ var res = ( client . Result . ConnectionProperties [ "advertised_host" ] == "replica2" ) ;
261+ Assert . True ( res ) ;
262+ }
263+
225264 [ Fact ]
226265 public void CompressUnCompressShouldHaveTheSize ( )
227266 {
0 commit comments