TopicLengthTest.cpp
3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/****************************************************************************
* COpyright (c) 2023 Open Systems Development B.V.
****************************************************************************/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include <memory>
#include "mqttclient.h"
using namespace osdev::components::mqtt;
using namespace osdev::components::log;
static const std::string main_topic = "Topic Length Test/";
/****************************************************************************
* H E L P E R C L A S S E S
****************************************************************************/
class Publisher
{
public:
Publisher() : m_mqtt_client("TopicTester"){}
virtual ~Publisher() {}
void connect(const std::string &hostname,
int portnumber = 1883,
const std::string &username = std::string(),
const std::string &password = std::string(),
const std::string &lwt_topic = std::string(),
const std::string &lwt_message = std::string()
)
{
m_mqtt_client.connect(hostname, portnumber,
osdev::components::mqtt::Credentials(username, password),
osdev::components::mqtt::mqtt_LWT(lwt_topic, lwt_message),
true,
osdev::components::log::LogSettings
{
osdev::components::log::LogLevel::Debug,
osdev::components::log::LogMask::None
});
}
void publish(const std::string &message_topic, const std::string &message_payload)
{
osdev::components::mqtt::MqttMessage message(message_topic, true, false, message_payload);
osdev::components::mqtt::Token t_result = m_mqtt_client.publish(message, 0);
}
private:
osdev::components::mqtt::MqttClient m_mqtt_client;
};
/// @brief class to generate a cumulative topic..
class TopicTester
{
public:
TopicTester(std::shared_ptr<Publisher> publisher) : m_publisher(publisher){}
virtual ~TopicTester(){}
void RunTopicTester(int max_number_of_chars)
{
for(int nCount = 1; nCount < max_number_of_chars; nCount++)
{
std::string subtopic(nCount, 'a');
std::string topic = std::string(main_topic + subtopic);
std::string message(std::to_string(topic.size()) + " (" + std::to_string(subtopic.size()) + ")");
m_publisher->publish(topic, message);
}
}
private:
std::shared_ptr<Publisher> m_publisher;
};
/*****************************************************************************
* T H E A C T U A L T E S T S
*****************************************************************************/
/// TopicTester
TEST(topictest, TopicLengthTest)
{
std::shared_ptr<Publisher> pPublisher = std::make_shared<Publisher>();
pPublisher->connect("127.0.0.1", 1883);
TopicTester oTester(pPublisher);
oTester.RunTopicTester(101);
}