diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index b32f2c2..600ad24 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -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: diff --git a/tests/test_loop.py b/tests/test_loop.py index ccca924..4728ac1 100644 --- a/tests/test_loop.py +++ b/tests/test_loop.py @@ -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