From 80c987b2dc58738168a5952cae12e8cd11f02851 Mon Sep 17 00:00:00 2001 From: vitor-aignosi Date: Mon, 27 Oct 2025 07:47:11 -0300 Subject: [PATCH] SIENTIAPDE-1314 Refactor OPC Write Method and Comment Out Error Handling Logic - Reformatted the `write_opc_data` method for improved readability by adjusting its signature. - Commented out the error handling logic in `OpcRepository` related to disconnection due to multiple errors, preserving the original functionality for future reference. - Updated tests to reflect the commented-out error handling, ensuring clarity in the testing process. --- laborious/activities/opc.py | 4 ++- laborious/utils/repository/opc_repository.py | 32 +++++++++---------- .../utils/repository/test_opc_repository.py | 32 +++++++++---------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/laborious/activities/opc.py b/laborious/activities/opc.py index 852f701..2f1379c 100644 --- a/laborious/activities/opc.py +++ b/laborious/activities/opc.py @@ -269,7 +269,9 @@ class OPC(BaseActivity): return success, response_times @activity.defn(name='write_opc_data') - async def write_opc_data(self, input_data: dict[str, Any]) -> tuple[dict[Any, Any], dict[str, dict[str, float | None]]]: + async def write_opc_data( + self, input_data: dict[str, Any] + ) -> tuple[dict[Any, Any], dict[str, dict[str, float | None]]]: """ Write prediction and confidence data to OPC servers. The two writing operations are optional and independent of each other. diff --git a/laborious/utils/repository/opc_repository.py b/laborious/utils/repository/opc_repository.py index 2137c53..1bd5684 100644 --- a/laborious/utils/repository/opc_repository.py +++ b/laborious/utils/repository/opc_repository.py @@ -263,22 +263,22 @@ class OpcRepository(BaseActivity): if self.client is None: return await self.connect() - if self.error_count > 5: - self.logger.custom_warning( - f'OPC server {self.id} will be disconnected due to multiple errors', self.metadata - ) - try: - await self.disconnect() - except Exception as e: - trace = traceback.format_exc() - self.logger.custom_error( - f'Failed to disconnect from OPC server: {e}', self.metadata - ) - self.logger.custom_error(trace, self.metadata) - self.logger.custom_info( - f'Attempting to reconnect to OPC server {self.id}...', self.metadata - ) - return await self.connect() + # if self.error_count > 5: + # self.logger.custom_warning( + # f'OPC server {self.id} will be disconnected due to multiple errors', self.metadata + # ) + # try: + # await self.disconnect() + # except Exception as e: + # trace = traceback.format_exc() + # self.logger.custom_error( + # f'Failed to disconnect from OPC server: {e}', self.metadata + # ) + # self.logger.custom_error(trace, self.metadata) + # self.logger.custom_info( + # f'Attempting to reconnect to OPC server {self.id}...', self.metadata + # ) + # return await self.connect() # Check if client is connected using asyncua's connection state try: diff --git a/tests/laborious/utils/repository/test_opc_repository.py b/tests/laborious/utils/repository/test_opc_repository.py index 20c7bd2..4da51ee 100644 --- a/tests/laborious/utils/repository/test_opc_repository.py +++ b/tests/laborious/utils/repository/test_opc_repository.py @@ -1,6 +1,6 @@ import json from datetime import datetime -from unittest.mock import ANY, AsyncMock, MagicMock, Mock, call, patch +from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch import pytest from asyncua.crypto.security_policies import SecurityPolicyBasic256 @@ -236,22 +236,22 @@ async def test_validate_connection_none_client(opc_repository): opc_repository.connect.assert_called_once() -@pytest.mark.asyncio -async def test_validate_connection_error_count_disconnect_error(opc_repository): - opc_repository.error_count = 6 - opc_repository.client = AsyncMock() - opc_repository.disconnect = AsyncMock(side_effect=Exception('Test error')) - opc_repository.connect = AsyncMock(return_value=(True, {})) +# @pytest.mark.asyncio +# async def test_validate_connection_error_count_disconnect_error(opc_repository): +# opc_repository.error_count = 6 +# opc_repository.client = AsyncMock() +# opc_repository.disconnect = AsyncMock(side_effect=Exception('Test error')) +# opc_repository.connect = AsyncMock(return_value=(True, {})) - response = await opc_repository.validate_connection() - assert response == opc_repository.connect.return_value - opc_repository.disconnect.assert_called_once() - opc_repository.connect.assert_called_once() - opc_repository.logger.custom_error.assert_has_calls( - [ - call('Failed to disconnect from OPC server: Test error', ANY), - ] - ) +# response = await opc_repository.validate_connection() +# assert response == opc_repository.connect.return_value +# opc_repository.disconnect.assert_called_once() +# opc_repository.connect.assert_called_once() +# opc_repository.logger.custom_error.assert_has_calls( +# [ +# call('Failed to disconnect from OPC server: Test error', ANY), +# ] +# ) @pytest.mark.asyncio