Skip to content

Commit 389fc8e

Browse files
committed
PYTHON-5672 Fix test_client_checkout_setup_failure on freethreaded Python
patch.object cannot shadow a method on an instance when __slots__ is defined — the attribute is read-only. Use a subclass override instead.
1 parent b3ef09c commit 389fc8e

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

test/asynchronous/test_client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -822,21 +822,23 @@ async def test_client_checkout_setup_failure_returns_connection(self):
822822
# Verify that the connection is returned to the pool when an exception
823823
# is raised during _ClientCheckout.__aenter__ post-checkout setup
824824
# (e.g. session pinning or the auto-encryption wire-version check).
825-
from unittest.mock import patch
826-
825+
# Use a subclass to override contribute_socket because __slots__ prevents
826+
# instance-level patching of methods.
827827
from pymongo.asynchronous.mongo_client import _ClientCheckout
828828

829+
class _BrokenSetupCheckout(_ClientCheckout):
830+
def contribute_socket(self, conn, completed_handshake=True):
831+
raise RuntimeError("simulated failure in post-checkout setup")
832+
829833
client = await self.async_rs_or_single_client()
830834
server = await (await client._get_topology()).select_server(
831835
writable_server_selector, _Op.TEST
832836
)
833837
pool = server.pool
834838

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
839+
with self.assertRaises(RuntimeError):
840+
async with _BrokenSetupCheckout(client, server, None):
841+
pass
840842

841843
# Connection was returned to pool, not leaked.
842844
self.assertEqual(0, pool.active_sockets)

test/test_client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -797,19 +797,21 @@ def test_client_checkout_setup_failure_returns_connection(self):
797797
# Verify that the connection is returned to the pool when an exception
798798
# is raised during _ClientCheckout.__aenter__ post-checkout setup
799799
# (e.g. session pinning or the auto-encryption wire-version check).
800-
from unittest.mock import patch
801-
800+
# Use a subclass to override contribute_socket because __slots__ prevents
801+
# instance-level patching of methods.
802802
from pymongo.synchronous.mongo_client import _ClientCheckout
803803

804+
class _BrokenSetupCheckout(_ClientCheckout):
805+
def contribute_socket(self, conn, completed_handshake=True):
806+
raise RuntimeError("simulated failure in post-checkout setup")
807+
804808
client = self.rs_or_single_client()
805809
server = (client._get_topology()).select_server(writable_server_selector, _Op.TEST)
806810
pool = server.pool
807811

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
812+
with self.assertRaises(RuntimeError):
813+
with _BrokenSetupCheckout(client, server, None):
814+
pass
813815

814816
# Connection was returned to pool, not leaked.
815817
self.assertEqual(0, pool.active_sockets)

0 commit comments

Comments
 (0)