Skip to content

Commit bd02e75

Browse files
committed
Add test, revert pymongo changes
1 parent a62171c commit bd02e75

File tree

6 files changed

+50
-50
lines changed

6 files changed

+50
-50
lines changed

pymongo/asynchronous/pool.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ def _raise_connection_failure(
190190
) -> NoReturn:
191191
"""Convert a socket.error to ConnectionFailure and raise it."""
192192
host, port = address
193-
if isinstance(error, PyMongoError) and error._error_labels:
194-
labels = error._error_labels
195-
else:
196-
labels = None
197193
# If connecting to a Unix socket, port will be None.
198194
if port is not None:
199195
msg = "%s:%d: %s" % (host, port, error)
@@ -204,15 +200,15 @@ def _raise_connection_failure(
204200
if "configured timeouts" not in msg:
205201
msg += format_timeout_details(timeout_details)
206202
if isinstance(error, socket.timeout):
207-
raise NetworkTimeout(msg, errors={"errorLabels": labels}) from error
203+
raise NetworkTimeout(msg) from error
208204
elif isinstance(error, SSLError) and "timed out" in str(error):
209205
# Eventlet does not distinguish TLS network timeouts from other
210206
# SSLErrors (https://github.com/eventlet/eventlet/issues/692).
211207
# Luckily, we can work around this limitation because the phrase
212208
# 'timed out' appears in all the timeout related SSLErrors raised.
213-
raise NetworkTimeout(msg, errors={"errorLabels": labels}) from error
209+
raise NetworkTimeout(msg) from error
214210
else:
215-
raise AutoReconnect(msg, errors={"errorLabels": labels}) from error
211+
raise AutoReconnect(msg) from error
216212

217213

218214
def _get_timeout_details(options: PoolOptions) -> dict[str, float]:
@@ -1424,9 +1420,9 @@ def _raise_if_not_ready(self, checkout_started_time: float, emit_event: bool) ->
14241420
)
14251421

14261422
details = _get_timeout_details(self.opts)
1427-
error = AutoReconnect("connection pool paused")
1428-
error._add_error_label("TransientTransactionError")
1429-
_raise_connection_failure(self.address, error, timeout_details=details)
1423+
_raise_connection_failure(
1424+
self.address, AutoReconnect("connection pool paused"), timeout_details=details
1425+
)
14301426

