From c19b5039656da3c9305e271a334cd9f165ee5b2e Mon Sep 17 00:00:00 2001 From: jan iversen Date: Thu, 1 Jun 2023 19:40:15 +0200 Subject: [PATCH] Client async corrections (due to 3.1.2) (#1565) --- examples/client_async.py | 10 +++++++-- examples/client_calls.py | 15 +++++++------ examples/client_payload.py | 9 ++++++-- pymodbus/transport/transport.py | 4 +++- test/transport/test_basic.py | 38 ++++++++++++++++----------------- 5 files changed, 46 insertions(+), 30 deletions(-) diff --git a/examples/client_async.py b/examples/client_async.py index 6874d5262..c9df9a9de 100755 --- a/examples/client_async.py +++ b/examples/client_async.py @@ -121,6 +121,7 @@ async def run_async_client(client, modbus_calls=None): """Run sync client.""" _logger.info("### Client starting") await client.connect() + print("jan " + str(client.connected)) assert client.connected if modbus_calls: await modbus_calls(client) @@ -128,6 +129,11 @@ async def run_async_client(client, modbus_calls=None): _logger.info("### End of Program") -if __name__ == "__main__": +async def helper(): + """Combine the setup and run""" testclient = setup_async_client(description="Run asynchronous client.") - asyncio.run(run_async_client(testclient), debug=True) + await run_async_client(testclient) + + +if __name__ == "__main__": + asyncio.run(helper(), debug=True) diff --git a/examples/client_calls.py b/examples/client_calls.py index 7a2ac0a33..0a7f6134e 100755 --- a/examples/client_calls.py +++ b/examples/client_calls.py @@ -177,11 +177,11 @@ async def _handle_holding_registers(client): "read_address": 1, "read_count": 8, "write_address": 1, - "write_registers": [256, 128, 100, 50, 25, 10, 5, 1], + "values": [256, 128, 100, 50, 25, 10, 5, 1], } _check_call(await client.readwrite_registers(slave=SLAVE, **arguments)) rr = _check_call(await client.read_holding_registers(1, 8, slave=SLAVE)) - assert rr.registers == arguments["write_registers"] + assert rr.registers == arguments["values"] async def _handle_input_registers(client): @@ -289,11 +289,14 @@ def run_sync_calls(client): template_call(client) +async def helper(): + """Combine the setup and run""" + testclient = setup_async_client(description="Run asynchronous client.") + await run_async_client(testclient, modbus_calls=run_async_calls) + + if __name__ == "__main__": - testclient = setup_async_client( - description="Run modbus calls in asynchronous client." - ) - asyncio.run(run_async_client(testclient, modbus_calls=run_async_calls)) + asyncio.run(helper()) testclient = setup_sync_client( description="Run modbus calls in synchronous client." ) diff --git a/examples/client_payload.py b/examples/client_payload.py index 3ab16355c..9d0073e2f 100755 --- a/examples/client_payload.py +++ b/examples/client_payload.py @@ -136,6 +136,11 @@ async def run_payload_calls(client): print("\n") +async def helper(): + """Combine the setup and run""" + testclient = setup_async_client(description="Run asynchronous client.") + await run_async_client(testclient, modbus_calls=run_payload_calls) + + if __name__ == "__main__": - testclient = setup_async_client(description="Run payload client.") - asyncio.run(run_async_client(testclient, modbus_calls=run_payload_calls)) + asyncio.run(helper()) diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 0c089cb6d..622a25469 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -3,6 +3,7 @@ import asyncio from abc import abstractmethod +from contextlib import suppress from pymodbus.framer import ModbusFramer from pymodbus.logging import Log @@ -44,7 +45,8 @@ def __init__( # properties, can be read, but may not be mingled with self.reconnect_delay_current = self.reconnect_delay self.transport: asyncio.BaseTransport = None - self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() + with suppress(RuntimeError): + self.loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() self.reconnect_timer: asyncio.TimerHandle = None self.recv_buffer: bytes = b"" diff --git a/test/transport/test_basic.py b/test/transport/test_basic.py index c75829e7b..97485becd 100644 --- a/test/transport/test_basic.py +++ b/test/transport/test_basic.py @@ -25,7 +25,7 @@ def __init__(self): self.close = mock.MagicMock() @classmethod - def setup_BaseTransport(self): + async def setup_BaseTransport(self): """Create base object.""" base = BaseTransport( self.base_comm_name, @@ -40,9 +40,9 @@ def setup_BaseTransport(self): base.cb_handle_data = mock.MagicMock() return base - def test_properties(self): + async def test_properties(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() assert self.base_comm_name == base.comm_name assert self.base_framer == base.framer assert self.base_reconnect_delay == base.reconnect_delay @@ -53,16 +53,16 @@ def test_properties(self): async def test_magic(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() base.close = mock.MagicMock() async with base: pass base.close.assert_called_once() assert str(base) == f"BaseTransport({self.base_comm_name})" - def test_connection_made(self): + async def test_connection_made(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() transport = self.dummy_transport() base.connection_made(transport) assert base.transport == transport @@ -73,9 +73,9 @@ def test_connection_made(self): base.cb_handle_data.assert_not_called() base.close() - def test_connection_lost(self): + async def test_connection_lost(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() transport = self.dummy_transport() base.connection_lost(transport) assert not base.transport @@ -86,9 +86,9 @@ def test_connection_lost(self): base.cb_handle_data.assert_not_called() base.close() - def test_close_simple(self): + async def test_close_simple(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() transport = self.dummy_transport() base.connection_made(transport) base.cb_connection_made.reset_mock() @@ -105,9 +105,9 @@ def test_close_simple(self): assert not base.recv_buffer assert not base.reconnect_timer - def test_close_reconnect(self): + async def test_close_reconnect(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() transport = self.dummy_transport() base.connection_made(transport) base.reconnect_timer = None @@ -116,16 +116,16 @@ def test_close_reconnect(self): base.close() assert not base.reconnect_timer - def test_reset_delay(self): + async def test_reset_delay(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() base.reconnect_delay_current = self.base_reconnect_delay + 1 base.reset_delay() assert base.reconnect_delay_current == self.base_reconnect_delay async def test_connect(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() base.reconnect_delay_current = self.base_reconnect_delay + 1 base.close(reconnect=True) base.complete_connect() @@ -139,7 +139,7 @@ async def test_connect(self): async def test_reconnect(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() transport = self.dummy_transport() base.connection_made(transport) base.connect = mock.MagicMock() @@ -151,7 +151,7 @@ async def test_reconnect(self): async def test_datagram(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() base.data_received = mock.MagicMock() base.datagram_received(b"abc", "127.0.0.1") base.data_received.assert_called_once() @@ -159,7 +159,7 @@ async def test_datagram(self): async def test_receive(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() base.cb_handle_data = mock.MagicMock(return_value=2) base.data_received(b"123456") base.cb_handle_data.assert_called_once() @@ -170,6 +170,6 @@ async def test_receive(self): async def test_send(self): """Test properties.""" - base = self.setup_BaseTransport() + base = await self.setup_BaseTransport() await base.send(b"abc") base.close()