brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #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 Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 8 | #include "aos/common/util/thread.h" |
Brian Silverman | 8d2e56e | 2013-09-23 17:55:03 -0700 | [diff] [blame] | 9 | #include "aos/common/die.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | |
| 11 | using ::aos::time::Time; |
| 12 | |
| 13 | namespace aos { |
| 14 | namespace common { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | namespace testing { |
| 16 | |
| 17 | class QueueTest : public ::testing::Test { |
| 18 | protected: |
Brian Silverman | 8d2e56e | 2013-09-23 17:55:03 -0700 | [diff] [blame] | 19 | void SetUp() override { |
| 20 | SetDieTestMode(true); |
| 21 | } |
| 22 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 23 | 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 Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 32 | class MyThread : public util::Thread { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 33 | public: |
| 34 | MyThread() : threaded_test_queue(".aos.common.testing.test_queue") {} |
| 35 | |
| 36 | virtual void Run() { |
Brian Silverman | 428de56 | 2014-04-10 15:59:19 -0700 | [diff] [blame] | 37 | threaded_test_queue.FetchNextBlocking(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 38 | 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. |
| 50 | TEST_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 Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 56 | EXPECT_LE(t.threaded_test_queue.Age(), time::Time::InMS(57)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Tests that we can send a message with the message pointer and get it back. |
| 60 | TEST_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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 71 | // Tests that we can send a message with the builder and get it back. |
| 72 | TEST_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 Silverman | 42456d8 | 2014-08-19 12:43:59 -0400 | [diff] [blame] | 81 | // 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. |
| 84 | TEST_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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 92 | // Tests that various pointer deref functions at least seem to work. |
| 93 | TEST_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. |
| 104 | TEST_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. |
| 116 | TEST_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. |
| 125 | TEST_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. |
| 140 | TEST_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. |
| 149 | TEST_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. |
| 155 | TEST_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. |
| 161 | TEST_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. |
| 169 | TEST_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 | |
| 180 | class 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. |
| 196 | TEST_F(GroupTest, Hash) { |
| 197 | EXPECT_EQ(static_cast<uint32_t>(0x20561114), my_test_queuegroup.hash()); |
| 198 | } |
| 199 | |
| 200 | // Tests that the hash works. |
| 201 | TEST_F(GroupTest, RealHash) { |
Brian Silverman | 981a7bb | 2014-02-16 21:17:04 -0800 | [diff] [blame] | 202 | EXPECT_EQ(static_cast<uint32_t>(0x93596b2f), test_queuegroup.hash()); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // Tests that name works. |
| 206 | TEST_F(GroupTest, Name) { |
| 207 | EXPECT_EQ(std::string(".aos.common.testing.test_queuegroup"), |
| 208 | std::string(my_test_queuegroup.name())); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | class MessageTest : public ::testing::Test { |
| 213 | public: |
| 214 | TestingMessage msg; |
| 215 | }; |
| 216 | |
| 217 | TEST_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 | |
| 230 | TEST_F(MessageTest, Size) { |
| 231 | EXPECT_EQ(static_cast<size_t>(13), msg.Size()); |
| 232 | } |
| 233 | |
| 234 | TEST_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. |
| 251 | TEST_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 Silverman | 981a7bb | 2014-02-16 21:17:04 -0800 | [diff] [blame] | 257 | std::string golden("971.000000254s, T, 2056"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 258 | 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. |
| 266 | TEST_F(MessageTest, Hash) { |
Brian Silverman | 981a7bb | 2014-02-16 21:17:04 -0800 | [diff] [blame] | 267 | EXPECT_EQ(static_cast<uint32_t>(0xc33651ac), |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 268 | static_cast<uint32_t>(TestingMessage::kHash)); |
| 269 | } |
| 270 | |
| 271 | TEST_F(MessageTest, SetNow) { |
| 272 | msg.SetTimeToNow(); |
| 273 | EXPECT_LE(msg.sent_time - Time::Now(), Time::InMS(20)); |
| 274 | } |
| 275 | |
| 276 | } // namespace testing |
| 277 | } // namespace common |
| 278 | } // namespace aos |