|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ------------------------------------------------------------------------------ |
| 3 | +# |
| 4 | +# Copyright 2023-2026 Valory AG |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# ------------------------------------------------------------------------------ |
| 19 | + |
| 20 | +"""Tests for the WebSocketClient connection.""" |
| 21 | + |
| 22 | +# pylint: skip-file |
| 23 | + |
| 24 | +from unittest.mock import AsyncMock, MagicMock |
| 25 | + |
| 26 | +import pytest |
| 27 | + |
| 28 | +from packages.valory.connections.websocket_client.connection import WebSocketClient |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def connection() -> WebSocketClient: |
| 33 | + """Create a WebSocketClient with mocked internals for disconnect testing.""" |
| 34 | + conn = WebSocketClient.__new__(WebSocketClient) |
| 35 | + conn._executor = MagicMock() |
| 36 | + conn._outbox = MagicMock() |
| 37 | + conn._manager = AsyncMock() |
| 38 | + conn._state = MagicMock() |
| 39 | + conn.logger = MagicMock() |
| 40 | + return conn |
| 41 | + |
| 42 | + |
| 43 | +class TestDisconnectShutdownExecutor: |
| 44 | + """Tests that disconnect() properly releases the ThreadPoolExecutor.""" |
| 45 | + |
| 46 | + @pytest.mark.asyncio |
| 47 | + async def test_disconnect_shuts_down_executor( |
| 48 | + self, connection: WebSocketClient |
| 49 | + ) -> None: |
| 50 | + """disconnect() calls executor.shutdown(wait=True) to release thread resources.""" |
| 51 | + await connection.disconnect() |
| 52 | + |
| 53 | + connection._executor.shutdown.assert_called_once_with(wait=True) |
| 54 | + |
| 55 | + @pytest.mark.asyncio |
| 56 | + async def test_disconnect_order_of_operations( |
| 57 | + self, connection: WebSocketClient |
| 58 | + ) -> None: |
| 59 | + """disconnect() removes subscriptions and empties outbox before shutting down the executor.""" |
| 60 | + call_order = [] |
| 61 | + |
| 62 | + async def track_remove() -> None: |
| 63 | + call_order.append("remove_subscriptions") |
| 64 | + |
| 65 | + def track_empty() -> None: |
| 66 | + call_order.append("empty_outbox") |
| 67 | + |
| 68 | + def track_shutdown(wait: bool = False) -> None: |
| 69 | + call_order.append("shutdown_executor") |
| 70 | + |
| 71 | + connection._manager.remove_all_subscriptions = track_remove |
| 72 | + connection._outbox.empty = track_empty |
| 73 | + connection._executor.shutdown = track_shutdown |
| 74 | + |
| 75 | + await connection.disconnect() |
| 76 | + |
| 77 | + assert call_order == [ |
| 78 | + "remove_subscriptions", |
| 79 | + "empty_outbox", |
| 80 | + "shutdown_executor", |
| 81 | + ] |
0 commit comments