10 #include <gtest/gtest.h>
11 #include "../../include/message_bus.hpp"
12 #include <message_bus.hpp>
18 std::vector<subscriber *> get_subscribers(
message_bus *test_subject, uint8_t msg) {
19 return test_subject->subscribers[msg];
30 test_subject.
subscribe(msg, &test_subscriber);
31 std::vector<subscriber *> subscribers_in_bus = get_subscribers(&test_subject, msg);
32 ASSERT_EQ(1, subscribers_in_bus.size());
33 ASSERT_EQ(&test_subscriber, subscribers_in_bus.at(0));
48 test_subject.
subscribe(msg, &test_subscriber1);
49 test_subject.
subscribe(msg, &test_subscriber2);
50 test_subject.
subscribe(msg, &test_subscriber3);
51 test_subject.
subscribe(msg, &test_subscriber4);
52 test_subject.
subscribe(msg, &test_subscriber5);
54 std::vector<subscriber *> subscribers_in_bus = get_subscribers(&test_subject, msg);
56 ASSERT_EQ(5, subscribers_in_bus.size());
57 ASSERT_EQ(&test_subscriber1, subscribers_in_bus.at(0));
58 ASSERT_EQ(&test_subscriber2, subscribers_in_bus.at(1));
59 ASSERT_EQ(&test_subscriber3, subscribers_in_bus.at(2));
60 ASSERT_EQ(&test_subscriber4, subscribers_in_bus.at(3));
61 ASSERT_EQ(&test_subscriber5, subscribers_in_bus.at(4));
72 test_subject.
subscribe(msg1, &test_subscriber);
73 test_subject.
subscribe(msg2, &test_subscriber);
75 std::vector<subscriber *> subscribers_in_bus_for_msg1 = get_subscribers(&test_subject, msg1);
76 std::vector<subscriber *> subscribers_in_bus_for_msg2 = get_subscribers(&test_subject, msg2);
78 ASSERT_EQ(1, subscribers_in_bus_for_msg1.size());
79 ASSERT_EQ(&test_subscriber, subscribers_in_bus_for_msg1.at(0));
81 ASSERT_EQ(1, subscribers_in_bus_for_msg2.size());
82 ASSERT_EQ(&test_subscriber, subscribers_in_bus_for_msg2.at(0));
91 test_subject.
subscribe(msg, &test_subscriber);
97 ASSERT_EQ(msg, result->msg_name);
98 ASSERT_EQ(20, *
static_cast<int *
>(result->
get_data()));
109 test_subject.
subscribe(msg, &test_subscriber1);
110 test_subject.
subscribe(msg, &test_subscriber2);
117 ASSERT_TRUE(result1);
118 ASSERT_EQ(msg, result1->msg_name);
119 ASSERT_EQ(20, *
static_cast<int *
>(result1->
get_data()));
122 ASSERT_TRUE(result2);
123 ASSERT_EQ(msg, result2->msg_name);
124 ASSERT_EQ(20, *
static_cast<int *
>(result2->
get_data()));
135 test_subject.
subscribe(msg1, &test_subscriber);
136 test_subject.
subscribe(msg2, &test_subscriber);
144 ASSERT_TRUE(result1);
145 ASSERT_EQ(msg1, result1->msg_name);
146 ASSERT_EQ(20, *
static_cast<int *
>(result1->
get_data()));
149 ASSERT_TRUE(result2);
150 ASSERT_EQ(msg2, result2->msg_name);
151 ASSERT_EQ(30, *
static_cast<int *
>(result2->
get_data()));
155 int main(
int argc,
char **argv) {
156 ::testing::InitGoogleTest(&argc, argv);
157 return RUN_ALL_TESTS();
The central messaging system of the engine. All subsystem make extensive use of it.
void subscribe(uint8_t msg, subscriber *sub)
void send_msg(message *msg)
A message object passed around in the message bus. Holds anything created with make_data<>().
This class handles a small queue for messages.
message * get_next_message()