blob: 6d5dda60ca242af6d96f412e5fffae49af43fce5 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <unistd.h>
2
3#include <memory>
4
5#include "gtest/gtest.h"
6#include "aos/common/test_queue.q.h"
7#include "aos/common/queue_testutils.h"
Brian Silverman798c7782013-03-28 16:48:02 -07008#include "aos/common/util/thread.h"
Brian Silverman8d2e56e2013-09-23 17:55:03 -07009#include "aos/common/die.h"
brians343bc112013-02-10 01:53:46 +000010
11using ::aos::time::Time;
12
13namespace aos {
14namespace common {
brians343bc112013-02-10 01:53:46 +000015namespace testing {
16
17class QueueTest : public ::testing::Test {
18 protected:
Brian Silverman8d2e56e2013-09-23 17:55:03 -070019 void SetUp() override {
20 SetDieTestMode(true);
21 }
22
brians343bc112013-02-10 01:53:46 +000023 GlobalCoreInstance my_core;
24 // Create a new instance of the test queue so that it invalidates the queue
25 // that it points to. Otherwise, we will have a pointer to shared memory that
26 // is no longer valid.
27 ::aos::Queue<TestingMessage> my_test_queue;
28
29 QueueTest() : my_test_queue(".aos.common.testing.test_queue") {}
30};
31
Brian Silverman798c7782013-03-28 16:48:02 -070032class MyThread : public util::Thread {
brians343bc112013-02-10 01:53:46 +000033 public:
34 MyThread() : threaded_test_queue(".aos.common.testing.test_queue") {}
35
36 virtual void Run() {
Brian Silverman428de562014-04-10 15:59:19 -070037 threaded_test_queue.FetchNextBlocking();
brians343bc112013-02-10 01:53:46 +000038 EXPECT_TRUE(threaded_test_queue->test_bool);
39 EXPECT_EQ(0x971, threaded_test_queue->test_int);
40 }
41
42 ::aos::Queue<TestingMessage> threaded_test_queue;
43 private:
44 DISALLOW_COPY_AND_ASSIGN(MyThread);
45};
46
47
48// Tests that we can send a message to another thread and it blocking receives
49// it at the correct time.
50TEST_F(QueueTest, FetchBlocking) {
51 MyThread t;
52 t.Start();
53 usleep(50000);
54 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x971).Send();
55 t.Join();
Brian Silverman653491d2014-05-13 16:53:29 -070056 EXPECT_LE(t.threaded_test_queue.Age(), time::Time::InMS(57));
brians343bc112013-02-10 01:53:46 +000057}
58
59// Tests that we can send a message with the message pointer and get it back.
60TEST_F(QueueTest, SendMessage) {
61 ScopedMessagePtr<TestingMessage> msg = my_test_queue.MakeMessage();
62 msg->test_bool = true;
63 msg->test_int = 0x971;
64 msg.Send();
65
66 ASSERT_TRUE(my_test_queue.FetchLatest());
67 EXPECT_TRUE(my_test_queue->test_bool);
68 EXPECT_EQ(0x971, my_test_queue->test_int);
69}
70
brians343bc112013-02-10 01:53:46 +000071// Tests that we can send a message with the builder and get it back.
72TEST_F(QueueTest, SendWithBuilder) {
73 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x971).Send();
74
75 ASSERT_TRUE(my_test_queue.FetchLatest());
76 EXPECT_EQ(true, my_test_queue->test_bool);
77 EXPECT_EQ(0x971, my_test_queue->test_int);
78 EXPECT_EQ(true, my_test_queue.IsNewerThanMS(10000));
79}
80
Brian Silverman42456d82014-08-19 12:43:59 -040081// Makes sure that MakeWithBuilder zeros the message initially.
82// This might randomly succeed sometimes, but it will fail with asan if it
83// doesn't.
84TEST_F(QueueTest, BuilderZero) {
85 my_test_queue.MakeWithBuilder().Send();
86
87 ASSERT_TRUE(my_test_queue.FetchLatest());
88 EXPECT_FALSE(my_test_queue->test_bool);
89 EXPECT_EQ(0, my_test_queue->test_int);
90}
91
brians343bc112013-02-10 01:53:46 +000092// Tests that various pointer deref functions at least seem to work.
93TEST_F(QueueTest, PointerDeref) {
94 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x971).Send();
95
96 ASSERT_TRUE(my_test_queue.FetchLatest());
97 const TestingMessage *msg_ptr = my_test_queue.get();
98 ASSERT_NE(static_cast<TestingMessage*>(NULL), msg_ptr);
99 EXPECT_EQ(0x971, msg_ptr->test_int);
100 EXPECT_EQ(msg_ptr, &(*my_test_queue));
101}
102
103// Tests that FetchNext doesn't miss any messages.
104TEST_F(QueueTest, FetchNext) {
105 for (int i = 0; i < 10; ++i) {
106 my_test_queue.MakeWithBuilder().test_bool(true).test_int(i).Send();
107 }
108
109 for (int i = 0; i < 10; ++i) {
110 ASSERT_TRUE(my_test_queue.FetchNext());
111 EXPECT_EQ(i, my_test_queue->test_int);
112 }
113}
114
115// Tests that FetchLatest skips a missing message.
116TEST_F(QueueTest, FetchLatest) {
117 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x254).Send();
118 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x971).Send();
119
120 ASSERT_TRUE(my_test_queue.FetchLatest());
121 EXPECT_EQ(0x971, my_test_queue->test_int);
122}
123
124// Tests that FetchLatest works with multiple readers.
125TEST_F(QueueTest, FetchLatestMultiple) {
126 ::aos::Queue<TestingMessage> my_second_test_queue(
127 ".aos.common.testing.test_queue");
128 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x254).Send();
129 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x971).Send();
130
131 ASSERT_TRUE(my_test_queue.FetchLatest());
132 EXPECT_EQ(0x971, my_test_queue->test_int);
133 ASSERT_TRUE(my_second_test_queue.FetchLatest());
134 ASSERT_TRUE(my_second_test_queue.get() != NULL);
135 EXPECT_EQ(0x971, my_second_test_queue->test_int);
136}
137
138
139// Tests that fetching without a new message returns false.
140TEST_F(QueueTest, FetchLatestWithoutMessage) {
141 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x254).Send();
142 EXPECT_TRUE(my_test_queue.FetchLatest());
143 EXPECT_FALSE(my_test_queue.FetchLatest());
144 EXPECT_FALSE(my_test_queue.FetchLatest());
145 EXPECT_EQ(0x254, my_test_queue->test_int);
146}
147
148// Tests that fetching without a message returns false.
149TEST_F(QueueTest, FetchOnFreshQueue) {
150 EXPECT_FALSE(my_test_queue.FetchLatest());
151 EXPECT_EQ(static_cast<TestingMessage*>(NULL), my_test_queue.get());
152}
153
154// Tests that fetch next without a message returns false.
155TEST_F(QueueTest, FetchNextOnFreshQueue) {
156 EXPECT_FALSE(my_test_queue.FetchNext());
157 EXPECT_EQ(static_cast<TestingMessage*>(NULL), my_test_queue.get());
158}
159
160// Tests that fetch next without a new message returns false.
161TEST_F(QueueTest, FetchNextWithoutMessage) {
162 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x254).Send();
163 EXPECT_TRUE(my_test_queue.FetchNext());
164 EXPECT_FALSE(my_test_queue.FetchNext());
165 EXPECT_NE(static_cast<TestingMessage*>(NULL), my_test_queue.get());
166}
167
168// Tests that age makes some sense.
169TEST_F(QueueTest, Age) {
170 my_test_queue.MakeWithBuilder().test_bool(true).test_int(0x971).Send();
171
172 ASSERT_TRUE(my_test_queue.FetchLatest());
173 EXPECT_TRUE(my_test_queue.IsNewerThanMS(100));
174 const Time age = my_test_queue.Age();
175 EXPECT_EQ(0, age.sec());
176 EXPECT_GE(100000000, age.nsec());
177}
178
179
180class GroupTest : public ::testing::Test {
181 protected:
182 GlobalCoreInstance my_core;
183 // Create a new instance of the test group so that it invalidates the queue
184 // that it points to. Otherwise, we will have a pointer to shared memory that
185 // is no longer valid.
186 TwoQueues my_test_queuegroup;
187
188 GroupTest()
189 : my_test_queuegroup(".aos.common.testing.test_queuegroup",
190 0x20561114,
191 ".aos.common.testing.test_queuegroup.first",
192 ".aos.common.testing.test_queuegroup.second") {}
193};
194
195// Tests that the hash gets preserved.
196TEST_F(GroupTest, Hash) {
197 EXPECT_EQ(static_cast<uint32_t>(0x20561114), my_test_queuegroup.hash());
198}
199
200// Tests that the hash works.
201TEST_F(GroupTest, RealHash) {
Brian Silverman981a7bb2014-02-16 21:17:04 -0800202 EXPECT_EQ(static_cast<uint32_t>(0x93596b2f), test_queuegroup.hash());
brians343bc112013-02-10 01:53:46 +0000203}
204
205// Tests that name works.
206TEST_F(GroupTest, Name) {
207 EXPECT_EQ(std::string(".aos.common.testing.test_queuegroup"),
208 std::string(my_test_queuegroup.name()));
209}
210
211
212class MessageTest : public ::testing::Test {
213 public:
214 TestingMessage msg;
215};
216
217TEST_F(MessageTest, Zeroing) {
218 msg.test_bool = true;
219 msg.test_int = 0x254;
220 msg.SetTimeToNow();
221
222 msg.Zero();
223
224 EXPECT_FALSE(msg.test_bool);
225 EXPECT_EQ(0, msg.test_int);
226 EXPECT_EQ(0, msg.sent_time.sec());
227 EXPECT_EQ(0, msg.sent_time.nsec());
228}
229
230TEST_F(MessageTest, Size) {
231 EXPECT_EQ(static_cast<size_t>(13), msg.Size());
232}
233
234TEST_F(MessageTest, Serialize) {
235 char serialized_data[msg.Size()];
236 msg.test_bool = true;
237 msg.test_int = 0x254;
238 msg.SetTimeToNow();
239
240 msg.Serialize(serialized_data);
241
242 TestingMessage new_msg;
243 new_msg.Deserialize(serialized_data);
244
245 EXPECT_EQ(msg.test_bool, new_msg.test_bool);
246 EXPECT_EQ(msg.test_int, new_msg.test_int);
247 EXPECT_EQ(msg.sent_time, new_msg.sent_time);
248}
249
250// Tests that Print prints out a message nicely.
251TEST_F(MessageTest, Print) {
252 char printdata[1024];
253 msg.test_bool = true;
254 msg.test_int = 2056;
255 msg.sent_time = Time(971, 254);
256
Brian Silverman981a7bb2014-02-16 21:17:04 -0800257 std::string golden("971.000000254s, T, 2056");
brians343bc112013-02-10 01:53:46 +0000258 EXPECT_EQ(golden.size(), msg.Print(printdata, sizeof(printdata)));
259
260 EXPECT_EQ(golden, std::string(printdata));
261}
262
263// Tests that the hash never changes. If it changes, then someone broke the
264// hash routine or changed the message declaration. Both changes need to be
265// validated by hand.
266TEST_F(MessageTest, Hash) {
Brian Silverman981a7bb2014-02-16 21:17:04 -0800267 EXPECT_EQ(static_cast<uint32_t>(0xc33651ac),
brians343bc112013-02-10 01:53:46 +0000268 static_cast<uint32_t>(TestingMessage::kHash));
269}
270
271TEST_F(MessageTest, SetNow) {
272 msg.SetTimeToNow();
273 EXPECT_LE(msg.sent_time - Time::Now(), Time::InMS(20));
274}
275
Brian Silverman203a0fc2015-03-16 15:21:00 -0700276// Tests that EqualsNoTime works.
277TEST_F(MessageTest, EqualsNoTime) {
278 msg.test_bool = true;
279 msg.test_int = 971;
280 TestingMessage other;
281 other.test_int = 971;
282 EXPECT_FALSE(other.EqualsNoTime(msg));
283 EXPECT_FALSE(msg.EqualsNoTime(other));
284 other.test_bool = true;
285 EXPECT_TRUE(other.EqualsNoTime(msg));
286 EXPECT_TRUE(msg.EqualsNoTime(other));
287 msg.SetTimeToNow();
288 EXPECT_TRUE(msg.EqualsNoTime(other));
289}
290
brians343bc112013-02-10 01:53:46 +0000291} // namespace testing
292} // namespace common
293} // namespace aos