|
4 | 4 | import contextlib |
5 | 5 | import os |
6 | 6 | import tempfile |
7 | | -from unittest.mock import AsyncMock, PropertyMock, patch |
| 7 | +from unittest.mock import AsyncMock, PropertyMock, patch, MagicMock |
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 | from reactivex.subject import Subject |
11 | 11 |
|
| 12 | + |
| 13 | +from pybricksdev.ble.pybricks import PYBRICKS_COMMAND_EVENT_UUID |
| 14 | +from pybricksdev.usb.pybricks import PybricksUsbOutEpMessageType |
12 | 15 | from pybricksdev.connections.pybricks import ( |
13 | 16 | ConnectionState, |
14 | 17 | HubCapabilityFlag, |
15 | 18 | HubKind, |
16 | 19 | PybricksHubBLE, |
| 20 | + PybricksHubUSB, |
17 | 21 | StatusFlag, |
18 | 22 | ) |
19 | 23 |
|
@@ -180,3 +184,77 @@ async def test_run_modern_protocol(self): |
180 | 184 | # Verify the expected calls were made |
181 | 185 | hub.download_user_program.assert_called_once() |
182 | 186 | hub.start_user_program.assert_called_once() |
| 187 | + |
| 188 | + |
| 189 | +class TestPybricksHubUSB: |
| 190 | + """Tests for the PybricksHubUSB class functionality.""" |
| 191 | + |
| 192 | + @pytest.mark.asyncio |
| 193 | + async def test_pybricks_hub_usb_write_gatt_char_disconnect(self): |
| 194 | + """Test write_gatt_char when a disconnect event occurs.""" |
| 195 | + hub = PybricksHubUSB(MagicMock()) |
| 196 | + |
| 197 | + hub._ep_out = MagicMock() |
| 198 | + # Simulate _response_queue.get() blocking indefinitely |
| 199 | + hub._response_queue = AsyncMock() |
| 200 | + hub._response_queue.get = AsyncMock(side_effect=asyncio.Event().wait) |
| 201 | + |
| 202 | + mock_observable = MagicMock(spec=Subject) # Using Subject as a base for mock spec |
| 203 | + disconnect_callback_handler = None |
| 204 | + |
| 205 | + def mock_subscribe_side_effect(on_next_callback, *args, **kwargs): |
| 206 | + nonlocal disconnect_callback_handler |
| 207 | + disconnect_callback_handler = on_next_callback |
| 208 | + mock_subscription = MagicMock() |
| 209 | + mock_subscription.dispose = MagicMock() |
| 210 | + return mock_subscription |
| 211 | + |
| 212 | + mock_observable.subscribe = MagicMock(side_effect=mock_subscribe_side_effect) |
| 213 | + type(hub.connection_state_observable).value = PropertyMock(return_value=ConnectionState.CONNECTED) |
| 214 | + hub.connection_state_observable = mock_observable |
| 215 | + |
| 216 | + async def trigger_disconnect_event(): |
| 217 | + await asyncio.sleep(0.05) |
| 218 | + assert disconnect_callback_handler is not None, "Subscribe was not called by race_disconnect" |
| 219 | + disconnect_callback_handler(ConnectionState.DISCONNECTED) |
| 220 | + |
| 221 | + with pytest.raises(RuntimeError, match="disconnected during operation"): |
| 222 | + await asyncio.gather( |
| 223 | + hub.write_gatt_char(PYBRICKS_COMMAND_EVENT_UUID, b"test_data", True), |
| 224 | + trigger_disconnect_event() |
| 225 | + ) |
| 226 | + |
| 227 | + hub._ep_out.write.assert_called_once_with( |
| 228 | + bytes([PybricksUsbOutEpMessageType.COMMAND]) + b"test_data" |
| 229 | + ) |
| 230 | + |
| 231 | + @pytest.mark.asyncio |
| 232 | + async def test_pybricks_hub_usb_write_gatt_char_timeout(self): |
| 233 | + """Test write_gatt_char when a timeout occurs.""" |
| 234 | + hub = PybricksHubUSB(MagicMock()) |
| 235 | + |
| 236 | + hub._ep_out = MagicMock() |
| 237 | + hub._response_queue = AsyncMock() |
| 238 | + # Make _response_queue.get() block indefinitely |
| 239 | + hub._response_queue.get = AsyncMock(side_effect=asyncio.Event().wait) |
| 240 | + |
| 241 | + mock_observable = MagicMock(spec=Subject) |
| 242 | + |
| 243 | + def mock_subscribe_side_effect(on_next_callback, *args, **kwargs): |
| 244 | + mock_subscription = MagicMock() |
| 245 | + mock_subscription.dispose = MagicMock() |
| 246 | + return mock_subscription |
| 247 | + |
| 248 | + mock_observable.subscribe = MagicMock(side_effect=mock_subscribe_side_effect) |
| 249 | + type(hub.connection_state_observable).value = PropertyMock(return_value=ConnectionState.CONNECTED) |
| 250 | + hub.connection_state_observable = mock_observable |
| 251 | + |
| 252 | + # The method has a hardcoded timeout of 5.0s. |
| 253 | + # We can patch asyncio.wait_for to speed up the test. |
| 254 | + with patch('asyncio.wait_for', side_effect=asyncio.TimeoutError("Test-induced timeout")): |
| 255 | + with pytest.raises(asyncio.TimeoutError, match="Timeout waiting for USB response"): |
| 256 | + await hub.write_gatt_char(PYBRICKS_COMMAND_EVENT_UUID, b"test_data", True) |
| 257 | + |
| 258 | + hub._ep_out.write.assert_called_once_with( |
| 259 | + bytes([PybricksUsbOutEpMessageType.COMMAND]) + b"test_data" |
| 260 | + ) |
0 commit comments