|
| 1 | +// test-mongoc-primary-stepdown.c contains tests specified in: |
| 2 | +// `Connections Survive Primary Step Down Tests`. See: |
| 3 | +// https://github.com/mongodb/specifications/tree/db3114e957f7c0976a1af09882dbb46cb4a70049/source/connections-survive-step-down/tests |
| 4 | + |
1 | 5 | #include "mongoc/mongoc.h"
|
2 | 6 | #include "mongoc/mongoc-read-concern-private.h"
|
3 | 7 | #include "mongoc/mongoc-util-private.h"
|
|
9 | 13 | #include "test-conveniences.h"
|
10 | 14 | #include "test-libmongoc.h"
|
11 | 15 |
|
| 16 | +typedef struct { |
| 17 | + // If `use_pooled` is true, a test is run with a `mongoc_client_t` obtained |
| 18 | + // from a `mongoc_client_pool_t`. |
| 19 | + bool use_pooled; |
| 20 | +} test_ctx_t; |
12 | 21 |
|
13 | 22 | static mongoc_uri_t *
|
14 | 23 | _get_test_uri (void)
|
@@ -90,32 +99,34 @@ _connection_count (mongoc_client_t *client, uint32_t server_id)
|
90 | 99 | typedef void (*_test_fn_t) (mongoc_client_t *);
|
91 | 100 |
|
92 | 101 | static void
|
93 |
| -_run_test_single_and_pooled (_test_fn_t test) |
| 102 | +_run_test_single_or_pooled (_test_fn_t test, bool use_pooled) |
94 | 103 | {
|
95 | 104 | mongoc_uri_t *uri;
|
96 | 105 | mongoc_client_t *client;
|
97 | 106 | mongoc_client_pool_t *pool;
|
98 | 107 |
|
99 | 108 | uri = _get_test_uri ();
|
100 | 109 |
|
101 |
| - /* Run in single-threaded mode */ |
102 |
| - client = test_framework_client_new_from_uri (uri, NULL); |
103 |
| - test_framework_set_ssl_opts (client); |
104 |
| - _setup_test_with_client (client); |
105 |
| - test (client); |
106 |
| - mongoc_client_destroy (client); |
107 |
| - |
108 |
| - /* Run in pooled mode */ |
109 |
| - pool = test_framework_client_pool_new_from_uri (uri, NULL); |
110 |
| - test_framework_set_pool_ssl_opts (pool); |
111 |
| - client = mongoc_client_pool_pop (pool); |
112 |
| - _setup_test_with_client (client); |
113 |
| - /* Wait one second to be assured that the RTT connection has been established |
114 |
| - * as well. */ |
115 |
| - _mongoc_usleep (1000 * 1000); |
116 |
| - test (client); |
117 |
| - mongoc_client_pool_push (pool, client); |
118 |
| - mongoc_client_pool_destroy (pool); |
| 110 | + if (!use_pooled) { |
| 111 | + /* Run in single-threaded mode */ |
| 112 | + client = test_framework_client_new_from_uri (uri, NULL); |
| 113 | + test_framework_set_ssl_opts (client); |
| 114 | + _setup_test_with_client (client); |
| 115 | + test (client); |
| 116 | + mongoc_client_destroy (client); |
| 117 | + } else { |
| 118 | + /* Run in pooled mode */ |
| 119 | + pool = test_framework_client_pool_new_from_uri (uri, NULL); |
| 120 | + test_framework_set_pool_ssl_opts (pool); |
| 121 | + client = mongoc_client_pool_pop (pool); |
| 122 | + _setup_test_with_client (client); |
| 123 | + /* Wait one second to be assured that the RTT connection has been |
| 124 | + * established as well. */ |
| 125 | + _mongoc_usleep (1000 * 1000); |
| 126 | + test (client); |
| 127 | + mongoc_client_pool_push (pool, client); |
| 128 | + mongoc_client_pool_destroy (pool); |
| 129 | + } |
119 | 130 |
|
120 | 131 | mongoc_uri_destroy (uri);
|
121 | 132 | }
|
@@ -201,16 +212,16 @@ test_getmore_iteration (mongoc_client_t *client)
|
201 | 212 | }
|
202 | 213 |
|
203 | 214 | static void
|
204 |
| -test_getmore_iteration_runner (void *ctx) |
| 215 | +test_getmore_iteration_runner (void *ctx_void) |
205 | 216 | {
|
206 |
| - BSON_UNUSED (ctx); |
| 217 | + test_ctx_t *ctx = ctx_void; |
207 | 218 |
|
208 | 219 | /* Only run on 4.2 or higher */
|
209 | 220 | if (!test_framework_max_wire_version_at_least (8)) {
|
210 | 221 | return;
|
211 | 222 | }
|
212 | 223 |
|
213 |
| - _run_test_single_and_pooled (test_getmore_iteration); |
| 224 | + _run_test_single_or_pooled (test_getmore_iteration, ctx->use_pooled); |
214 | 225 | }
|
215 | 226 |
|
216 | 227 | static void
|
@@ -272,16 +283,16 @@ test_not_primary_keep_pool (mongoc_client_t *client)
|
272 | 283 | }
|
273 | 284 |
|
274 | 285 | static void
|
275 |
| -test_not_primary_keep_pool_runner (void *ctx) |
| 286 | +test_not_primary_keep_pool_runner (void *ctx_void) |
276 | 287 | {
|
277 |
| - BSON_UNUSED (ctx); |
| 288 | + test_ctx_t *ctx = ctx_void; |
278 | 289 |
|
279 | 290 | /* Only run on 4.2 and higher */
|
280 | 291 | if (!test_framework_max_wire_version_at_least (8)) {
|
281 | 292 | return;
|
282 | 293 | }
|
283 | 294 |
|
284 |
| - _run_test_single_and_pooled (test_not_primary_keep_pool); |
| 295 | + _run_test_single_or_pooled (test_not_primary_keep_pool, ctx->use_pooled); |
285 | 296 | }
|
286 | 297 |
|
287 | 298 | static void
|
@@ -346,19 +357,19 @@ test_not_primary_reset_pool (mongoc_client_t *client)
|
346 | 357 | }
|
347 | 358 |
|
348 | 359 | static void
|
349 |
| -test_not_primary_reset_pool_runner (void *ctx) |
| 360 | +test_not_primary_reset_pool_runner (void *ctx_void) |
350 | 361 | {
|
351 | 362 | int64_t max_wire_version;
|
352 | 363 |
|
353 |
| - BSON_UNUSED (ctx); |
| 364 | + test_ctx_t *ctx = ctx_void; |
354 | 365 |
|
355 | 366 | /* Only run if version 4.0 */
|
356 | 367 | test_framework_get_max_wire_version (&max_wire_version);
|
357 | 368 | if (max_wire_version != WIRE_VERSION_4_0) {
|
358 | 369 | return;
|
359 | 370 | }
|
360 | 371 |
|
361 |
| - _run_test_single_and_pooled (test_not_primary_reset_pool); |
| 372 | + _run_test_single_or_pooled (test_not_primary_reset_pool, ctx->use_pooled); |
362 | 373 | }
|
363 | 374 |
|
364 | 375 | static void
|
@@ -420,16 +431,16 @@ test_shutdown_reset_pool (mongoc_client_t *client)
|
420 | 431 | }
|
421 | 432 |
|
422 | 433 | static void
|
423 |
| -test_shutdown_reset_pool_runner (void *ctx) |
| 434 | +test_shutdown_reset_pool_runner (void *ctx_void) |
424 | 435 | {
|
425 |
| - BSON_UNUSED (ctx); |
| 436 | + test_ctx_t *ctx = ctx_void; |
426 | 437 |
|
427 | 438 | /* Only run if version >= 4.0 */
|
428 | 439 | if (!test_framework_max_wire_version_at_least (WIRE_VERSION_4_0)) {
|
429 | 440 | return;
|
430 | 441 | }
|
431 | 442 |
|
432 |
| - _run_test_single_and_pooled (test_shutdown_reset_pool); |
| 443 | + _run_test_single_or_pooled (test_shutdown_reset_pool, ctx->use_pooled); |
433 | 444 | }
|
434 | 445 |
|
435 | 446 | static void
|
@@ -491,58 +502,48 @@ test_interrupted_shutdown_reset_pool (mongoc_client_t *client)
|
491 | 502 | }
|
492 | 503 |
|
493 | 504 | static void
|
494 |
| -test_interrupted_shutdown_reset_pool_runner (void *ctx) |
| 505 | +test_interrupted_shutdown_reset_pool_runner (void *ctx_void) |
495 | 506 | {
|
496 |
| - BSON_UNUSED (ctx); |
| 507 | + test_ctx_t *ctx = ctx_void; |
497 | 508 |
|
498 | 509 | /* Only run if version >= 4.0 */
|
499 | 510 | if (!test_framework_max_wire_version_at_least (WIRE_VERSION_4_0)) {
|
500 | 511 | return;
|
501 | 512 | }
|
502 | 513 |
|
503 |
| - _run_test_single_and_pooled (test_interrupted_shutdown_reset_pool); |
| 514 | + _run_test_single_or_pooled (test_interrupted_shutdown_reset_pool, |
| 515 | + ctx->use_pooled); |
504 | 516 | }
|
505 | 517 |
|
506 | 518 | void
|
507 | 519 | test_primary_stepdown_install (TestSuite *suite)
|
508 | 520 | {
|
509 |
| - TestSuite_AddFull (suite, |
510 |
| - "/Stepdown/getmore", |
511 |
| - test_getmore_iteration_runner, |
512 |
| - NULL, |
513 |
| - NULL, |
514 |
| - test_framework_skip_if_auth, |
| 521 | + test_ctx_t single_ctx = {.use_pooled = false}; |
| 522 | + test_ctx_t pooled_ctx = {.use_pooled = true}; |
| 523 | + |
| 524 | +#define TestPooledAndSingle(name, fn) \ |
| 525 | + TestSuite_AddFull (suite, \ |
| 526 | + name "/single", \ |
| 527 | + fn, \ |
| 528 | + NULL, \ |
| 529 | + &single_ctx, \ |
| 530 | + test_framework_skip_if_auth, \ |
| 531 | + test_framework_skip_if_not_replset); \ |
| 532 | + TestSuite_AddFull (suite, \ |
| 533 | + name "/pooled", \ |
| 534 | + fn, \ |
| 535 | + NULL, \ |
| 536 | + &pooled_ctx, \ |
| 537 | + test_framework_skip_if_auth, \ |
515 | 538 | test_framework_skip_if_not_replset);
|
516 | 539 |
|
517 |
| - TestSuite_AddFull (suite, |
518 |
| - "/Stepdown/not_primary_keep", |
519 |
| - test_not_primary_keep_pool_runner, |
520 |
| - NULL, |
521 |
| - NULL, |
522 |
| - test_framework_skip_if_auth, |
523 |
| - test_framework_skip_if_not_replset); |
524 |
| - |
525 |
| - TestSuite_AddFull (suite, |
526 |
| - "/Stepdown/not_primary_reset", |
527 |
| - test_not_primary_reset_pool_runner, |
528 |
| - NULL, |
529 |
| - NULL, |
530 |
| - test_framework_skip_if_auth, |
531 |
| - test_framework_skip_if_not_replset); |
532 |
| - |
533 |
| - TestSuite_AddFull (suite, |
534 |
| - "/Stepdown/shutdown_reset_pool", |
535 |
| - test_shutdown_reset_pool_runner, |
536 |
| - NULL, |
537 |
| - NULL, |
538 |
| - test_framework_skip_if_auth, |
539 |
| - test_framework_skip_if_not_replset); |
540 |
| - |
541 |
| - TestSuite_AddFull (suite, |
542 |
| - "/Stepdown/interrupt_shutdown", |
543 |
| - test_interrupted_shutdown_reset_pool_runner, |
544 |
| - NULL, |
545 |
| - NULL, |
546 |
| - test_framework_skip_if_auth, |
547 |
| - test_framework_skip_if_not_replset); |
| 540 | + TestPooledAndSingle ("/Stepdown/getmore", test_getmore_iteration_runner); |
| 541 | + TestPooledAndSingle ("/Stepdown/not_primary_keep", |
| 542 | + test_not_primary_keep_pool_runner); |
| 543 | + TestPooledAndSingle ("/Stepdown/not_primary_reset", |
| 544 | + test_not_primary_reset_pool_runner); |
| 545 | + TestPooledAndSingle ("/Stepdown/shutdown_reset_pool", |
| 546 | + test_shutdown_reset_pool_runner); |
| 547 | + TestPooledAndSingle ("/Stepdown/interrupt_shutdown", |
| 548 | + test_interrupted_shutdown_reset_pool_runner); |
548 | 549 | }
|
0 commit comments