Skip to content

Commit b3ef09c

Browse files
committed
PYTHON-5672 Test _ClientCheckout post-checkout setup failure returns connection
Covers the except BaseException block in _ClientCheckout.__aenter__() that fires when post-checkout work (session pinning, ConfigurationError check) raises — verifying the connection is returned to the pool and not leaked.
1 parent be24573 commit b3ef09c

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

test/asynchronous/test_client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,29 @@ async def test_max_idle_time_checkout(self):
818818
self.assertEqual(conn, new_con)
819819
self.assertEqual(1, len(server._pool.conns))
820820

821+
async def test_client_checkout_setup_failure_returns_connection(self):
822+
# Verify that the connection is returned to the pool when an exception
823+
# is raised during _ClientCheckout.__aenter__ post-checkout setup
824+
# (e.g. session pinning or the auto-encryption wire-version check).
825+
from unittest.mock import patch
826+
827+
from pymongo.asynchronous.mongo_client import _ClientCheckout
828+
829+
client = await self.async_rs_or_single_client()
830+
server = await (await client._get_topology()).select_server(
831+
writable_server_selector, _Op.TEST
832+
)
833+
pool = server.pool
834+
835+
checkout = _ClientCheckout(client, server, None)
836+
with patch.object(checkout, "contribute_socket", side_effect=RuntimeError("simulated")):
837+
with self.assertRaises(RuntimeError):
838+
async with checkout:
839+
pass
840+
841+
# Connection was returned to pool, not leaked.
842+
self.assertEqual(0, pool.active_sockets)
843+
821844
async def test_constants(self):
822845
"""This test uses AsyncMongoClient explicitly to make sure that host and
823846
port are not overloaded.

test/test_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,27 @@ def test_max_idle_time_checkout(self):
793793
self.assertEqual(conn, new_con)
794794
self.assertEqual(1, len(server._pool.conns))
795795

796+
def test_client_checkout_setup_failure_returns_connection(self):
797+
# Verify that the connection is returned to the pool when an exception
798+
# is raised during _ClientCheckout.__aenter__ post-checkout setup
799+
# (e.g. session pinning or the auto-encryption wire-version check).
800+
from unittest.mock import patch
801+
802+
from pymongo.synchronous.mongo_client import _ClientCheckout
803+
804+
client = self.rs_or_single_client()
805+
server = (client._get_topology()).select_server(writable_server_selector, _Op.TEST)
806+
pool = server.pool
807+
808+
checkout = _ClientCheckout(client, server, None)
809+
with patch.object(checkout, "contribute_socket", side_effect=RuntimeError("simulated")):
810+
with self.assertRaises(RuntimeError):
811+
with checkout:
812+
pass
813+
814+
# Connection was returned to pool, not leaked.
815+
self.assertEqual(0, pool.active_sockets)
816+
796817
def test_constants(self):
797818
"""This test uses MongoClient explicitly to make sure that host and
798819
port are not overloaded.

0 commit comments

Comments
 (0)