Skip to content

Commit 05a6bcd

Browse files
committed
CDRIVER-897 report error or timeout from ismaster
1 parent bef2b38 commit 05a6bcd

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/mongoc/mongoc-topology-scanner.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ mongoc_topology_scanner_ismaster_handler (mongoc_async_cmd_result_t async_status
256256
{
257257
mongoc_topology_scanner_node_t *node;
258258
int64_t now;
259+
const char *message;
259260

260261
bson_return_if_fail (data);
261262

@@ -275,6 +276,15 @@ mongoc_topology_scanner_ismaster_handler (mongoc_async_cmd_result_t async_status
275276
mongoc_stream_failed (node->stream);
276277
node->stream = NULL;
277278
node->last_failed = now;
279+
message = async_status == MONGOC_ASYNC_CMD_TIMEOUT ?
280+
"connection error" :
281+
"connection timeout";
282+
bson_set_error (&node->last_error,
283+
MONGOC_ERROR_CLIENT,
284+
MONGOC_ERROR_STREAM_CONNECT,
285+
"%s calling ismaster on \'%s\'",
286+
message,
287+
node->host.host_and_port);
278288
} else {
279289
node->last_failed = -1;
280290
}

tests/test-mongoc-topology.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "mock_server/mock-server.h"
1111
#include "mock_server/future.h"
1212
#include "mock_server/future-functions.h"
13+
#include "test-conveniences.h"
1314

1415
#undef MONGOC_LOG_DOMAIN
1516
#define MONGOC_LOG_DOMAIN "topology-test"
@@ -725,6 +726,37 @@ test_connect_timeout_try_once_false(void)
725726
}
726727

727728

729+
static void
730+
test_multiple_selection_errors (void)
731+
{
732+
const char *uri = "mongodb://doesntexist,example.com:2/?replicaSet=rs"
733+
"&connectTimeoutMS=100";
734+
mongoc_client_t *client;
735+
bson_t reply;
736+
bson_error_t error;
737+
738+
client = mongoc_client_new (uri);
739+
mongoc_client_command_simple (client, "test", tmp_bson ("{'ping': 1}"),
740+
NULL, &reply, &error);
741+
742+
ASSERT_CMPINT (MONGOC_ERROR_SERVER_SELECTION, ==, error.domain);
743+
ASSERT_CMPINT (MONGOC_ERROR_SERVER_SELECTION_FAILURE, ==, error.code);
744+
745+
/* Like:
746+
* "No suitable servers found (`serverselectiontryonce` set):
747+
* [Failed to resolve 'doesntexist'] [connection error]"
748+
*/
749+
ASSERT_CONTAINS (error.message,
750+
"No suitable servers found");
751+
ASSERT_CONTAINS (error.message,
752+
"[connection error calling ismaster on 'example.com:2']");
753+
ASSERT_CONTAINS (error.message,
754+
"[Failed to resolve 'doesntexist']");
755+
756+
mongoc_client_destroy (client);
757+
}
758+
759+
728760
void
729761
test_topology_install (TestSuite *suite)
730762
{
@@ -741,4 +773,5 @@ test_topology_install (TestSuite *suite)
741773
TestSuite_Add (suite, "/Topology/connect_timeout/pooled", test_connect_timeout_pooled);
742774
TestSuite_Add (suite, "/Topology/connect_timeout/single/try_once", test_connect_timeout_single);
743775
TestSuite_Add (suite, "/Topology/connect_timeout/single/try_once_false", test_connect_timeout_try_once_false);
776+
TestSuite_Add (suite, "/Topology/multiple_selection_errors", test_multiple_selection_errors);
744777
}

0 commit comments

Comments
 (0)