@@ -119,13 +119,11 @@ def __init__(self, comm: CommType):
119119 host ,
120120 port = test_port ,
121121 )
122- elif comm == CommType .SERIAL :
122+ else : # if comm == CommType.SERIAL:
123123 host = f"{ NULLMODEM_HOST } :{ test_port } "
124124 self .client = modbusClient .AsyncModbusSerialClient (
125125 host ,
126126 )
127- else :
128- raise RuntimeError ("ERROR: CommType not implemented" )
129127 server_params = self .client .ctx .comm_params .copy ()
130128 server_params .source_address = (host , test_port )
131129 self .stub = TransportStub (server_params , True , simulate_server )
@@ -142,6 +140,7 @@ async def run(self):
142140 await client_calls (self .client )
143141 Log .debug ("--> Closing." )
144142 self .client .close ()
143+ self .stub .close ()
145144
146145
147146class ServerTester : # pylint: disable=too-few-public-methods
@@ -169,19 +168,14 @@ def __init__(self, comm: CommType):
169168 identity = self .identity ,
170169 address = (NULLMODEM_HOST , test_port ),
171170 )
172- elif comm == CommType .SERIAL :
171+ else : # if comm == CommType.SERIAL:
173172 self .server = modbusServer .ModbusSerialServer (
174173 self .context ,
175174 framer = FramerType .SOCKET ,
176175 identity = self .identity ,
177176 port = f"{ NULLMODEM_HOST } :{ test_port } " ,
178177 )
179- else :
180- raise RuntimeError ("ERROR: CommType not implemented" )
181178 client_params = self .server .comm_params .copy ()
182- if client_params .source_address :
183- client_params .host = client_params .source_address [0 ]
184- client_params .port = client_params .source_address [1 ]
185179 client_params .timeout_connect = 1.0
186180 self .stub = TransportStub (client_params , False , simulate_client )
187181 test_port += 1
@@ -195,7 +189,8 @@ async def run(self):
195189 await self .stub .start_run ()
196190 await server_calls (self .stub , (self .comm == CommType .TCP ))
197191 Log .debug ("--> Shutting down." )
198- await self .server .shutdown ()
192+ self .stub .close ()
193+ self .server .close ()
199194
200195
201196async def main (comm : CommType , use_server : bool ):
@@ -215,11 +210,11 @@ async def client_calls(client):
215210 Log .debug ("--> Client calls starting." )
216211 try :
217212 resp = await client .read_holding_registers (address = 124 , count = 4 , device_id = 1 )
218- except ModbusException as exc :
213+ except ModbusException as exc : # pragma: no cover
219214 txt = f"ERROR: exception in pymodbus { exc } "
220215 Log .error (txt )
221216 return
222- if resp .isError ():
217+ if resp .isError (): # pragma: no cover
223218 txt = "ERROR: pymodbus returned an error!"
224219 Log .error (txt )
225220 await asyncio .sleep (1 )
@@ -231,7 +226,7 @@ async def server_calls(transport: ModbusProtocol, is_tcp: bool):
231226 Log .debug ("--> Server calls starting." )
232227
233228 if is_tcp :
234- request = b'\x00 \x02 \x00 \x00 \x00 \x06 \x01 \x03 \x00 \x00 \x00 \x01 '
229+ request = b'\x00 \x02 \x00 \x00 \x00 \x06 \x00 \x03 \x00 \x00 \x00 \x01 '
235230 else :
236231 # 2 responses:
237232 # response = b'\x00\x02\x00\x00\x00\x06\x01\x03\x00\x00\x00\x01' +
@@ -247,7 +242,7 @@ def simulate_server(transport: ModbusProtocol, is_tcp: bool, request: bytes):
247242 """Respond to request at transport level."""
248243 Log .debug ("--> Server simulator called with request {}." , request , ":hex" )
249244 if is_tcp :
250- response = b'\x00 \x01 \x00 \x00 \x00 \x06 \x00 \x03 \x00 \x7c \x00 \x04 '
245+ response = b'\x00 \x01 \x00 \x00 \x00 \x06 \x01 \x03 \x00 \x7c \x00 \x04 '
251246 else :
252247 response = b'\x01 \x03 \x08 \x00 \x05 \x00 \x05 \x00 \x00 \x00 \x00 \x0c \xd7 '
253248
@@ -260,12 +255,15 @@ def simulate_server(transport: ModbusProtocol, is_tcp: bool, request: bytes):
260255
261256def simulate_client (_transport : ModbusProtocol , _is_tcp : bool , response : bytes ):
262257 """Respond to request at transport level."""
263- Log .debug ("--> Client simulator called with response {}." , response , ":hex" )
258+ Log .debug ("--> Client simulator called with response {}." , response , ":hex" ) # pragma: no cover
264259
260+ async def run_test ():
261+ """Run whole test."""
262+ await main (CommType .SERIAL , False )
263+ await main (CommType .SERIAL , True )
264+ await main (CommType .TCP , False )
265+ await main (CommType .TCP , True )
265266
266267if __name__ == "__main__" :
267268 # True for Server test, False for Client test
268- asyncio .run (main (CommType .SERIAL , False ), debug = True )
269- asyncio .run (main (CommType .SERIAL , True ), debug = True )
270- asyncio .run (main (CommType .TCP , False ), debug = True )
271- asyncio .run (main (CommType .TCP , True ), debug = True )
269+ asyncio .run (run_test (), debug = True )
0 commit comments