Skip to content

Commit da86c48

Browse files
committed
CDRIVER-2056: Parse zero values for localThresholdMS URI option
1 parent bf5e1d3 commit da86c48

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

src/mongoc/mongoc-topology.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "mongoc-topology-private.h"
2525
#include "mongoc-topology-description-apm-private.h"
2626
#include "mongoc-client-private.h"
27+
#include "mongoc-uri-private.h"
2728
#include "mongoc-util-private.h"
2829

2930
#include "utlist.h"
@@ -253,9 +254,7 @@ mongoc_topology_new (const mongoc_uri_t *uri,
253254
topology->uri, "serverselectiontimeoutms",
254255
MONGOC_TOPOLOGY_SERVER_SELECTION_TIMEOUT_MS);
255256

256-
topology->local_threshold_msec = mongoc_uri_get_option_as_int32 (
257-
topology->uri, "localthresholdms",
258-
MONGOC_TOPOLOGY_LOCAL_THRESHOLD_MS);
257+
topology->local_threshold_msec = mongoc_uri_get_local_threshold_option (topology->uri);
259258

260259
/* Total time allowed to check a server is connectTimeoutMS.
261260
* Server Discovery And Monitoring Spec:

src/mongoc/mongoc-uri-private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ bool
3939
mongoc_uri_parse_host ( mongoc_uri_t *uri,
4040
const char *str);
4141

42+
int32_t
43+
mongoc_uri_get_local_threshold_option (const mongoc_uri_t *uri);
44+
4245
BSON_END_DECLS
4346

4447

src/mongoc/mongoc-uri.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "mongoc-log.h"
3131
#include "mongoc-handshake-private.h"
3232
#include "mongoc-socket.h"
33+
#include "mongoc-topology-private.h"
3334
#include "mongoc-uri-private.h"
3435
#include "mongoc-read-concern-private.h"
3536
#include "mongoc-write-concern-private.h"
@@ -1216,6 +1217,24 @@ mongoc_uri_set_appname (mongoc_uri_t *uri,
12161217
return true;
12171218
}
12181219

1220+
/* can't use mongoc_uri_get_option_as_int32, it treats 0 specially */
1221+
int32_t
1222+
mongoc_uri_get_local_threshold_option (const mongoc_uri_t *uri)
1223+
{
1224+
const bson_t *options;
1225+
bson_iter_t iter;
1226+
int32_t retval = MONGOC_TOPOLOGY_LOCAL_THRESHOLD_MS;
1227+
1228+
if ((options = mongoc_uri_get_options (uri)) &&
1229+
bson_iter_init_find_case (&iter, options, "localthresholdms") &&
1230+
BSON_ITER_HOLDS_INT32 (&iter)) {
1231+
1232+
retval = bson_iter_int32 (&iter);
1233+
}
1234+
1235+
return retval;
1236+
}
1237+
12191238
const bson_t *
12201239
mongoc_uri_get_options (const mongoc_uri_t *uri)
12211240
{

tests/test-mongoc-uri.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <mongoc.h>
22

33
#include "mongoc-client-private.h"
4+
#include "mongoc-topology-private.h"
45
#include "mongoc-uri-private.h"
56
#include "mongoc-host-list-private.h"
67

@@ -415,7 +416,6 @@ test_mongoc_uri_functions (void)
415416
uri = mongoc_uri_new("mongodb://localhost/?serverselectiontimeoutms=3"
416417
"&journal=true"
417418
"&wtimeoutms=42"
418-
"&localthresholdms=17"
419419
"&canonicalizeHostname=false");
420420

421421
ASSERT_CMPINT(mongoc_uri_get_option_as_int32(uri, "serverselectiontimeoutms", 18), ==, 3);
@@ -426,10 +426,6 @@ test_mongoc_uri_functions (void)
426426
ASSERT(mongoc_uri_set_option_as_int32(uri, "wtimeoutms", 18));
427427
ASSERT_CMPINT(mongoc_uri_get_option_as_int32(uri, "wtimeoutms", 19), ==, 18);
428428

429-
ASSERT_CMPINT(mongoc_uri_get_option_as_int32(uri, "localthresholdms", 99), ==, 17);
430-
ASSERT(mongoc_uri_set_option_as_int32(uri, "localthresholdms", 99));
431-
ASSERT_CMPINT(mongoc_uri_get_option_as_int32(uri, "localthresholdms", 42), ==, 99);
432-
433429
/* socketcheckintervalms isn't set, return our fallback */
434430
ASSERT_CMPINT(mongoc_uri_get_option_as_int32(uri, "socketcheckintervalms", 123), ==, 123);
435431
ASSERT(mongoc_uri_set_option_as_int32(uri, "socketcheckintervalms", 18));
@@ -833,6 +829,7 @@ test_mongoc_uri_read_concern (void)
833829
mongoc_uri_destroy (uri);
834830
}
835831

832+
836833
static void
837834
test_mongoc_uri_long_hostname (void)
838835
{
@@ -882,6 +879,32 @@ test_mongoc_uri_long_hostname (void)
882879
bson_free (host_and_port);
883880
bson_free (host);
884881
}
882+
test_mongoc_uri_local_threshold_ms (void)
883+
{
884+
mongoc_uri_t *uri;
885+
886+
uri = mongoc_uri_new ("mongodb://localhost/");
887+
888+
/* localthresholdms isn't set, return the default */
889+
ASSERT_CMPINT (mongoc_uri_get_local_threshold_option (uri), ==,
890+
MONGOC_TOPOLOGY_LOCAL_THRESHOLD_MS);
891+
ASSERT (
892+
mongoc_uri_set_option_as_int32 (uri, "localthresholdms", 99));
893+
ASSERT_CMPINT (mongoc_uri_get_local_threshold_option (uri), ==, 99);
894+
895+
mongoc_uri_destroy(uri);
896+
897+
898+
uri = mongoc_uri_new ("mongodb://localhost/?localthresholdms=0");
899+
900+
ASSERT_CMPINT (mongoc_uri_get_local_threshold_option (uri), ==, 0);
901+
ASSERT (
902+
mongoc_uri_set_option_as_int32 (uri, "localthresholdms", 99));
903+
ASSERT_CMPINT (mongoc_uri_get_local_threshold_option (uri), ==, 99);
904+
905+
mongoc_uri_destroy(uri);
906+
}
907+
885908

886909
void
887910
test_uri_install (TestSuite *suite)
@@ -896,4 +919,5 @@ test_uri_install (TestSuite *suite)
896919
TestSuite_Add (suite, "/Uri/functions", test_mongoc_uri_functions);
897920
TestSuite_Add (suite, "/Uri/compound_setters", test_mongoc_uri_compound_setters);
898921
TestSuite_Add (suite, "/Uri/long_hostname", test_mongoc_uri_long_hostname);
922+
TestSuite_Add (suite, "/Uri/local_threshold_ms", test_mongoc_uri_local_threshold_ms);
899923
}

0 commit comments

Comments
 (0)