|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Project CHIP Authors |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +import websockets |
| 21 | + |
| 22 | +from matter.testing.matter_testing import MatterBaseTest |
| 23 | + |
| 24 | +# WebSocket server URI for sending commands to the DUT |
| 25 | +SERVER_URI = "ws://localhost:9002" |
| 26 | + |
| 27 | + |
| 28 | +class WEBRTCRTestBase(MatterBaseTest): |
| 29 | + """ |
| 30 | + Base class for WebRTC Transport Requestor test cases. |
| 31 | +
|
| 32 | + Provides common functionality including: |
| 33 | + - WebSocket-based command sending to the DUT |
| 34 | + - Shared SERVER_URI constant |
| 35 | + """ |
| 36 | + |
| 37 | + async def send_command(self, command): |
| 38 | + """ |
| 39 | + Send a command to the DUT via WebSocket and wait for response. |
| 40 | +
|
| 41 | + Args: |
| 42 | + command: The command string to send to the DUT |
| 43 | +
|
| 44 | + This method: |
| 45 | + 1. Connects to the WebSocket server at SERVER_URI |
| 46 | + 2. Sends the command |
| 47 | + 3. Waits for and receives the response |
| 48 | + 4. Logs the connection, command, and response status |
| 49 | +
|
| 50 | + Raises: |
| 51 | + Exception: Re-raises any exceptions after logging them clearly |
| 52 | + """ |
| 53 | + try: |
| 54 | + async with websockets.connect(SERVER_URI) as websocket: |
| 55 | + logging.info(f"Connected to {SERVER_URI}") |
| 56 | + |
| 57 | + # Send command |
| 58 | + logging.info(f"Sending command: {command}") |
| 59 | + await websocket.send(command) |
| 60 | + |
| 61 | + # Receive response |
| 62 | + await websocket.recv() |
| 63 | + logging.info("Received command response") |
| 64 | + |
| 65 | + except ConnectionRefusedError as e: |
| 66 | + logging.error(f"Failed to connect to WebSocket server at {SERVER_URI}: Connection refused. " |
| 67 | + f"Is the DUT WebSocket server running? Error: {e}") |
| 68 | + raise |
| 69 | + |
| 70 | + except websockets.exceptions.WebSocketException as e: |
| 71 | + logging.error(f"WebSocket error while communicating with {SERVER_URI}: {e}") |
| 72 | + raise |
| 73 | + |
| 74 | + except OSError as e: |
| 75 | + logging.error(f"Network error while connecting to {SERVER_URI}: {e}") |
| 76 | + raise |
| 77 | + |
| 78 | + except Exception as e: |
| 79 | + logging.error(f"Unexpected error while sending command '{command}' to {SERVER_URI}: {e}") |
| 80 | + raise |
0 commit comments