From d5fffe993e7f3c4479d342cbf828898fb32a35e0 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Fri, 26 Jan 2024 21:12:28 +0100 Subject: [PATCH 1/4] enforce loop timeout < socket timeout fixes #195 --- adafruit_minimqtt/adafruit_minimqtt.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 7ffb723..355b55e 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -1029,6 +1029,11 @@ 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( + f"loop timeout ({timeout}) must be bigger than socket timeout ({self._socket_timeout})" + ) + self._connected() self.logger.debug(f"waiting for messages for {timeout} seconds") if self._timestamp == 0: From d8d4e393055a3a9578d0987573abf18e0bb5d095 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Fri, 26 Jan 2024 21:20:53 +0100 Subject: [PATCH 2/4] add unit test --- tests/test_loop.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_loop.py b/tests/test_loop.py index 6a762fd..8c38e76 100644 --- a/tests/test_loop.py +++ b/tests/test_loop.py @@ -68,6 +68,24 @@ def test_loop_basic(self) -> None: assert ret_code == expected_rc expected_rc += 1 + 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 From f942dd4a088551816ae33c64d9111664d5cd7a9b Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Fri, 26 Jan 2024 21:28:04 +0100 Subject: [PATCH 3/4] fix pylint --- adafruit_minimqtt/adafruit_minimqtt.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_minimqtt/adafruit_minimqtt.py b/adafruit_minimqtt/adafruit_minimqtt.py index 355b55e..2ec0100 100644 --- a/adafruit_minimqtt/adafruit_minimqtt.py +++ b/adafruit_minimqtt/adafruit_minimqtt.py @@ -1031,7 +1031,9 @@ def loop(self, timeout: float = 0) -> Optional[list[int]]: """ if timeout < self._socket_timeout: raise MMQTTException( - f"loop timeout ({timeout}) must be bigger than socket timeout ({self._socket_timeout})" + # pylint: disable=consider-using-f-string + "loop timeout ({}) must be bigger ".format(timeout) + + "than socket timeout ({}))".format(self._socket_timeout) ) self._connected() From c93efd90173f29f03689ef8a0d8b4922fe529a61 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Fri, 26 Jan 2024 21:29:40 +0100 Subject: [PATCH 4/4] fix pylint --- tests/test_loop.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_loop.py b/tests/test_loop.py index 8c38e76..0115a5d 100644 --- a/tests/test_loop.py +++ b/tests/test_loop.py @@ -68,16 +68,18 @@ 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. + 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 + socket_timeout=1, ) mqtt_client.is_connected = lambda: True