Test async RPC functionality (async main function).
(port: str, baudrate: int, use_pyserial: bool)
| 16 | |
| 17 | |
| 18 | async def main_async(port: str, baudrate: int, use_pyserial: bool) -> int: |
| 19 | """Test async RPC functionality (async main function).""" |
| 20 | print(f"🔌 Connecting to {port} at {baudrate} baud...") |
| 21 | |
| 22 | serial_iface = create_serial_interface( |
| 23 | port, baud_rate=baudrate, use_pyserial=use_pyserial |
| 24 | ) |
| 25 | async with RpcClient( |
| 26 | port, baudrate=baudrate, serial_interface=serial_iface |
| 27 | ) as client: |
| 28 | print("✅ Connected") |
| 29 | |
| 30 | # Test 1: Single request with ID |
| 31 | print("\n📡 Test 1: Single request with ID correlation") |
| 32 | response = await client.send("ping") |
| 33 | print(f" Request ID: {client._next_id - 1}") # Previous ID used |
| 34 | print(f" Response ID: {response._id}") |
| 35 | print(f" Response data: {response.data}") |
| 36 | assert response._id is not None, "Response should have an ID" |
| 37 | |
| 38 | # Test 2: Multiple sequential requests with unique IDs |
| 39 | print("\n📡 Test 2: Multiple sequential requests with unique IDs") |
| 40 | for i in range(3): |
| 41 | expected_id = client._next_id |
| 42 | response = await client.send("ping") |
| 43 | print( |
| 44 | f" Request #{i + 1} - Expected ID: {expected_id}, Response ID: {response._id}" |
| 45 | ) |
| 46 | assert response._id == expected_id, ( |
| 47 | f"Response ID {response._id} should match request ID {expected_id}" |
| 48 | ) |
| 49 | |
| 50 | # Test 3: Verify ID wrapping at uint32 boundary |
| 51 | print("\n📡 Test 3: Verify ID counter wrapping (uint32 overflow)") |
| 52 | client._next_id = 0xFFFFFFFE # Near uint32 max (4294967294) |
| 53 | response1 = await client.send("ping") |
| 54 | print( |
| 55 | f" Request at 0xFFFFFFFE - Response ID: {response1._id} (should be 4294967294)" |
| 56 | ) |
| 57 | response2 = await client.send("ping") |
| 58 | print( |
| 59 | f" Request at 0xFFFFFFFF - Response ID: {response2._id} (should be 4294967295)" |
| 60 | ) |
| 61 | response3 = await client.send("ping") |
| 62 | print( |
| 63 | f" Request at 0x00000000 - Response ID: {response3._id} (should wrap to 0)" |
| 64 | ) |
| 65 | assert response3._id == 0, f"ID should wrap to 0, got {response3._id}" |
| 66 | |
| 67 | print("\n✅ All async RPC ID correlation tests passed!") |
| 68 | print( |
| 69 | "\n💡 ID management is internal - public API only uses response.data, response.success" |
| 70 | ) |
| 71 | |
| 72 | return 0 |
| 73 | |
| 74 | |
| 75 | def main() -> int: |
no test coverage detected