[test] Test added for retain

This commit is contained in:
hsaturn
2023-03-22 20:31:05 +01:00
parent e4ad27c805
commit 02496bef73
2 changed files with 131 additions and 24 deletions

View File

@@ -1,7 +1,7 @@
# GCC
# CXXFLAGS = -Wextra -Wall -std=gnu++11 -fno-exceptions -fno-threadsafe-statics
EXTRA_CXXFLAGS=-g3 -O0
EXTRA_CXXFLAGS=-g3 -O0 -std=c++17
CXXFLAGS=-D_GNU_SOURCE -Werror=return-type -std=gnu++17 -Wall -g3 -O0

View File

@@ -85,7 +85,7 @@ std::map<string, std::map<Topic, int>> published; // map[client_id] => map[t
char* lastPayload = nullptr;
size_t lastLength;
void start_servers(int n, bool early_accept = true)
void start_many_wifi_esp(int n, bool early_accept = true)
{
ESP8266WiFiClass::resetInstances();
ESP8266WiFiClass::earlyAccept = early_accept;
@@ -107,7 +107,7 @@ void onPublish(const MqttClient* srce, const Topic& topic, const char* payload,
lastLength = length;
}
test(network_single_broker_begin)
test(single_broker_begin)
{
assertEqual(WiFi.status(), WL_CONNECTED);
@@ -119,7 +119,7 @@ test(network_single_broker_begin)
test(suback)
{
start_servers(2, true);
start_many_wifi_esp(2, true);
assertEqual(WiFi.status(), WL_CONNECTED);
MqttBroker broker(1883);
@@ -144,10 +144,10 @@ test(suback)
assertEqual(MqttClient::counters[MqttMessage::Type::SubAck], 1);
}
test(network_client_keep_alive_high)
test(client_keep_alive_high)
{
const uint32_t keep_alive=1000;
start_servers(2, true);
start_many_wifi_esp(2, true);
assertEqual(WiFi.status(), WL_CONNECTED);
MqttBroker broker(1883);
@@ -179,9 +179,116 @@ test(network_client_keep_alive_high)
}
test(network_client_to_broker_connexion)
test(retained_message)
{
start_servers(2, true);
published.clear();
start_many_wifi_esp(2, true);
assertEqual(WiFi.status(), WL_CONNECTED);
MqttBroker broker(1883);
broker.begin();
broker.retain(10);
IPAddress broker_ip = WiFi.localIP();
MqttClient local_client(&broker);
// Send a retained message
// No remote client connected
local_client.publish("topic", "retained", true);
for(int i=0; i<2; i++)
{
broker.loop();
local_client.loop();
};
// No connect a client from 2nd Esp
ESP8266WiFiClass::selectInstance(2);
MqttClient remote_client;
remote_client.connect(broker_ip, 1883);
remote_client.setCallback(onPublish);
assertTrue(remote_client.connected());
for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); };
assertEqual(broker.clientsCount(), (size_t) 2);
// Should not have received anything yet
assertEqual(published.size(), (size_t)0);
// Now, remote client subscribes to topic
remote_client.subscribe("topic");
for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); };
// Check that the retained message is published
assertEqual(published.size(), (size_t)1);
// FIXME we should check that
// 1 - Retained message has the retain flag set
// 2 - Published retained messages that are send normally have their retain flag off
// The next part of this test does not pass yet (due to remote_client.close()
// that does not work well.
return;
// Now remove the retained message
remote_client.close();
for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); };
assertFalse(remote_client.connected());
assertEqual(broker.clientsCount(), (size_t) 1);
// Disconnect / reconnect the remote clien that should receive again the message
remote_client.connect(broker_ip, 1883);
remote_client.subscribe("topic");
assertTrue(remote_client.connected());
for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); };
assertEqual(broker.clientsCount(), (size_t) 2);
assertEqual(published.size(), (size_t)2);
// Remove the retained message now
local_client.publish("topic", "", true);
assertEqual(published.size(), (size_t)1);
// And reconnect the remote client
remote_client.connect(broker_ip, 1883);
for(int i=0; i<4; i++) { broker.loop(); local_client.loop(); remote_client.loop(); };
assertEqual(broker.clientsCount(), (size_t) 2);
// check that the message was received
assertEqual(published.size(), (size_t)2);
}
test(remote_client_disconnect_reconnect)
{
published.clear();
start_many_wifi_esp(2, true);
assertEqual(WiFi.status(), WL_CONNECTED);
MqttBroker broker(1883);
broker.begin();
IPAddress broker_ip = WiFi.localIP();
ESP8266WiFiClass::selectInstance(2);
MqttClient client;
client.connect(broker_ip, 1883);
for(int i=0; i<4; i++) { broker.loop(); client.loop(); };
assertEqual(broker.clientsCount(), (size_t) 1);
// Disconnect the client
client.close();
for(int i=0; i<4; i++) { broker.loop(); client.loop(); };
assertEqual(broker.clientsCount(), (size_t) 0);
// Reconnect the client
client.connect(broker_ip, 1883);
for(int i=0; i<4; i++) { broker.loop(); client.loop(); };
assertEqual(broker.clientsCount(), (size_t) 1);
}
test(client_to_broker_connexion)
{
start_many_wifi_esp(2, true);
assertEqual(WiFi.status(), WL_CONNECTED);
MqttBroker broker(1883);
@@ -197,9 +304,9 @@ test(network_client_to_broker_connexion)
assertTrue(client.connected());
}
test(network_one_client_one_broker_publish_and_subscribe_through_network)
test(one_client_one_broker_publish_and_subscribe)
{
start_servers(2, true);
start_many_wifi_esp(2, true);
published.clear();
assertEqual(WiFi.status(), WL_CONNECTED);
@@ -228,9 +335,9 @@ test(network_one_client_one_broker_publish_and_subscribe_through_network)
assertEqual((int)lastLength, (int)2); // sizeof(ab)
}
test(network_one_client_one_broker_hudge_publish_and_subscribe_through_network)
test(one_client_one_broker_hudge_payload)
{
start_servers(2, true);
start_many_wifi_esp(2, true);
published.clear();
assertEqual(WiFi.status(), WL_CONNECTED);
@@ -247,8 +354,8 @@ test(network_one_client_one_broker_hudge_publish_and_subscribe_through_network)
std::string sent;
for(int i=0; i<200; i++)
sent += char('0'+i%10);
for(int i=0; i<400; i++)
sent += char('a'+i%26);
client.setCallback(onPublish);
client.subscribe("a/b");
@@ -264,7 +371,7 @@ test(network_one_client_one_broker_hudge_publish_and_subscribe_through_network)
assertEqual((unsigned int)lastLength, (unsigned int)sent.size());
}
test(network_client_should_unregister_when_destroyed)
test(client_should_unregister_when_destroyed)
{
assertEqual(broker.clientsCount(), (size_t)0);
{
@@ -278,7 +385,7 @@ test(network_client_should_unregister_when_destroyed)
// THESE TESTS ARE IN LOCAL MODE
// WE HAVE TO CONVERT THEM TO WIFI MODE (pass through virtual TCP link)
test(network_connect)
test(connect)
{
assertEqual(broker.clientsCount(), (size_t)0);
@@ -287,7 +394,7 @@ test(network_connect)
assertEqual(broker.clientsCount(), (size_t)1);
}
test(network_publish_should_be_dispatched)
test(publish_should_be_dispatched)
{
published.clear();
assertEqual(broker.clientsCount(), (size_t)0);
@@ -307,7 +414,7 @@ test(network_publish_should_be_dispatched)
assertEqual(published[TINY_MQTT_DEFAULT_CLIENT_ID]["a/c"], 2);
}
test(network_publish_should_be_dispatched_to_clients)
test(publish_should_be_dispatched_to_clients)
{
published.clear();
assertEqual(broker.clientsCount(), (size_t)0);
@@ -332,7 +439,7 @@ test(network_publish_should_be_dispatched_to_clients)
assertEqual(published["B"]["a/c"], 0);
}
test(network_unsubscribe)
test(unsubscribe)
{
published.clear();
assertEqual(broker.clientsCount(), (size_t)0);
@@ -352,7 +459,7 @@ test(network_unsubscribe)
assertEqual(published[TINY_MQTT_DEFAULT_CLIENT_ID]["a/b"], 1); // Only one publish has been received
}
test(network_nocallback_when_destroyed)
test(nocallback_when_destroyed)
{
published.clear();
assertEqual(broker.clientsCount(), (size_t)0);
@@ -371,7 +478,7 @@ test(network_nocallback_when_destroyed)
assertEqual(published.size(), (size_t)1); // Only one publish has been received
}
test(network_small_payload)
test(small_payload)
{
published.clear();
@@ -389,7 +496,7 @@ test(network_small_payload)
assertEqual(lastLength, (size_t)4);
}
test(network_hudge_payload)
test(hudge_payload)
{
const char* payload="This payload is hudge, just because its length exceeds 127. Thus when encoding length, we have to encode it on two bytes at min. This should not prevent the message from being encoded and decoded successfully !";
@@ -436,7 +543,7 @@ test(connack)
}
);
start_servers(2, true);
start_many_wifi_esp(2, true);
assertEqual(WiFi.status(), WL_CONNECTED);
MqttBroker broker(1883);
@@ -469,7 +576,7 @@ void setup() {
while(!Serial);
*/
Serial.println("=============[ FAKE NETWORK TinyMqtt TESTS ]========================");
Serial.println("=============[ NETWORK TinyMqtt TESTS ]========================");
WiFi.mode(WIFI_STA);
WiFi.begin("network", "password");