Skip to content

Commit e19ece6

Browse files
authored
Merge pull request #200 from vladak/loop_timeout_vs_socket_timeout
enforce loop timeout to be bigger than socket timeout
2 parents 1bf2c0e + c93efd9 commit e19ece6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,13 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
10291029
:param float timeout: return after this timeout, in seconds.
10301030
10311031
"""
1032+
if timeout < self._socket_timeout:
1033+
raise MMQTTException(
1034+
# pylint: disable=consider-using-f-string
1035+
"loop timeout ({}) must be bigger ".format(timeout)
1036+
+ "than socket timeout ({}))".format(self._socket_timeout)
1037+
)
1038+
10321039
self._connected()
10331040
self.logger.debug(f"waiting for messages for {timeout} seconds")
10341041
if self._timestamp == 0:

tests/test_loop.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ def test_loop_basic(self) -> None:
6868
assert ret_code == expected_rc
6969
expected_rc += 1
7070

71+
# pylint: disable=invalid-name
72+
def test_loop_timeout_vs_socket_timeout(self):
73+
"""
74+
loop() should throw MMQTTException if the timeout argument
75+
is bigger than the socket timeout.
76+
"""
77+
mqtt_client = MQTT.MQTT(
78+
broker="127.0.0.1",
79+
port=1883,
80+
socket_pool=socket,
81+
ssl_context=ssl.create_default_context(),
82+
socket_timeout=1,
83+
)
84+
85+
mqtt_client.is_connected = lambda: True
86+
with self.assertRaises(MQTT.MMQTTException) as context:
87+
mqtt_client.loop(timeout=0.5)
88+
89+
assert "loop timeout" in str(context.exception)
90+
7191
def test_loop_is_connected(self):
7292
"""
7393
loop() should throw MMQTTException if not connected

0 commit comments

Comments
 (0)