diff --git a/pymodbus/transaction/transaction.py b/pymodbus/transaction/transaction.py index 8317be588..2a514942d 100644 --- a/pymodbus/transaction/transaction.py +++ b/pymodbus/transaction/transaction.py @@ -105,7 +105,6 @@ def sync_execute(self, no_response_expected: bool, request: ModbusPDU) -> Modbus ) self.count_no_responses += 1 Log.error(f"No response received after {self.retries} retries, continue with next request") - self.response_future = asyncio.Future() return None async def execute(self, no_response_expected: bool, request: ModbusPDU) -> ModbusPDU | None: diff --git a/test/transaction/test_transaction.py b/test/transaction/test_transaction.py index 2a2a3ebae..c2ce96059 100755 --- a/test/transaction/test_transaction.py +++ b/test/transaction/test_transaction.py @@ -108,7 +108,7 @@ async def test_transaction_execute(self, use_clc, scenario): transact.comm_params.timeout_connect = 0.1 transact.connection_lost = mock.Mock() assert not await transact.execute(False, request) - elif scenario == 5: # response + else: # if scenario == 5: # response transact.comm_params.timeout_connect = 0.2 transact.response_future.set_result(response) resp = asyncio.create_task(transact.execute(False, request)) @@ -134,19 +134,18 @@ async def test_client_protocol_execute_outside(self, use_clc, no_resp): transact = TransactionManager(use_clc, FramerSocket(DecodePDU(False)), 5, False) transact.send = mock.Mock() request = ReadCoilsRequest(address=117, count=5) - response = ReadCoilsResponse(bits=[True, False, True, True, False]) transact.retries = 0 transact.connection_made(mock.AsyncMock()) resp = asyncio.create_task(transact.execute(no_resp, request)) await asyncio.sleep(0.2) data = b"\x00\x00\x12\x34\x00\x06\xff\x01\x01\x02\x00\x04" transact.data_received(data) - response = await resp + result = await resp if no_resp: - assert not response + assert not result else: - assert not response.isError() - assert isinstance(response, ReadCoilsResponse) + assert not result.isError() + assert isinstance(result, ReadCoilsResponse) @pytest.mark.parametrize("use_port", [5098]) @@ -193,7 +192,7 @@ async def test_sync_transaction_execute(self, use_clc, scenario): elif scenario == 4: # wait receive,timeout, no_responses pass transact.comm_params.timeout_connect = 0.1 assert not transact.sync_execute(False, request) - elif scenario == 5: # response + else: # if scenario == 5 # response transact.transport = 1 resp_bytes = transact.framer.buildFrame(response) transact.sync_client.recv = mock.Mock(return_value=resp_bytes) @@ -206,35 +205,47 @@ def test_sync_transaction_receiver(self, use_clc): """Test tracers in disconnect.""" client = ModbusBaseSyncClient(FramerType.SOCKET, 5, use_clc) transact = TransactionManager(use_clc, FramerRTU(DecodePDU(False)), 5, False, sync_client=client) - pytest.fail("TBD!") - transact.send = mock.Mock() - # response = ReadCoilsResponse(bits=[True, False, True, True, False]) + transact.sync_client.send = mock.Mock() + request = ReadCoilsRequest(address=117, count=5) + response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False]) transact.retries = 0 - transact.connection_made(mock.AsyncMock()) - - data = b"\x00\x00\x12\x34\x00\x06\xff\x01\x01\x02\x00\x04" - transact.data_received(data) - # response = await transact.response_future - # assert isinstance(response, ReadCoilsResponse) + transact.transport = 1 + resp_bytes = transact.framer.buildFrame(response) + transact.sync_client.recv = mock.Mock(return_value=resp_bytes) + transact.sync_client.send = mock.Mock() + transact.comm_params.timeout_connect = 0.2 + resp = transact.sync_execute(False, request) + assert response.bits == resp.bits @pytest.mark.parametrize("no_resp", [False, True]) def test_sync_client_protocol_execute_outside(self, use_clc, no_resp): """Test the transaction execute method.""" client = ModbusBaseSyncClient(FramerType.SOCKET, 5, use_clc) transact = TransactionManager(use_clc, FramerRTU(DecodePDU(False)), 5, False, sync_client=client) - pytest.fail("TBD!") - transact.send = mock.Mock() - # request = ReadCoilsRequest(address=117, count=5) - response = ReadCoilsResponse(bits=[True, False, True, True, False]) + request = ReadCoilsRequest(address=117, count=5) + response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False]) transact.retries = 0 - transact.connection_made(mock.AsyncMock()) - # resp = asyncio.create_task(transact.execute(no_resp, request)) - # await asyncio.sleep(0.2) - data = b"\x00\x00\x12\x34\x00\x06\xff\x01\x01\x02\x00\x04" - transact.data_received(data) - # response = await resp + transact.transport = 1 + resp_bytes = transact.framer.buildFrame(response) + transact.sync_client.recv = mock.Mock(return_value=resp_bytes) + transact.sync_client.send = mock.Mock() + result = transact.sync_execute(no_resp, request) if no_resp: - assert not response + assert not result else: - assert not response.isError() + assert not result.isError() assert isinstance(response, ReadCoilsResponse) + + def test_sync_client_protocol_execute_no_pdu(self, use_clc): + """Test the transaction execute method.""" + client = ModbusBaseSyncClient(FramerType.SOCKET, 5, use_clc) + transact = TransactionManager(use_clc, FramerRTU(DecodePDU(False)), 5, False, sync_client=client) + request = ReadCoilsRequest(address=117, count=5) + response = ReadCoilsResponse(bits=[True, False, True, True, False, False, False, False]) + transact.retries = 0 + transact.transport = 1 + resp_bytes = transact.framer.buildFrame(response)[:-1] + transact.sync_client.recv = mock.Mock(side_effect=[resp_bytes, b'']) + transact.sync_client.send = mock.Mock() + result = transact.sync_execute(False, request) + assert not result