Skip to content

Commit ec47e54

Browse files
committed
MONGOC_TEST_FUTURE_TIMEOUT_MS environment variable
Prevents mock server test framework from timing out when you are stopped at a breakpoint.
1 parent 7415d34 commit ec47e54

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

build/future_function_templates/future.c.template

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
#include "mongoc-array-private.h"
44
#include "mongoc-thread-private.h"
55
#include "future.h"
6+
#include "../test-libmongoc.h"
67

78
{{ header_comment }}
89

9-
#define FUTURE_TIMEOUT_MS 10 * 1000
10+
#define DEFAULT_FUTURE_TIMEOUT_MS 10 * 1000
11+
12+
static int64_t
13+
get_future_timeout_ms ()
14+
{
15+
return test_framework_getenv_int64 ("MONGOC_TEST_FUTURE_TIMEOUT_MS",
16+
DEFAULT_FUTURE_TIMEOUT_MS);
17+
}
1018

1119
void
1220
future_get_void (future_t *future)
@@ -79,13 +87,14 @@ future_resolve (future_t *future, future_value_t return_value)
7987
bool
8088
future_wait (future_t *future)
8189
{
82-
/* TODO: configurable timeout */
83-
int64_t deadline = bson_get_monotonic_time () + FUTURE_TIMEOUT_MS * 1000;
90+
int64_t deadline = bson_get_monotonic_time () + get_future_timeout_ms ();
8491
bool resolved;
8592

8693
mongoc_mutex_lock (&future->mutex);
8794
while (!future->resolved && bson_get_monotonic_time () <= deadline) {
88-
mongoc_cond_timedwait (&future->cond, &future->mutex, FUTURE_TIMEOUT_MS);
95+
mongoc_cond_timedwait (&future->cond,
96+
&future->mutex,
97+
get_future_timeout_ms ());
8998
}
9099
resolved = future->resolved;
91100
mongoc_mutex_unlock (&future->mutex);

tests/mock_server/future.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "mongoc-array-private.h"
44
#include "mongoc-thread-private.h"
55
#include "future.h"
6+
#include "../test-libmongoc.h"
67

78
/**************************************************
89
*
@@ -12,7 +13,14 @@
1213
*
1314
*************************************************/
1415

15-
#define FUTURE_TIMEOUT_MS 10 * 1000
16+
#define DEFAULT_FUTURE_TIMEOUT_MS 10 * 1000
17+
18+
static int64_t
19+
get_future_timeout_ms ()
20+
{
21+
return test_framework_getenv_int64 ("MONGOC_TEST_FUTURE_TIMEOUT_MS",
22+
DEFAULT_FUTURE_TIMEOUT_MS);
23+
}
1624

1725
void
1826
future_get_void (future_t *future)
@@ -316,13 +324,14 @@ future_resolve (future_t *future, future_value_t return_value)
316324
bool
317325
future_wait (future_t *future)
318326
{
319-
/* TODO: configurable timeout */
320-
int64_t deadline = bson_get_monotonic_time () + FUTURE_TIMEOUT_MS * 1000;
327+
int64_t deadline = bson_get_monotonic_time () + get_future_timeout_ms ();
321328
bool resolved;
322329

323330
mongoc_mutex_lock (&future->mutex);
324331
while (!future->resolved && bson_get_monotonic_time () <= deadline) {
325-
mongoc_cond_timedwait (&future->cond, &future->mutex, FUTURE_TIMEOUT_MS);
332+
mongoc_cond_timedwait (&future->cond,
333+
&future->mutex,
334+
get_future_timeout_ms ());
326335
}
327336
resolved = future->resolved;
328337
mongoc_mutex_unlock (&future->mutex);

tests/test-libmongoc.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,45 @@ test_framework_getenv_bool (const char *name)
191191
return ret;
192192
}
193193

194+
195+
/*
196+
*--------------------------------------------------------------------------
197+
*
198+
* test_framework_getenv_int64 --
199+
*
200+
* Get a number from an environment variable.
201+
*
202+
* Returns:
203+
* The number, or default.
204+
*
205+
* Side effects:
206+
* Logs and aborts if there is a non-numeric value.
207+
*
208+
*--------------------------------------------------------------------------
209+
*/
210+
int64_t
211+
test_framework_getenv_int64 (const char *name,
212+
int64_t default_value)
213+
{
214+
char *value = test_framework_getenv (name);
215+
char *endptr;
216+
int64_t ret;
217+
218+
if (value) {
219+
errno = 0;
220+
ret = bson_ascii_strtoll (value, &endptr, 10);
221+
if (errno) {
222+
perror (bson_strdup_printf ("Parsing %s from environment", name));
223+
abort ();
224+
}
225+
226+
bson_free (value);
227+
return ret;
228+
}
229+
230+
return default_value;
231+
}
232+
194233
/*
195234
*--------------------------------------------------------------------------
196235
*

tests/test-libmongoc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ char *gen_collection_name (const char *prefix);
2323
void suppress_one_message (void);
2424
char *test_framework_getenv (const char *name);
2525
bool test_framework_getenv_bool (const char *name);
26+
int64_t test_framework_getenv_int64 (const char *name,
27+
int64_t default_value);
2628
char *test_framework_get_host (void);
2729
uint16_t test_framework_get_port (void);
2830
char *test_framework_get_admin_user (void);

0 commit comments

Comments
 (0)