2727using MongoDB . Driver . Core . Helpers ;
2828using NSubstitute ;
2929using NUnit . Framework ;
30+ using MongoDB . Bson ;
3031
3132namespace MongoDB . Driver . Core . Clusters
3233{
@@ -73,7 +74,7 @@ public void Constructor_should_throw_if_cluster_connection_mode_is_not_supported
7374 [ Test ]
7475 public void Description_should_be_correct_after_initialization ( )
7576 {
76- _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint } ) ;
77+ _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint } ) ;
7778
7879 var subject = CreateSubject ( ) ;
7980 subject . Initialize ( ) ;
@@ -87,7 +88,7 @@ public void Description_should_be_correct_after_initialization()
8788 [ Test ]
8889 public void Initialize_should_throw_when_already_disposed ( )
8990 {
90- _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint } ) ;
91+ _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint } ) ;
9192 var subject = CreateSubject ( ) ;
9293 subject . Dispose ( ) ;
9394
@@ -137,7 +138,7 @@ public void Should_remove_a_server_that_is_no_longer_in_the_primary_host_list()
137138 subject . Initialize ( ) ;
138139
139140 PublishDescription ( _firstEndPoint , ServerType . ReplicaSetPrimary ,
140- hosts : new [ ] { _firstEndPoint , _secondEndPoint } ) ;
141+ hosts : new [ ] { _firstEndPoint , _secondEndPoint } ) ;
141142
142143 var description = subject . Description ;
143144 description . State . Should ( ) . Be ( ClusterState . Connected ) ;
@@ -249,7 +250,7 @@ public void Should_ignore_changes_from_a_ReplicaSetGhost()
249250 subject . Initialize ( ) ;
250251
251252 PublishDescription ( _firstEndPoint , ServerType . ReplicaSetGhost ,
252- hosts : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
253+ hosts : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
253254
254255 var description = subject . Description ;
255256 description . Servers . Should ( ) . BeEquivalentTo ( GetDescriptions ( _firstEndPoint ) ) ;
@@ -289,7 +290,7 @@ public void Should_remove_server_from_the_seed_list_that_is_not_in_the_hosts_lis
289290 }
290291
291292 [ Test ]
292- public void Should_invalidate_existing_primary_when_a_new_primary_shows_up ( )
293+ public void Should_invalidate_existing_primary_when_a_new_primary_shows_up_and_current_election_id_is_null ( )
293294 {
294295 _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
295296
@@ -305,6 +306,57 @@ public void Should_invalidate_existing_primary_when_a_new_primary_shows_up()
305306 . Concat ( GetDescriptions ( _secondEndPoint , _thirdEndPoint ) ) ) ;
306307 }
307308
309+ [ Test ]
310+ public void Should_invalidate_existing_primary_when_a_new_primary_shows_up_with_an_election_id_and_current_id_is_null ( )
311+ {
312+ _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
313+
314+ var subject = CreateSubject ( ) ;
315+ subject . Initialize ( ) ;
316+
317+ PublishDescription ( _firstEndPoint , ServerType . ReplicaSetPrimary ) ;
318+ PublishDescription ( _secondEndPoint , ServerType . ReplicaSetPrimary , electionId : new ElectionId ( ObjectId . GenerateNewId ( ) ) ) ;
319+
320+ var description = subject . Description ;
321+ description . Servers . Should ( ) . BeEquivalentTo (
322+ new [ ] { GetDisconnectedDescription ( _firstEndPoint ) }
323+ . Concat ( GetDescriptions ( _secondEndPoint , _thirdEndPoint ) ) ) ;
324+ }
325+
326+ [ Test ]
327+ public void Should_invalidate_existing_primary_when_a_new_primary_shows_up_with_a_higher_election_id ( )
328+ {
329+ _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
330+
331+ var subject = CreateSubject ( ) ;
332+ subject . Initialize ( ) ;
333+
334+ PublishDescription ( _firstEndPoint , ServerType . ReplicaSetPrimary , electionId : new ElectionId ( ObjectId . Empty ) ) ;
335+ PublishDescription ( _secondEndPoint , ServerType . ReplicaSetPrimary , electionId : new ElectionId ( ObjectId . GenerateNewId ( ) ) ) ;
336+
337+ var description = subject . Description ;
338+ description . Servers . Should ( ) . BeEquivalentTo (
339+ new [ ] { GetDisconnectedDescription ( _firstEndPoint ) }
340+ . Concat ( GetDescriptions ( _secondEndPoint , _thirdEndPoint ) ) ) ;
341+ }
342+
343+ [ Test ]
344+ public void Should_invalidate_new_primary_when_it_shows_up_with_a_lesser_election_id ( )
345+ {
346+ _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
347+
348+ var subject = CreateSubject ( ) ;
349+ subject . Initialize ( ) ;
350+
351+ PublishDescription ( _firstEndPoint , ServerType . ReplicaSetPrimary , electionId : new ElectionId ( ObjectId . GenerateNewId ( ) ) ) ;
352+ PublishDescription ( _secondEndPoint , ServerType . ReplicaSetPrimary , electionId : new ElectionId ( ObjectId . Empty ) ) ;
353+
354+ var description = subject . Description ;
355+ description . Servers . Should ( ) . BeEquivalentTo (
356+ new [ ] { GetDisconnectedDescription ( _secondEndPoint ) }
357+ . Concat ( GetDescriptions ( _firstEndPoint , _thirdEndPoint ) ) ) ;
358+ }
359+
308360 [ Test ]
309361 public void Should_ignore_a_notification_from_a_server_which_has_been_removed ( )
310362 {
@@ -314,7 +366,7 @@ public void Should_ignore_a_notification_from_a_server_which_has_been_removed()
314366 subject . Initialize ( ) ;
315367
316368 PublishDescription ( _firstEndPoint , ServerType . ReplicaSetPrimary ,
317- hosts : new [ ] { _firstEndPoint , _secondEndPoint } ) ;
369+ hosts : new [ ] { _firstEndPoint , _secondEndPoint } ) ;
318370
319371 PublishDescription ( _thirdEndPoint , ServerType . ReplicaSetPrimary ) ;
320372
@@ -335,7 +387,7 @@ public void Should_call_initialize_on_all_servers()
335387 PublishDescription ( _firstEndPoint , ServerType . ReplicaSetPrimary ,
336388 hosts : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
337389
338- foreach ( var endPoint in new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } )
390+ foreach ( var endPoint in new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } )
339391 {
340392 var server = _serverFactory . GetServer ( endPoint ) ;
341393 server . Received ( ) . Initialize ( ) ;
@@ -397,7 +449,7 @@ private void PublishDisconnectedDescription(EndPoint endPoint)
397449 _serverFactory . PublishDescription ( description ) ;
398450 }
399451
400- private void PublishDescription ( EndPoint endPoint , ServerType serverType , IEnumerable < EndPoint > hosts = null , string setName = null , EndPoint primary = null )
452+ private void PublishDescription ( EndPoint endPoint , ServerType serverType , IEnumerable < EndPoint > hosts = null , string setName = null , EndPoint primary = null , ElectionId electionId = null )
401453 {
402454 var current = _serverFactory . GetServerDescription ( endPoint ) ;
403455
@@ -410,6 +462,7 @@ private void PublishDescription(EndPoint endPoint, ServerType serverType, IEnume
410462 var description = current . With (
411463 averageRoundTripTime : TimeSpan . FromMilliseconds ( 10 ) ,
412464 replicaSetConfig : serverType . IsReplicaSetMember ( ) ? config : null ,
465+ electionId : electionId ,
413466 state : ServerState . Connected ,
414467 tags : null ,
415468 type : serverType ,
0 commit comments