Skip to content

Commit 04b90bf

Browse files
committed
CDRIVER-721 test rs and mongos, connected and not
1 parent 7cb06d3 commit 04b90bf

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

tests/test-mongoc-client.c

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <fcntl.h>
22
#include <mongoc.h>
3+
#include <mongoc-cluster-private.h>
34

45
#include "mongoc-cursor-private.h"
56
#include "mock-server.h"
@@ -504,25 +505,29 @@ test_unavailable_seeds(void)
504505

505506

506507
/* 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)
509511
{
510512
uint16_t port;
511513
char *uri_str;
512514
mongoc_uri_t *uri;
513515
mock_server_t *server;
514-
int i;
516+
uint32_t i;
517+
uint32_t expected_nodes_len;
515518
const mongoc_host_list_t *hosts;
516519
mongoc_client_t *client;
517520
bson_error_t error;
518521

519522
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" : "");
522526
uri = mongoc_uri_new (uri_str);
523527
hosts = mongoc_uri_get_hosts (uri);
528+
524529
server = mock_server_new_rs ("127.0.0.1", port, NULL, NULL,
525-
"rs", hosts);
530+
rs ? "rs" : NULL, hosts);
526531

527532
mock_server_run_in_thread (server);
528533

@@ -532,16 +537,61 @@ test_large_seed_list(void)
532537

533538
client = mongoc_client_new_from_uri (uri);
534539
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+
}
536556

557+
/* testing for crashes like CDRIVER-721 */
537558
mongoc_client_destroy (client);
559+
538560
mock_server_quit (server, 0);
539561
mock_server_destroy (server);
540562
mongoc_uri_destroy (uri);
541563
bson_free (uri_str);
542564
}
543565

544566

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+
545595
static void
546596
test_exhaust_cursor (void)
547597
{
@@ -781,7 +831,10 @@ test_client_install (TestSuite *suite)
781831
TestSuite_Add (suite, "/Client/command_secondary", test_mongoc_client_command_secondary);
782832
TestSuite_Add (suite, "/Client/preselect", test_mongoc_client_preselect);
783833
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);
785838
TestSuite_Add (suite, "/Client/exhaust_cursor", test_exhaust_cursor);
786839
TestSuite_Add (suite, "/Client/server_status", test_server_status);
787840
}

0 commit comments

Comments
 (0)