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