Skip to content

Commit 6c88886

Browse files
authored
Test reuse of client object. (#1475)
1 parent 7963266 commit 6c88886

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

test/test_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,24 @@ def test_client_tcp_connect():
509509
assert not client.connect()
510510

511511

512+
def test_client_tcp_reuse():
513+
"""Test the tcp client connection method"""
514+
with mock.patch.object(socket, "create_connection") as mock_method:
515+
_socket = mock.MagicMock()
516+
mock_method.return_value = _socket
517+
client = lib_client.ModbusTcpClient("127.0.0.1")
518+
_socket.getsockname.return_value = ("dmmy", 1234)
519+
assert client.connect()
520+
client.close()
521+
with mock.patch.object(socket, "create_connection") as mock_method:
522+
_socket = mock.MagicMock()
523+
mock_method.return_value = _socket
524+
client = lib_client.ModbusTcpClient("127.0.0.1")
525+
_socket.getsockname.return_value = ("dmmy", 1234)
526+
assert client.connect()
527+
client.close()
528+
529+
512530
def test_client_tls_connect():
513531
"""Test the tls client connection method"""
514532
with mock.patch.object(ssl.SSLSocket, "connect") as mock_method:

test/test_server_task.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,40 @@ async def test_async_task_ok(comm):
179179
await task
180180

181181

182+
@pytest.mark.xdist_group(name="server_serialize")
183+
@pytest.mark.parametrize("comm", TEST_TYPES)
184+
async def test_async_task_reuse(comm):
185+
"""Test normal client/server handling."""
186+
run_server, server_args, run_client, client_args = helper_config(comm, "async")
187+
188+
task = asyncio.create_task(run_server(**server_args))
189+
await asyncio.sleep(0.1)
190+
client = run_client(**client_args)
191+
await client.connect()
192+
await asyncio.sleep(0.1)
193+
assert client._connected # pylint: disable=protected-access
194+
rr = await client.read_coils(1, 1, slave=0x01)
195+
assert len(rr.bits) == 8
196+
197+
await client.close()
198+
await asyncio.sleep(0.1)
199+
assert not client._connected # pylint: disable=protected-access
200+
201+
await client.connect()
202+
await asyncio.sleep(0.1)
203+
assert client._connected # pylint: disable=protected-access
204+
rr = await client.read_coils(1, 1, slave=0x01)
205+
assert len(rr.bits) == 8
206+
207+
await client.close()
208+
await asyncio.sleep(0.1)
209+
assert not client._connected # pylint: disable=protected-access
210+
211+
await server.ServerAsyncStop()
212+
task.cancel()
213+
await task
214+
215+
182216
@pytest.mark.xdist_group(name="server_serialize")
183217
@pytest.mark.parametrize("comm", TEST_TYPES)
184218
async def test_async_task_server_stop(comm):

0 commit comments

Comments
 (0)