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