21
21
#include "TestSuite.h"
22
22
23
23
static char *
24
- loadbalanced_uri (void )
24
+ loadbalanced_single_uri (void )
25
25
{
26
26
/* TODO (CDRIVER-4062): This will need to add TLS and auth to the URI when
27
27
* run in evergreen. */
@@ -33,7 +33,7 @@ test_loadbalanced_sessions_supported (void *unused)
33
33
{
34
34
mongoc_client_t * client ;
35
35
mongoc_client_session_t * session ;
36
- char * uristr = loadbalanced_uri ();
36
+ char * uristr = loadbalanced_single_uri ();
37
37
bson_error_t error ;
38
38
39
39
client = mongoc_client_new (uristr );
@@ -50,7 +50,7 @@ test_loadbalanced_sessions_do_not_expire (void *unused)
50
50
{
51
51
mongoc_client_t * client ;
52
52
mongoc_client_session_t * session ;
53
- char * uristr = loadbalanced_uri ();
53
+ char * uristr = loadbalanced_single_uri ();
54
54
bson_error_t error ;
55
55
bson_t * lsid1 ;
56
56
bson_t * lsid2 ;
@@ -79,6 +79,102 @@ test_loadbalanced_sessions_do_not_expire (void *unused)
79
79
mongoc_client_destroy (client );
80
80
}
81
81
82
+ typedef struct {
83
+ int topology_changed ;
84
+ int server_changed ;
85
+ int heartbeat_started ;
86
+ int heartbeat_succeeded ;
87
+ int heartbeat_failed ;
88
+ } sdam_event_counters_t ;
89
+
90
+ static void server_changed (mongoc_apm_server_changed_t * event ) {
91
+ sdam_event_counters_t * counters = (sdam_event_counters_t * ) mongoc_apm_server_changed_get_context (event );
92
+ counters -> server_changed ++ ;
93
+ }
94
+ static void topology_changed (mongoc_apm_topology_changed_t * event ) {
95
+ sdam_event_counters_t * counters = (sdam_event_counters_t * ) mongoc_apm_topology_changed_get_context (event );
96
+ counters -> topology_changed ++ ;
97
+ }
98
+ static void heartbeat_started (mongoc_apm_server_heartbeat_started_t * event ) {
99
+ sdam_event_counters_t * counters = (sdam_event_counters_t * ) mongoc_apm_server_heartbeat_started_get_context (event );
100
+ counters -> heartbeat_started ++ ;
101
+ }
102
+ static void heartbeat_failed (mongoc_apm_server_heartbeat_failed_t * event ) {
103
+ sdam_event_counters_t * counters = (sdam_event_counters_t * ) mongoc_apm_server_heartbeat_failed_get_context (event );
104
+ counters -> heartbeat_failed ++ ;
105
+ }
106
+ static void heartbeat_succeeded (mongoc_apm_server_heartbeat_succeeded_t * event ) {
107
+ sdam_event_counters_t * counters = (sdam_event_counters_t * ) mongoc_apm_server_heartbeat_succeeded_get_context (event );
108
+ counters -> heartbeat_succeeded ++ ;
109
+ }
110
+
111
+ static mongoc_apm_callbacks_t * make_callbacks (void ) {
112
+ mongoc_apm_callbacks_t * callbacks ;
113
+
114
+ callbacks = mongoc_apm_callbacks_new ();
115
+ mongoc_apm_set_server_changed_cb (callbacks , server_changed );
116
+ mongoc_apm_set_topology_changed_cb (callbacks , topology_changed );
117
+ mongoc_apm_set_server_heartbeat_started_cb (callbacks , heartbeat_started );
118
+ mongoc_apm_set_server_heartbeat_failed_cb (callbacks , heartbeat_failed );
119
+ mongoc_apm_set_server_heartbeat_succeeded_cb (callbacks , heartbeat_succeeded );
120
+ return callbacks ;
121
+ }
122
+
123
+ /* Test that connection to a load balanced cluster is possible, and the topology does not change after a connection is created. */
124
+ static void
125
+ test_loadbalanced_ping_single (void * unused ) {
126
+ mongoc_client_t * client ;
127
+ mongoc_client_session_t * session ;
128
+ char * uristr = loadbalanced_single_uri ();
129
+ bson_error_t error ;
130
+ mongoc_server_description_t * sd ;
131
+ mongoc_apm_callbacks_t * callbacks ;
132
+ sdam_event_counters_t counters = {0 };
133
+ bool ret ;
134
+
135
+ client = mongoc_client_new (uristr );
136
+ callbacks = make_callbacks ();
137
+ mongoc_client_set_apm_callbacks (client , callbacks , & counters );
138
+
139
+ sd = mongoc_client_select_server (client , true, NULL /* read prefs */ , & error );
140
+ ASSERT_OR_PRINT (sd != NULL , error );
141
+
142
+ /* There are two topology changed events:
143
+ * - The initial changed event
144
+ * - The server transition of Unknown => LoadBalancer */
145
+ ASSERT_CMPINT (counters .topology_changed , = = , 2 );
146
+ ASSERT_CMPINT (counters .server_changed , = = , 1 );
147
+ /* Selecting a server does NOT open a new connection in load balanced mode. */
148
+ ASSERT_CMPINT (counters .heartbeat_started , = = , 0 );
149
+
150
+ // LBTODO: this is where I left off.
151
+ // The server description is updated by monitoring and includes the wire version.
152
+ // If we skip monitoring and force the server description, how do we update the wire version from the handshake?
153
+ /* The first operation requiring I/O opens a connection. */
154
+ ret = mongoc_client_command_simple (client , "admin" , tmp_bson ("{'ping': 1}" ), NULL /* read prefs */ , NULL /* reply */ , & error );
155
+ ASSERT_OR_PRINT (ret , error );
156
+
157
+ /* No topology changes should have occurred */
158
+ ASSERT_CMPINT (counters .topology_changed , = = , 2 );
159
+ ASSERT_CMPINT (counters .server_changed , = = , 1 );
160
+ /* The new connection should have produced a heartbeat event. */
161
+ ASSERT_CMPINT (counters .heartbeat_started , = = , 1 );
162
+ ASSERT_CMPINT (counters .heartbeat_succeeded , = = , 1 );
163
+
164
+ bson_free (uristr );
165
+ mongoc_server_description_destroy (sd );
166
+ mongoc_apm_callbacks_destroy (callbacks );
167
+ mongoc_client_destroy (client );
168
+ }
169
+
170
+ /* Test that the handshake reply does not update the topology in load balanced mode. */
171
+ static void
172
+ test_loadbalanced_handshake_does_not_update (void * unused );
173
+
174
+ /* Test that re-establishing a connection after a network error does not wait for the cooldown period in single threaded mode. */
175
+ static void
176
+ test_loadbalanced_no_cooldown_on_reconnect (void * unused );
177
+
82
178
static int
83
179
skip_if_not_loadbalanced (void )
84
180
{
@@ -104,4 +200,10 @@ test_loadbalanced_install (TestSuite *suite)
104
200
NULL /* ctx */ ,
105
201
NULL /* dtor */ ,
106
202
skip_if_not_loadbalanced );
203
+ TestSuite_AddFull (suite ,
204
+ "/loadbalanced/ping/single" ,
205
+ test_loadbalanced_ping_single ,
206
+ NULL /* ctx */ ,
207
+ NULL /* dtor */ ,
208
+ skip_if_not_loadbalanced );
107
209
}
0 commit comments