Simulated time for unit test

This change needs some modification in EpoxyDuino that are not yet accepted.
This commit is contained in:
hsaturn
2022-12-29 00:54:15 +01:00
parent f348d82167
commit f0af2b95e3
5 changed files with 60 additions and 11 deletions

View File

@@ -88,9 +88,9 @@ void MqttClient::close(bool bSendDisconnect)
void MqttClient::connect(MqttBroker* local)
{
debug("MqttClient::connect_local");
alive = 0;
close();
local_broker = local;
clientAlive();
}
void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka)
@@ -271,7 +271,7 @@ void MqttClient::clientAlive()
debug("MqttClient::clientAlive");
if (keep_alive)
{
alive=millis()+1000*(keep_alive+local_broker ? 5 : 0);
alive=millis()+1000*(keep_alive+(local_broker ? TINY_MQTT_CLIENT_ALIVE_TOLERANCE : 0));
}
else
alive=0;

View File

@@ -7,6 +7,7 @@
#ifndef TINY_MQTT_DEFAULT_ALIVE
#define TINY_MQTT_DEFAULT_ALIVE 10
#endif
#define TINY_MQTT_CLIENT_ALIVE_TOLERANCE 5
// TODO Should add a AUnit with both TINY_MQTT_ASYNC and not TINY_MQTT_ASYNC
// #define TINY_MQTT_ASYNC // Uncomment this to use ESPAsyncTCP instead of normal cnx

View File

@@ -1,6 +1,8 @@
SUB=
tests:
set -e; \
for i in *-tests/Makefile; do \
for i in ${SUB}*-tests/Makefile; do \
echo '==== Making:' $$(dirname $$i); \
$(MAKE) -C $$(dirname $$i) -j; \
done
@@ -15,14 +17,14 @@ runtests: debugtest
$(MAKE) clean
$(MAKE) tests
set -e; \
for i in *-tests/Makefile; do \
for i in ${SUB}*-tests/Makefile; do \
echo '==== Running:' $$(dirname $$i); \
$$(dirname $$i)/$$(dirname $$i).out; \
done
clean:
set -e; \
for i in *-tests/Makefile; do \
for i in ${SUB}*-tests/Makefile; do \
echo '==== Cleaning:' $$(dirname $$i); \
$(MAKE) -C $$(dirname $$i) clean; \
done

View File

@@ -42,14 +42,20 @@ test(local_client_should_unregister_when_destroyed)
test(local_client_alive)
{
set_millis(0);
MqttBroker broker(1883);
MqttClient client(&broker);
for(int i=0; i<10; i++)
{
assertEqual(broker.clientsCount(), (size_t)1); // Ensure client is now connected
broker.loop();
usleep(TINY_MQTT_DEFAULT_ALIVE*1000000/2);
}
broker.loop();
assertEqual(broker.clientsCount(), (size_t)1); // Ensure client is now connected
add_millis(TINY_MQTT_DEFAULT_ALIVE*1000/2);
broker.loop();
assertEqual(broker.clientsCount(), (size_t)1); // Ensure client is still connected
add_seconds(TINY_MQTT_DEFAULT_ALIVE*5);
broker.loop();
assertEqual(broker.clientsCount(), (size_t)1); // Ensure client is still connected
}
#if 0

View File

@@ -142,6 +142,46 @@ test(suback)
assertEqual(MqttClient::counters[MqttMessage::Type::SubAck], 1);
}
test(network_client_alive)
{
const uint32_t keep_alive=1;
start_servers(2, true);
assertEqual(WiFi.status(), WL_CONNECTED);
set_millis(0); // Enter simulated time
MqttBroker broker(1883);
broker.begin();
IPAddress broker_ip = WiFi.localIP();
ESP8266WiFiClass::selectInstance(2);
MqttClient client;
client.connect(broker_ip.toString().c_str(), 1883, keep_alive);
broker.loop();
client.loop();
assertTrue(broker.clientsCount() == 1);
assertTrue(client.connected());
uint32_t ka = broker.getClients()[0]->keepAlive();
assertEqual(ka, keep_alive);
assertEqual(broker.clientsCount(), (size_t)1);
// All is going well if we call client.loop()
// The client is able to send PingReq to the broker
add_seconds(keep_alive);
client.loop();
broker.loop();
assertEqual(broker.clientsCount(), (size_t)1);
// Now simulate that the client is frozen for
// a too long time
add_seconds(TINY_MQTT_CLIENT_ALIVE_TOLERANCE*2);
broker.loop();
assertEqual(broker.clientsCount(), (size_t)0);
set_real_time();
}
test(network_client_keep_alive_high)
{
const uint32_t keep_alive=1000;