33# test names are self-explanatory, docstrings would only restate them
44
55import asyncio
6+ import time
67from types import SimpleNamespace
78from unittest .mock import AsyncMock , MagicMock , patch
89
910import pytest
1011
12+ from science_mode_4 .protocol import protocol_helper as protocol_helper_module
1113from science_mode_4 .protocol .commands import Commands
1214from science_mode_4 .protocol .exceptions import ProtocolError
1315from science_mode_4 .protocol .protocol_helper import ProtocolHelper
@@ -57,6 +59,24 @@ def test_general_error_ack_raises_immediately():
5759 with pytest .raises (ProtocolError ):
5860 asyncio .run (ProtocolHelper .send_packet_and_wait (request , request .number , packet_buffer ))
5961
62+ # regression test: waiting is aborted, so the acknowledge must no longer be counted as open
63+ packet_buffer .remove_open_acknowledge .assert_called_once_with (request )
64+
65+
66+ def test_unknown_command_ack_raises_immediately_and_removes_open_acknowledge ():
67+ request = _make_request_packet (command = 10 , number = 1 )
68+ error_ack = SimpleNamespace (command = Commands .UNKNOWN_COMMAND , number = 1 ,
69+ result_error = SimpleNamespace (name = "INVALID_CMD_ERROR" ))
70+
71+ packet_buffer = MagicMock ()
72+ packet_buffer .get_packet_from_buffer .side_effect = [error_ack ]
73+
74+ with patch ("science_mode_4.protocol.protocol_helper.asyncio.sleep" , new_callable = AsyncMock ):
75+ with pytest .raises (ProtocolError ):
76+ asyncio .run (ProtocolHelper .send_packet_and_wait (request , request .number , packet_buffer ))
77+
78+ packet_buffer .remove_open_acknowledge .assert_called_once_with (request )
79+
6080
6181def test_no_ack_available_still_sleeps_and_eventually_times_out ():
6282 request = _make_request_packet (command = 10 , number = 1 )
@@ -71,3 +91,57 @@ def test_no_ack_available_still_sleeps_and_eventually_times_out():
7191
7292 assert mock_sleep .call_count > 0
7393 packet_buffer .remove_open_acknowledge .assert_called_once_with (request )
94+
95+
96+ def test_timeout_duration_matches_requested_timeout ():
97+ # regression test: the timeout was implemented by counting sleep cycles, but asyncio.sleep(0.01)
98+ # sleeps longer than requested (~15.6 ms on Windows), so a 1 s timeout took ~1.55 s
99+ request = _make_request_packet (command = 10 , number = 1 )
100+ packet_buffer = MagicMock ()
101+ packet_buffer .get_packet_from_buffer .return_value = None
102+
103+ start = time .monotonic ()
104+ with pytest .raises (ProtocolError ):
105+ asyncio .run (ProtocolHelper .send_packet_and_wait (request , request .number , packet_buffer , timeout_in_seconds = 0.3 ))
106+ elapsed = time .monotonic () - start
107+
108+ assert 0.3 <= elapsed < 0.4
109+
110+
111+ def test_ack_available_after_sleep_crossing_deadline_is_accepted ():
112+ # the deadline is checked only after the buffer was processed, so an acknowledge
113+ # that arrived during the last sleep is not discarded
114+ request = _make_request_packet (command = 10 , number = 3 )
115+ matching_ack = _fake_ack (command = 11 , number = 3 )
116+ packet_buffer = MagicMock ()
117+ packet_buffer .get_packet_from_buffer .side_effect = [None , matching_ack ]
118+
119+ now = [0.0 ]
120+ async def sleep_past_deadline (_duration ):
121+ now [0 ] += 10.0
122+
123+ with patch .object (protocol_helper_module , "time" , SimpleNamespace (monotonic = lambda : now [0 ])), \
124+ patch ("science_mode_4.protocol.protocol_helper.asyncio.sleep" , side_effect = sleep_past_deadline ):
125+ result = asyncio .run (ProtocolHelper .send_packet_and_wait (request , request .number , packet_buffer , timeout_in_seconds = 1 ))
126+
127+ assert result is matching_ack
128+
129+
130+ def test_timeout_error_message_contains_command_name ():
131+ request = _make_request_packet (command = Commands .GET_DEVICE_ID , number = 1 )
132+ packet_buffer = MagicMock ()
133+ packet_buffer .get_packet_from_buffer .return_value = None
134+
135+ with patch ("science_mode_4.protocol.protocol_helper.asyncio.sleep" , new_callable = AsyncMock ):
136+ with pytest .raises (ProtocolError , match = "GET_DEVICE_ID" ):
137+ asyncio .run (ProtocolHelper .send_packet_and_wait (request , request .number , packet_buffer , timeout_in_seconds = 0.02 ))
138+
139+
140+ def test_timeout_error_message_contains_number_of_unknown_command ():
141+ request = _make_request_packet (command = 76 , number = 1 )
142+ packet_buffer = MagicMock ()
143+ packet_buffer .get_packet_from_buffer .return_value = None
144+
145+ with patch ("science_mode_4.protocol.protocol_helper.asyncio.sleep" , new_callable = AsyncMock ):
146+ with pytest .raises (ProtocolError , match = "packet 76 " ):
147+ asyncio .run (ProtocolHelper .send_packet_and_wait (request , request .number , packet_buffer , timeout_in_seconds = 0.02 ))
0 commit comments