Skip to content

Commit 5ec5f9d

Browse files
authored
Activate pytest-asyncio. (#949)
1 parent c2d4239 commit 5ec5f9d

File tree

2 files changed

+122
-144
lines changed

2 files changed

+122
-144
lines changed

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,5 @@ universal=1
526526
[tool:pytest]
527527
testpaths = test
528528
addopts = -p no:warnings
529+
asyncio_mode = auto
530+

test/test_client_async_asyncio.py

Lines changed: 120 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def test_factory_initialization_state(self): # pylint: disable=no-self-use
9292
assert client.loop is mock_loop # nosec
9393
assert client.protocol_class is mock_protocol_class # nosec
9494

95-
@pytest.mark.asyncio
9695
async def test_initialization_tcp_in_loop(self): # pylint: disable=no-self-use
9796
"""Test initialization tcp in loop."""
9897
_, client = AsyncModbusTCPClient( # pylint: disable=unpacking-non-sequence
@@ -105,7 +104,6 @@ async def test_initialization_tcp_in_loop(self): # pylint: disable=no-self-use
105104
assert client.port == 5020 # nosec
106105
assert client.delay_ms < client.DELAY_MAX_MS # nosec
107106

108-
@pytest.mark.asyncio
109107
async def test_initialization_udp_in_loop(self): # pylint: disable=no-self-use
110108
"""Test initialization udp in loop."""
111109
_, client = AsyncModbusUDPClient( # pylint: disable=unpacking-non-sequence
@@ -117,7 +115,6 @@ async def test_initialization_udp_in_loop(self): # pylint: disable=no-self-use
117115
assert client.port == 5020 # nosec
118116
assert client.delay_ms < client.DELAY_MAX_MS # nosec
119117

120-
@pytest.mark.asyncio
121118
async def test_initialization_tls_in_loop(self): # pylint: disable=no-self-use
122119
"""Test initialization tls in loop."""
123120
_, client = AsyncModbusTLSClient( # pylint: disable=unpacking-non-sequence
@@ -129,7 +126,6 @@ async def test_initialization_tls_in_loop(self): # pylint: disable=no-self-use
129126
assert client.port == 5020 # nosec
130127
assert client.delay_ms < client.DELAY_MAX_MS # nosec
131128

132-
@pytest.mark.asyncio
133129
def test_initialization_serial_in_loop(self): # pylint: disable=no-self-use
134130
"""Test initialization serial in loop."""
135131
_, client = AsyncModbusSerialClient( # pylint: disable=unpacking-non-sequence
@@ -225,7 +221,6 @@ def test_factory_protocol_lost_connection(
225221
assert not client.connected # nosec
226222
assert client.protocol is None # nosec
227223

228-
@pytest.mark.asyncio
229224
async def test_factory_start_success(self): # pylint: disable=no-self-use
230225
"""Test factory start success."""
231226
mock_protocol_class = mock.MagicMock()
@@ -259,7 +254,6 @@ def test_factory_start_failing_and_retried(
259254
mock.sentinel.RECONNECT_GENERATOR, loop=mock_loop
260255
)
261256

262-
# @pytest.mark.asyncio
263257
@mock.patch("pymodbus.client.asynchronous.async_io.asyncio.sleep")
264258
def test_factory_reconnect(self, mock_sleep): # pylint: disable=no-self-use
265259
"""Test factory reconnect."""
@@ -275,150 +269,132 @@ def test_factory_reconnect(self, mock_sleep): # pylint: disable=no-self-use
275269
mock_sleep.assert_called_once_with(5)
276270
assert mock_loop.create_connection.call_count == 1 # nosec
277271

278-
@pytest.mark.parametrize("protocol", protocols)
279-
def test_client_protocol_connection_made(
280-
self, protocol
281-
): # pylint: disable=no-self-use
272+
def test_client_protocol_connection_made(self): # pylint: disable=no-self-use
282273
"""Test the client protocol close."""
283-
protocol = protocol(ModbusSocketFramer(ClientDecoder()))
284-
transport = mock.MagicMock()
285-
factory = mock.MagicMock()
286-
if isinstance(protocol, ModbusUdpClientProtocol):
287-
protocol.factory = factory
288-
protocol.connection_made(transport)
289-
assert protocol.transport == transport # nosec
290-
assert protocol.connected # nosec
291-
if isinstance(protocol, ModbusUdpClientProtocol):
292-
assert protocol.factory.protocol_made_connection.call_count == 1 # nosec
293-
294-
@pytest.mark.parametrize("protocol", protocols)
295-
def test_client_protocol_close(self, protocol): # pylint: disable=no-self-use
274+
for protocol in protocols:
275+
protocol = protocol(ModbusSocketFramer(ClientDecoder()))
276+
transport = mock.MagicMock()
277+
factory = mock.MagicMock()
278+
if isinstance(protocol, ModbusUdpClientProtocol):
279+
protocol.factory = factory
280+
protocol.connection_made(transport)
281+
assert protocol.transport == transport # nosec
282+
assert protocol.connected # nosec
283+
if isinstance(protocol, ModbusUdpClientProtocol):
284+
assert protocol.factory.protocol_made_connection.call_count == 1 # nosec
285+
286+
def test_client_protocol_close(self,): # pylint: disable=no-self-use
296287
"""Test the client protocol close."""
297-
protocol = protocol(ModbusSocketFramer(ClientDecoder()))
298-
transport = mock.MagicMock()
299-
factory = mock.MagicMock()
300-
if isinstance(protocol, ModbusUdpClientProtocol):
301-
protocol.factory = factory
302-
protocol.connection_made(transport)
303-
assert protocol.transport == transport # nosec
304-
assert protocol.connected # nosec
305-
protocol.close()
306-
transport.close.assert_called_once_with()
307-
assert not protocol.connected # nosec
288+
for protocol in protocols:
289+
protocol = protocol(ModbusSocketFramer(ClientDecoder()))
290+
transport = mock.MagicMock()
291+
factory = mock.MagicMock()
292+
if isinstance(protocol, ModbusUdpClientProtocol):
293+
protocol.factory = factory
294+
protocol.connection_made(transport)
295+
assert protocol.transport == transport # nosec
296+
assert protocol.connected # nosec
297+
protocol.close()
298+
transport.close.assert_called_once_with()
299+
assert not protocol.connected # nosec
308300

309301
@pytest.mark.skip("To fix")
310-
@pytest.mark.parametrize("protocol", protocols)
311-
def test_client_protocol_connection_lost(
312-
self, protocol
313-
): # pylint: disable=no-self-use
302+
def test_client_protocol_connection_lost(self): # pylint: disable=no-self-use
314303
"""Test the client protocol connection lost"""
315-
framer = ModbusSocketFramer(None)
316-
protocol = protocol(framer=framer, timeout=0)
317-
protocol.execute = mock.MagicMock()
318-
# future = asyncio.Future()
319-
# future.set_result(ReadCoilsResponse([1]))
320-
# protocol._execute = mock.MagicMock(side_effect=future)
321-
transport = mock.MagicMock()
322-
factory = mock.MagicMock()
323-
if isinstance(protocol, ModbusUdpClientProtocol):
324-
protocol.factory = factory
325-
protocol.connection_made(transport)
326-
protocol.transport.write = mock.Mock()
327-
328-
request = ReadCoilsRequest(1, 1)
329-
response = protocol.execute(request)
330-
# d = await d
331-
protocol.connection_lost("REASON")
332-
excp = response.exception()
333-
assert isinstance(excp, ConnectionException) # nosec
334-
if isinstance(protocol, ModbusUdpClientProtocol):
335-
assert protocol.factory.protocol_lost_connection.call_count == 1 # nosec
336-
337-
@pytest.mark.parametrize("protocol", protocols)
338-
async def test_client_protocol_data_received(
339-
self, protocol
340-
): # pylint: disable=no-self-use
304+
for protocol in protocols:
305+
framer = ModbusSocketFramer(None)
306+
protocol = protocol(framer=framer, timeout=0)
307+
protocol.execute = mock.MagicMock()
308+
transport = mock.MagicMock()
309+
factory = mock.MagicMock()
310+
if isinstance(protocol, ModbusUdpClientProtocol):
311+
protocol.factory = factory
312+
protocol.connection_made(transport)
313+
protocol.transport.write = mock.Mock()
314+
315+
request = ReadCoilsRequest(1, 1)
316+
response = protocol.execute(request)
317+
protocol.connection_lost("REASON")
318+
excp = response.exception()
319+
assert isinstance(excp, ConnectionException) # nosec
320+
if isinstance(protocol, ModbusUdpClientProtocol):
321+
assert protocol.factory.protocol_lost_connection.call_count == 1 # nosec
322+
323+
async def test_client_protocol_data_received(self): # pylint: disable=no-self-use
341324
"""Test the client protocol data received"""
342-
protocol = protocol(ModbusSocketFramer(ClientDecoder()))
343-
transport = mock.MagicMock()
344-
protocol.connection_made(transport)
345-
assert protocol.transport == transport # nosec
346-
assert protocol.connected # nosec
347-
data = b"\x00\x00\x12\x34\x00\x06\xff\x01\x01\x02\x00\x04"
348-
349-
# setup existing request
350-
response = protocol._buildResponse(0x00) # pylint: disable=protected-access
351-
if isinstance(protocol, ModbusUdpClientProtocol):
352-
protocol.datagram_received(data, None)
353-
else:
354-
protocol.data_received(data)
355-
result = response.result()
356-
assert isinstance(result, ReadCoilsResponse) # nosec
357-
358-
# @pytest.mark.skip("To fix")
359-
@pytest.mark.asyncio
360-
@pytest.mark.parametrize("protocol", protocols)
361-
async def test_client_protocol_execute(
362-
self, protocol
363-
): # pylint: disable=no-self-use
325+
for tryprotocol in protocols:
326+
protocol = tryprotocol(ModbusSocketFramer(ClientDecoder()))
327+
transport = mock.MagicMock()
328+
protocol.connection_made(transport)
329+
assert protocol.transport == transport # nosec
330+
assert protocol.connected # nosec
331+
data = b"\x00\x00\x12\x34\x00\x06\xff\x01\x01\x02\x00\x04"
332+
333+
# setup existing request
334+
response = protocol._build_response(0x00) # pylint: disable=protected-access
335+
if isinstance(protocol, ModbusUdpClientProtocol):
336+
protocol.datagram_received(data, None)
337+
else:
338+
protocol.data_received(data)
339+
result = response.result()
340+
assert isinstance(result, ReadCoilsResponse) # nosec
341+
342+
async def test_client_protocol_execute(self): # pylint: disable=no-self-use
364343
"""Test the client protocol execute method"""
365-
framer = ModbusSocketFramer(None)
366-
protocol = protocol(framer=framer)
367-
protocol.create_future = mock.MagicMock()
368-
fut = asyncio.Future()
369-
fut.set_result(fut)
370-
protocol.create_future.return_value = fut
371-
transport = mock.MagicMock()
372-
protocol.connection_made(transport)
373-
protocol.transport.write = mock.Mock()
374-
375-
request = ReadCoilsRequest(1, 1)
376-
response = await protocol.execute(request)
377-
tid = request.transaction_id
378-
f_trans = protocol.transaction.getTransaction(tid)
379-
assert response == f_trans # nosec
380-
381-
@pytest.mark.parametrize("protocol", protocols)
382-
async def test_client_protocol_handle_response(
383-
self, protocol
384-
): # pylint: disable=no-self-use
344+
for protocol in protocols:
345+
framer = ModbusSocketFramer(None)
346+
protocol = protocol(framer=framer)
347+
protocol.create_future = mock.MagicMock()
348+
fut = asyncio.Future()
349+
fut.set_result(fut)
350+
protocol.create_future.return_value = fut
351+
transport = mock.MagicMock()
352+
protocol.connection_made(transport)
353+
protocol.transport.write = mock.Mock()
354+
355+
request = ReadCoilsRequest(1, 1)
356+
response = await protocol.execute(request)
357+
tid = request.transaction_id
358+
f_trans = protocol.transaction.getTransaction(tid)
359+
assert response == f_trans # nosec
360+
361+
async def test_client_protocol_handle_response(self): # pylint: disable=no-self-use
385362
"""Test the client protocol handles responses"""
386-
protocol = protocol()
387-
transport = mock.MagicMock()
388-
protocol.connection_made(transport=transport)
389-
reply = ReadCoilsRequest(1, 1)
390-
reply.transaction_id = 0x00
391-
# if isinstance(protocol.create_future, mock.MagicMock):
392-
# import asyncio
393-
# protocol.create_future.return_value = asyncio.Future()
394-
# handle skipped cases
395-
protocol._handleResponse(None) # pylint: disable=protected-access
396-
protocol._handleResponse(reply) # pylint: disable=protected-access
397-
398-
# handle existing cases
399-
response = protocol._buildResponse(0x00) # pylint: disable=protected-access
400-
protocol._handleResponse(reply) # pylint: disable=protected-access
401-
result = response.result()
402-
assert result == reply # nosec
403-
404-
@pytest.mark.parametrize("protocol", protocols)
405-
async def test_client_protocol_build_response(
406-
self, protocol
407-
): # pylint: disable=no-self-use
363+
for protocol in protocols:
364+
protocol = protocol()
365+
transport = mock.MagicMock()
366+
protocol.connection_made(transport=transport)
367+
reply = ReadCoilsRequest(1, 1)
368+
reply.transaction_id = 0x00
369+
# if isinstance(protocol.create_future, mock.MagicMock):
370+
# import asyncio
371+
# protocol.create_future.return_value = asyncio.Future()
372+
# handle skipped cases
373+
protocol._handle_response(None) # pylint: disable=protected-access
374+
protocol._handle_response(reply) # pylint: disable=protected-access
375+
376+
# handle existing cases
377+
response = protocol._build_response(0x00) # pylint: disable=protected-access
378+
protocol._handle_response(reply) # pylint: disable=protected-access
379+
result = response.result()
380+
assert result == reply # nosec
381+
382+
async def test_client_protocol_build_response(self): # pylint: disable=no-self-use
408383
"""Test the udp client protocol builds responses"""
409-
protocol = protocol()
410-
assert not len( # nosec pylint: disable=use-implicit-booleaness-not-len
411-
list(protocol.transaction)
412-
)
413-
response = protocol._buildResponse( # nosec pylint: disable=protected-access
414-
0x00
415-
)
416-
excp = response.exception()
417-
assert isinstance(excp, ConnectionException) # nosec
418-
assert not len( # nosec pylint: disable=use-implicit-booleaness-not-len
419-
list(protocol.transaction)
420-
)
421-
422-
protocol._connected = True # pylint: disable=protected-access
423-
protocol._buildResponse(0x00) # pylint: disable=protected-access
424-
assert len(list(protocol.transaction)) == 1 # nosec
384+
for protocol in protocols:
385+
protocol = protocol()
386+
assert not len( # nosec pylint: disable=use-implicit-booleaness-not-len
387+
list(protocol.transaction)
388+
)
389+
response = protocol._build_response( # nosec pylint: disable=protected-access
390+
0x00
391+
)
392+
excp = response.exception()
393+
assert isinstance(excp, ConnectionException) # nosec
394+
assert not len( # nosec pylint: disable=use-implicit-booleaness-not-len
395+
list(protocol.transaction)
396+
)
397+
398+
protocol._connected = True # pylint: disable=protected-access
399+
protocol._build_response(0x00) # pylint: disable=protected-access
400+
assert len(list(protocol.transaction)) == 1 # nosec

0 commit comments

Comments
 (0)