Skip to content

Commit bc2cc1e

Browse files
authored
PYTHON-4924 - PoolClearedError should have TransientTransactionError … (#2244)
1 parent 2c077ba commit bc2cc1e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Diff for: test/asynchronous/test_transactions.py

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from test.asynchronous.utils_spec_runner import AsyncSpecRunner
2121

2222
from gridfs.asynchronous.grid_file import AsyncGridFS, AsyncGridFSBucket
23+
from pymongo.asynchronous.pool import PoolState
24+
from pymongo.server_selectors import writable_server_selector
2325

2426
sys.path[0:0] = [""]
2527

@@ -39,6 +41,7 @@
3941
from pymongo.asynchronous.cursor import AsyncCursor
4042
from pymongo.asynchronous.helpers import anext
4143
from pymongo.errors import (
44+
AutoReconnect,
4245
CollectionInvalid,
4346
ConfigurationError,
4447
ConnectionFailure,
@@ -394,6 +397,22 @@ async def find_raw_batches(*args, **kwargs):
394397
if isinstance(res, (AsyncCommandCursor, AsyncCursor)):
395398
await res.to_list()
396399

400+
@async_client_context.require_transactions
401+
async def test_transaction_pool_cleared_error_labelled_transient(self):
402+
c = await self.async_single_client()
403+
404+
with self.assertRaises(AutoReconnect) as context:
405+
async with c.start_session() as session:
406+
async with await session.start_transaction():
407+
server = await c._select_server(writable_server_selector, session, "test")
408+
# Pause the server's pool, causing it to fail connection checkout.
409+
server.pool.state = PoolState.PAUSED
410+
async with c._checkout(server, session):
411+
pass
412+
413+
# Verify that the TransientTransactionError label is present in the error.
414+
self.assertTrue(context.exception.has_error_label("TransientTransactionError"))
415+
397416

398417
class PatchSessionTimeout:
399418
"""Patches the client_session's with_transaction timeout for testing."""

Diff for: test/test_transactions.py

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from test.utils_spec_runner import SpecRunner
2121

2222
from gridfs.synchronous.grid_file import GridFS, GridFSBucket
23+
from pymongo.server_selectors import writable_server_selector
24+
from pymongo.synchronous.pool import PoolState
2325

2426
sys.path[0:0] = [""]
2527

@@ -34,6 +36,7 @@
3436
from bson.raw_bson import RawBSONDocument
3537
from pymongo import WriteConcern, _csot
3638
from pymongo.errors import (
39+
AutoReconnect,
3740
CollectionInvalid,
3841
ConfigurationError,
3942
ConnectionFailure,
@@ -386,6 +389,22 @@ def find_raw_batches(*args, **kwargs):
386389
if isinstance(res, (CommandCursor, Cursor)):
387390
res.to_list()
388391

392+
@client_context.require_transactions
393+
def test_transaction_pool_cleared_error_labelled_transient(self):
394+
c = self.single_client()
395+
396+
with self.assertRaises(AutoReconnect) as context:
397+
with c.start_session() as session:
398+
with session.start_transaction():
399+
server = c._select_server(writable_server_selector, session, "test")
400+
# Pause the server's pool, causing it to fail connection checkout.
401+
server.pool.state = PoolState.PAUSED
402+
with c._checkout(server, session):
403+
pass
404+
405+
# Verify that the TransientTransactionError label is present in the error.
406+
self.assertTrue(context.exception.has_error_label("TransientTransactionError"))
407+
389408

390409
class PatchSessionTimeout:
391410
"""Patches the client_session's with_transaction timeout for testing."""

0 commit comments

Comments
 (0)