ST_engine  0.3-ALPHA
message_bus_tests.cpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #include <gtest/gtest.h>
11 #include "../../include/message_bus.hpp"
12 #include <message_bus.hpp>
13 
14 class message_bus_tests : public ::testing::Test {
15 
16 protected:
17 
18  std::vector<subscriber *> get_subscribers(message_bus *test_subject, uint8_t msg) {
19  return test_subject->subscribers[msg];
20  }
21 };
22 
23 TEST_F(message_bus_tests, test_one_subscriber_to_message) {
24  //Set up
25  uint8_t msg = 1;
26  message_bus test_subject;
27  subscriber test_subscriber;
28 
29  //Test
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));
34 }
35 
36 
37 TEST_F(message_bus_tests, test_five_subscribers_to_message) {
38  //Set up
39  uint8_t msg = 1;
40  message_bus test_subject;
41  subscriber test_subscriber1;
42  subscriber test_subscriber2;
43  subscriber test_subscriber3;
44  subscriber test_subscriber4;
45  subscriber test_subscriber5;
46 
47  //Test
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);
53 
54  std::vector<subscriber *> subscribers_in_bus = get_subscribers(&test_subject, msg);
55 
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));
62 }
63 
64 TEST_F(message_bus_tests, test_one_subscriber_to_two_messages) {
65  //Set up
66  uint8_t msg1 = 2;
67  uint8_t msg2 = 3;
68  message_bus test_subject;
69  subscriber test_subscriber;
70 
71  //Test
72  test_subject.subscribe(msg1, &test_subscriber);
73  test_subject.subscribe(msg2, &test_subscriber);
74 
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);
77 
78  ASSERT_EQ(1, subscribers_in_bus_for_msg1.size());
79  ASSERT_EQ(&test_subscriber, subscribers_in_bus_for_msg1.at(0));
80 
81  ASSERT_EQ(1, subscribers_in_bus_for_msg2.size());
82  ASSERT_EQ(&test_subscriber, subscribers_in_bus_for_msg2.at(0));
83 }
84 
85 TEST_F(message_bus_tests, test_send_message_to_one_subscriber) {
86 
87  //Set up
88  uint8_t msg = 1;
89  message_bus test_subject;
90  subscriber test_subscriber;
91  test_subject.subscribe(msg, &test_subscriber);
92 
93  //Test
94  test_subject.send_msg(new message(msg, make_data(20)));
95  message *result = test_subscriber.get_next_message();
96  ASSERT_TRUE(result);
97  ASSERT_EQ(msg, result->msg_name);
98  ASSERT_EQ(20, *static_cast<int *>(result->get_data()));
99  delete (result);
100 }
101 
102 TEST_F(message_bus_tests, test_send_message_to_two_subscribers) {
103 
104  //Set up
105  uint8_t msg = 2;
106  message_bus test_subject;
107  subscriber test_subscriber1;
108  subscriber test_subscriber2;
109  test_subject.subscribe(msg, &test_subscriber1);
110  test_subject.subscribe(msg, &test_subscriber2);
111 
112  //Test
113  test_subject.send_msg(new message(msg, make_data(20)));
114  message *result1 = test_subscriber1.get_next_message();
115  message *result2 = test_subscriber2.get_next_message();
116 
117  ASSERT_TRUE(result1);
118  ASSERT_EQ(msg, result1->msg_name);
119  ASSERT_EQ(20, *static_cast<int *>(result1->get_data()));
120  delete (result1);
121 
122  ASSERT_TRUE(result2);
123  ASSERT_EQ(msg, result2->msg_name);
124  ASSERT_EQ(20, *static_cast<int *>(result2->get_data()));
125  delete (result2);
126 }
127 
128 TEST_F(message_bus_tests, test_send_two_messages_to_one_subscriber) {
129 
130  //Set up
131  uint8_t msg1 = 2;
132  uint8_t msg2 = 3;
133  message_bus test_subject;
134  subscriber test_subscriber;
135  test_subject.subscribe(msg1, &test_subscriber);
136  test_subject.subscribe(msg2, &test_subscriber);
137  //Test
138  test_subject.send_msg(new message(msg1, make_data(20)));
139  test_subject.send_msg(new message(msg2, make_data(30)));
140 
141  message *result1 = test_subscriber.get_next_message();
142  message *result2 = test_subscriber.get_next_message();
143 
144  ASSERT_TRUE(result1);
145  ASSERT_EQ(msg1, result1->msg_name);
146  ASSERT_EQ(20, *static_cast<int *>(result1->get_data()));
147  delete (result1);
148 
149  ASSERT_TRUE(result2);
150  ASSERT_EQ(msg2, result2->msg_name);
151  ASSERT_EQ(30, *static_cast<int *>(result2->get_data()));
152  delete (result2);
153 }
154 
155 int main(int argc, char **argv) {
156  ::testing::InitGoogleTest(&argc, argv);
157  return RUN_ALL_TESTS();
158 }
The central messaging system of the engine. All subsystem make extensive use of it.
Definition: message_bus.hpp:29
void subscribe(uint8_t msg, subscriber *sub)
Definition: message_bus.cpp:71
void send_msg(message *msg)
Definition: message_bus.cpp:19
A message object passed around in the message bus. Holds anything created with make_data<>().
Definition: message.hpp:21
void * get_data() const
Definition: message.hpp:80
This class handles a small queue for messages.
Definition: subscriber.hpp:20
message * get_next_message()
Definition: subscriber.hpp:39