Skip to content

PYTHON-4924 - PoolClearedError should have TransientTransactionError … #2244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions test/asynchronous/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from test.asynchronous.utils_spec_runner import AsyncSpecRunner

from gridfs.asynchronous.grid_file import AsyncGridFS, AsyncGridFSBucket
from pymongo.asynchronous.pool import PoolState
from pymongo.server_selectors import writable_server_selector

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

Expand All @@ -39,6 +41,7 @@
from pymongo.asynchronous.cursor import AsyncCursor
from pymongo.asynchronous.helpers import anext
from pymongo.errors import (
AutoReconnect,
CollectionInvalid,
ConfigurationError,
ConnectionFailure,
Expand Down Expand Up @@ -386,6 +389,22 @@ async def find_raw_batches(*args, **kwargs):
if isinstance(res, (AsyncCommandCursor, AsyncCursor)):
await res.to_list()

@async_client_context.require_transactions
async def test_transaction_pool_cleared_error_labelled_transient(self):
c = await self.async_single_client()

with self.assertRaises(AutoReconnect) as context:
async with c.start_session() as session:
async with await session.start_transaction():
server = await c._select_server(writable_server_selector, session, "test")
# Pause the server's pool, causing it to fail connection checkout.
server.pool.state = PoolState.PAUSED
async with c._checkout(server, session):
pass

# Verify that the TransientTransactionError label is present in the error.
self.assertTrue(context.exception.has_error_label("TransientTransactionError"))


class PatchSessionTimeout:
"""Patches the client_session's with_transaction timeout for testing."""
Expand Down
19 changes: 19 additions & 0 deletions test/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from test.utils_spec_runner import SpecRunner

from gridfs.synchronous.grid_file import GridFS, GridFSBucket
from pymongo.server_selectors import writable_server_selector
from pymongo.synchronous.pool import PoolState

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

Expand All @@ -34,6 +36,7 @@
from bson.raw_bson import RawBSONDocument
from pymongo import WriteConcern
from pymongo.errors import (
AutoReconnect,
CollectionInvalid,
ConfigurationError,
ConnectionFailure,
Expand Down Expand Up @@ -378,6 +381,22 @@ def find_raw_batches(*args, **kwargs):
if isinstance(res, (CommandCursor, Cursor)):
res.to_list()

@client_context.require_transactions
def test_transaction_pool_cleared_error_labelled_transient(self):
c = self.single_client()

with self.assertRaises(AutoReconnect) as context:
with c.start_session() as session:
with session.start_transaction():
server = c._select_server(writable_server_selector, session, "test")
# Pause the server's pool, causing it to fail connection checkout.
server.pool.state = PoolState.PAUSED
with c._checkout(server, session):
pass

# Verify that the TransientTransactionError label is present in the error.
self.assertTrue(context.exception.has_error_label("TransientTransactionError"))


class PatchSessionTimeout:
"""Patches the client_session's with_transaction timeout for testing."""
Expand Down
Loading