|
15 | 15 |
|
16 | 16 | import inspect
|
17 | 17 | from typing import Any, Callable, Mapping, Optional
|
18 |
| -from unittest.mock import AsyncMock, patch |
| 18 | +from unittest.mock import AsyncMock, Mock, patch |
19 | 19 |
|
20 | 20 | import pytest
|
21 | 21 | from aioresponses import CallbackResult, aioresponses
|
@@ -44,8 +44,12 @@ def sync_client_environment():
|
44 | 44 | # This ensures any client created will start a new loop/thread.
|
45 | 45 |
|
46 | 46 | # Ensure no loop/thread is running from a previous misbehaving test or setup
|
47 |
| - assert original_loop is None or not original_loop.is_running() |
48 |
| - assert original_thread is None or not original_thread.is_alive() |
| 47 | + if original_loop and original_loop.is_running(): |
| 48 | + original_loop.call_soon_threadsafe(original_loop.stop) |
| 49 | + if original_thread and original_thread.is_alive(): |
| 50 | + original_thread.join(timeout=5) |
| 51 | + ToolboxSyncClient._ToolboxSyncClient__loop = None |
| 52 | + ToolboxSyncClient._ToolboxSyncClient__thread = None |
49 | 53 |
|
50 | 54 | ToolboxSyncClient._ToolboxSyncClient__loop = None
|
51 | 55 | ToolboxSyncClient._ToolboxSyncClient__thread = None
|
@@ -408,20 +412,32 @@ def post_callback(url, **kwargs):
|
408 | 412 | result = tool(param1="test")
|
409 | 413 | assert result == expected_payload["result"]
|
410 | 414 |
|
411 |
| - @pytest.mark.usefixtures("sync_client_environment") |
412 | 415 | def test_sync_add_headers_duplicate_fail(self):
|
413 |
| - """ |
414 |
| - Tests that adding a duplicate header via add_headers raises ValueError. |
415 |
| - Manually create client to control initial headers. |
416 |
| - """ |
| 416 | + """Tests that adding a duplicate header via add_headers raises ValueError (from async client).""" |
417 | 417 | initial_headers = {"X-Initial-Header": "initial_value"}
|
| 418 | + mock_async_client = AsyncMock(spec=ToolboxClient) |
418 | 419 |
|
419 |
| - with ToolboxSyncClient(TEST_BASE_URL, client_headers=initial_headers) as client: |
420 |
| - with pytest.raises( |
421 |
| - ValueError, |
422 |
| - match="Client header\\(s\\) `X-Initial-Header` already registered", |
423 |
| - ): |
424 |
| - client.add_headers({"X-Initial-Header": "another_value"}) |
| 420 | + # Configure add_headers to simulate the ValueError from ToolboxClient |
| 421 | + def mock_add_headers(headers): |
| 422 | + # Simulate ToolboxClient's check |
| 423 | + if "X-Initial-Header" in headers: |
| 424 | + raise ValueError( |
| 425 | + "Client header(s) `X-Initial-Header` already registered" |
| 426 | + ) |
| 427 | + |
| 428 | + mock_async_client.add_headers = Mock(side_effect=mock_add_headers) |
| 429 | + |
| 430 | + with patch( |
| 431 | + "toolbox_core.sync_client.ToolboxClient", return_value=mock_async_client |
| 432 | + ): |
| 433 | + with ToolboxSyncClient( |
| 434 | + TEST_BASE_URL, client_headers=initial_headers |
| 435 | + ) as client: |
| 436 | + with pytest.raises( |
| 437 | + ValueError, |
| 438 | + match="Client header\\(s\\) `X-Initial-Header` already registered", |
| 439 | + ): |
| 440 | + client.add_headers({"X-Initial-Header": "another_value"}) |
425 | 441 |
|
426 | 442 |
|
427 | 443 | class TestSyncAuth:
|
|
0 commit comments