Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1 | #include "aos/events/shm-event-loop.h" |
| 2 | |
| 3 | #include "aos/events/event-loop_param_test.h" |
| 4 | #include "aos/testing/test_shm.h" |
| 5 | #include "gtest/gtest.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | namespace testing { |
| 9 | namespace { |
| 10 | |
| 11 | class ShmEventLoopTestFactory : public EventLoopTestFactory { |
| 12 | public: |
| 13 | std::unique_ptr<EventLoop> Make() override { |
| 14 | return std::unique_ptr<EventLoop>(new ShmEventLoop()); |
| 15 | } |
| 16 | |
| 17 | ::aos::testing::TestSharedMemory my_shm_; |
| 18 | }; |
| 19 | |
| 20 | INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest, |
| 21 | ::testing::Values([]() { |
| 22 | return new ShmEventLoopTestFactory(); |
| 23 | })); |
| 24 | |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 25 | struct TestMessage : public ::aos::Message { |
| 26 | enum { kQueueLength = 100, kHash = 0x696c0cdc }; |
| 27 | int msg_value; |
| 28 | |
| 29 | void Zero() { msg_value = 0; } |
| 30 | static size_t Size() { return 1 + ::aos::Message::Size(); } |
| 31 | size_t Print(char *buffer, size_t length) const; |
| 32 | TestMessage() { Zero(); } |
| 33 | }; |
| 34 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 35 | } // namespace |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 36 | |
| 37 | // Tests that FetchNext behaves correctly when we get two messages in the queue |
| 38 | // but don't consume the first until after the second has been sent. |
| 39 | // This cannot be abstracted to AbstractEventLoopTest because not all |
| 40 | // event loops currently support FetchNext(). |
| 41 | TEST(ShmEventLoopTest, FetchNextTest) { |
| 42 | ::aos::testing::TestSharedMemory my_shm; |
| 43 | |
| 44 | ShmEventLoop send_loop; |
| 45 | ShmEventLoop fetch_loop; |
| 46 | auto sender = send_loop.MakeSender<TestMessage>("/test"); |
| 47 | Fetcher<TestMessage> fetcher = fetch_loop.MakeFetcher<TestMessage>("/test"); |
| 48 | |
| 49 | { |
| 50 | auto msg = sender.MakeMessage(); |
| 51 | msg->msg_value = 100; |
| 52 | ASSERT_TRUE(msg.Send()); |
| 53 | } |
| 54 | |
| 55 | { |
| 56 | auto msg = sender.MakeMessage(); |
| 57 | msg->msg_value = 200; |
| 58 | ASSERT_TRUE(msg.Send()); |
| 59 | } |
| 60 | |
| 61 | ASSERT_TRUE(fetcher.FetchNext()); |
| 62 | ASSERT_NE(nullptr, fetcher.get()); |
| 63 | EXPECT_EQ(100, fetcher->msg_value); |
| 64 | |
| 65 | ASSERT_TRUE(fetcher.FetchNext()); |
| 66 | ASSERT_NE(nullptr, fetcher.get()); |
| 67 | EXPECT_EQ(200, fetcher->msg_value); |
| 68 | |
| 69 | // When we run off the end of the queue, expect to still have the old message: |
| 70 | ASSERT_FALSE(fetcher.FetchNext()); |
| 71 | ASSERT_NE(nullptr, fetcher.get()); |
| 72 | EXPECT_EQ(200, fetcher->msg_value); |
| 73 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 74 | } // namespace testing |
| 75 | } // namespace aos |