Skip to content

Commit 6cf87df

Browse files
committed
Ready for big test.
1 parent 13445d3 commit 6cf87df

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

pymodbus/transport/stub.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
"""ModbusProtocol network stub."""
22
from __future__ import annotations
33

4-
from pymodbus.transport.transport import ModbusProtocol
4+
from pymodbus.transport.transport import CommParams, ModbusProtocol
55

66

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

10+
def __init__(
11+
self,
12+
params: CommParams,
13+
is_server: bool,
14+
handler: callable | None = None,
15+
) -> None:
16+
"""Initialize a stub instance."""
17+
self.stub_handle_data = handler if handler else self.dummy_handler
18+
super().__init__(params, is_server)
19+
20+
1021
async def start_run(self):
1122
"""Call need functions to start server/client."""
1223
if self.is_server:
1324
return await self.transport_listen()
1425
return await self.transport_connect()
1526

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

2234
def callback_new_connection(self) -> ModbusProtocol:
2335
"""Call when listener receive new connection request."""
24-
return ModbusProtocolStub(self.comm_params, False)
36+
new_stub = ModbusProtocolStub(self.comm_params, False)
37+
new_stub.stub_handle_data = self.stub_handle_data
38+
return new_stub
2539

2640
# ---------------- #
2741
# external methods #
2842
# ---------------- #
29-
def stub_handle_data(self, data: bytes) -> bytes | None:
43+
def dummy_handler(self, data: bytes) -> bytes | None:
3044
"""Handle received data."""
3145
return data

test/test_network.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,24 @@ async def test_stub(self, use_port, use_cls):
3030

3131
async def test_double_packet(self, use_port, use_cls):
3232
"""Test double packet on network."""
33-
Log.debug("test_double_packet {}", use_port)
34-
client = AsyncModbusTcpClient(NULLMODEM_HOST, port=use_port)
35-
stub = ModbusProtocolStub(use_cls, True)
33+
old_data = b''
34+
35+
def local_handle_data(data: bytes) -> bytes | None:
36+
"""Handle server side for this test case."""
37+
nonlocal old_data
38+
39+
addr = int(data[9])
40+
if addr == 1:
41+
return None
42+
return data
43+
44+
stub = ModbusProtocolStub(use_cls, True, handler=local_handle_data)
45+
stub.stub_handle_data = local_handle_data
3646
await stub.start_run()
47+
48+
client = AsyncModbusTcpClient(NULLMODEM_HOST, port=use_port)
3749
assert await client.connect()
38-
# await client.read_holding_registers(address=1, count=2)
50+
await client.read_holding_registers(address=10, count=2)
3951
# await asyncio.gather(*[client.read_holding_registers(address=x, count=2) for x in range(0, 1000, 100)])
4052
client.transport_close()
4153
stub.transport_close()

0 commit comments

Comments
 (0)