Skip to content

Commit 07d9c73

Browse files
authored
Coverage not allowed below 99.5%. (#2765)
1 parent aa121e4 commit 07d9c73

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

examples/server_hook.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import asyncio
1010
import logging
11+
import sys
1112

1213
from pymodbus import FramerType, pymodbus_apply_logging_config
1314
from pymodbus.datastore import (
@@ -19,6 +20,15 @@
1920
from pymodbus.server import ModbusTcpServer
2021

2122

23+
try:
24+
import helper # type: ignore[import-not-found]
25+
except ImportError:
26+
print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\
27+
https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\
28+
for more information.")
29+
sys.exit(-1)
30+
31+
2232
class Manipulator:
2333
"""A Class to run the server."""
2434

@@ -43,8 +53,9 @@ def trace_connect(self, connect: bool) -> None:
4353
txt = "Connected" if connect else "Disconnected"
4454
print(f"---> {txt}")
4555

46-
async def setup(self):
56+
async def setup(self, cmdline):
4757
"""Prepare server."""
58+
args = helper.get_commandline(server=True, description="server hooks", cmdline=cmdline)
4859
pymodbus_apply_logging_config(logging.DEBUG)
4960
datablock = ModbusSequentialDataBlock(0x00, [17] * 100)
5061
context = ModbusServerContext(
@@ -53,11 +64,12 @@ async def setup(self):
5364
),
5465
single=True,
5566
)
67+
address: tuple[str, int] = (args.host if args.host else "", args.port if args.port else 0)
5668
self.server = ModbusTcpServer(
5769
context,
5870
framer=FramerType.SOCKET,
5971
identity=None,
60-
address=("127.0.0.1", 5020),
72+
address=address,
6173
trace_packet=self.trace_packet,
6274
trace_pdu=self.trace_pdu,
6375
trace_connect=self.trace_connect,
@@ -68,10 +80,10 @@ async def run(self):
6880
await self.server.serve_forever()
6981

7082

71-
async def main():
83+
async def main(cmdline=None):
7284
"""Run example."""
7385
server = Manipulator()
74-
await server.setup()
86+
await server.setup(cmdline=cmdline)
7587
await server.run()
7688

7789

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ exclude_also = [
244244
"if __name__ == .__main__.:",
245245
]
246246
skip_covered = true
247-
fail_under = 94.9
247+
fail_under = 99.5
248248

249249
[tool.coverage.html]
250250
directory = "build/cov"

test/examples/test_examples.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from examples.server_async import setup_server
2323
from examples.server_callback import run_callback_server
2424
from examples.server_datamodel import main as run_main_datamodel
25+
from examples.server_hook import main as main_hook_server
2526
from examples.server_sync import run_sync_server
2627
from examples.server_updating import main as main_updating_server
2728
from examples.simple_async_client import run_async_simple_client
@@ -70,6 +71,20 @@ async def test_updating_server(self, use_port, use_host):
7071
await asyncio.sleep(0.1)
7172
client = setup_async_client(cmdline=cmdargs)
7273
await run_async_client(client, modbus_calls=run_a_few_calls)
74+
await asyncio.sleep(10)
75+
await ServerAsyncStop()
76+
await asyncio.sleep(0.1)
77+
task.cancel()
78+
await task
79+
80+
async def test_hook_server(self, use_port, use_host):
81+
"""Test server server hooks."""
82+
cmdargs = ["--port", str(use_port), "--host", use_host]
83+
task = asyncio.create_task(main_hook_server(cmdline=cmdargs))
84+
task.set_name("run main_hook_server")
85+
await asyncio.sleep(0.1)
86+
client = setup_async_client(cmdline=cmdargs)
87+
await run_async_client(client, modbus_calls=run_a_few_calls)
7388
await asyncio.sleep(0.1)
7489
await ServerAsyncStop()
7590
await asyncio.sleep(0.1)

0 commit comments

Comments
 (0)