1
1
#include <fcntl.h>
2
2
#include <mongoc.h>
3
+ #include <mongoc-cluster-private.h>
3
4
4
5
#include "mongoc-cursor-private.h"
5
6
#include "mock-server.h"
@@ -504,25 +505,29 @@ test_unavailable_seeds(void)
504
505
505
506
506
507
/* CDRIVER-721 catch errors in _mongoc_cluster_destroy */
507
- static void
508
- test_large_seed_list (void )
508
+ static void
509
+ test_seed_list (bool rs ,
510
+ bool connect )
509
511
{
510
512
uint16_t port ;
511
513
char * uri_str ;
512
514
mongoc_uri_t * uri ;
513
515
mock_server_t * server ;
514
- int i ;
516
+ uint32_t i ;
517
+ uint32_t expected_nodes_len ;
515
518
const mongoc_host_list_t * hosts ;
516
519
mongoc_client_t * client ;
517
520
bson_error_t error ;
518
521
519
522
port = 20000 + (rand () % 1000 );
520
- uri_str = bson_strdup_printf ("mongodb://localhost:%hu,a,b,c/?replicaSet=rs" ,
521
- port );
523
+ uri_str = bson_strdup_printf ("mongodb://localhost:%hu,a,b,c/%s" ,
524
+ port ,
525
+ rs ? "?replicaSet=rs" : "" );
522
526
uri = mongoc_uri_new (uri_str );
523
527
hosts = mongoc_uri_get_hosts (uri );
528
+
524
529
server = mock_server_new_rs ("127.0.0.1" , port , NULL , NULL ,
525
- "rs" , hosts );
530
+ rs ? "rs" : NULL , hosts );
526
531
527
532
mock_server_run_in_thread (server );
528
533
@@ -532,16 +537,61 @@ test_large_seed_list(void)
532
537
533
538
client = mongoc_client_new_from_uri (uri );
534
539
assert (client );
535
- assert (_mongoc_client_warm_up (client , & error ));
540
+ ASSERT_CMPINT (4 , = = , client -> cluster .nodes_len );
541
+ for (i = 0 ; i < 4 ; i ++ ) {
542
+ assert (client -> cluster .nodes [i ].valid );
543
+ }
544
+
545
+ if (connect ) {
546
+ /* only localhost:port responds, nodes_len is set to 1 */
547
+ assert (_mongoc_client_warm_up (client , & error ));
548
+
549
+ /* a mongos load-balanced connection never removes down nodes */
550
+ expected_nodes_len = rs ? 1 : 4 ;
551
+ ASSERT_CMPINT (expected_nodes_len , = = , client -> cluster .nodes_len );
552
+ for (i = 0 ; i < expected_nodes_len ; i ++ ) {
553
+ assert (client -> cluster .nodes [i ].valid );
554
+ }
555
+ }
536
556
557
+ /* testing for crashes like CDRIVER-721 */
537
558
mongoc_client_destroy (client );
559
+
538
560
mock_server_quit (server , 0 );
539
561
mock_server_destroy (server );
540
562
mongoc_uri_destroy (uri );
541
563
bson_free (uri_str );
542
564
}
543
565
544
566
567
+ static void
568
+ test_rs_seeds_no_connect (void )
569
+ {
570
+ test_seed_list (true, false);
571
+ }
572
+
573
+
574
+ static void
575
+ test_rs_seeds_connect (void )
576
+ {
577
+ test_seed_list (true, true);
578
+ }
579
+
580
+
581
+ static void
582
+ test_mongos_seeds_no_connect (void )
583
+ {
584
+ test_seed_list (false, false);
585
+ }
586
+
587
+
588
+ static void
589
+ test_mongos_seeds_connect (void )
590
+ {
591
+ test_seed_list (false, true);
592
+ }
593
+
594
+
545
595
static void
546
596
test_exhaust_cursor (void )
547
597
{
@@ -781,7 +831,10 @@ test_client_install (TestSuite *suite)
781
831
TestSuite_Add (suite , "/Client/command_secondary" , test_mongoc_client_command_secondary );
782
832
TestSuite_Add (suite , "/Client/preselect" , test_mongoc_client_preselect );
783
833
TestSuite_Add (suite , "/Client/unavailable_seeds" , test_unavailable_seeds );
784
- TestSuite_Add (suite , "/Client/large_seed_list" , test_large_seed_list );
834
+ TestSuite_Add (suite , "/Client/rs_seeds_no_connect" , test_rs_seeds_no_connect );
835
+ TestSuite_Add (suite , "/Client/rs_seeds_connect" , test_rs_seeds_connect );
836
+ TestSuite_Add (suite , "/Client/mongos_seeds_no_connect" , test_mongos_seeds_no_connect );
837
+ TestSuite_Add (suite , "/Client/mongos_seeds_connect" , test_mongos_seeds_connect );
785
838
TestSuite_Add (suite , "/Client/exhaust_cursor" , test_exhaust_cursor );
786
839
TestSuite_Add (suite , "/Client/server_status" , test_server_status );
787
840
}
0 commit comments