Skip to content

Commit e0dd510

Browse files
committed
test: keep mocked connections out of shared HTTP pools
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
1 parent 9954f0c commit e0dd510

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎tests/e2e/common/retry_test_mixins.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from contextlib import contextmanager
1+
from contextlib import contextmanager, ExitStack
22
import threading
33
import time
44
from typing import Optional, List
@@ -21,7 +21,7 @@
2121

2222
@contextmanager
2323
def _mock_pool_method(method_name):
24-
"""Mock synchronous test requests without intercepting background telemetry."""
24+
"""Mock the calling test thread; worker threads use the original pool methods."""
2525
original = getattr(HTTPSConnectionPool, method_name)
2626
test_thread = threading.current_thread()
2727
mock_method = MagicMock()
@@ -31,7 +31,19 @@ def dispatch(pool, *args, **kwargs):
3131
return mock_method(*args, **kwargs)
3232
return original(pool, *args, **kwargs)
3333

34-
with patch.object(HTTPSConnectionPool, method_name, dispatch):
34+
with ExitStack() as stack:
35+
stack.enter_context(patch.object(HTTPSConnectionPool, method_name, dispatch))
36+
if method_name == "_get_conn":
37+
original_put = HTTPSConnectionPool._put_conn
38+
39+
def put_connection(pool, connection):
40+
# Fake acquisition never removed a slot from the real queue.
41+
if connection is not mock_method.return_value:
42+
return original_put(pool, connection)
43+
44+
stack.enter_context(
45+
patch.object(HTTPSConnectionPool, "_put_conn", put_connection)
46+
)
3547
yield mock_method
3648

3749

‎tests/unit/test_retry_test_isolation.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
@contextmanager
2020
def _offline_pool():
2121
connection = MagicMock()
22+
connection.sock = None
2223
connection.close.return_value = None
2324
connection.getresponse.side_effect = lambda: SimpleHttpResponse(200, {})
2425
with HTTPSConnectionPool("example.invalid") as pool, patch.object(
@@ -86,3 +87,44 @@ def test_background_telemetry_does_not_receive_retry_errors(pending_telemetry):
8687
with pytest.raises(MaxRetryError):
8788
pool.urlopen("POST", "/sql", retries=Retry(total=1, allowed_methods=None))
8889
assert validate.call_count == 2
90+
91+
92+
def test_mocked_connections_do_not_leak_into_a_shared_pool():
93+
ready, release = Event(), Event()
94+
responses = [
95+
{"status": 429, "headers": {"Retry-After": "0"}, "redirect_location": None},
96+
{"status": 503, "headers": {}, "redirect_location": None},
97+
]
98+
with _offline_pool() as pool, ThreadPoolExecutor(max_workers=1) as executor:
99+
connection = pool._new_conn.return_value
100+
101+
def new_connection():
102+
# Hold the real pool's slot while the test sends mocked requests.
103+
ready.set()
104+
assert release.wait(timeout=5)
105+
return connection
106+
107+
def send():
108+
return [
109+
pool.urlopen("POST", "/telemetry-ext", retries=False).status
110+
for _ in range(2)
111+
]
112+
113+
pool._new_conn.side_effect = new_connection
114+
future = executor.submit(send)
115+
assert ready.wait(timeout=5)
116+
try:
117+
with mock_sequential_server_responses(responses) as mocked_connection:
118+
with pytest.raises(MaxRetryError):
119+
pool.urlopen(
120+
"POST",
121+
"/sql",
122+
retries=Retry(
123+
total=1, status_forcelist=[429, 503], allowed_methods=None
124+
),
125+
)
126+
release.set()
127+
assert future.result(timeout=5) == [200, 200]
128+
assert mocked_connection.return_value.getresponse.call_count == 2
129+
finally:
130+
release.set()

0 commit comments

Comments
 (0)