27
27
using MongoDB . Driver . Core . Helpers ;
28
28
using NSubstitute ;
29
29
using NUnit . Framework ;
30
+ using MongoDB . Bson ;
30
31
31
32
namespace MongoDB . Driver . Core . Clusters
32
33
{
@@ -73,7 +74,7 @@ public void Constructor_should_throw_if_cluster_connection_mode_is_not_supported
73
74
[ Test ]
74
75
public void Description_should_be_correct_after_initialization ( )
75
76
{
76
- _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint } ) ;
77
+ _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint } ) ;
77
78
78
79
var subject = CreateSubject ( ) ;
79
80
subject . Initialize ( ) ;
@@ -87,7 +88,7 @@ public void Description_should_be_correct_after_initialization()
87
88
[ Test ]
88
89
public void Initialize_should_throw_when_already_disposed ( )
89
90
{
90
- _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint } ) ;
91
+ _settings = _settings . With ( endPoints : new [ ] { _firstEndPoint } ) ;
91
92
var subject = CreateSubject ( ) ;
92
93
subject . Dispose ( ) ;
93
94
@@ -137,7 +138,7 @@ public void Should_remove_a_server_that_is_no_longer_in_the_primary_host_list()
137
138
subject . Initialize ( ) ;
138
139
139
140
PublishDescription ( _firstEndPoint , ServerType . ReplicaSetPrimary ,
140
- hosts : new [ ] { _firstEndPoint , _secondEndPoint } ) ;
141
+ hosts : new [ ] { _firstEndPoint , _secondEndPoint } ) ;
141
142
142
143
var description = subject . Description ;
143
144
description . State . Should ( ) . Be ( ClusterState . Connected ) ;
@@ -249,7 +250,7 @@ public void Should_ignore_changes_from_a_ReplicaSetGhost()
249
250
subject . Initialize ( ) ;
250
251
251
252
PublishDescription ( _firstEndPoint , ServerType . ReplicaSetGhost ,
252
- hosts : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
253
+ hosts : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
253
254
254
255
var description = subject . Description ;
255
256
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
289
290
}
290
291
291
292
[ 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 ( )
293
294
{
294
295
_settings = _settings . With ( endPoints : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
295
296
@@ -305,6 +306,57 @@ public void Should_invalidate_existing_primary_when_a_new_primary_shows_up()
305
306
. Concat ( GetDescriptions ( _secondEndPoint , _thirdEndPoint ) ) ) ;
306
307
}
307
308
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
+
308
360
[ Test ]
309
361
public void Should_ignore_a_notification_from_a_server_which_has_been_removed ( )
310
362
{
@@ -314,7 +366,7 @@ public void Should_ignore_a_notification_from_a_server_which_has_been_removed()
314
366
subject . Initialize ( ) ;
315
367
316
368
PublishDescription ( _firstEndPoint , ServerType . ReplicaSetPrimary ,
317
- hosts : new [ ] { _firstEndPoint , _secondEndPoint } ) ;
369
+ hosts : new [ ] { _firstEndPoint , _secondEndPoint } ) ;
318
370
319
371
PublishDescription ( _thirdEndPoint , ServerType . ReplicaSetPrimary ) ;
320
372
@@ -335,7 +387,7 @@ public void Should_call_initialize_on_all_servers()
335
387
PublishDescription ( _firstEndPoint , ServerType . ReplicaSetPrimary ,
336
388
hosts : new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } ) ;
337
389
338
- foreach ( var endPoint in new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } )
390
+ foreach ( var endPoint in new [ ] { _firstEndPoint , _secondEndPoint , _thirdEndPoint } )
339
391
{
340
392
var server = _serverFactory . GetServer ( endPoint ) ;
341
393
server . Received ( ) . Initialize ( ) ;
@@ -397,7 +449,7 @@ private void PublishDisconnectedDescription(EndPoint endPoint)
397
449
_serverFactory . PublishDescription ( description ) ;
398
450
}
399
451
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 )
401
453
{
402
454
var current = _serverFactory . GetServerDescription ( endPoint ) ;
403
455
@@ -410,6 +462,7 @@ private void PublishDescription(EndPoint endPoint, ServerType serverType, IEnume
410
462
var description = current . With (
411
463
averageRoundTripTime : TimeSpan . FromMilliseconds ( 10 ) ,
412
464
replicaSetConfig : serverType . IsReplicaSetMember ( ) ? config : null ,
465
+ electionId : electionId ,
413
466
state : ServerState . Connected ,
414
467
tags : null ,
415
468
type : serverType ,
0 commit comments