Skip to content

Commit b93e345

Browse files
authored
CDRIVER-4184 don't emit closed on invalid topology (#884)
1 parent 3b9f212 commit b93e345

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

src/libmongoc/src/mongoc/mongoc-topology.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,12 @@ mongoc_topology_destroy (mongoc_topology_t *topology)
617617
bson_mutex_destroy (&topology->apm_mutex);
618618
mongoc_cond_destroy (&topology->srv_polling_cond);
619619
}
620-
_mongoc_topology_description_monitor_closed (&topology->description);
620+
621+
if (topology->valid) {
622+
/* Do not emit a topology_closed event. A topology opening event was not
623+
* emitted. */
624+
_mongoc_topology_description_monitor_closed (&topology->description);
625+
}
621626

622627
mongoc_uri_destroy (topology->uri);
623628
mongoc_topology_description_cleanup (&topology->description);

src/libmongoc/tests/test-mongoc-dns.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,127 @@ prose_test_9_pooled (void *unused)
728728
_prose_test_9 (true);
729729
}
730730

731+
/* cb_stats_t tracks counters for the test_invalid_topology_pooled and
732+
* test_invalid_topology_single tests. */
733+
typedef struct {
734+
int num_topology_opening;
735+
int num_topology_closed;
736+
int num_server_opening;
737+
int num_server_closed;
738+
} cb_stats_t;
739+
740+
/* invalid_topology_opening is used as a callback for the
741+
* test_invalid_topology_pooled and test_invalid_topology_single tests. */
742+
static void
743+
invalid_topology_opening (const mongoc_apm_topology_opening_t *event) {
744+
cb_stats_t *stats = (cb_stats_t*) mongoc_apm_topology_opening_get_context (event);
745+
stats->num_topology_opening++;
746+
}
747+
748+
/* invalid_topology_closed is used as a callback for the
749+
* test_invalid_topology_pooled and test_invalid_topology_single tests. */
750+
static void
751+
invalid_topology_closed (const mongoc_apm_topology_closed_t *event) {
752+
cb_stats_t *stats = (cb_stats_t*) mongoc_apm_topology_closed_get_context (event);
753+
stats->num_topology_closed++;
754+
}
755+
756+
/* invalid_server_closed is used as a callback for the
757+
* test_invalid_topology_pooled and test_invalid_topology_single tests. */
758+
static void
759+
invalid_server_closed (const mongoc_apm_server_closed_t *event) {
760+
cb_stats_t *stats = (cb_stats_t*) mongoc_apm_server_closed_get_context (event);
761+
stats->num_server_closed++;
762+
}
763+
764+
/* invalid_server_opening is used as a callback for the
765+
* test_invalid_topology_pooled and test_invalid_topology_single tests. */
766+
static void
767+
invalid_server_opening (const mongoc_apm_server_opening_t *event) {
768+
cb_stats_t *stats = (cb_stats_t*) mongoc_apm_server_opening_get_context (event);
769+
stats->num_server_opening++;
770+
}
771+
772+
/* CDRIVER-4184 Test that an invalid topology does not emit a topology_closed
773+
* event. */
774+
static void
775+
test_invalid_topology_pooled (void *unused) {
776+
mongoc_client_pool_t *pool;
777+
mongoc_client_t *client;
778+
mongoc_uri_t *uri;
779+
mongoc_apm_callbacks_t *cbs;
780+
cb_stats_t stats = {0};
781+
782+
/* TXT record for test20.test.build.10gen.cc resolves to "loadBalanced=true". */
783+
uri = mongoc_uri_new ("mongodb+srv://test20.test.build.10gen.cc/?replicaSet=rs0");
784+
pool = mongoc_client_pool_new (uri);
785+
cbs = mongoc_apm_callbacks_new ();
786+
mongoc_apm_set_topology_opening_cb (cbs, invalid_topology_opening);
787+
mongoc_apm_set_topology_closed_cb (cbs, invalid_topology_closed);
788+
mongoc_apm_set_server_opening_cb (cbs, invalid_server_opening);
789+
mongoc_apm_set_server_closed_cb (cbs, invalid_server_closed);
790+
mongoc_client_pool_set_apm_callbacks (pool, cbs, &stats);
791+
792+
ASSERT_CMPINT(stats.num_topology_opening, ==, 0);
793+
794+
/* Pop a client to attempt to start monitoring. Monitoring emits the topology_opening event on valid topologies. */
795+
client = mongoc_client_pool_pop (pool);
796+
mongoc_client_pool_push (pool, client);
797+
798+
mongoc_apm_callbacks_destroy (cbs);
799+
mongoc_client_pool_destroy (pool);
800+
mongoc_uri_destroy (uri);
801+
802+
ASSERT_CMPINT(stats.num_topology_opening, ==, 0);
803+
ASSERT_CMPINT(stats.num_server_opening, ==, 0);
804+
ASSERT_CMPINT(stats.num_server_closed, ==, 0);
805+
ASSERT_CMPINT(stats.num_topology_closed, ==, 0);
806+
}
807+
808+
/* CDRIVER-4184 Test that an invalid topology does not emit a topology_closed
809+
* event. */
810+
static void
811+
test_invalid_topology_single (void *unused) {
812+
mongoc_client_t *client;
813+
mongoc_uri_t *uri;
814+
mongoc_apm_callbacks_t *cbs;
815+
cb_stats_t stats = {0};
816+
bson_error_t error;
817+
mongoc_server_description_t *sd;
818+
819+
/* TXT records for test20.test.build.10gen.cc resolve to loadBalanced=true. */
820+
uri = mongoc_uri_new ("mongodb+srv://test20.test.build.10gen.cc/?replicaSet=true");
821+
client = mongoc_client_new_from_uri (uri);
822+
cbs = mongoc_apm_callbacks_new ();
823+
mongoc_apm_set_topology_opening_cb (cbs, invalid_topology_opening);
824+
mongoc_apm_set_topology_closed_cb (cbs, invalid_topology_closed);
825+
mongoc_apm_set_server_opening_cb (cbs, invalid_server_opening);
826+
mongoc_apm_set_server_closed_cb (cbs, invalid_server_closed);
827+
mongoc_client_set_apm_callbacks (client, cbs, &stats);
828+
829+
ASSERT_CMPINT(stats.num_topology_opening, ==, 0);
830+
831+
/* Perform server selection. Server selection emits the topology_opening
832+
* event on valid topologies. */
833+
sd = mongoc_client_select_server (
834+
client, false /* for_writes */, NULL /* read_prefs */, &error);
835+
ASSERT_ERROR_CONTAINS (
836+
error,
837+
MONGOC_ERROR_SERVER_SELECTION,
838+
MONGOC_ERROR_SERVER_SELECTION_FAILURE,
839+
"URI with \"loadbalanced\" enabled must not contain option \"replicaset\"");
840+
ASSERT (!sd);
841+
842+
mongoc_apm_callbacks_destroy (cbs);
843+
mongoc_client_destroy (client);
844+
mongoc_uri_destroy (uri);
845+
846+
ASSERT_CMPINT(stats.num_topology_opening, ==, 0);
847+
ASSERT_CMPINT(stats.num_server_opening, ==, 0);
848+
ASSERT_CMPINT(stats.num_server_closed, ==, 0);
849+
ASSERT_CMPINT(stats.num_topology_closed, ==, 0);
850+
}
851+
731852
void
732853
test_dns_install (TestSuite *suite)
733854
{
@@ -770,4 +891,20 @@ test_dns_install (TestSuite *suite)
770891
NULL,
771892
NULL,
772893
test_dns_check_loadbalanced);
894+
895+
TestSuite_AddFull (
896+
suite,
897+
"/initial_dns_seedlist_discovery/load-balanced/invalid_topology/pooled",
898+
test_invalid_topology_pooled,
899+
NULL,
900+
NULL,
901+
test_dns_check_loadbalanced);
902+
903+
TestSuite_AddFull (
904+
suite,
905+
"/initial_dns_seedlist_discovery/load-balanced/invalid_topology/single",
906+
test_invalid_topology_single,
907+
NULL,
908+
NULL,
909+
test_dns_check_loadbalanced);
773910
}

0 commit comments

Comments
 (0)