diff --git a/pymodbus/client/base.py b/pymodbus/client/base.py index fc463097c..39c3ca101 100644 --- a/pymodbus/client/base.py +++ b/pymodbus/client/base.py @@ -88,8 +88,8 @@ def __init__( # pylint: disable=too-many-arguments close_comm_on_error: bool = Defaults.CloseCommOnError, strict: bool = Defaults.Strict, broadcast_enable: bool = Defaults.BroadcastEnable, - reconnect_delay: int = Defaults.ReconnectDelay, - reconnect_delay_max: int = Defaults.ReconnectDelayMax, + reconnect_delay: int = 0.1, + reconnect_delay_max: int = 300, on_reconnect_callback: Callable[[], None] | None = None, **kwargs: Any, ) -> None: diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 80d5263e3..b03222b9a 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -1,9 +1,11 @@ """Base for all transport types.""" +# mypy: disable-error-code="name-defined" +# needed because asyncio.Server is not defined (to mypy) in v3.8.16 from __future__ import annotations import asyncio -import platform import ssl +import sys from contextlib import suppress from dataclasses import dataclass from typing import Any, Callable, Coroutine @@ -13,10 +15,6 @@ from pymodbus.transport.serial_asyncio import create_serial_connection -with suppress(ImportError): - pass - - class BaseTransport: """Base class for transport types. @@ -93,7 +91,7 @@ def __init__( ) self.reconnect_delay_current: float = 0 - self.transport: asyncio.BaseTransport | asyncio.Server = None # type: ignore[name-defined] + self.transport: asyncio.BaseTransport | asyncio.Server = None self.protocol: asyncio.BaseProtocol = None with suppress(RuntimeError): self.loop: asyncio.AbstractEventLoop = asyncio.get_running_loop() @@ -107,7 +105,7 @@ def __init__( # ----------------------------- # def setup_unix(self, setup_server: bool, host: str): """Prepare transport unix""" - if platform.system().lower() == "windows": + if sys.platform.startswith("win"): raise RuntimeError("Modbus_unix is not supported on Windows!") self.comm_params.check_done() self.comm_params.done = True @@ -366,7 +364,7 @@ def close(self, reconnect: bool = False) -> None: Log.debug( "Waiting {} {} ms reconnecting.", self.comm_params.comm_name, - self.reconnect_delay_current, + self.reconnect_delay_current * 1000, ) self.reconnect_timer = self.loop.call_later( self.reconnect_delay_current, diff --git a/test/test_server_task.py b/test/test_server_task.py index 0281dbf0b..df382c65d 100755 --- a/test/test_server_task.py +++ b/test/test_server_task.py @@ -144,7 +144,7 @@ def helper_config(request, def_type): @pytest.mark.parametrize("comm", TEST_TYPES) async def test_async_task_no_server(comm): """Test normal client/server handling.""" - run_server, server_args, run_client, client_args = helper_config(comm, "async") + _run_server, _server_args, run_client, client_args = helper_config(comm, "async") client = run_client(**client_args) try: await client.connect() @@ -215,11 +215,11 @@ async def test_async_task_reuse(comm): @pytest.mark.xdist_group(name="server_serialize") @pytest.mark.parametrize("comm", TEST_TYPES) -async def xtest_async_task_server_stop(comm): +async def test_async_task_server_stop(comm): """Test normal client/server handling.""" run_server, server_args, run_client, client_args = helper_config(comm, "async") task = asyncio.create_task(run_server(**server_args)) - await asyncio.sleep(0.1) + await asyncio.sleep(0.5) on_reconnect_callback = mock.Mock() @@ -243,12 +243,10 @@ async def xtest_async_task_server_stop(comm): await asyncio.sleep(1) timer_allowed = 100 - while not client.transport: + while not client.transport and timer_allowed: await asyncio.sleep(0.1) timer_allowed -= 1 - if not timer_allowed: - pytest.fail("client do not reconnect") - assert client.transport + assert client.transport, "client do not reconnect" # TBD on_reconnect_callback.assert_called() rr = await client.read_coils(1, 1, slave=0x01) diff --git a/test/transport/test_comm.py b/test/transport/test_comm.py index 4ec158893..e810fa332 100644 --- a/test/transport/test_comm.py +++ b/test/transport/test_comm.py @@ -1,6 +1,7 @@ """Test transport.""" import asyncio import os +import sys import time from tempfile import gettempdir @@ -363,13 +364,19 @@ async def test_connect_reconnect(self): client.setup_tcp(False, "localhost", 5101) assert await client.transport_connect() != (None, None) server.close() - await asyncio.sleep(1) - assert not client.transport - assert client.reconnect_timer - assert client.reconnect_delay_current == 2 * client.comm_params.reconnect_delay - await asyncio.sleep(client.reconnect_delay_current * 1.2) - assert client.transport - assert client.reconnect_timer - assert client.reconnect_delay_current == client.comm_params.reconnect_delay + count = 100 + while client.transport and count: + await asyncio.sleep(0.1) + count -= 1 + if not sys.platform.startswith("win"): + assert not client.transport + assert client.reconnect_timer + assert ( + client.reconnect_delay_current == 2 * client.comm_params.reconnect_delay + ) + await asyncio.sleep(client.reconnect_delay_current * 1.2) + assert client.transport + assert client.reconnect_timer + assert client.reconnect_delay_current == client.comm_params.reconnect_delay client.close() server.close()