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