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

Stabilize windows tests. #1567

Merged
merged 7 commits into from
Jun 2, 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
4 changes: 2 additions & 2 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 6 additions & 8 deletions pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,10 +15,6 @@
from pymodbus.transport.serial_asyncio import create_serial_connection


with suppress(ImportError):
pass


class BaseTransport:
"""Base class for transport types.

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions test/test_server_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down
23 changes: 15 additions & 8 deletions test/transport/test_comm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test transport."""
import asyncio
import os
import sys
import time
from tempfile import gettempdir

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