From 8d9b9eef479a8ead1e6a362d88fea838d4f452f9 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Sat, 7 Jun 2025 12:21:49 +0200 Subject: [PATCH] test: update last_ping handling to prevent premature ping triggers --- tests/test_mqtt_client.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/test_mqtt_client.py b/tests/test_mqtt_client.py index 76f6860..22626f6 100644 --- a/tests/test_mqtt_client.py +++ b/tests/test_mqtt_client.py @@ -4,6 +4,8 @@ Tests for the MQTT Client module. This module contains tests for the MQTTClient class in the mqtt_client.py module. """ +import time + import pytest import socket import struct @@ -197,7 +199,8 @@ class TestMQTTClient: # Set up the client as connected mqtt_client.sock = mock_sock mqtt_client.connected = True - mqtt_client.last_ping = 0 + # Set last_ping to current time to prevent ping from being triggered + mqtt_client.last_ping = time.time() # Call publish with QoS 0 mqtt_client.publish("test/topic", "test message") @@ -207,6 +210,8 @@ class TestMQTTClient: # Test with QoS 1 mock_sock.reset_mock() + # Reset last_ping to current time to prevent ping from being triggered + mqtt_client.last_ping = time.time() # Mock the _recv_packet method instead of directly mocking socket.recv with patch.object( @@ -219,6 +224,8 @@ class TestMQTTClient: # Test with QoS 1 and timeout mock_sock.reset_mock() + # Reset last_ping to current time to prevent ping from being triggered + mqtt_client.last_ping = time.time() # Mock _recv_packet to return None (simulating timeout) with patch.object(mqtt_client, "_recv_packet", return_value=(None, None)): @@ -238,7 +245,8 @@ class TestMQTTClient: # Set up the client as connected mqtt_client.sock = mock_sock mqtt_client.connected = True - mqtt_client.last_ping = 0 + # Set last_ping to current time to prevent ping from being triggered + mqtt_client.last_ping = time.time() # Mock the _recv_packet method to return a successful SUBACK with patch.object( @@ -256,6 +264,8 @@ class TestMQTTClient: # Test with timeout mock_sock.reset_mock() + # Reset last_ping to current time to prevent ping from being triggered + mqtt_client.last_ping = time.time() # Mock _recv_packet to return None (simulating timeout) with patch.object(mqtt_client, "_recv_packet", return_value=(None, None)): @@ -278,7 +288,8 @@ class TestMQTTClient: # Set up the client as connected mqtt_client.sock = mock_sock mqtt_client.connected = True - mqtt_client.last_ping = 0 + # Set last_ping to current time to prevent ping from being triggered + mqtt_client.last_ping = time.time() # Set up a mock callback mock_callback = MagicMock()