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