Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enforce loop timeout to be bigger than socket timeout #200

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading