Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/shm_event_loop.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #include "aos/events/event_loop_param_test.h" |
| 4 | #include "glog/logging.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 5 | #include "gtest/gtest.h" |
| 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/events/test_message_generated.h" |
| 8 | |
| 9 | DECLARE_string(shm_base); |
| 10 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 11 | namespace aos { |
| 12 | namespace testing { |
| 13 | namespace { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 14 | namespace chrono = ::std::chrono; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 15 | |
| 16 | class ShmEventLoopTestFactory : public EventLoopTestFactory { |
| 17 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | ShmEventLoopTestFactory() { |
| 19 | // Put all the queue files in ${TEST_TMPDIR} if it is set, otherwise |
| 20 | // everything will be reusing /dev/shm when sharded. |
| 21 | char *test_tmpdir = getenv("TEST_TMPDIR"); |
| 22 | if (test_tmpdir != nullptr) { |
| 23 | FLAGS_shm_base = std::string(test_tmpdir) + "/aos"; |
| 24 | } |
| 25 | |
| 26 | // Clean up anything left there before. |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 27 | unlink((FLAGS_shm_base + "/test/aos.TestMessage.v0").c_str()); |
| 28 | unlink((FLAGS_shm_base + "/test1/aos.TestMessage.v0").c_str()); |
| 29 | unlink((FLAGS_shm_base + "/test2/aos.TestMessage.v0").c_str()); |
| 30 | unlink((FLAGS_shm_base + "/test2/aos.TestMessage.v0").c_str()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 33 | ::std::unique_ptr<EventLoop> Make() override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | return ::std::unique_ptr<EventLoop>(new ShmEventLoop(configuration())); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 37 | ::std::unique_ptr<EventLoop> MakePrimary() override { |
| 38 | ::std::unique_ptr<ShmEventLoop> loop = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 39 | ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop(configuration())); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 40 | primary_event_loop_ = loop.get(); |
| 41 | return ::std::move(loop); |
| 42 | } |
| 43 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 45 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 46 | void Exit() override { CHECK_NOTNULL(primary_event_loop_)->Exit(); } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 47 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 48 | void SleepFor(::std::chrono::nanoseconds duration) override { |
| 49 | ::std::this_thread::sleep_for(duration); |
| 50 | } |
| 51 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 52 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 53 | ::aos::ShmEventLoop *primary_event_loop_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest, |
| 57 | ::testing::Values([]() { |
| 58 | return new ShmEventLoopTestFactory(); |
| 59 | })); |
| 60 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 61 | INSTANTIATE_TEST_CASE_P(ShmEventLoopDeathTest, AbstractEventLoopDeathTest, |
| 62 | ::testing::Values([]() { |
| 63 | return new ShmEventLoopTestFactory(); |
| 64 | })); |
| 65 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 66 | } // namespace |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 67 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 68 | bool IsRealtime() { |
| 69 | int scheduler; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 70 | PCHECK((scheduler = sched_getscheduler(0)) != -1); |
| 71 | |
| 72 | LOG(INFO) << "scheduler is " << scheduler; |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 73 | return scheduler == SCHED_FIFO || scheduler == SCHED_RR; |
| 74 | } |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 75 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 76 | // Tests that every handler type is realtime and runs. There are threads |
| 77 | // involved and it's easy to miss one. |
| 78 | TEST(ShmEventLoopTest, AllHandlersAreRealtime) { |
| 79 | ShmEventLoopTestFactory factory; |
| 80 | auto loop = factory.MakePrimary(); |
| 81 | auto loop2 = factory.Make(); |
| 82 | |
| 83 | loop->SetRuntimeRealtimePriority(1); |
| 84 | |
| 85 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 86 | |
| 87 | bool did_onrun = false; |
| 88 | bool did_timer = false; |
| 89 | bool did_watcher = false; |
| 90 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | auto timer = loop->AddTimer([&did_timer, &factory]() { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(IsRealtime()); |
| 93 | did_timer = true; |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 94 | factory.Exit(); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 95 | }); |
| 96 | |
| 97 | loop->MakeWatcher("/test", [&did_watcher](const TestMessage &) { |
| 98 | EXPECT_TRUE(IsRealtime()); |
| 99 | did_watcher = true; |
| 100 | }); |
| 101 | |
| 102 | loop->OnRun([&loop, &did_onrun, &sender, timer]() { |
| 103 | EXPECT_TRUE(IsRealtime()); |
| 104 | did_onrun = true; |
| 105 | timer->Setup(loop->monotonic_now() + chrono::milliseconds(100)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 106 | |
| 107 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 108 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 109 | builder.add_value(200); |
| 110 | msg.Send(builder.Finish()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 111 | }); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 112 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 113 | factory.Run(); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 114 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 115 | EXPECT_TRUE(did_onrun); |
| 116 | EXPECT_TRUE(did_timer); |
| 117 | EXPECT_TRUE(did_watcher); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 118 | } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 119 | |
| 120 | // Tests that missing a deadline inside the function still results in PhasedLoop |
| 121 | // running at the right offset. |
| 122 | TEST(ShmEventLoopTest, DelayedPhasedLoop) { |
| 123 | ShmEventLoopTestFactory factory; |
| 124 | auto loop1 = factory.MakePrimary(); |
| 125 | |
| 126 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 127 | |
| 128 | constexpr chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 129 | |
| 130 | loop1->AddPhasedLoop( |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 131 | [×, &loop1, &kOffset, &factory](int count) { |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 132 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 133 | loop1->monotonic_now(); |
| 134 | |
| 135 | // Compute our offset. |
| 136 | const ::aos::monotonic_clock::duration remainder = |
| 137 | monotonic_now.time_since_epoch() - |
| 138 | chrono::duration_cast<chrono::seconds>( |
| 139 | monotonic_now.time_since_epoch()); |
| 140 | |
| 141 | // Make sure we we are called near where we should be even when we |
| 142 | // delay. |
| 143 | constexpr chrono::milliseconds kEpsilon(200); |
| 144 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 145 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 146 | |
| 147 | // Confirm that we see the missed count when we sleep. |
| 148 | if (times.size() == 0) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 149 | CHECK_EQ(count, 1); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 150 | } else { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame^] | 151 | CHECK_EQ(count, 3); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | times.push_back(loop1->monotonic_now()); |
| 155 | if (times.size() == 2) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 156 | factory.Exit(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Now, add a large delay. This should push us up to 3 cycles. |
| 160 | ::std::this_thread::sleep_for(chrono::milliseconds(2500)); |
| 161 | }, |
| 162 | chrono::seconds(1), kOffset); |
| 163 | |
| 164 | factory.Run(); |
| 165 | |
| 166 | EXPECT_EQ(times.size(), 2u); |
| 167 | } |
| 168 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 169 | } // namespace testing |
| 170 | } // namespace aos |