-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.cpp
172 lines (130 loc) · 3.66 KB
/
tests.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
#include <limits.h>
#include "gtest/gtest.h"
#include "eventbus.hpp"
#include "event.hpp"
#include <memory>
class BusTest : public ::testing::Test {
protected:
virtual void SetUp() {}
virtual void TearDown() {}
};
extern EventBus bus;
/* Module Foo thread */
void StartFooAsyn() {
Foo foo;
foo.RunTestAsyn();
}
/* Module FooB thread */
void StartFooBAsyn() {
FooB foob;
foob.RunTestAsyn();
}
void StartFooASyn() {
Foo foo;
foo.RunTestSyn();
}
/* Module FooB thread */
void StartFooBSyn() {
FooB foob;
foob.RunTestSyn();
}
/* Declare a bus and a listener and let the listener do a subscribe
* Check that the map has at least 1 element and that the listener is found in the map*/
TEST(BusTest, subscribe)
{
EventBus bus;
Foo foo;
bus.subscribe<EventA>(&foo);
auto map = bus.getListenersMap();
auto mapEvent = map[GetTypeId<EventA>()];
/* The map should now contain 1 element */
EXPECT_EQ(1, mapEvent.getVector().size());
}
/*Do a couple of subscription and one unsubscription and checks for correctess */
TEST(BusTest, unsubscribe)
{
EventBus bus;
Foo foo;
FooB foo2;
bool success = false;
bus.subscribe<EventA>(&foo);
bus.subscribe<EventA>(&foo2);
bus.unsubscribe<EventA>(&foo);
//Check the element not to be present
bool found = bus.checkListenerPresent<EventA>(&foo);
bool found2 = bus.checkListenerPresent<EventA>(&foo2);
//Instead foo2 has to be present
auto map = bus.getListenersMap();
auto mapEvent = map[GetTypeId<EventA>()];
if(found == false && found2) success = true;
/* The map should now contain 1 element */
EXPECT_EQ(1, mapEvent.getVector().size());
EXPECT_EQ(success, true);
}
/* Test the publish in async mode, do some subscribe do a send async and test the map */
TEST(BusTest, sendAsync)
{
EventBus bus;
Foo foo;
FooB foob;
const EventA event(5);
bool success = false;
bool found = false;
bus.subscribe<EventA>(&foo);
bus.subscribe<EventA>(&foob);
int notified = bus.sendAsync<EventA>(event);
/* The map should now contain 1 element */
EXPECT_EQ(2, notified);
}
/* Test the publish in sync mode, do some subscribe do a send async and test the map */
TEST(BusTest, sendSync)
{
EventBus bus;
Foo foo;
FooB foob;
const EventA event(5);
bool success = false;
bool found = false;
bus.subscribe<EventA>(&foo);
bus.subscribe<EventA>(&foob);
int notified = bus.sendAsync<EventA>(event);
/* The map should now contain 1 element */
EXPECT_EQ(2, notified);
}
/* Test multithreading async*/
TEST(BusTest, multithreadingAsync)
{
/* Run the threads and let the process wait for the bus */
bus.createThreads();
std::thread t2(StartFooAsyn);
std::thread t3(StartFooBAsyn);
t2.detach();
t3.detach();
// Wait for thread-scenarios to be completed
sleep(1);
auto map = bus.getListenersMap();
auto mapEvent = map[GetTypeId<EventA>()];
auto mapEvent2 = map[GetTypeId<EventB>()];
/* The map should now contain 4 elements (to be coherent with scenario runned) */
EXPECT_EQ(3, mapEvent.getVector().size() + mapEvent2.getVector().size());
/* Terminate both the process and all threads */
}
/* Test multithreading sync*/
TEST(BusTest, multithreadingSync)
{
bus.Reset();
/* Run the threads and let the process wait for the bus */
bus.createThreads();
std::thread t2(StartFooASyn);
std::thread t3(StartFooBSyn);
t2.detach();
t3.detach();
// Wait for thread-scenarios to be completed
sleep(1);
auto map = bus.getListenersMap();
auto mapEvent = map[GetTypeId<EventA>()];
auto mapEvent2 = map[GetTypeId<EventB>()];
/* The map should now contain 4 elements (to be coherent with scenario runned) */
EXPECT_EQ(3, mapEvent.getVector().size() + mapEvent2.getVector().size());
/* Terminate both the process and all threads */
}