Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client async corrections (due to 3.1.2) #1565

Merged
merged 3 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions examples/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ 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)
client.close()
_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)
15 changes: 9 additions & 6 deletions examples/client_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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."
)
Expand Down
9 changes: 7 additions & 2 deletions examples/client_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
4 changes: 3 additions & 1 deletion pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import asyncio
from abc import abstractmethod
from contextlib import suppress

from pymodbus.framer import ModbusFramer
from pymodbus.logging import Log
Expand Down Expand Up @@ -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""

Expand Down
38 changes: 19 additions & 19 deletions test/transport/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -151,15 +151,15 @@ 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()
base.close()

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()
Expand All @@ -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()