@@ -44,16 +44,21 @@ def test_loop_basic(self) -> None:
44
44
ssl_context = ssl .create_default_context (),
45
45
)
46
46
47
- with patch .object (mqtt_client , "_wait_for_msg" ) as mock_method :
48
- mock_method .side_effect = self .fake_wait_for_msg
47
+ with patch .object (
48
+ mqtt_client , "_wait_for_msg"
49
+ ) as wait_for_msg_mock , patch .object (
50
+ mqtt_client , "is_connected"
51
+ ) as is_connected_mock :
52
+ wait_for_msg_mock .side_effect = self .fake_wait_for_msg
53
+ is_connected_mock .side_effect = lambda : True
49
54
50
55
time_before = time .monotonic ()
51
56
timeout = random .randint (3 , 8 )
52
57
rcs = mqtt_client .loop (timeout = timeout )
53
58
time_after = time .monotonic ()
54
59
55
60
assert time_after - time_before >= timeout
56
- mock_method .assert_called ()
61
+ wait_for_msg_mock .assert_called ()
57
62
58
63
# Check the return value.
59
64
assert rcs is not None
@@ -63,6 +68,22 @@ def test_loop_basic(self) -> None:
63
68
assert ret_code == expected_rc
64
69
expected_rc += 1
65
70
71
+ def test_loop_is_connected (self ):
72
+ """
73
+ loop() should throw MMQTTException if not connected
74
+ """
75
+ mqtt_client = MQTT .MQTT (
76
+ broker = "127.0.0.1" ,
77
+ port = 1883 ,
78
+ socket_pool = socket ,
79
+ ssl_context = ssl .create_default_context (),
80
+ )
81
+
82
+ with self .assertRaises (MQTT .MMQTTException ) as context :
83
+ mqtt_client .loop (timeout = 1 )
84
+
85
+ assert "not connected" in str (context .exception )
86
+
66
87
67
88
if __name__ == "__main__" :
68
89
main ()
0 commit comments