Repository: pymodbus-dev/pymodbus

Issue:
optimize slow tests.
the slowest tests are:

3.53s test_comm.py::TestCommModbusProtocol::test_connected_multiple
3.31s test_comm.py::TestCommModbusProtocol::test_connected
3.01s test_client_sync.py::TestSynchronousClient::test_basic_sync_serial_client
3.00s test_client_sync.py::TestSynchronousClient::test_serial_client_recv
2.52s test_client_server_sync.py::TestClientServerSyncExamples::test_server_client_twice
2.20s test_examples.py::TestSyncExamples::test_client_calls[serial-rtu-localhost]
1.86s test_client_server_sync.py::TestClientServerSyncExamples::test_combinations[serial-rtu-localhost]
1.85s test_examples.py::TestSyncExamples::test_sync_simple_client[serial-rtu-localhost]
1.53s test_examples.py::TestSyncExamples::test_client_calls[tcp-socket-localhost]
1.53s test_examples.py::TestSyncExamples::test_client_calls[udp-socket-localhost]
1.53s test_examples.py::TestSyncExamples::test_sync_simple_client[tcp-socket-localhost]
1.52s call     test/sub_examples/test_examples.py::TestSyncExamples::test_sync_simple_client[udp-socket-localhost]
1.52s call     test/sub_examples/test_client_server_sync.py::TestClientServerSyncExamples::test_combinations[tcp-socket-localhost]


Most likely the problem is missing use of NULL_MODEM (apart from test_connected*)

Another problem is asyncio.sleep(), which can in many cases be changed to a "wait with timeout".


Discussion:

Resolving patch:
diff --git a/pymodbus/server/async_io.py b/pymodbus/server/async_io.py
--- a/pymodbus/server/async_io.py
+++ b/pymodbus/server/async_io.py
@@ -581,7 +581,7 @@ async def async_stop(cls):
         if os.name == "nt":
             await asyncio.sleep(1)
         else:
-            await asyncio.sleep(0.1)
+            await asyncio.sleep(0)
         cls.active_server = None
 
     @classmethod
@@ -597,7 +597,7 @@ def stop(cls):
         if os.name == "nt":
             time.sleep(10)
         else:
-            time.sleep(0.5)
+            time.sleep(0.1)
 
 
 async def StartAsyncTcpServer(  # pylint: disable=invalid-name,dangerous-default-value
diff --git a/pymodbus/server/simulator/http_server.py b/pymodbus/server/simulator/http_server.py
--- a/pymodbus/server/simulator/http_server.py
+++ b/pymodbus/server/simulator/http_server.py
@@ -256,7 +256,7 @@ async def stop(self):
         self.site = None
         if not self.serving.done():
             self.serving.set_result(True)
-        await asyncio.sleep(0.1)
+        await asyncio.sleep(0)
 
     async def handle_html_static(self, request):
         """Handle static html."""


Test patch:
diff --git a/test/sub_server/test_simulator.py b/test/sub_server/test_simulator.py
--- a/test/sub_server/test_simulator.py
+++ b/test/sub_server/test_simulator.py
@@ -9,6 +9,7 @@
 from pymodbus.datastore import ModbusSimulatorContext
 from pymodbus.datastore.simulator import Cell, CellType, Label
 from pymodbus.server import ModbusSimulatorServer
+from pymodbus.transport import NULLMODEM_HOST
 
 
 FX_READ_BIT = 1
@@ -101,7 +102,7 @@ class TestSimulator:
     default_server_config = {
         "server": {
             "comm": "tcp",
-            "host": "0.0.0.0",
+            "host": NULLMODEM_HOST,
             "port": 5020,
             "ignore_missing_slaves": False,
             "framer": "socket",
@@ -585,5 +586,5 @@ async def test_simulator_server_tcp(self):
         """Test init simulator server."""
         task = ModbusSimulatorServer()
         await task.run_forever(only_start=True)
-        await asyncio.sleep(5)
+        await asyncio.sleep(0.5)
         await task.stop()