Skip to content

Commit c59ea5e

Browse files
committed
test.
1 parent 6cf9942 commit c59ea5e

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

pymodbus/server/async_io.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint: disable=missing-type-doc
33
import asyncio
44
import ssl
5+
import time
56
import traceback
67

78
from pymodbus.client.serial_asyncio import create_serial_connection
@@ -1082,6 +1083,8 @@ def stop(cls):
10821083
if not cls.active_server:
10831084
raise RuntimeError("ServerStop called with loop stopped.")
10841085
asyncio.run_coroutine_threadsafe(cls.async_stop(), cls.active_server.loop)
1086+
time.sleep(10)
1087+
10851088

10861089

10871090
async def StartAsyncUnixServer( # pylint: disable=invalid-name,dangerous-default-value

test/test_server_task.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def helper_config(request, def_type):
100100
"certfile": f"{path}/certificates/pymodbus.crt",
101101
"keyfile": f"{path}/certificates/pymodbus.key",
102102
"server_hostname": "localhost",
103-
"timeout": 0.2,
103+
"timeout": 2,
104104
},
105105
"async": {
106106
"srv": server.StartAsyncTlsServer,
@@ -245,7 +245,7 @@ def test_sync_task_no_server(comm):
245245
def test_sync_task_ok(comm):
246246
"""Test normal client/server handling."""
247247
run_server, server_args, run_client, client_args = helper_config(comm, "sync")
248-
if comm in {}:
248+
if comm in {"tls", "udp", "serial"}:
249249
return
250250
thread = Thread(target=run_server, kwargs=server_args)
251251
thread.daemon = True
@@ -262,49 +262,51 @@ def test_sync_task_ok(comm):
262262
sleep(0.1)
263263
assert not client.socket
264264
server.ServerStop()
265+
thread.join()
265266

266267

267268
@pytest.mark.xdist_group(name="task_serialize")
268269
@pytest.mark.parametrize("comm", TEST_TYPES)
269270
def test_sync_task_server_stop(comm):
270271
"""Test normal client/server handling."""
271272
run_server, server_args, run_client, client_args = helper_config(comm, "sync")
272-
if comm:
273-
return # SKIP TEST FOR NOW
273+
if comm in {"tls", "udp", "serial", "tcp"}:
274+
return
274275

275-
# task = asyncio.create_task(run_server(**server_args))
276-
# await asyncio.sleep(0.1)
277-
# client = run_client(**client_args)
278-
# await client.connect()
279-
# assert client.protocol
280-
# rr = await client.read_coils(1, 1, slave=0x01)
281-
# assert len(rr.bits) == 8
276+
thread = Thread(target=run_server, kwargs=server_args)
277+
thread.daemon = True
278+
thread.start()
279+
sleep(0.1)
280+
client = run_client(**client_args)
281+
client.connect()
282+
assert client.socket
283+
rr = client.read_coils(1, 1, slave=0x01)
284+
assert len(rr.bits) == 8
282285

283286
# Server breakdown
284-
# await server.ServerAsyncStop()
285-
# await task
287+
server.ServerStop()
286288

287-
# with pytest.raises((ConnectionException, asyncio.exceptions.TimeoutError)):
288-
# rr = await client.read_coils(1, 1, slave=0x01)
289-
# assert not client.protocol
289+
with pytest.raises((ConnectionException, asyncio.exceptions.TimeoutError)):
290+
rr = client.read_coils(1, 1, slave=0x01)
291+
assert not client.socket
290292

291293
# Server back online
292-
# task = asyncio.create_task(run_server(**server_args))
293-
# await asyncio.sleep(0.1)
294-
295-
# timer_allowed = 100
296-
# while not client.protocol:
297-
# await asyncio.sleep(0.1)
298-
# timer_allowed -= 1
299-
# if not timer_allowed:
300-
# assert False, "client do not reconnect"
301-
# assert client.protocol
302-
303-
# rr = await client.read_coils(1, 1, slave=0x01)
304-
# assert len(rr.bits) == 8
305-
306-
# await client.close()
307-
# await asyncio.sleep(0.5)
308-
# assert not client.protocol
309-
# await server.ServerAsyncStop()
310-
# await task
294+
thread = Thread(target=run_server, kwargs=server_args)
295+
thread.daemon = True
296+
thread.start()
297+
298+
timer_allowed = 100
299+
while not client.protocol:
300+
sleep(0.1)
301+
timer_allowed -= 1
302+
if not timer_allowed:
303+
assert False, "client do not reconnect"
304+
assert client.socket
305+
306+
rr = client.read_coils(1, 1, slave=0x01)
307+
assert len(rr.bits) == 8
308+
309+
client.close()
310+
sleep(0.5)
311+
assert not client.socket
312+
server.ServerStop()

0 commit comments

Comments
 (0)