Skip to content

Bump coverage to 95,5% #2658

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

Merged
merged 5 commits into from
May 1, 2025
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
Binary file modified doc/source/_static/examples.tgz
Binary file not shown.
Binary file modified doc/source/_static/examples.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions doc/source/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ It is allowed to have multiple client objects that e.g. each communicate with a

Client performance
------------------
There are currently a big performance gap between the 2 clients
(try it on your computer :github:`examples/client_performance.py`).
There are currently a big performance gap between the 2 clients.

This is due to a rather old implementation of the synchronous client, we are currently working to update the client code.
Our aim is to achieve a similar data rate with both clients and at least double the data rate while keeping the stability.
Table below is a test with 1000 calls each reading 10 registers.
Expand Down
16 changes: 0 additions & 16 deletions doc/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ Source: :github:`examples/simple_sync_client.py`
.. literalinclude:: ../../examples/simple_sync_client.py


Client performance sync vs async
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Source: :github:`examples/client_performance.py`

.. literalinclude:: ../../examples/client_performance.py


Advanced examples
-----------------

Expand Down Expand Up @@ -169,15 +162,6 @@ Source: :github:`examples/message_parser.py`
:noindex:


Modbus forwarder
^^^^^^^^^^^^^^^^
Source: :github:`examples/modbus_forwarder.py`

.. automodule:: examples.modbus_forwarder
:undoc-members:
:noindex:


Examples contributions
----------------------

Expand Down
2 changes: 1 addition & 1 deletion examples/client_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def template_call(client):
"""Show complete modbus call, sync version."""
try:
rr = client.read_coils(32, count=1, device_id=DEVICE_ID)
except client_sync.ModbusException as exc:
except client_sync.ModbusException as exc: # pragma: no cover
txt = f"ERROR: exception in pymodbus {exc}"
_logger.error(txt)
raise exc
Expand Down
83 changes: 0 additions & 83 deletions examples/client_performance.py

This file was deleted.

4 changes: 2 additions & 2 deletions examples/client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def setup_sync_client(description=None, cmdline=None):
# UDP setup parameters
# source_address=None,
)
elif args.comm == "serial":
elif args.comm == "serial": # pragma: no cover
client = modbusClient.ModbusSerialClient(
port=args.port, # serial port
# Common optional parameters:
Expand All @@ -97,7 +97,7 @@ def setup_sync_client(description=None, cmdline=None):
# stopbits=1,
# handle_local_echo=False,
)
elif args.comm == "tls":
elif args.comm == "tls": # pragma: no cover
client = modbusClient.ModbusTlsClient(
args.host,
port=args.port,
Expand Down
95 changes: 0 additions & 95 deletions examples/modbus_forwarder.py

This file was deleted.

36 changes: 17 additions & 19 deletions examples/package_test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,11 @@ def __init__(self, comm: CommType):
host,
port=test_port,
)
elif comm == CommType.SERIAL:
else: # if comm == CommType.SERIAL:
host = f"{NULLMODEM_HOST}:{test_port}"
self.client = modbusClient.AsyncModbusSerialClient(
host,
)
else:
raise RuntimeError("ERROR: CommType not implemented")
server_params = self.client.ctx.comm_params.copy()
server_params.source_address = (host, test_port)
self.stub = TransportStub(server_params, True, simulate_server)
Expand All @@ -142,6 +140,7 @@ async def run(self):
await client_calls(self.client)
Log.debug("--> Closing.")
self.client.close()
self.stub.close()


class ServerTester: # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -169,19 +168,14 @@ def __init__(self, comm: CommType):
identity=self.identity,
address=(NULLMODEM_HOST, test_port),
)
elif comm == CommType.SERIAL:
else: # if comm == CommType.SERIAL:
self.server = modbusServer.ModbusSerialServer(
self.context,
framer=FramerType.SOCKET,
identity=self.identity,
port=f"{NULLMODEM_HOST}:{test_port}",
)
else:
raise RuntimeError("ERROR: CommType not implemented")
client_params = self.server.comm_params.copy()
if client_params.source_address:
client_params.host = client_params.source_address[0]
client_params.port = client_params.source_address[1]
client_params.timeout_connect = 1.0
self.stub = TransportStub(client_params, False, simulate_client)
test_port += 1
Expand All @@ -195,7 +189,8 @@ async def run(self):
await self.stub.start_run()
await server_calls(self.stub, (self.comm == CommType.TCP))
Log.debug("--> Shutting down.")
await self.server.shutdown()
self.stub.close()
self.server.close()


