Skip to content

Commit 18c1f14

Browse files
authored
PYTHON-5529 Introduce optin setting to await for MinPoolSize population (#2664)
1 parent 6ccaae5 commit 18c1f14

14 files changed

+164
-60
lines changed

test/asynchronous/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2398,7 +2398,7 @@ def test_gevent_timeout_when_creating_connection(self):
23982398
client = self.async_rs_or_single_client()
23992399
self.addCleanup(client.close)
24002400
coll = client.pymongo_test.test
2401-
pool = async_get_pool(client)
2401+
pool = async_get_pool(client) # type:ignore
24022402

24032403
# Patch the pool to delay the connect method.
24042404
def delayed_connect(*args, **kwargs):

test/asynchronous/unified_format.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,17 @@ async def _create_entity(self, entity_spec, uri=None):
329329
kwargs["h"] = uri
330330
client = await self.test.async_rs_or_single_client(**kwargs)
331331
await client.aconnect()
332+
# Wait for pool to be populated.
333+
if "awaitMinPoolSizeMS" in spec:
334+
pool = await async_get_pool(client)
335+
t0 = time.monotonic()
336+
while True:
337+
if (time.monotonic() - t0) > spec["awaitMinPoolSizeMS"] * 1000:
338+
raise ValueError("Test timed out during awaitMinPoolSize")
339+
async with pool.lock:
340+
if len(pool.conns) + pool.active_sockets >= pool.opts.min_pool_size:
341+
break
342+
await asyncio.sleep(0.1)
332343
self[spec["id"]] = client
333344
return
334345
elif entity_type == "database":
@@ -463,7 +474,7 @@ class UnifiedSpecTestMixinV1(AsyncIntegrationTest):
463474
a class attribute ``TEST_SPEC``.
464475
"""
465476

466-
SCHEMA_VERSION = Version.from_string("1.25")
477+
SCHEMA_VERSION = Version.from_string("1.26")
467478
RUN_ON_LOAD_BALANCER = True
468479
TEST_SPEC: Any
469480
TEST_PATH = "" # This gets filled in by generate_test_classes
@@ -1551,7 +1562,6 @@ async def test_case(self):
15511562
if re.search(fail_pattern, description):
15521563
test_method = unittest.expectedFailure(test_method)
15531564
break
1554-
15551565
setattr(cls, test_name, test_method)
15561566

15571567

test/asynchronous/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@
2828

2929
from bson.son import SON
3030
from pymongo import AsyncMongoClient
31+
from pymongo.asynchronous.pool import Pool, _CancellationContext, _PoolGeneration
3132
from pymongo.errors import ConfigurationError
3233
from pymongo.hello import HelloCompat
3334
from pymongo.lock import _async_create_lock
3435
from pymongo.operations import _Op
3536
from pymongo.read_preferences import ReadPreference
3637
from pymongo.server_selectors import any_server_selector, writable_server_selector
37-
from pymongo.synchronous.pool import _CancellationContext, _PoolGeneration
3838

3939
_IS_SYNC = False
4040

4141

42-
async def async_get_pool(client):
42+
async def async_get_pool(client: AsyncMongoClient) -> Pool:
4343
"""Get the standalone, primary, or mongos pool."""
4444
topology = await client._get_topology()
4545
server = await topology._select_server(writable_server_selector, _Op.TEST)
4646
return server.pool
4747

4848

49-
async def async_get_pools(client):
49+
async def async_get_pools(client: AsyncMongoClient) -> list[Pool]:
5050
"""Get all pools."""
5151
return [
5252
server.pool

test/csot/command-execution.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "timeoutMS behaves correctly during command execution",
3-
"schemaVersion": "1.9",
3+
"schemaVersion": "1.26",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.4.7",
@@ -69,8 +69,10 @@
6969
"appName": "reduceMaxTimeMSTest",
7070
"w": 1,
7171
"timeoutMS": 500,
72-
"heartbeatFrequencyMS": 500
72+
"heartbeatFrequencyMS": 500,
73+
"minPoolSize": 1
7374
},
75+
"awaitMinPoolSizeMS": 10000,
7476
"observeEvents": [
7577
"commandStartedEvent"
7678
]
@@ -185,8 +187,10 @@
185187
"appName": "rttTooHighTest",
186188
"w": 1,
187189
"timeoutMS": 10,
188-
"heartbeatFrequencyMS": 500
190+
"heartbeatFrequencyMS": 500,
191+
"minPoolSize": 1
189192
},
193+
"awaitMinPoolSizeMS": 10000,
190194
"observeEvents": [
191195
"commandStartedEvent"
192196
]
@@ -316,8 +320,10 @@
316320
"appName": "reduceMaxTimeMSTest",
317321
"w": 1,
318322
"timeoutMS": 90,
319-
"heartbeatFrequencyMS": 100000
323+
"heartbeatFrequencyMS": 100000,
324+
"minPoolSize": 1
320325
},
326+
"awaitMinPoolSizeMS": 10000,
321327
"observeEvents": [
322328
"commandStartedEvent"
323329
]

test/csot/convenient-transactions.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "timeoutMS behaves correctly for the withTransaction API",
3-
"schemaVersion": "1.9",
3+
"schemaVersion": "1.26",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.4",
@@ -21,8 +21,10 @@
2121
"client": {
2222
"id": "client",
2323
"uriOptions": {
24-
"timeoutMS": 500
24+
"timeoutMS": 500,
25+
"minPoolSize": 1
2526
},
27+
"awaitMinPoolSizeMS": 10000,
2628
"useMultipleMongoses": false,
2729
"observeEvents": [
2830
"commandStartedEvent"

test/csot/error-transformations.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "MaxTimeMSExpired server errors are transformed into a custom timeout error",
3-
"schemaVersion": "1.9",
3+
"schemaVersion": "1.26",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.0",
@@ -26,8 +26,10 @@
2626
"client": {
2727
"id": "client",
2828
"uriOptions": {
29-
"timeoutMS": 250
29+
"timeoutMS": 250,
30+
"minPoolSize": 1
3031
},
32+
"awaitMinPoolSizeMS": 10000,
3133
"useMultipleMongoses": false,
3234
"observeEvents": [
3335
"commandStartedEvent"

0 commit comments

Comments
 (0)