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 { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 10 | namespace chrono = ::std::chrono; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 11 | |
| 12 | class ShmEventLoopTestFactory : public EventLoopTestFactory { |
| 13 | public: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 14 | ::std::unique_ptr<EventLoop> Make() override { |
| 15 | return ::std::unique_ptr<EventLoop>(new ShmEventLoop()); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 16 | } |
| 17 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 18 | ::std::unique_ptr<EventLoop> MakePrimary() override { |
| 19 | ::std::unique_ptr<ShmEventLoop> loop = |
| 20 | ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop()); |
| 21 | primary_event_loop_ = loop.get(); |
| 22 | return ::std::move(loop); |
| 23 | } |
| 24 | |
| 25 | void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); } |
| 26 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 27 | void Exit() override { CHECK_NOTNULL(primary_event_loop_)->Exit(); } |
| 28 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 29 | void SleepFor(::std::chrono::nanoseconds duration) override { |
| 30 | ::std::this_thread::sleep_for(duration); |
| 31 | } |
| 32 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 33 | private: |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 34 | ::aos::testing::TestSharedMemory my_shm_; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 35 | |
| 36 | ::aos::ShmEventLoop *primary_event_loop_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest, |
| 40 | ::testing::Values([]() { |
| 41 | return new ShmEventLoopTestFactory(); |
| 42 | })); |
| 43 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 44 | INSTANTIATE_TEST_CASE_P(ShmEventLoopDeathTest, AbstractEventLoopDeathTest, |
| 45 | ::testing::Values([]() { |
| 46 | return new ShmEventLoopTestFactory(); |
| 47 | })); |
| 48 | |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 49 | struct TestMessage : public ::aos::Message { |
| 50 | enum { kQueueLength = 100, kHash = 0x696c0cdc }; |
| 51 | int msg_value; |
| 52 | |
| 53 | void Zero() { msg_value = 0; } |
| 54 | static size_t Size() { return 1 + ::aos::Message::Size(); } |
| 55 | size_t Print(char *buffer, size_t length) const; |
| 56 | TestMessage() { Zero(); } |
| 57 | }; |
| 58 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 59 | } // namespace |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 60 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 61 | bool IsRealtime() { |
| 62 | int scheduler; |
| 63 | if ((scheduler = sched_getscheduler(0)) == -1) { |
| 64 | PLOG(FATAL, "sched_getscheduler(0) failed\n"); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 65 | } |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 66 | LOG(INFO, "scheduler is %d\n", scheduler); |
| 67 | return scheduler == SCHED_FIFO || scheduler == SCHED_RR; |
| 68 | } |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 69 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 70 | // Tests that every handler type is realtime and runs. There are threads |
| 71 | // involved and it's easy to miss one. |
| 72 | TEST(ShmEventLoopTest, AllHandlersAreRealtime) { |
| 73 | ShmEventLoopTestFactory factory; |
| 74 | auto loop = factory.MakePrimary(); |
| 75 | auto loop2 = factory.Make(); |
| 76 | |
| 77 | loop->SetRuntimeRealtimePriority(1); |
| 78 | |
| 79 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 80 | |
| 81 | bool did_onrun = false; |
| 82 | bool did_timer = false; |
| 83 | bool did_watcher = false; |
| 84 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 85 | auto timer = loop->AddTimer([&did_timer, &loop, &factory]() { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(IsRealtime()); |
| 87 | did_timer = true; |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 88 | factory.Exit(); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 89 | }); |
| 90 | |
| 91 | loop->MakeWatcher("/test", [&did_watcher](const TestMessage &) { |
| 92 | EXPECT_TRUE(IsRealtime()); |
| 93 | did_watcher = true; |
| 94 | }); |
| 95 | |
| 96 | loop->OnRun([&loop, &did_onrun, &sender, timer]() { |
| 97 | EXPECT_TRUE(IsRealtime()); |
| 98 | did_onrun = true; |
| 99 | timer->Setup(loop->monotonic_now() + chrono::milliseconds(100)); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 100 | auto msg = sender.MakeMessage(); |
| 101 | msg->msg_value = 200; |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 102 | msg.Send(); |
| 103 | }); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 104 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 105 | factory.Run(); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 106 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 107 | EXPECT_TRUE(did_onrun); |
| 108 | EXPECT_TRUE(did_timer); |
| 109 | EXPECT_TRUE(did_watcher); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 110 | } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 111 | |
| 112 | // Tests that missing a deadline inside the function still results in PhasedLoop |
| 113 | // running at the right offset. |
| 114 | TEST(ShmEventLoopTest, DelayedPhasedLoop) { |
| 115 | ShmEventLoopTestFactory factory; |
| 116 | auto loop1 = factory.MakePrimary(); |
| 117 | |
| 118 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 119 | |
| 120 | constexpr chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 121 | |
| 122 | loop1->AddPhasedLoop( |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 123 | [×, &loop1, &kOffset, &factory](int count) { |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 124 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 125 | loop1->monotonic_now(); |
| 126 | |
| 127 | // Compute our offset. |
| 128 | const ::aos::monotonic_clock::duration remainder = |
| 129 | monotonic_now.time_since_epoch() - |
| 130 | chrono::duration_cast<chrono::seconds>( |
| 131 | monotonic_now.time_since_epoch()); |
| 132 | |
| 133 | // Make sure we we are called near where we should be even when we |
| 134 | // delay. |
| 135 | constexpr chrono::milliseconds kEpsilon(200); |
| 136 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 137 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 138 | |
| 139 | // Confirm that we see the missed count when we sleep. |
| 140 | if (times.size() == 0) { |
| 141 | EXPECT_EQ(count, 1); |
| 142 | } else { |
| 143 | EXPECT_EQ(count, 3); |
| 144 | } |
| 145 | |
| 146 | times.push_back(loop1->monotonic_now()); |
| 147 | if (times.size() == 2) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 148 | factory.Exit(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Now, add a large delay. This should push us up to 3 cycles. |
| 152 | ::std::this_thread::sleep_for(chrono::milliseconds(2500)); |
| 153 | }, |
| 154 | chrono::seconds(1), kOffset); |
| 155 | |
| 156 | factory.Run(); |
| 157 | |
| 158 | EXPECT_EQ(times.size(), 2u); |
| 159 | } |
| 160 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 161 | } // namespace testing |
| 162 | } // namespace aos |