From f0af2b95e3cc61001a886db51791213d1e99b109 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Thu, 29 Dec 2022 00:54:15 +0100 Subject: [PATCH] Simulated time for unit test This change needs some modification in EpoxyDuino that are not yet accepted. --- src/TinyMqtt.cpp | 4 +-- src/TinyMqtt.h | 1 + tests/Makefile | 8 ++++-- tests/local-tests/local-tests.ino | 18 ++++++++---- tests/network-tests/network-tests.ino | 40 +++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 4c9a78b..19a95ed 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -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; diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 089856b..a6743b7 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -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 diff --git a/tests/Makefile b/tests/Makefile index 3457bb4..4f66525 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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 diff --git a/tests/local-tests/local-tests.ino b/tests/local-tests/local-tests.ino index da8622e..2e006f1 100644 --- a/tests/local-tests/local-tests.ino +++ b/tests/local-tests/local-tests.ino @@ -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 diff --git a/tests/network-tests/network-tests.ino b/tests/network-tests/network-tests.ino index b758eda..ea7e910 100644 --- a/tests/network-tests/network-tests.ino +++ b/tests/network-tests/network-tests.ino @@ -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;