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