Skip to content

Commit

Permalink
Merge pull request #200 from vladak/loop_timeout_vs_socket_timeout
Browse files Browse the repository at this point in the history
enforce loop timeout to be bigger than socket timeout
  • Loading branch information
FoamyGuy authored Jan 29, 2024
2 parents 1bf2c0e + c93efd9 commit e19ece6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,13 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]:
:param float timeout: return after this timeout, in seconds.
"""
if timeout < self._socket_timeout:
raise MMQTTException(
# pylint: disable=consider-using-f-string
"loop timeout ({}) must be bigger ".format(timeout)
+ "than socket timeout ({}))".format(self._socket_timeout)
)

self._connected()
self.logger.debug(f"waiting for messages for {timeout} seconds")
if self._timestamp == 0:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ def test_loop_basic(self) -> None:
assert ret_code == expected_rc
expected_rc += 1

# pylint: disable=invalid-name
def test_loop_timeout_vs_socket_timeout(self):
"""
loop() should throw MMQTTException if the timeout argument
is bigger than the socket timeout.
"""
mqtt_client = MQTT.MQTT(
broker="127.0.0.1",
port=1883,
socket_pool=socket,
ssl_context=ssl.create_default_context(),
socket_timeout=1,
)

mqtt_client.is_connected = lambda: True
with self.assertRaises(MQTT.MMQTTException) as context:
mqtt_client.loop(timeout=0.5)

assert "loop timeout" in str(context.exception)

def test_loop_is_connected(self):
"""
loop() should throw MMQTTException if not connected
Expand Down

0 comments on commit e19ece6

Please sign in to comment.