blob: d25e2f896879c2105be0c548e8c635ecdbe88ef0 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include "aos/events/shm_event_loop.h"
Parker Schuhe4a70d62017-12-27 20:10:20 -08002
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08003#include <string_view>
4
Alex Perrycb7da4b2019-08-28 19:35:56 -07005#include "aos/events/event_loop_param_test.h"
6#include "glog/logging.h"
Parker Schuhe4a70d62017-12-27 20:10:20 -08007#include "gtest/gtest.h"
8
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "aos/events/test_message_generated.h"
Jim Ostrowski2192ddb2020-06-24 19:07:31 -070010#include "aos/network/team_number.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011
Parker Schuhe4a70d62017-12-27 20:10:20 -080012namespace aos {
13namespace testing {
14namespace {
Austin Schuh3115a202019-05-27 21:02:14 -070015namespace chrono = ::std::chrono;
Parker Schuhe4a70d62017-12-27 20:10:20 -080016
17class ShmEventLoopTestFactory : public EventLoopTestFactory {
18 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070019 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.
Austin Schuh3328d132020-02-28 13:54:57 -080028 unlink((FLAGS_shm_base + "/test/aos.TestMessage.v2").c_str());
29 unlink((FLAGS_shm_base + "/test1/aos.TestMessage.v2").c_str());
30 unlink((FLAGS_shm_base + "/test2/aos.TestMessage.v2").c_str());
31 unlink((FLAGS_shm_base + "/test2/aos.TestMessage.v2").c_str());
32 unlink((FLAGS_shm_base + "/aos/aos.timing.Report.v2").c_str());
33 unlink((FLAGS_shm_base + "/aos/aos.logging.LogMessageFbs.v2").c_str());
Alex Perrycb7da4b2019-08-28 19:35:56 -070034 }
35
Austin Schuh217a9782019-12-21 23:02:50 -080036 ~ShmEventLoopTestFactory() { FLAGS_override_hostname = ""; }
37
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080038 ::std::unique_ptr<EventLoop> Make(std::string_view name) override {
Austin Schuh217a9782019-12-21 23:02:50 -080039 if (configuration()->has_nodes()) {
Austin Schuh898f4972020-01-11 17:21:25 -080040 FLAGS_override_hostname =
41 std::string(my_node()->hostname()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -080042 }
43 ::std::unique_ptr<ShmEventLoop> loop(new ShmEventLoop(configuration()));
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080044 loop->set_name(name);
Austin Schuh217a9782019-12-21 23:02:50 -080045 return std::move(loop);
Parker Schuhe4a70d62017-12-27 20:10:20 -080046 }
47
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080048 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override {
Austin Schuh217a9782019-12-21 23:02:50 -080049 if (configuration()->has_nodes()) {
Austin Schuh898f4972020-01-11 17:21:25 -080050 FLAGS_override_hostname =
51 std::string(my_node()->hostname()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -080052 }
Austin Schuh44019f92019-05-19 19:58:27 -070053 ::std::unique_ptr<ShmEventLoop> loop =
Alex Perrycb7da4b2019-08-28 19:35:56 -070054 ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop(configuration()));
Austin Schuh44019f92019-05-19 19:58:27 -070055 primary_event_loop_ = loop.get();
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080056 loop->set_name(name);
57 return std::move(loop);
Austin Schuh44019f92019-05-19 19:58:27 -070058 }
59
Alex Perrycb7da4b2019-08-28 19:35:56 -070060 void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); }
Austin Schuh44019f92019-05-19 19:58:27 -070061
Alex Perrycb7da4b2019-08-28 19:35:56 -070062 void Exit() override { CHECK_NOTNULL(primary_event_loop_)->Exit(); }
Austin Schuh9fe68f72019-08-10 19:32:03 -070063
Austin Schuh52d325c2019-06-23 18:59:06 -070064 void SleepFor(::std::chrono::nanoseconds duration) override {
65 ::std::this_thread::sleep_for(duration);
66 }
67
Austin Schuh44019f92019-05-19 19:58:27 -070068 private:
Austin Schuh44019f92019-05-19 19:58:27 -070069 ::aos::ShmEventLoop *primary_event_loop_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080070};
71
72INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest,
73 ::testing::Values([]() {
74 return new ShmEventLoopTestFactory();
75 }));
76
Austin Schuh6b6dfa52019-06-12 20:16:20 -070077INSTANTIATE_TEST_CASE_P(ShmEventLoopDeathTest, AbstractEventLoopDeathTest,
78 ::testing::Values([]() {
79 return new ShmEventLoopTestFactory();
80 }));
81
Parker Schuhe4a70d62017-12-27 20:10:20 -080082} // namespace
James Kuszmaulc79768b2019-02-18 15:08:44 -080083
Austin Schuh3115a202019-05-27 21:02:14 -070084bool IsRealtime() {
85 int scheduler;
Alex Perrycb7da4b2019-08-28 19:35:56 -070086 PCHECK((scheduler = sched_getscheduler(0)) != -1);
87
88 LOG(INFO) << "scheduler is " << scheduler;
Austin Schuh3115a202019-05-27 21:02:14 -070089 return scheduler == SCHED_FIFO || scheduler == SCHED_RR;
90}
James Kuszmaulc79768b2019-02-18 15:08:44 -080091
Austin Schuh3115a202019-05-27 21:02:14 -070092// Tests that every handler type is realtime and runs. There are threads
93// involved and it's easy to miss one.
94TEST(ShmEventLoopTest, AllHandlersAreRealtime) {
95 ShmEventLoopTestFactory factory;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080096 auto loop = factory.MakePrimary("primary");
97 auto loop2 = factory.Make("loop2");
Austin Schuh3115a202019-05-27 21:02:14 -070098
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 Perrycb7da4b2019-08-28 19:35:56 -0700107 auto timer = loop->AddTimer([&did_timer, &factory]() {
Austin Schuh3115a202019-05-27 21:02:14 -0700108 EXPECT_TRUE(IsRealtime());
109 did_timer = true;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700110 factory.Exit();
Austin Schuh3115a202019-05-27 21:02:14 -0700111 });
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 Perrycb7da4b2019-08-28 19:35:56 -0700122
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 Schuh3115a202019-05-27 21:02:14 -0700127 });
James Kuszmaulc79768b2019-02-18 15:08:44 -0800128
Austin Schuh3115a202019-05-27 21:02:14 -0700129 factory.Run();
James Kuszmaulc79768b2019-02-18 15:08:44 -0800130
Austin Schuh3115a202019-05-27 21:02:14 -0700131 EXPECT_TRUE(did_onrun);
132 EXPECT_TRUE(did_timer);
133 EXPECT_TRUE(did_watcher);
James Kuszmaulc79768b2019-02-18 15:08:44 -0800134}
Austin Schuh52d325c2019-06-23 18:59:06 -0700135
136// Tests that missing a deadline inside the function still results in PhasedLoop
137// running at the right offset.
138TEST(ShmEventLoopTest, DelayedPhasedLoop) {
139 ShmEventLoopTestFactory factory;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800140 auto loop1 = factory.MakePrimary("primary");
Austin Schuh52d325c2019-06-23 18:59:06 -0700141
142 ::std::vector<::aos::monotonic_clock::time_point> times;
143
144 constexpr chrono::milliseconds kOffset = chrono::milliseconds(400);
145
146 loop1->AddPhasedLoop(
Austin Schuh9fe68f72019-08-10 19:32:03 -0700147 [&times, &loop1, &kOffset, &factory](int count) {
Austin Schuh52d325c2019-06-23 18:59:06 -0700148 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 Schuhde8a8ff2019-11-30 15:25:36 -0800165 CHECK_EQ(count, 1);
Austin Schuh52d325c2019-06-23 18:59:06 -0700166 } else {
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800167 CHECK_EQ(count, 3);
Austin Schuh52d325c2019-06-23 18:59:06 -0700168 }
169
170 times.push_back(loop1->monotonic_now());
171 if (times.size() == 2) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700172 factory.Exit();
Austin Schuh52d325c2019-06-23 18:59:06 -0700173 }
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 Silverman5120afb2020-01-31 17:44:35 -0800185// Test GetWatcherSharedMemory in a few basic scenarios.
186TEST(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
203TEST(ShmEventLoopTest, GetSenderSharedMemory) {
204 ShmEventLoopTestFactory factory;
205 auto generic_loop1 = factory.MakePrimary("primary");
206 ShmEventLoop *const loop1 = static_cast<ShmEventLoop *>(generic_loop1.get());
207
Brian Silverman6d2b3592020-06-18 14:40:15 -0700208 // check that GetSenderSharedMemory returns non-null/non-empty memory span.
Brian Silverman5120afb2020-01-31 17:44:35 -0800209 auto sender = loop1->MakeSender<TestMessage>("/test");
210 EXPECT_FALSE(loop1->GetSenderSharedMemory(&sender).empty());
211}
212
Brian Silverman6d2b3592020-06-18 14:40:15 -0700213TEST(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 Schuh39788ff2019-12-01 18:22:57 -0800223// TODO(austin): Test that missing a deadline with a timer recovers as expected.
224
Parker Schuhe4a70d62017-12-27 20:10:20 -0800225} // namespace testing
226} // namespace aos