Skip to content

Commit

Permalink
Ready for big test.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Feb 4, 2024
1 parent 13445d3 commit 6cf87df
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
20 changes: 17 additions & 3 deletions pymodbus/transport/stub.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
"""ModbusProtocol network stub."""
from __future__ import annotations

from pymodbus.transport.transport import ModbusProtocol
from pymodbus.transport.transport import CommParams, ModbusProtocol


class ModbusProtocolStub(ModbusProtocol):
"""Protocol layer including transport."""

def __init__(
self,
params: CommParams,
is_server: bool,
handler: callable | None = None,
) -> None:
"""Initialize a stub instance."""
self.stub_handle_data = handler if handler else self.dummy_handler
super().__init__(params, is_server)


async def start_run(self):
"""Call need functions to start server/client."""
if self.is_server:
return await self.transport_listen()
return await self.transport_connect()


def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
"""Handle received data."""
if (response := self.stub_handle_data(data)):
Expand All @@ -21,11 +33,13 @@ def callback_data(self, data: bytes, addr: tuple | None = None) -> int:

def callback_new_connection(self) -> ModbusProtocol:
"""Call when listener receive new connection request."""
return ModbusProtocolStub(self.comm_params, False)
new_stub = ModbusProtocolStub(self.comm_params, False)
new_stub.stub_handle_data = self.stub_handle_data
return new_stub

# ---------------- #
# external methods #
# ---------------- #
def stub_handle_data(self, data: bytes) -> bytes | None:
def dummy_handler(self, data: bytes) -> bytes | None:
"""Handle received data."""
return data
20 changes: 16 additions & 4 deletions test/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@ async def test_stub(self, use_port, use_cls):

async def test_double_packet(self, use_port, use_cls):
"""Test double packet on network."""
Log.debug("test_double_packet {}", use_port)
client = AsyncModbusTcpClient(NULLMODEM_HOST, port=use_port)
stub = ModbusProtocolStub(use_cls, True)
old_data = b''

def local_handle_data(data: bytes) -> bytes | None:
"""Handle server side for this test case."""
nonlocal old_data

addr = int(data[9])
if addr == 1:
return None
return data

stub = ModbusProtocolStub(use_cls, True, handler=local_handle_data)
stub.stub_handle_data = local_handle_data
await stub.start_run()

client = AsyncModbusTcpClient(NULLMODEM_HOST, port=use_port)
assert await client.connect()
# await client.read_holding_registers(address=1, count=2)
await client.read_holding_registers(address=10, count=2)
# await asyncio.gather(*[client.read_holding_registers(address=x, count=2) for x in range(0, 1000, 100)])
client.transport_close()
stub.transport_close()

0 comments on commit 6cf87df

Please sign in to comment.