1717from pymodbus .client .asynchronous .tls import AsyncModbusTLSClient
1818from pymodbus .client .asynchronous .udp import AsyncModbusUDPClient
1919from pymodbus .client .asynchronous .tornado import (
20- AsyncModbusSerialClient as AsyncTornadoModbusSerialClient
20+ AsyncModbusSerialClient as AsyncTornadoModbusSerialClient ,
21+ )
22+ from pymodbus .client .asynchronous .tornado import (
23+ AsyncModbusTCPClient as AsyncTornadoModbusTcpClient ,
24+ )
25+ from pymodbus .client .asynchronous .tornado import (
26+ AsyncModbusUDPClient as AsyncTornadoModbusUdoClient ,
2127)
22- from pymodbus .client .asynchronous .tornado import AsyncModbusTCPClient as AsyncTornadoModbusTcpClient
23- from pymodbus .client .asynchronous .tornado import AsyncModbusUDPClient as AsyncTornadoModbusUdoClient
2428from pymodbus .client .asynchronous import schedulers
2529from pymodbus .factory import ClientDecoder
2630from pymodbus .exceptions import ConnectionException
@@ -58,20 +62,26 @@ class TestAsynchronousClient:
5862 def test_tcp_twisted_client (self ): # pylint: disable=no-self-use
5963 """Test the TCP Twisted client."""
6064 with patch ("twisted.internet.reactor" ):
65+
6166 def test_callback (client ): # pylint: disable=unused-argument
6267 pass
6368
64- AsyncModbusTCPClient (schedulers .REACTOR ,
65- framer = ModbusSocketFramer (ClientDecoder ()),
66- callback = test_callback ,
67- errback = test_callback )
69+ AsyncModbusTCPClient (
70+ schedulers .REACTOR ,
71+ framer = ModbusSocketFramer (ClientDecoder ()),
72+ callback = test_callback ,
73+ errback = test_callback ,
74+ )
6875
6976 @patch ("pymodbus.client.asynchronous.tornado.IOLoop" )
7077 @patch ("pymodbus.client.asynchronous.tornado.IOStream" )
71- def test_tcp_tornado_client (self , mock_iostream , mock_ioloop ): # pylint: disable=no-self-use,unused-argument
78+ def test_tcp_tornado_client (
79+ self , mock_iostream , mock_ioloop
80+ ): # pylint: disable=no-self-use,unused-argument
7281 """Test the TCP tornado client client initialize"""
73- protocol , future = AsyncModbusTCPClient ( # NOSOANR pylint: disable=unpacking-non-sequence
74- schedulers .IO_LOOP , framer = ModbusSocketFramer (ClientDecoder ()))
82+ protocol , future = AsyncModbusTCPClient ( # NOSONAR pylint: disable=unpacking-non-sequence
83+ schedulers .IO_LOOP , framer = ModbusSocketFramer (ClientDecoder ())
84+ )
7585 client = future .result ()
7686 assert isinstance (client , AsyncTornadoModbusTcpClient ) # nosec
7787 assert not list (client .transaction ) # nosec
@@ -94,7 +104,9 @@ def handle_failure(failure):
94104
95105 @patch ("asyncio.get_event_loop" )
96106 @patch ("asyncio.gather" )
97- def test_tcp_asyncio_client (self , mock_gather , mock_loop ): # pylint: disable=no-self-use,unused-argument
107+ def test_tcp_asyncio_client (
108+ self , mock_gather , mock_loop
109+ ): # pylint: disable=no-self-use,unused-argument
98110 """Test the TCP Twisted client."""
99111 pytest .skip ("TBD" )
100112
@@ -104,7 +116,9 @@ def test_tcp_asyncio_client(self, mock_gather, mock_loop): # pylint: disable=no
104116
105117 def test_tls_asyncio_client (self ): # pylint: disable=no-self-use
106118 """Test the TLS AsyncIO client."""
107- _ , client = AsyncModbusTLSClient (schedulers .ASYNC_IO ) # NOSONAR pylint: disable=unpacking-non-sequence
119+ _ , client = AsyncModbusTLSClient ( # NOSONAR pylint: disable=unpacking-non-sequence
120+ schedulers .ASYNC_IO
121+ )
108122 assert isinstance (client , ReconnectingAsyncioModbusTlsClient ) # nosec
109123 assert isinstance (client .framer , ModbusTlsFramer ) # nosec
110124 assert isinstance (client .sslctx , ssl .SSLContext ) # nosec
@@ -119,10 +133,13 @@ def test_tls_asyncio_client(self): # pylint: disable=no-self-use
119133
120134 @patch ("pymodbus.client.asynchronous.tornado.IOLoop" )
121135 @patch ("pymodbus.client.asynchronous.tornado.IOStream" )
122- def test_udp_tornado_client (self , mock_iostream , mock_ioloop ): # pylint: disable=no-self-use,unused-argument
136+ def test_udp_tornado_client (
137+ self , mock_iostream , mock_ioloop
138+ ): # pylint: disable=no-self-use,unused-argument
123139 """Test the udp tornado client client initialize"""
124140 protocol , future = AsyncModbusUDPClient ( # NOSONAR pylint: disable=unpacking-non-sequence
125- schedulers .IO_LOOP , framer = ModbusSocketFramer (ClientDecoder ()))
141+ schedulers .IO_LOOP , framer = ModbusSocketFramer (ClientDecoder ())
142+ )
126143 client = future .result ()
127144 assert isinstance (client , AsyncTornadoModbusUdoClient ) # nosec
128145 assert not list (client .transaction ) # nosec
@@ -144,12 +161,15 @@ def handle_failure(failure):
144161 def test_udp_twisted_client (self ): # pylint: disable=no-self-use
145162 """Test the udp twisted client client initialize"""
146163 with pytest .raises (NotImplementedError ):
147- AsyncModbusUDPClient (schedulers .REACTOR ,
148- framer = ModbusSocketFramer (ClientDecoder ()))
164+ AsyncModbusUDPClient (
165+ schedulers .REACTOR , framer = ModbusSocketFramer (ClientDecoder ())
166+ )
149167
150168 @patch ("asyncio.get_event_loop" )
151169 @patch ("asyncio.gather" , side_effect = mock_asyncio_gather )
152- def test_udp_asyncio_client (self , mock_gather , mock_event_loop ): # pylint: disable=no-self-use,unused-argument
170+ def test_udp_asyncio_client (
171+ self , mock_gather , mock_event_loop
172+ ): # pylint: disable=no-self-use,unused-argument
153173 """Test the udp asyncio client"""
154174 pytest .skip ("TBD" )
155175
@@ -161,51 +181,78 @@ def test_udp_asyncio_client(self, mock_gather, mock_event_loop): # pylint: disa
161181 sys .platform == "win32" and platform .python_implementation () == "PyPy" ,
162182 reason = "Twisted serial requires pywin32 which is not compatible with PyPy" ,
163183 )
164- @pytest .mark .parametrize ("method, framer" , [("rtu" , ModbusRtuFramer ),
165- ("socket" , ModbusSocketFramer ),
166- ("binary" , ModbusBinaryFramer ),
167- ("ascii" , ModbusAsciiFramer )])
184+ @pytest .mark .parametrize (
185+ "method, framer" ,
186+ [
187+ ("rtu" , ModbusRtuFramer ),
188+ ("socket" , ModbusSocketFramer ),
189+ ("binary" , ModbusBinaryFramer ),
190+ ("ascii" , ModbusAsciiFramer ),
191+ ],
192+ )
168193 def test_serial_twisted_client (self , method , framer ): # pylint: disable=no-self-use
169194 """Test the serial twisted client client initialize"""
170195 with patch ("serial.Serial" ):
171- from twisted .internet .serialport import SerialPort # pylint: disable=import-outside-toplevel
172- with maybe_manage (sys .platform == "win32" , patch .object (
173- SerialPort , "_finishPortSetup" )):
196+ from twisted .internet .serialport import ( # pylint: disable=import-outside-toplevel
197+ SerialPort ,
198+ )
199+
200+ with maybe_manage (
201+ sys .platform == "win32" , patch .object (SerialPort , "_finishPortSetup" )
202+ ):
174203 with patch ("twisted.internet.reactor" ):
175204
176205 protocol , client = AsyncModbusSerialClient ( # NOSONAR pylint: disable=unpacking-non-sequence
177206 schedulers .REACTOR ,
178207 method = method ,
179208 port = pytest .SERIAL_PORT ,
180- proto_cls = ModbusSerClientProtocol
209+ proto_cls = ModbusSerClientProtocol ,
181210 )
182211
183212 assert isinstance (client , SerialPort ) # nosec
184213 assert isinstance (client .protocol , ModbusSerClientProtocol ) # nosec
185214 assert not list (client .protocol .transaction ) # nosec
186215 assert isinstance (client .protocol .framer , framer ) # nosec
187- assert client .protocol ._connected # nosec pylint: disable=protected-access
216+ assert (
217+ client .protocol ._connected # nosec pylint: disable=protected-access
218+ )
188219
189220 def handle_failure (failure ):
190- assert (isinstance (failure .exception (), ConnectionException )) # nosec
221+ assert isinstance (
222+ failure .exception (), ConnectionException # nosec
223+ )
191224
192- response = client .protocol ._buildResponse (0x00 ) # pylint: disable=protected-access
225+ response = client .protocol ._buildResponse ( # pylint: disable=protected-access
226+ 0x00
227+ )
193228 response .addCallback (handle_failure )
194229
195- assert client .protocol ._connected # nosec pylint: disable=protected-access
230+ assert (
231+ client .protocol ._connected # nosec pylint: disable=protected-access
232+ )
196233 client .protocol .close ()
197234 protocol .stop ()
198- assert not client .protocol ._connected # nosec pylint: disable=protected-access
235+ assert (
236+ not client .protocol ._connected # nosec pylint: disable=protected-access
237+ )
199238
200- @pytest .mark .parametrize ("method, framer" , [("rtu" , ModbusRtuFramer ),
201- ("socket" , ModbusSocketFramer ),
202- ("binary" , ModbusBinaryFramer ),
203- ("ascii" , ModbusAsciiFramer )])
239+ @pytest .mark .parametrize (
240+ "method, framer" ,
241+ [
242+ ("rtu" , ModbusRtuFramer ),
243+ ("socket" , ModbusSocketFramer ),
244+ ("binary" , ModbusBinaryFramer ),
245+ ("ascii" , ModbusAsciiFramer ),
246+ ],
247+ )
204248 def test_serial_tornado_client (self , method , framer ): # pylint: disable=no-self-use
205249 """Test the serial tornado client client initialize"""
206- with maybe_manage (sys .platform in {"darwin" , "win32" }, patch .object (Serial , "open" )):
250+ with maybe_manage (
251+ sys .platform in {"darwin" , "win32" }, patch .object (Serial , "open" )
252+ ):
207253 protocol , future = AsyncModbusSerialClient ( # NOSONAR pylint: disable=unpacking-non-sequence
208- schedulers .IO_LOOP , method = method , port = pytest .SERIAL_PORT )
254+ schedulers .IO_LOOP , method = method , port = pytest .SERIAL_PORT
255+ )
209256 client = future .result ()
210257 assert isinstance (client , AsyncTornadoModbusSerialClient ) # nosec
211258 assert not list (client .transaction ) # nosec
@@ -226,18 +273,35 @@ def handle_failure(failure):
226273
227274 @patch ("asyncio.get_event_loop" )
228275 @patch ("asyncio.gather" , side_effect = mock_asyncio_gather )
229- @pytest .mark .parametrize ("method, framer" , [("rtu" , ModbusRtuFramer ),
230- ("socket" , ModbusSocketFramer ),
231- ("binary" , ModbusBinaryFramer ),
232- ("ascii" , ModbusAsciiFramer )])
233- def test_serial_asyncio_client (self , mock_gather , # pylint: disable=no-self-use,unused-argument
234- mock_event_loop , method , framer ): # pylint: disable=unused-argument
276+ @pytest .mark .parametrize (
277+ "method, framer" ,
278+ [
279+ ("rtu" , ModbusRtuFramer ),
280+ ("socket" , ModbusSocketFramer ),
281+ ("binary" , ModbusBinaryFramer ),
282+ ("ascii" , ModbusAsciiFramer ),
283+ ],
284+ )
285+ def test_serial_asyncio_client ( # pylint: disable=no-self-use
286+ self ,
287+ mock_gather , # pylint: disable=unused-argument
288+ mock_event_loop ,
289+ method ,
290+ framer ,
291+ ): # pylint: disable=unused-argument
235292 """Test that AsyncModbusSerialClient instantiates AsyncioModbusSerialClient for asyncio scheduler."""
236293 loop = asyncio .get_event_loop ()
237294 loop .is_running .side_effect = lambda : False
238295 loop , client = AsyncModbusSerialClient ( # NOSONAR pylint: disable=unpacking-non-sequence
239- schedulers .ASYNC_IO , method = method , port = pytest .SERIAL_PORT , loop = loop ,
240- baudrate = 19200 , parity = "E" , stopbits = 2 , bytesize = 7 )
296+ schedulers .ASYNC_IO ,
297+ method = method ,
298+ port = pytest .SERIAL_PORT ,
299+ loop = loop ,
300+ baudrate = 19200 ,
301+ parity = "E" ,
302+ stopbits = 2 ,
303+ bytesize = 7 ,
304+ )
241305 assert isinstance (client , AsyncioModbusSerialClient ) # nosec
242306 assert isinstance (client .framer , framer ) # nosec
243307 assert client .port == pytest .SERIAL_PORT # nosec
@@ -248,6 +312,7 @@ def test_serial_asyncio_client(self, mock_gather, # pylint: disable=no-self-use
248312 client .stop ()
249313 loop .stop ()
250314
315+
251316# ---------------------------------------------------------------------------#
252317# Main
253318# ---------------------------------------------------------------------------#
0 commit comments