blob: 949d7dbc80b7d16e736d2038c2af0af4ac1ae00f [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"
10
11DECLARE_string(shm_base);
Austin Schuh217a9782019-12-21 23:02:50 -080012DECLARE_string(override_hostname);
Alex Perrycb7da4b2019-08-28 19:35:56 -070013
Parker Schuhe4a70d62017-12-27 20:10:20 -080014namespace aos {
15namespace testing {
16namespace {
Austin Schuh3115a202019-05-27 21:02:14 -070017namespace chrono = ::std::chrono;
Parker Schuhe4a70d62017-12-27 20:10:20 -080018
19class ShmEventLoopTestFactory : public EventLoopTestFactory {
20 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070021 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 Schuhad154822019-12-27 15:45:13 -080030 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 Perrycb7da4b2019-08-28 19:35:56 -070035 }
36
Austin Schuh217a9782019-12-21 23:02:50 -080037 ~ShmEventLoopTestFactory() { FLAGS_override_hostname = ""; }
38
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080039 ::std::unique_ptr<EventLoop> Make(std::string_view name) override {
Austin Schuh217a9782019-12-21 23:02:50 -080040 if (configuration()->has_nodes()) {
41 FLAGS_override_hostname = "myhostname";
42 }
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()) {
50 FLAGS_override_hostname = "myhostname";
51 }
Austin Schuh44019f92019-05-19 19:58:27 -070052 ::std::unique_ptr<ShmEventLoop> loop =
Alex Perrycb7da4b2019-08-28 19:35:56 -070053 ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop(configuration()));
Austin Schuh44019f92019-05-19 19:58:27 -070054 primary_event_loop_ = loop.get();
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080055 loop->set_name(name);
56 return std::move(loop);
Austin Schuh44019f92019-05-19 19:58:27 -070057 }
58
Alex Perrycb7da4b2019-08-28 19:35:56 -070059 void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); }
Austin Schuh44019f92019-05-19 19:58:27 -070060
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 void Exit() override { CHECK_NOTNULL(primary_event_loop_)->Exit(); }
Austin Schuh9fe68f72019-08-10 19:32:03 -070062
Austin Schuh52d325c2019-06-23 18:59:06 -070063 void SleepFor(::std::chrono::nanoseconds duration) override {
64 ::std::this_thread::sleep_for(duration);
65 }
66
Austin Schuh44019f92019-05-19 19:58:27 -070067 private:
Austin Schuh44019f92019-05-19 19:58:27 -070068 ::aos::ShmEventLoop *primary_event_loop_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080069};
70
71INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest,
72 ::testing::Values([]() {
73 return new ShmEventLoopTestFactory();
74 }));
75
Austin Schuh6b6dfa52019-06-12 20:16:20 -070076INSTANTIATE_TEST_CASE_P(ShmEventLoopDeathTest, AbstractEventLoopDeathTest,
77 ::testing::Values([]() {
78 return new ShmEventLoopTestFactory();
79 }));
80
Parker Schuhe4a70d62017-12-27 20:10:20 -080081} // namespace
James Kuszmaulc79768b2019-02-18 15:08:44 -080082
Austin Schuh3115a202019-05-27 21:02:14 -070083bool IsRealtime() {
84 int scheduler;
Alex Perrycb7da4b2019-08-28 19:35:56 -070085 PCHECK((scheduler = sched_getscheduler(0)) != -1);
86
87 LOG(INFO) << "scheduler is " << scheduler;
Austin Schuh3115a202019-05-27 21:02:14 -070088 return scheduler == SCHED_FIFO || scheduler == SCHED_RR;
89}
James Kuszmaulc79768b2019-02-18 15:08:44 -080090
Austin Schuh3115a202019-05-27 21:02:14 -070091// Tests that every handler type is realtime and runs. There are threads
92// involved and it's easy to miss one.
93TEST(ShmEventLoopTest, AllHandlersAreRealtime) {
94 ShmEventLoopTestFactory factory;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080095 auto loop = factory.MakePrimary("primary");
96 auto loop2 = factory.Make("loop2");
Austin Schuh3115a202019-05-27 21:02:14 -070097
98 loop->SetRuntimeRealtimePriority(1);
99
100 auto sender = loop2->MakeSender<TestMessage>("/test");
101
102 bool did_onrun = false;
103 bool did_timer = false;
104 bool did_watcher = false;
105
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106 auto timer = loop->AddTimer([&did_timer, &factory]() {
Austin Schuh3115a202019-05-27 21:02:14 -0700107 EXPECT_TRUE(IsRealtime());
108 did_timer = true;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700109 factory.Exit();
Austin Schuh3115a202019-05-27 21:02:14 -0700110 });
111
112 loop->MakeWatcher("/test", [&did_watcher](const TestMessage &) {
113 EXPECT_TRUE(IsRealtime());
114 did_watcher = true;
115 });
116
117 loop->OnRun([&loop, &did_onrun, &sender, timer]() {
118 EXPECT_TRUE(IsRealtime());
119 did_onrun = true;
120 timer->Setup(loop->monotonic_now() + chrono::milliseconds(100));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700121
122 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
123 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
124 builder.add_value(200);
125 msg.Send(builder.Finish());
Austin Schuh3115a202019-05-27 21:02:14 -0700126 });
James Kuszmaulc79768b2019-02-18 15:08:44 -0800127
Austin Schuh3115a202019-05-27 21:02:14 -0700128 factory.Run();
James Kuszmaulc79768b2019-02-18 15:08:44 -0800129
Austin Schuh3115a202019-05-27 21:02:14 -0700130 EXPECT_TRUE(did_onrun);
131 EXPECT_TRUE(did_timer);
132 EXPECT_TRUE(did_watcher);
James Kuszmaulc79768b2019-02-18 15:08:44 -0800133}
Austin Schuh52d325c2019-06-23 18:59:06 -0700134
135// Tests that missing a deadline inside the function still results in PhasedLoop
136// running at the right offset.
137TEST(ShmEventLoopTest, DelayedPhasedLoop) {
138 ShmEventLoopTestFactory factory;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800139 auto loop1 = factory.MakePrimary("primary");
Austin Schuh52d325c2019-06-23 18:59:06 -0700140
141 ::std::vector<::aos::monotonic_clock::time_point> times;
142
143 constexpr chrono::milliseconds kOffset = chrono::milliseconds(400);
144
145 loop1->AddPhasedLoop(
Austin Schuh9fe68f72019-08-10 19:32:03 -0700146 [&times, &loop1, &kOffset, &factory](int count) {
Austin Schuh52d325c2019-06-23 18:59:06 -0700147 const ::aos::monotonic_clock::time_point monotonic_now =
148 loop1->monotonic_now();
149
150 // Compute our offset.
151 const ::aos::monotonic_clock::duration remainder =
152 monotonic_now.time_since_epoch() -
153 chrono::duration_cast<chrono::seconds>(
154 monotonic_now.time_since_epoch());
155
156 // Make sure we we are called near where we should be even when we
157 // delay.
158 constexpr chrono::milliseconds kEpsilon(200);
159 EXPECT_LT(remainder, kOffset + kEpsilon);
160 EXPECT_GT(remainder, kOffset - kEpsilon);
161
162 // Confirm that we see the missed count when we sleep.
163 if (times.size() == 0) {
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800164 CHECK_EQ(count, 1);
Austin Schuh52d325c2019-06-23 18:59:06 -0700165 } else {
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800166 CHECK_EQ(count, 3);
Austin Schuh52d325c2019-06-23 18:59:06 -0700167 }
168
169 times.push_back(loop1->monotonic_now());
170 if (times.size() == 2) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700171 factory.Exit();
Austin Schuh52d325c2019-06-23 18:59:06 -0700172 }
173
174 // Now, add a large delay. This should push us up to 3 cycles.
175 ::std::this_thread::sleep_for(chrono::milliseconds(2500));
176 },
177 chrono::seconds(1), kOffset);
178
179 factory.Run();
180
181 EXPECT_EQ(times.size(), 2u);
182}
183
Brian Silverman5120afb2020-01-31 17:44:35 -0800184// Test GetWatcherSharedMemory in a few basic scenarios.
185TEST(ShmEventLoopDeathTest, GetWatcherSharedMemory) {
186 ShmEventLoopTestFactory factory;
187 auto generic_loop1 = factory.MakePrimary("primary");
188 ShmEventLoop *const loop1 = static_cast<ShmEventLoop *>(generic_loop1.get());
189 const auto channel = configuration::GetChannel(
190 loop1->configuration(), "/test", TestMessage::GetFullyQualifiedName(),
191 loop1->name(), loop1->node());
192
193 // First verify it handles an invalid channel reasonably.
194 EXPECT_DEATH(loop1->GetWatcherSharedMemory(channel),
195 "No watcher found for channel");
196
197 // Then, actually create a watcher, and verify it returns something sane.
198 loop1->MakeWatcher("/test", [](const TestMessage &) {});
199 EXPECT_FALSE(loop1->GetWatcherSharedMemory(channel).empty());
200}
201
202TEST(ShmEventLoopTest, GetSenderSharedMemory) {
203 ShmEventLoopTestFactory factory;
204 auto generic_loop1 = factory.MakePrimary("primary");
205 ShmEventLoop *const loop1 = static_cast<ShmEventLoop *>(generic_loop1.get());
206
207 // check that GetSenderSharedMemory returns non-null/non-empty memory span
208 auto sender = loop1->MakeSender<TestMessage>("/test");
209 EXPECT_FALSE(loop1->GetSenderSharedMemory(&sender).empty());
210}
211
Austin Schuh39788ff2019-12-01 18:22:57 -0800212// TODO(austin): Test that missing a deadline with a timer recovers as expected.
213
Parker Schuhe4a70d62017-12-27 20:10:20 -0800214} // namespace testing
215} // namespace aos