Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,24 @@ def test_client_tcp_connect():
assert not client.connect()


def test_client_tcp_reuse():
"""Test the tcp client connection method"""
with mock.patch.object(socket, "create_connection") as mock_method:
_socket = mock.MagicMock()
mock_method.return_value = _socket
client = lib_client.ModbusTcpClient("127.0.0.1")
_socket.getsockname.return_value = ("dmmy", 1234)
assert client.connect()
client.close()
with mock.patch.object(socket, "create_connection") as mock_method:
_socket = mock.MagicMock()
mock_method.return_value = _socket
client = lib_client.ModbusTcpClient("127.0.0.1")
_socket.getsockname.return_value = ("dmmy", 1234)
assert client.connect()
client.close()


def test_client_tls_connect():
"""Test the tls client connection method"""
with mock.patch.object(ssl.SSLSocket, "connect") as mock_method:
Expand Down
34 changes: 34 additions & 0 deletions test/test_server_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,40 @@ async def test_async_task_ok(comm):
await task


@pytest.mark.xdist_group(name="server_serialize")
@pytest.mark.parametrize("comm", TEST_TYPES)
async def test_async_task_reuse(comm):
"""Test normal client/server handling."""
run_server, server_args, run_client, client_args = helper_config(comm, "async")

task = asyncio.create_task(run_server(**server_args))
await asyncio.sleep(0.1)
client = run_client(**client_args)
await client.connect()
await asyncio.sleep(0.1)
assert client._connected # pylint: disable=protected-access
rr = await client.read_coils(1, 1, slave=0x01)
assert len(rr.bits) == 8

await client.close()
await asyncio.sleep(0.1)
assert not client._connected # pylint: disable=protected-access

await client.connect()
await asyncio.sleep(0.1)
assert client._connected # pylint: disable=protected-access
rr = await client.read_coils(1, 1, slave=0x01)
assert len(rr.bits) == 8

await client.close()
await asyncio.sleep(0.1)
assert not client._connected # pylint: disable=protected-access

await server.ServerAsyncStop()
task.cancel()
await task


@pytest.mark.xdist_group(name="server_serialize")
@pytest.mark.parametrize("comm", TEST_TYPES)
async def test_async_task_server_stop(comm):
Expand Down