14311427
async def _get_conn(
14321428
self, checkout_started_time: float, handler: Optional[_MongoClientErrorHandler] = None

pymongo/synchronous/pool.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ def _raise_connection_failure(
190190
) -> NoReturn:
191191
"""Convert a socket.error to ConnectionFailure and raise it."""
192192
host, port = address
193-
if isinstance(error, PyMongoError) and error._error_labels:
194-
labels = error._error_labels
195-
else:
196-
labels = None
197193
# If connecting to a Unix socket, port will be None.
198194
if port is not None:
199195
msg = "%s:%d: %s" % (host, port, error)
@@ -204,15 +200,15 @@ def _raise_connection_failure(
204200
if "configured timeouts" not in msg:
205201
msg += format_timeout_details(timeout_details)
206202
if isinstance(error, socket.timeout):
207-
raise NetworkTimeout(msg, errors={"errorLabels": labels}) from error
203+
raise NetworkTimeout(msg) from error
208204
elif isinstance(error, SSLError) and "timed out" in str(error):
209205
# Eventlet does not distinguish TLS network timeouts from other
210206
# SSLErrors (https://github.com/eventlet/eventlet/issues/692).
211207
# Luckily, we can work around this limitation because the phrase
212208
# 'timed out' appears in all the timeout related SSLErrors raised.
213-
raise NetworkTimeout(msg, errors={"errorLabels": labels}) from error
209+
raise NetworkTimeout(msg) from error
214210
else:
215-
raise AutoReconnect(msg, errors={"errorLabels": labels}) from error
211+
raise AutoReconnect(msg) from error
216212

217213

218214
def _get_timeout_details(options: PoolOptions) -> dict[str, float]:
@@ -1418,9 +1414,9 @@ def _raise_if_not_ready(self, checkout_started_time: float, emit_event: bool) ->
14181414
)
14191415

14201416
details = _get_timeout_details(self.opts)
1421-
error = AutoReconnect("connection pool paused")
1422-
error._add_error_label("TransientTransactionError")
1423-
_raise_connection_failure(self.address, error, timeout_details=details)
1417+
_raise_connection_failure(
1418+
self.address, AutoReconnect("connection pool paused"), timeout_details=details
1419+
)
14241420

14251421
def _get_conn(
14261422
self, checkout_started_time: float, handler: Optional[_MongoClientErrorHandler] = None

test/asynchronous/test_pooling.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -608,21 +608,6 @@ async def test_max_pool_size_with_connection_failure(self):
608608
# seems error-prone, so check the message too.
609609
self.assertNotIn("waiting for socket from pool", str(context.exception))
610610

611-
async def test_pool_cleared_error_labelled_transient(self):
612-
test_pool = Pool(
613-
("localhost", 27017),
614-
PoolOptions(max_pool_size=1),
615-
)
616-
# Pause the pool, causing it to fail connection checkout.
617-
test_pool.state = PoolState.PAUSED
618-
619-
with self.assertRaises(AutoReconnect) as context:
620-
async with test_pool.checkout():
621-
pass
622-
623-
# Verify that the TransientTransactionError label is present in the error.
624-
self.assertTrue(context.exception.has_error_label("TransientTransactionError"))
625-
626611

627612
if __name__ == "__main__":
628613
unittest.main()

test/asynchronous/test_transactions.py

Lines changed: 19 additions & 0 deletions
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,
@@ -386,6 +389,22 @@ async def find_raw_batches(*args, **kwargs):
386389
if isinstance(res, (AsyncCommandCursor, AsyncCursor)):
387390
await res.to_list()
388391

392+
@async_client_context.require_transactions
393+
async def test_transaction_pool_cleared_error_labelled_transient(self):
394+
c = await self.async_single_client()
395+
396+
with self.assertRaises(AutoReconnect) as context:
397+
async with c.start_session() as session:
398+
async with await session.start_transaction():
399+
server = await 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+
async 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."""

test/test_pooling.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -606,21 +606,6 @@ def test_max_pool_size_with_connection_failure(self):
606606
# seems error-prone, so check the message too.
607607
self.assertNotIn("waiting for socket from pool", str(context.exception))
608608

609-
def test_pool_cleared_error_labelled_transient(self):
610-
test_pool = Pool(
611-
("localhost", 27017),
612-
PoolOptions(max_pool_size=1),
613-
)
614-
# Pause the pool, causing it to fail connection checkout.
615-
test_pool.state = PoolState.PAUSED
616-
617-
with self.assertRaises(AutoReconnect) as context:
618-
with test_pool.checkout():
619-
pass
620-
621-
# Verify that the TransientTransactionError label is present in the error.
622-
self.assertTrue(context.exception.has_error_label("TransientTransactionError"))
623-
624609

625610
if __name__ == "__main__":
626611
unittest.main()

test/test_transactions.py

Lines changed: 19 additions & 0 deletions
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
3638
from pymongo.errors import (
39+
AutoReconnect,
3740
CollectionInvalid,
3841
ConfigurationError,
3942
ConnectionFailure,
@@ -378,6 +381,22 @@ def find_raw_batches(*args, **kwargs):
378381
if isinstance(res, (CommandCursor, Cursor)):
379382
res.to_list()
380383

384+
@client_context.require_transactions
385+
def test_transaction_pool_cleared_error_labelled_transient(self):
386+
c = self.single_client()
387+
388+
with self.assertRaises(AutoReconnect) as context:
389+
with c.start_session() as session:
390+
with session.start_transaction():
391+
server = c._select_server(writable_server_selector, session, "test")
392+
# Pause the server's pool, causing it to fail connection checkout.
393+
server.pool.state = PoolState.PAUSED
394+
with c._checkout(server, session):
395+
pass
396+
397+
# Verify that the TransientTransactionError label is present in the error.
398+
self.assertTrue(context.exception.has_error_label("TransientTransactionError"))
399+
381400

382401
class PatchSessionTimeout:
383402
"""Patches the client_session's with_transaction timeout for testing."""

0 commit comments

Comments
 (0)