Skip to content

Commit 5814fd0

Browse files
committed
add subtest for timeout exception
1 parent 913d275 commit 5814fd0

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

tests/test_recv_timeout.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,39 @@ class RecvTimeout(TestCase):
1717

1818
def test_recv_timeout_vs_keepalive(self) -> None:
1919
"""verify that receive timeout as used via ping() is different to keep alive timeout"""
20-
host = "127.0.0.1"
21-
22-
recv_timeout = 4
23-
keep_alive = recv_timeout * 2
24-
mqtt_client = MQTT.MQTT(
25-
broker=host,
26-
socket_pool=socket,
27-
connect_retries=1,
28-
socket_timeout=recv_timeout // 2,
29-
recv_timeout=recv_timeout,
30-
keep_alive=keep_alive,
31-
)
32-
33-
# Create a mock socket that will accept anything and return nothing.
34-
socket_mock = Mock()
35-
socket_mock.recv_into = Mock(side_effect=lambda ret_buf, buf_size: 0)
36-
# pylint: disable=protected-access
37-
mqtt_client._sock = socket_mock
38-
39-
mqtt_client._connected = lambda: True
40-
start = time.monotonic()
41-
with self.assertRaises(MQTT.MMQTTException):
42-
mqtt_client.ping()
43-
44-
# Verify the mock interactions.
45-
socket_mock.send.assert_called_once()
46-
socket_mock.recv_into.assert_called()
47-
48-
now = time.monotonic()
49-
assert recv_timeout <= (now - start) < keep_alive
20+
21+
for side_effect in [lambda ret_buf, buf_size: 0, socket.timeout]:
22+
with self.subTest():
23+
host = "127.0.0.1"
24+
25+
recv_timeout = 4
26+
keep_alive = recv_timeout * 2
27+
mqtt_client = MQTT.MQTT(
28+
broker=host,
29+
socket_pool=socket,
30+
connect_retries=1,
31+
socket_timeout=recv_timeout // 2,
32+
recv_timeout=recv_timeout,
33+
keep_alive=keep_alive,
34+
)
35+
36+
# Create a mock socket that will accept anything and return nothing.
37+
socket_mock = Mock()
38+
socket_mock.recv_into = Mock(side_effect=side_effect)
39+
# pylint: disable=protected-access
40+
mqtt_client._sock = socket_mock
41+
42+
mqtt_client._connected = lambda: True
43+
start = time.monotonic()
44+
with self.assertRaises(MQTT.MMQTTException):
45+
mqtt_client.ping()
46+
47+
# Verify the mock interactions.
48+
socket_mock.send.assert_called_once()
49+
socket_mock.recv_into.assert_called()
50+
51+
now = time.monotonic()
52+
assert recv_timeout <= (now - start) < keep_alive
5053

5154

5255
if __name__ == "__main__":

0 commit comments

Comments
 (0)