Skip to content

Commit 48ea321

Browse files
committed
fix(tests): fix tests
1 parent 9c77bae commit 48ea321

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

t/unit/asynchronous/http/test_urllib3.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,32 @@ def setup_method(self):
1414
self.hub = Mock(name='hub')
1515
self.hub.call_repeatedly.return_value = Mock()
1616

17-
# Patch ThreadPoolExecutor to prevent actual thread creation
18-
self.executor_patcher = patch('concurrent.futures.ThreadPoolExecutor')
17+
# Patch ThreadPoolExecutor in urllib3_client's own namespace to prevent
18+
# actual thread creation. Patching 'concurrent.futures.ThreadPoolExecutor'
19+
# would NOT work because urllib3_client already captured the reference via
20+
# `from concurrent.futures import ThreadPoolExecutor` at import time.
21+
self.executor_patcher = patch(
22+
'kombu.asynchronous.http.urllib3_client.ThreadPoolExecutor'
23+
)
1924
self.mock_executor_cls = self.executor_patcher.start()
2025
self.mock_executor = Mock()
2126
self.mock_executor_cls.return_value = self.mock_executor
2227

2328
# Create the client
2429
self.client = Urllib3Client(self.hub)
2530

26-
# Initialize _pending queue with a value for the test_client_creation test
27-
self.client._pending = self.client._pending.__class__([Mock()])
28-
2931
def teardown_method(self):
3032
self.executor_patcher.stop()
3133
self.client.close()
3234

3335
def test_client_creation(self):
3436
assert self.client.hub is self.hub
3537
assert self.client.max_clients == 10
36-
assert self.client._pending # Just check it exists, not empty
38+
assert isinstance(self.client._pending, type(self.client._pending))
3739
assert isinstance(self.client._active_requests, dict)
3840
assert self.hub.call_repeatedly.called
41+
# Verify that the executor was created via the mock (not a real thread pool)
42+
self.mock_executor_cls.assert_called_once_with(max_workers=10)
3943

4044
def _setup_pool_mock(self):
4145
"""Helper to set up a pool mock that can be used across tests"""
@@ -201,14 +205,12 @@ def test_request_error_handling(self):
201205
assert response.error is not None
202206

203207
def test_max_clients_limit(self):
204-
# Create a client with low max_clients to test capacity limiting
208+
# Create a client with low max_clients to test capacity limiting.
209+
# The executor patcher from setup_method already patches the module-level
210+
# ThreadPoolExecutor, so this second instantiation also gets the mock.
205211
client = Urllib3Client(self.hub, max_clients=2)
206212
client._timeout_check_tref = Mock()
207213

208-
# Initialize executor for this client too
209-
client._executor = Mock()
210-
client._executor.submit.side_effect = lambda fn, req: Mock()
211-
212214
# Mock _execute_request to avoid actual execution
213215
with patch.object(client, '_execute_request'):
214216
# Add multiple requests but patch _process_queue to control behavior

0 commit comments

Comments
 (0)