Skip to content

Commit 06dcffb

Browse files
committed
Test fallback to legacy hello after connection is closed
1 parent 7d58f04 commit 06dcffb

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,7 @@ _test_hello_ok (bool pooled)
23132313
mongoc_client_pool_t *pool;
23142314
mongoc_client_t *client;
23152315
char *hello;
2316+
char *hello_not_ok;
23162317
future_t *future;
23172318
request_t *request;
23182319
bson_error_t error;
@@ -2338,6 +2339,14 @@ _test_hello_ok (bool pooled)
23382339
" 'hosts': ['%s']}",
23392340
mock_server_get_host_and_port (server));
23402341

2342+
hello_not_ok = bson_strdup_printf ("{'ok': 1,"
2343+
" 'isWritablePrimary': true,"
2344+
" 'setName': 'rs',"
2345+
" 'minWireVersion': 2,"
2346+
" 'maxWireVersion': 5,"
2347+
" 'hosts': ['%s']}",
2348+
mock_server_get_host_and_port (server));
2349+
23412350
/* For client pools, the first handshake happens when the client is popped.
23422351
* For non-pooled clients, send a ping command to trigger a handshake. */
23432352
if (!pooled) {
@@ -2366,9 +2375,40 @@ _test_hello_ok (bool pooled)
23662375
client, "admin", tmp_bson ("{'ping': 1}"), NULL, NULL, &error);
23672376
}
23682377

2378+
/* Hang up to ensure that the next check runs legacy hello again */
23692379
request = mock_server_receives_hello (server);
23702380
BSON_ASSERT (request);
2371-
mock_server_replies_simple (request, hello);
2381+
mock_server_hangs_up (request);
2382+
request_destroy (request);
2383+
2384+
/* The previous failure will trigger another handshake using legacy hello */
2385+
request = mock_server_receives_legacy_hello (
2386+
server, "{'" HANDSHAKE_CMD_LEGACY_HELLO "': 1, 'helloOk': true}");
2387+
BSON_ASSERT (request);
2388+
mock_server_replies_simple (request, hello_not_ok);
2389+
request_destroy (request);
2390+
2391+
/* Once again, handle the ping */
2392+
if (!pooled) {
2393+
request = mock_server_receives_command (
2394+
server, "admin", MONGOC_QUERY_SECONDARY_OK, "{'ping': 1}");
2395+
mock_server_replies_ok_and_destroys (request);
2396+
BSON_ASSERT (future_get_bool (future));
2397+
future_destroy (future);
2398+
2399+
/* Send off another ping for non-pooled clients, making sure to wait long
2400+
* enough to require another heartbeat. */
2401+
_mongoc_usleep (600 * 1000);
2402+
future = future_client_command_simple (
2403+
client, "admin", tmp_bson ("{'ping': 1}"), NULL, NULL, &error);
2404+
}
2405+
2406+
/* Since we never responded with helloOk: true, we're expecting another
2407+
* legacy hello. */
2408+
request = mock_server_receives_legacy_hello (
2409+
server, "{'" HANDSHAKE_CMD_LEGACY_HELLO "': 1, 'helloOk': true}");
2410+
BSON_ASSERT (request);
2411+
mock_server_replies_simple (request, hello_not_ok);
23722412
request_destroy (request);
23732413

23742414
/* Once again, handle the ping */
@@ -2390,6 +2430,7 @@ _test_hello_ok (bool pooled)
23902430
mongoc_uri_destroy (uri);
23912431
mock_server_destroy (server);
23922432
bson_free (hello);
2433+
bson_free (hello_not_ok);
23932434
}
23942435

23952436
static void

0 commit comments

Comments
 (0)