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