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