Skip to content

Commit b745141

Browse files
CDRIVER-2931 initialize pop() and try_pop() pool clients the same way
1 parent 864f78b commit b745141

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

src/libmongoc/src/mongoc/mongoc-client-pool.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,26 @@ _start_scanner_if_needed (mongoc_client_pool_t *pool)
208208
}
209209
}
210210

211+
static void
212+
_initialize_new_client (mongoc_client_pool_t *pool, mongoc_client_t *client)
213+
{
214+
/* for tests */
215+
mongoc_client_set_stream_initiator (
216+
client,
217+
pool->topology->scanner->initiator,
218+
pool->topology->scanner->initiator_context);
219+
220+
client->error_api_version = pool->error_api_version;
221+
_mongoc_client_set_apm_callbacks_private (
222+
client, &pool->apm_callbacks, pool->apm_context);
223+
224+
#ifdef MONGOC_ENABLE_SSL
225+
if (pool->ssl_opts_set) {
226+
mongoc_client_set_ssl_opts (client, &pool->ssl_opts);
227+
}
228+
#endif
229+
}
230+
211231
mongoc_client_t *
212232
mongoc_client_pool_pop (mongoc_client_pool_t *pool)
213233
{
@@ -223,21 +243,7 @@ mongoc_client_pool_pop (mongoc_client_pool_t *pool)
223243
if (!(client = (mongoc_client_t *) _mongoc_queue_pop_head (&pool->queue))) {
224244
if (pool->size < pool->max_pool_size) {
225245
client = _mongoc_client_new_from_uri (pool->topology);
226-
227-
/* for tests */
228-
mongoc_client_set_stream_initiator (
229-
client,
230-
pool->topology->scanner->initiator,
231-
pool->topology->scanner->initiator_context);
232-
233-
client->error_api_version = pool->error_api_version;
234-
_mongoc_client_set_apm_callbacks_private (
235-
client, &pool->apm_callbacks, pool->apm_context);
236-
#ifdef MONGOC_ENABLE_SSL
237-
if (pool->ssl_opts_set) {
238-
mongoc_client_set_ssl_opts (client, &pool->ssl_opts);
239-
}
240-
#endif
246+
_initialize_new_client (pool, client);
241247
pool->size++;
242248
} else {
243249
mongoc_cond_wait (&pool->cond, &pool->mutex);
@@ -266,11 +272,7 @@ mongoc_client_pool_try_pop (mongoc_client_pool_t *pool)
266272
if (!(client = (mongoc_client_t *) _mongoc_queue_pop_head (&pool->queue))) {
267273
if (pool->size < pool->max_pool_size) {
268274
client = _mongoc_client_new_from_uri (pool->topology);
269-
#ifdef MONGOC_ENABLE_SSL
270-
if (pool->ssl_opts_set) {
271-
mongoc_client_set_ssl_opts (client, &pool->ssl_opts);
272-
}
273-
#endif
275+
_initialize_new_client (pool, client);
274276
pool->size++;
275277
}
276278
}

src/libmongoc/tests/test-mongoc-command-monitoring.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ test_set_callbacks_cb (const mongoc_apm_command_started_t *event)
333333

334334

335335
static void
336-
_test_set_callbacks (bool pooled)
336+
_test_set_callbacks (bool pooled, bool try_pop)
337337
{
338338
mongoc_client_t *client;
339339
mongoc_client_pool_t *pool = NULL;
@@ -348,7 +348,11 @@ _test_set_callbacks (bool pooled)
348348
pool = test_framework_client_pool_new ();
349349
ASSERT (mongoc_client_pool_set_apm_callbacks (
350350
pool, callbacks, (void *) &n_calls));
351-
client = mongoc_client_pool_pop (pool);
351+
if (try_pop) {
352+
client = mongoc_client_pool_try_pop (pool);
353+
} else {
354+
client = mongoc_client_pool_pop (pool);
355+
}
352356
} else {
353357
client = test_framework_client_new ();
354358
ASSERT (mongoc_client_set_apm_callbacks (
@@ -395,14 +399,20 @@ _test_set_callbacks (bool pooled)
395399
static void
396400
test_set_callbacks_single (void)
397401
{
398-
_test_set_callbacks (false);
402+
_test_set_callbacks (false, false);
399403
}
400404

401405

402406
static void
403407
test_set_callbacks_pooled (void)
404408
{
405-
_test_set_callbacks (true);
409+
_test_set_callbacks (true, false);
410+
}
411+
412+
static void
413+
test_set_callbacks_pooled_try_pop (void)
414+
{
415+
_test_set_callbacks (true, true);
406416
}
407417

408418

@@ -1182,6 +1192,9 @@ test_command_monitoring_install (TestSuite *suite)
11821192
TestSuite_AddLive (suite,
11831193
"/command_monitoring/set_callbacks/pooled",
11841194
test_set_callbacks_pooled);
1195+
TestSuite_AddLive (suite,
1196+
"/command_monitoring/set_callbacks/pooled_try_pop",
1197+
test_set_callbacks_pooled_try_pop);
11851198
/* require aggregation cursor */
11861199
TestSuite_AddLive (
11871200
suite, "/command_monitoring/set_callbacks/change", test_change_callbacks);

0 commit comments

Comments
 (0)