forked from uNetworking/uWebSockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopicTree.cpp
199 lines (147 loc) · 6.04 KB
/
TopicTree.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
#include "../src/TopicTree.h"
#include <cassert>
#include <iostream>
/* Modifying the topicTree inside callback is not allowed, we had
* tests for this before but we never need this to work anyways.
* Closing a socket when reaching too much backpressure is done
* deferred to next event loop iteration so we never need to modify
* topicTree inside callback - removed this test */
/* This tests pretty much all features for obvious incorrectness */
void testCorrectness() {
std::cout << "TestCorrectness" << std::endl;
uWS::TopicTree<std::string, std::string_view> *topicTree;
std::map<void *, std::string> expectedResult;
std::map<void *, std::string> actualResult;
topicTree = new uWS::TopicTree<std::string, std::string_view>([&topicTree, &actualResult](uWS::Subscriber *s, std::string &message, auto flags) {
actualResult[s] += message;
/* Success */
return false;
});
uWS::Subscriber *s1 = topicTree->createSubscriber();
uWS::Subscriber *s2 = topicTree->createSubscriber();
/* Make sure s1 < s2 (for debugging) */
if (s2 < s1) {
uWS::Subscriber *tmp = s1;
s1 = s2;
s2 = tmp;
}
/* Publish to topic3 - nobody should see this */
topicTree->publish(nullptr, "topic3", "Nobody should see");
/* Subscribe s1 to topic3 - s1 should not see above message */
topicTree->subscribe(s1, "topic3");
/* Publish to topic3 with s1 as sender - s1 should not get its own messages */
topicTree->publish(s1, "topic3", "Nobody should see");
/* Subscribe s2 to topic3 - should not get any message */
topicTree->subscribe(s2, "topic3");
/* Publish to topic3 without sender - both should see */
topicTree->publish(nullptr, "topic3", "Both should see");
/* Publish to topic3 with s2 as sender - s1 should see */
topicTree->publish(s2, "topic3", "s1 should see, not s2");
/* Publish to topic3 with s1 as sender - s2 should see */
topicTree->publish(s1, "topic3", "s2 should see, not s1");
/* Publish to topic3 without sender - both should see */
topicTree->publish(nullptr, "topic3", "Again, both should see this as well");
// todo: add more cases involving more topics and duplicates, etc
/* Fill out expectedResult */
expectedResult = {
{s1, "Both should sees1 should see, not s2Again, both should see this as well"},
{s2, "Both should sees2 should see, not s1Again, both should see this as well"}
};
/* Compare result with expected result for every subscriber */
topicTree->drain();
for (auto &p : expectedResult) {
std::cout << "Subscriber: " << p.first << std::endl;
if (p.second != actualResult[p.first]) {
std::cout << "ERROR: <" << actualResult[p.first] << "> should be <" << p.second << ">" << std::endl;
exit(1);
}
}
/* Release resources */
topicTree->freeSubscriber(s1);
topicTree->freeSubscriber(s2);
delete topicTree;
}
void testBugReport() {
std::cout << "TestBugReport" << std::endl;
uWS::TopicTree<std::string, std::string_view> *topicTree;
std::map<void *, std::string> expectedResult;
std::map<void *, std::string> actualResult;
topicTree = new uWS::TopicTree<std::string, std::string_view>([&topicTree, &actualResult](uWS::Subscriber *s, std::string &message, auto flags) {
actualResult[s] += message;
/* Success */
return false;
});
uWS::Subscriber *s1 = topicTree->createSubscriber();
uWS::Subscriber *s2 = topicTree->createSubscriber();
/* Make sure s1 < s2 (for debugging) */
if (s2 < s1) {
uWS::Subscriber *tmp = s1;
s1 = s2;
s2 = tmp;
}
/* Each subscriber to its own topic */
topicTree->subscribe(s1, "b1");
topicTree->subscribe(s2, "b2");
/* This one should send b2 to s2 */
topicTree->publish(s1, "b1", "b1");
topicTree->publish(s1, "b2", "b2");
/* This one should send b1 to s1 */
topicTree->publish(s2, "b1", "b1");
topicTree->publish(s2, "b2", "b2");
/* Fill out expectedResult */
expectedResult = {
{s1, "b1"},
{s2, "b2"}
};
/* Compare result with expected result for every subscriber */
topicTree->drain();
for (auto &p : expectedResult) {
std::cout << "Subscriber: " << p.first << std::endl;
if (p.second != actualResult[p.first]) {
std::cout << "ERROR: <" << actualResult[p.first] << "> should be <" << p.second << ">" << std::endl;
exit(1);
}
}
/* Release resources */
topicTree->freeSubscriber(s1);
topicTree->freeSubscriber(s2);
delete topicTree;
}
void testReorderingv19() {
std::cout << "TestReorderingv19" << std::endl;
uWS::TopicTree<std::string, std::string_view> *topicTree;
std::map<void *, std::string> expectedResult;
std::map<void *, std::string> actualResult;
topicTree = new uWS::TopicTree<std::string, std::string_view>([&topicTree, &actualResult](uWS::Subscriber *s, std::string &message, auto flags) {
actualResult[s] += message;
/* Success */
return false;
});
uWS::Subscriber *s1 = topicTree->createSubscriber();
/* Subscribe to 100 topics */
for (int i = 0; i < 100; i++) {
topicTree->subscribe(s1, std::to_string(i));
}
/* Publish to 100 topics in order with messages in order */
for (int i = 0; i < 100; i++) {
topicTree->publish(nullptr, std::to_string(i), std::to_string(i) + ",");
expectedResult[s1].append(std::to_string(i) + ",");
}
/* Compare result with expected result for every subscriber */
topicTree->drain();
for (auto &p : expectedResult) {
std::cout << "Subscriber: " << p.first << std::endl;
if (p.second != actualResult[p.first]) {
std::cout << "ERROR: <" << actualResult[p.first] << "> should be <" << p.second << ">" << std::endl;
exit(1);
}
}
/* Release resources */
topicTree->freeSubscriber(s1);
delete topicTree;
}
int main() {
testCorrectness();
testBugReport();
testReorderingv19();
}