44import contextlib
55import ssl
66import unittest
7- from unittest .mock import patch
87
98import pytest
109
1110from pymodbus .client .asynchronous .async_io import (
1211 AsyncioModbusSerialClient ,
1312 ReconnectingAsyncioModbusTcpClient ,
1413 ReconnectingAsyncioModbusTlsClient ,
14+ ReconnectingAsyncioModbusUdpClient ,
1515)
1616from pymodbus .client .asynchronous .serial import AsyncModbusSerialClient
1717from pymodbus .client .asynchronous .tls import AsyncModbusTLSClient
1818from pymodbus .client .asynchronous .tcp import AsyncModbusTCPClient
19+ from pymodbus .client .asynchronous .udp import AsyncModbusUDPClient
1920from pymodbus .transaction import (
2021 ModbusAsciiFramer ,
2122 ModbusBinaryFramer ,
@@ -50,11 +51,16 @@ class TestAsynchronousClient:
5051 # -----------------------------------------------------------------------#
5152 # Test TCP Client client
5253 # -----------------------------------------------------------------------#
53- def test_tcp_asyncio_client (self ):
54+ def test_tcp_no_asyncio_client (self ):
5455 """Test the TCP client."""
5556 client = AsyncModbusTCPClient ()
57+ assert asyncio .iscoroutine (client )
58+
59+ async def test_tcp_asyncio_client (self ):
60+ """Test the TCP client."""
61+ client = await AsyncModbusTCPClient ()
5662 assert isinstance (client , ReconnectingAsyncioModbusTcpClient ) # nosec
57- # assert isinstance(client.framer, ModbusSocketFramer) # nosec
63+ assert isinstance (client .framer , ModbusSocketFramer ) # nosec
5864 assert client .port == 502 # nosec
5965
6066 client .stop ()
@@ -64,9 +70,14 @@ def test_tcp_asyncio_client(self):
6470 # Test TLS Client client
6571 # -----------------------------------------------------------------------#
6672
67- def test_tls_asyncio_client (self ):
73+ def test_tls_no_asyncio_client (self ):
6874 """Test the TLS AsyncIO client."""
6975 client = AsyncModbusTLSClient ()
76+ assert asyncio .iscoroutine (client )
77+
78+ async def test_tls_asyncio_client (self ):
79+ """Test the TLS AsyncIO client."""
80+ client = await AsyncModbusTLSClient ()
7081 assert isinstance (client , ReconnectingAsyncioModbusTlsClient ) # nosec
7182 assert isinstance (client .framer , ModbusTlsFramer ) # nosec
7283 assert isinstance (client .sslctx , ssl .SSLContext ) # nosec
@@ -78,22 +89,30 @@ def test_tls_asyncio_client(self):
7889 # -----------------------------------------------------------------------#
7990 # Test UDP client
8091 # -----------------------------------------------------------------------#
81- def test_udp_asyncio_client (self ):
92+ def test_udp_no_asyncio_client (self ):
8293 """Test the udp asyncio client"""
83- # client = AsyncModbusUDPClient()
84- # assert isinstance(client, ReconnectingAsyncioModbusTcpClient) # nosec
85- # assert isinstance(client.framer, ModbusSocketFramer) # nosec
86- # assert client.port == 502 # nosec
94+ client = AsyncModbusUDPClient ()
95+ assert asyncio .iscoroutine (client )
8796
88- # client.stop()
89- # assert client.host is None # nosec
97+ async def test_udp_asyncio_client (self ):
98+ """Test the udp asyncio client"""
99+ client = await AsyncModbusUDPClient ()
100+ assert isinstance (client , ReconnectingAsyncioModbusUdpClient ) # nosec
101+ assert isinstance (client .framer , ModbusSocketFramer ) # nosec
102+ assert client .port == 502 # nosec
103+
104+ client .stop ()
105+ assert client .host is None # nosec
90106
91107 # -----------------------------------------------------------------------#
92108 # Test Serial client
93109 # -----------------------------------------------------------------------#
94110
95- @patch ("asyncio.get_event_loop" )
96- @patch ("asyncio.gather" , side_effect = mock_asyncio_gather )
111+ def test_serial_no_asyncio_client (self ):
112+ """Test that AsyncModbusSerialClient instantiates AsyncioModbusSerialClient for asyncio scheduler."""
113+ client = AsyncModbusSerialClient (port = "not here" , framer = ModbusRtuFramer )
114+ assert asyncio .iscoroutine (client )
115+
97116 @pytest .mark .parametrize (
98117 "framer" ,
99118 [
@@ -103,20 +122,14 @@ def test_udp_asyncio_client(self):
103122 ModbusAsciiFramer ,
104123 ],
105124 )
106- @pytest .mark .asyncio
107125 async def test_serial_asyncio_client (
108126 self ,
109- mock_gather , # pylint: disable=unused-argument
110- mock_event_loop ,
111127 framer ,
112- ): # pylint: disable=unused-argument
128+ ):
113129 """Test that AsyncModbusSerialClient instantiates AsyncioModbusSerialClient for asyncio scheduler."""
114- loop = asyncio .get_event_loop ()
115- loop .is_running .side_effect = lambda : False
116- client = AsyncModbusSerialClient (
130+ client = await AsyncModbusSerialClient (
117131 framer = framer ,
118132 port = pytest .SERIAL_PORT ,
119- loop = loop ,
120133 baudrate = 19200 ,
121134 parity = "E" ,
122135 stopbits = 2 ,
@@ -132,7 +145,6 @@ async def test_serial_asyncio_client(
132145 assert client .bytesize == 7 # nosec
133146 asyncio .wait_for (client .connect (), timeout = 1 )
134147 client .stop ()
135- loop .stop ()
136148
137149
138150# ---------------------------------------------------------------------------#
0 commit comments