-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.cpp
207 lines (157 loc) · 5.62 KB
/
main_test.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// Created by wkl04 on 10-12-2019.
//
#include "precomp.h"
#include "subscription_map.h"
#include "retained_topic_map.h"
#include <iostream>
void TestSingleSubscription()
{
std::string text = "example/test/A";
single_subscription_map< std::function< void() > > map;
map.insert("example/test/A", [] { std::cout << "example/test/A: Subscriber 1" << std::endl; } );
try {
map.insert("example/test/A", [] { std::cout << "example/test/A: Subscriber 2" << std::endl; } );
} catch(std::exception &e)
{
// Should fail due to duplicate subscription
std::cout << e.what() << std::endl;
}
map.insert("example/test/B", [] { std::cout << "example/test/B" << std::endl; } );
map.insert("example/+/A", [] { std::cout << "example/+/A: Plus" << std::endl; } );
map.insert("example/#", [] { std::cout << "example/#" << std::endl; } );
std::cout << "Test with hash, plus and full path" << std::endl;
map.find("example/test/A", [](std::function< void() > const &a) {
a();
});
map.remove("example/+/A");
std::cout << "Test with hash, full path" << std::endl;
map.find("example/test/A", [](std::function< void() > const &a) {
a();
});
map.remove("example/#");
std::cout << "Test with full path only" << std::endl;
map.find("example/test/A", [](std::function< void() > const &a) {
a();
});
map.remove("example/test/A");
// map.remove("example/test/A");
map.remove("example/test/B");
std::cout << "No entries exist anymore" << std::endl;
map.find("example/test/A", [](std::function< void() > const &a) {
a();
});
std::cout << "Remaining size should be 1 (root element only)" << std::endl;
std::cout << "Remaining size: " << map.size() << std::endl;
}
void TestMultipleSubscription()
{
std::string text = "example/test/A";
multiple_subscription_map<std::string, std::deque> map;
try {
map.insert("doubleslash//#", "Should fail");
} catch(std::exception &e)
{
// Should fail due to empty path usage
std::cout << e.what() << std::endl;
}
map.insert("example/test/A", "example/test/A: Subscriber 1");
map.insert("example/test/A", "example/test/A: Subscriber 2");
map.insert("example/test/B", "example/test/B");
map.insert("example/+/A", "example/+/A: Plus");
map.insert("example/#", "example/#");
std::cout << "Test with hash, plus and full path" << std::endl;
map.find("example/test/A", [](std::string const &a) {
std::cout << a << std::endl;
});
map.remove("example/+/A", "example/+/A: Plus");
std::cout << "Test with hash, full path" << std::endl;
map.find("example/test/A", [](std::string const &a) {
std::cout << a << std::endl;
});
map.remove("example/#", "example/#");
std::cout << "Test with full path only" << std::endl;
map.find("example/test/A", [](std::string const &a) {
std::cout << a << std::endl;
});
map.remove("example/test/A", "example/test/A: Subscriber 1");
map.remove("example/test/A", "example/test/A: Subscriber 2");
map.remove("example/test/B", "example/test/B");
std::cout << "No entries exist anymore" << std::endl;
map.find("example/test/A", [](std::string const &a) {
std::cout << a << std::endl;
});
std::cout << "Remaining size should be 1 (root element only)" << std::endl;
std::cout << "Remaining size: " << map.size() << std::endl;
}
void TestRetainedTopics()
{
retained_topic_map<std::string> map;
map.insert_or_update("example/test/A", "example/test/A: Content 1");
map.insert_or_update("example/test/B", "example/test/B: Content 1");
map.insert_or_update("example/A/test", "example/A/test: Content 1");
map.insert_or_update("example/B/test", "example/B/test: Content 1");
/*map.find("example/test/A", [](std::string const &a) {
std::cout << a << std::endl;
});
map.find("example/test/B", [](std::string const &a) {
std::cout << a << std::endl;
});
map.find("example/test/+", [](std::string const &a) {
std::cout << a << std::endl;
});
map.find("example/+/B", [](std::string const &a) {
std::cout << a << std::endl;
}); */
map.find("example/+/A", [](std::string const &a) {
std::cout << a << std::endl;
});
map.remove("example/test/A");
map.find("example/+/A", [](std::string const &a) {
std::cout << a << std::endl;
});
}
#include <mqtt/subscribe_options.hpp>
#include <mqtt/buffer.hpp>
struct session
{
using session_subs_t = std::map< MQTT_NS::buffer, MQTT_NS::qos >;
MQTT_NS::buffer client_id;
session_subs_t subscriptions;
session(const mqtt::buffer &clientId)
: client_id(clientId)
{ }
void add_subscription(MQTT_NS::buffer topic, MQTT_NS::qos qos)
{
subscriptions[topic] = qos;
}
};
using session_ptr_t = std::shared_ptr<session>;
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
namespace mi = boost::multi_index;
class session_db
{
struct tag_client_id { };
using session_index = mi::multi_index_container<
session_ptr_t,
mi::indexed_by<mi::ordered_unique< mi::tag<tag_client_id>, mi::member<session, MQTT_NS::buffer, &session::client_id> > >
>;
session_index sessions;
};
void TestSessions()
{
};
int main(int, char**)
{
try {
TestSingleSubscription();
TestMultipleSubscription();
TestRetainedTopics();
TestSessions();
} catch(std::exception &e)
{
std::cout << e.what() << std::endl;
}
}