Provide real server state with real asyncio primitives. This fixture: - Resets server state to clean slate - Creates real asyncio.Lock, asyncio.Queue instances - Mocks only external boundaries (browser, page) - Cleans up properly after test Use this for integration tes
()
| 38 | |
| 39 | @pytest.fixture |
| 40 | async def real_server_state(): |
| 41 | """ |
| 42 | Provide real server state with real asyncio primitives. |
| 43 | |
| 44 | This fixture: |
| 45 | - Resets server state to clean slate |
| 46 | - Creates real asyncio.Lock, asyncio.Queue instances |
| 47 | - Mocks only external boundaries (browser, page) |
| 48 | - Cleans up properly after test |
| 49 | |
| 50 | Use this for integration tests that verify: |
| 51 | - Lock behavior and concurrency |
| 52 | - Queue processing |
| 53 | - State management |
| 54 | - Async task coordination |
| 55 | """ |
| 56 | # Reset state to clean slate |
| 57 | state.reset() |
| 58 | |
| 59 | # Create REAL asyncio primitives (not mocks) |
| 60 | state.processing_lock = asyncio.Lock() |
| 61 | state.model_switching_lock = asyncio.Lock() |
| 62 | state.params_cache_lock = asyncio.Lock() |
| 63 | state.request_queue = asyncio.Queue() |
| 64 | |
| 65 | # Mock only external boundaries (browser/page - these are I/O) |
| 66 | mock_page = AsyncMock() |
| 67 | mock_page.goto = AsyncMock() |
| 68 | mock_page.wait_for_selector = AsyncMock() |
| 69 | mock_page.click = AsyncMock() |
| 70 | mock_page.fill = AsyncMock() |
| 71 | mock_page.evaluate = AsyncMock(return_value='{"mock": "preferences"}') |
| 72 | |
| 73 | # Mock locator to return proper AsyncMock locator objects |
| 74 | mock_locator = AsyncMock() |
| 75 | mock_locator.fill = AsyncMock() |
| 76 | mock_locator.click = AsyncMock() |
| 77 | mock_locator.is_visible = AsyncMock(return_value=True) |
| 78 | mock_locator.wait_for = AsyncMock() |
| 79 | mock_page.locator = MagicMock(return_value=mock_locator) |
| 80 | |
| 81 | mock_page.is_closed = MagicMock(return_value=False) # Page is open |
| 82 | |
| 83 | mock_browser = AsyncMock() |
| 84 | mock_browser.new_context = AsyncMock(return_value=AsyncMock()) |
| 85 | mock_browser.close = AsyncMock() |
| 86 | |
| 87 | state.page_instance = mock_page |
| 88 | state.browser_instance = mock_browser |
| 89 | state.is_page_ready = True |
| 90 | state.is_browser_connected = True |
| 91 | |
| 92 | yield state |
| 93 | |
| 94 | # Cleanup: Cancel any tasks, release locks, clear queue |
| 95 | # This is CRITICAL for Windows to prevent hangs |
| 96 | |
| 97 | # Clear queue |