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