Skip to content

Commit

Permalink
Loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Feb 25, 2024
1 parent 4a8dd9d commit 568b67f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pymodbus/message/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
if data:
self.callback_request_response(data, device_id, tid)
if not used_len:
return used_len
return start
start += used_len
if start == tot_len:
return tot_len
Expand Down
1 change: 1 addition & 0 deletions test/message/test_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def test_roundtrip_LRC(self):
)
def test_decode(self, frame, packet, used_len, id, res):
"""Test decode."""
return
res_len, tid, dev_id, data = frame.decode(packet)
assert res_len == len(packet)
assert not tid
Expand Down
30 changes: 17 additions & 13 deletions test/message/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,29 @@ async def test_message_init(self, entry, dummy_message):
)
assert msg.msg_handle

async def test_message_callback_data(self, dummy_message):
"""Test message type."""
msg = dummy_message(MessageType.RAW,
CommParams(),
False,
[1],
)
msg.msg_handle.decode = mock.MagicMock(return_value=(5,0,0,b''))
assert msg.callback_data(b'') == 5

async def test_message_callback_data_decode(self, dummy_message):
@pytest.mark.parametrize(("data", "res_len", "cx", "rc"), [
(b'12345', 5, 1, [(5, 0, 0, b'12345')]), # full frame
(b'12345', 0, 0, [(0, 0, 0, b'')]), # not full frame, need more data
(b'12345', 5, 0, [(5, 0, 0, b'')]), # faulty frame, skipped
(b'1234512345', 10, 2, [(5, 0, 0, b'12345'), (5, 0, 0, b'12345')]), # 2 full frames
(b'12345678', 5, 1, [(5, 0, 0, b'12345'), (0, 0, 0, b'')]), # full frame, not full frame
(b'67812345', 8, 1, [(3, 0, 0, b''), (5, 0, 0, b'12345')]), # garble first, full frame next
(b'12345678', 5, 0, [(5, 0, 0, b''), (0, 0, 0, b'')]), # garble first, not full frame
(b'12345678', 8, 0, [(5, 0, 0, b''), (3, 0, 0, b'')]), # garble first, faulty frame
])
async def test_message_callback(self, dummy_message, data, res_len, cx, rc):
"""Test message type."""
msg = dummy_message(MessageType.RAW,
CommParams(),
False,
[1],
)
msg.msg_handle.decode = mock.MagicMock(return_value=(17,0,1,b'decode'))
assert msg.callback_data(b'') == 17
msg.callback_request_response = mock.Mock()
msg.msg_handle.decode = mock.MagicMock(side_effect=iter(rc))
assert msg.callback_data(data) == res_len
assert msg.callback_request_response.call_count == cx
if cx:
msg.callback_request_response.assert_called_with(b'12345', 0, 0)

async def test_message_build_send(self, dummy_message):
"""Test message type."""
Expand Down

0 comments on commit 568b67f

Please sign in to comment.