|
15 | 15 | import pymodbus.register_write_message as pdu_req_write |
16 | 16 | from pymodbus.client.base import ModbusBaseClient |
17 | 17 | from pymodbus.client.mixin import ModbusClientMixin |
| 18 | +from pymodbus.datastore import ModbusSlaveContext |
| 19 | +from pymodbus.datastore.store import ModbusSequentialDataBlock |
18 | 20 | from pymodbus.exceptions import ConnectionException |
19 | 21 | from pymodbus.framer.ascii_framer import ModbusAsciiFramer |
20 | 22 | from pymodbus.framer.rtu_framer import ModbusRtuFramer |
@@ -360,23 +362,43 @@ async def test_client_protocol_handler(): |
360 | 362 | assert result == reply |
361 | 363 |
|
362 | 364 |
|
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 | + |
364 | 392 | async def test_client_protocol_execute(): |
365 | 393 | """Test the client protocol execute method""" |
366 | 394 | 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 | | - |
371 | 395 | 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) |
376 | 398 |
|
377 | | - base.params.broadcast_enable = True |
378 | | - request = pdu_bit_read.ReadCoilsRequest(1, 1) |
379 | 399 | response = await base.async_execute(request) |
| 400 | + assert not response.isError() |
| 401 | + assert isinstance(response, pdu_bit_read.ReadCoilsResponse) |
380 | 402 |
|
381 | 403 |
|
382 | 404 | def test_client_udp_connect(): |
|
0 commit comments