async def main(comm: CommType, use_server: bool):
Expand All @@ -215,11 +210,11 @@ async def client_calls(client):
Log.debug("--> Client calls starting.")
try:
resp = await client.read_holding_registers(address=124, count=4, device_id=1)
except ModbusException as exc:
except ModbusException as exc: # pragma: no cover
txt = f"ERROR: exception in pymodbus {exc}"
Log.error(txt)
return
if resp.isError():
if resp.isError(): # pragma: no cover
txt = "ERROR: pymodbus returned an error!"
Log.error(txt)
await asyncio.sleep(1)
Expand All @@ -231,7 +226,7 @@ async def server_calls(transport: ModbusProtocol, is_tcp: bool):
Log.debug("--> Server calls starting.")

if is_tcp:
request = b'\x00\x02\x00\x00\x00\x06\x01\x03\x00\x00\x00\x01'
request = b'\x00\x02\x00\x00\x00\x06\x00\x03\x00\x00\x00\x01'
else:
# 2 responses:
# response = b'\x00\x02\x00\x00\x00\x06\x01\x03\x00\x00\x00\x01' +
Expand All @@ -247,7 +242,7 @@ def simulate_server(transport: ModbusProtocol, is_tcp: bool, request: bytes):
"""Respond to request at transport level."""
Log.debug("--> Server simulator called with request {}.", request, ":hex")
if is_tcp:
response = b'\x00\x01\x00\x00\x00\x06\x00\x03\x00\x7c\x00\x04'
response = b'\x00\x01\x00\x00\x00\x06\x01\x03\x00\x7c\x00\x04'
else:
response = b'\x01\x03\x08\x00\x05\x00\x05\x00\x00\x00\x00\x0c\xd7'

Expand All @@ -260,12 +255,15 @@ def simulate_server(transport: ModbusProtocol, is_tcp: bool, request: bytes):

def simulate_client(_transport: ModbusProtocol, _is_tcp: bool, response: bytes):
"""Respond to request at transport level."""
Log.debug("--> Client simulator called with response {}.", response, ":hex")
Log.debug("--> Client simulator called with response {}.", response, ":hex") # pragma: no cover

async def run_test():
"""Run whole test."""
await main(CommType.SERIAL, False)
await main(CommType.SERIAL, True)
await main(CommType.TCP, False)
await main(CommType.TCP, True)

if __name__ == "__main__":
# True for Server test, False for Client test
asyncio.run(main(CommType.SERIAL, False), debug=True)
asyncio.run(main(CommType.SERIAL, True), debug=True)
asyncio.run(main(CommType.TCP, False), debug=True)
asyncio.run(main(CommType.TCP, True), debug=True)
asyncio.run(run_test(), debug=True)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ exclude_also = [
"if __name__ == .__main__.:",
]
skip_covered = true
fail_under = 93.0
fail_under = 95.5

[tool.coverage.html]
directory = "build/cov"
Expand Down
1 change: 1 addition & 0 deletions test/examples/test_client_server_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ async def test_server_client_twice(self, mock_server, use_comm, mock_clc):
await run_async_client(test_client, modbus_calls=run_a_few_calls)
await asyncio.sleep(0.5)
await run_async_client(test_client, modbus_calls=run_a_few_calls)
await run_async_client(test_client)

async def test_client_no_server(self, mock_clc):
"""Run async client without server."""
Expand Down
1 change: 1 addition & 0 deletions test/examples/test_client_server_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def test_server_client_twice(self, mock_cls, mock_clc, use_comm):
run_sync_client(test_client, modbus_calls=run_a_few_calls)
sleep(SLEEPING)
run_sync_client(test_client, modbus_calls=run_a_few_calls)
run_sync_client(test_client)
ServerStop()

def test_client_no_server(self, mock_clc):
Expand Down
Loading
Loading