Skip to content

Commit 3bd68f9

Browse files
janiversenlsgunth
andauthored
test_client: Fix test_client_protocol_execute() (#1751)
Co-authored-by: Logan Gunthorpe <logang@deltatee.com>
1 parent b1b560a commit 3bd68f9

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

test/sub_client/test_client.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import pymodbus.register_write_message as pdu_req_write
1616
from pymodbus.client.base import ModbusBaseClient
1717
from pymodbus.client.mixin import ModbusClientMixin
18+
from pymodbus.datastore import ModbusSlaveContext
19+
from pymodbus.datastore.store import ModbusSequentialDataBlock
1820
from pymodbus.exceptions import ConnectionException
1921
from pymodbus.framer.ascii_framer import ModbusAsciiFramer
2022
from pymodbus.framer.rtu_framer import ModbusRtuFramer
@@ -360,23 +362,43 @@ async def test_client_protocol_handler():
360362
assert result == reply
361363

362364

363-
@pytest.mark.skip()
365+
class MockTransport:
366+
"""Mock transport class which responds with an appropriate encoded packet"""
367+
368+
def __init__(self, base, req):
369+
"""Initialize MockTransport"""
370+
self.base = base
371+
372+
db = ModbusSequentialDataBlock(0, [0] * 100)
373+
self.ctx = ModbusSlaveContext(di=db, co=db, hr=db, ir=db)
374+
self.req = req
375+
376+
async def delayed_resp(self):
377+
"""Send a response to a received packet"""
378+
await asyncio.sleep(0.05)
379+
resp = self.req.execute(self.ctx)
380+
pkt = self.base.framer.buildPacket(resp)
381+
self.base.data_received(pkt)
382+
383+
def write(self, data, addr=None):
384+
"""Write data to the transport, start a task to send the response"""
385+
self.delayed_resp_task = asyncio.create_task(self.delayed_resp())
386+
387+
def close(self):
388+
"""Close the transport"""
389+
pass
390+
391+
364392
async def test_client_protocol_execute():
365393
"""Test the client protocol execute method"""
366394
base = ModbusBaseClient(host="127.0.0.1", framer=ModbusSocketFramer)
367-
transport = mock.MagicMock()
368-
base.connection_made(transport)
369-
base.transport.write = mock.Mock()
370-
371395
request = pdu_bit_read.ReadCoilsRequest(1, 1)
372-
response = await base.async_execute(request)
373-
tid = request.transaction_id
374-
f_trans = base.transaction.getTransaction(tid)
375-
assert response == f_trans
396+
transport = MockTransport(base, request)
397+
base.connection_made(transport=transport)
376398

377-
base.params.broadcast_enable = True
378-
request = pdu_bit_read.ReadCoilsRequest(1, 1)
379399
response = await base.async_execute(request)
400+
assert not response.isError()
401+
assert isinstance(response, pdu_bit_read.ReadCoilsResponse)
380402

381403

382404
def test_client_udp_connect():

0 commit comments

Comments
 (0)