-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from vladak/recv_timeout_vs_keep_alive
honor recv_timeout in _sock_exact_recv() and ping()
- Loading branch information
Showing
2 changed files
with
63 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# SPDX-FileCopyrightText: 2024 Vladimír Kotal | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
|
||
"""receive timeout tests""" | ||
|
||
import socket | ||
import time | ||
from unittest import TestCase, main | ||
from unittest.mock import Mock | ||
|
||
import adafruit_minimqtt.adafruit_minimqtt as MQTT | ||
|
||
|
||
class RecvTimeout(TestCase): | ||
"""This class contains tests for receive timeout handling.""" | ||
|
||
def test_recv_timeout_vs_keepalive(self) -> None: | ||
"""verify that receive timeout as used via ping() is different to keep alive timeout""" | ||
|
||
for side_effect in [lambda ret_buf, buf_size: 0, socket.timeout]: | ||
with self.subTest(): | ||
host = "127.0.0.1" | ||
|
||
recv_timeout = 4 | ||
keep_alive = recv_timeout * 2 | ||
mqtt_client = MQTT.MQTT( | ||
broker=host, | ||
socket_pool=socket, | ||
connect_retries=1, | ||
socket_timeout=recv_timeout // 2, | ||
recv_timeout=recv_timeout, | ||
keep_alive=keep_alive, | ||
) | ||
|
||
# Create a mock socket that will accept anything and return nothing. | ||
socket_mock = Mock() | ||
socket_mock.recv_into = Mock(side_effect=side_effect) | ||
# pylint: disable=protected-access | ||
mqtt_client._sock = socket_mock | ||
|
||
mqtt_client._connected = lambda: True | ||
start = time.monotonic() | ||
with self.assertRaises(MQTT.MMQTTException): | ||
mqtt_client.ping() | ||
|
||
# Verify the mock interactions. | ||
socket_mock.send.assert_called_once() | ||
socket_mock.recv_into.assert_called() | ||
|
||
now = time.monotonic() | ||
assert recv_timeout <= (now - start) < keep_alive | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |