blob: 984f97850a73dbf9195dc08eba2b6b376d465277 [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()) {
Austin Schuh898f4972020-01-11 17:21:25 -080041 FLAGS_override_hostname =
42 std::string(my_node()->hostname()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -080043 }
44 ::std::unique_ptr<ShmEventLoop> loop(new ShmEventLoop(configuration()));
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080045 loop->set_name(name);
Austin Schuh217a9782019-12-21 23:02:50 -080046 return std::move(loop);
Parker Schuhe4a70d62017-12-27 20:10:20 -080047 }
48
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080049 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override {
Austin Schuh217a9782019-12-21 23:02:50 -080050 if (configuration()->has_nodes()) {
Austin Schuh898f4972020-01-11 17:21:25 -080051 FLAGS_override_hostname =
52 std::string(my_node()->hostname()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -080053 }
Austin Schuh44019f92019-05-19 19:58:27 -070054 ::std::unique_ptr<ShmEventLoop> loop =
Alex Perrycb7da4b2019-08-28 19:35:56 -070055 ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop(configuration()));
Austin Schuh44019f92019-05-19 19:58:27 -070056 primary_event_loop_ = loop.get();
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080057 loop->set_name(name);
58 return std::move(loop);
Austin Schuh44019f92019-05-19 19:58:27 -070059 }
60
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); }
Austin Schuh44019f92019-05-19 19:58:27 -070062
Alex Perrycb7da4b2019-08-28 19:35:56 -070063 void Exit() override { CHECK_NOTNULL(primary_event_loop_)->Exit(); }
Austin Schuh9fe68f72019-08-10 19:32:03 -070064
Austin Schuh52d325c2019-06-23 18:59:06 -070065 void SleepFor(::std::chrono::nanoseconds duration) override {
66 ::std::this_thread::sleep_for(duration);
67 }
68
Austin Schuh44019f92019-05-19 19:58:27 -070069 private:
Austin Schuh44019f92019-05-19 19:58:27 -070070 ::aos::ShmEventLoop *primary_event_loop_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080071};
72
73INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest,
74 ::testing::Values([]() {
75 return new ShmEventLoopTestFactory();
76 }));
77
Austin Schuh6b6dfa52019-06-12 20:16:20 -070078INSTANTIATE_TEST_CASE_P(ShmEventLoopDeathTest, AbstractEventLoopDeathTest,
79 ::testing::Values([]() {
80 return new ShmEventLoopTestFactory();
81 }));
82
Parker Schuhe4a70d62017-12-27 20:10:20 -080083} // namespace
James Kuszmaulc79768b2019-02-18 15:08:44 -080084
Austin Schuh3115a202019-05-27 21:02:14 -070085bool IsRealtime() {
86 int scheduler;
Alex Perrycb7da4b2019-08-28 19:35:56 -070087 PCHECK((scheduler = sched_getscheduler(0)) != -1);
88
89 LOG(INFO) << "scheduler is " << scheduler;
Austin Schuh3115a202019-05-27 21:02:14 -070090 return scheduler == SCHED_FIFO || scheduler == SCHED_RR;
91}
James Kuszmaulc79768b2019-02-18 15:08:44 -080092
Austin Schuh3115a202019-05-27 21:02:14 -070093// Tests that every handler type is realtime and runs. There are threads
94// involved and it's easy to miss one.
95TEST(ShmEventLoopTest, AllHandlersAreRealtime) {
96 ShmEventLoopTestFactory factory;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080097 auto loop = factory.MakePrimary("primary");
98 auto loop2 = factory.Make("loop2");
Austin Schuh3115a202019-05-27 21:02:14 -070099
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 Perrycb7da4b2019-08-28 19:35:56 -0700108 auto timer = loop->AddTimer([&did_timer, &factory]() {
Austin Schuh3115a202019-05-27 21:02:14 -0700109 EXPECT_TRUE(IsRealtime());
110 did_timer = true;
Austin Schuh9fe68f72019-08-10 19:32:03 -0700111 factory.Exit();
Austin Schuh3115a202019-05-27 21:02:14 -0700112 });
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 Perrycb7da4b2019-08-28 19:35:56 -0700123
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 Schuh3115a202019-05-27 21:02:14 -0700128 });
James Kuszmaulc79768b2019-02-18 15:08:44 -0800129
Austin Schuh3115a202019-05-27 21:02:14 -0700130 factory.Run();
James Kuszmaulc79768b2019-02-18 15:08:44 -0800131
Austin Schuh3115a202019-05-27 21:02:14 -0700132 EXPECT_TRUE(did_onrun);
133 EXPECT_TRUE(did_timer);
134 EXPECT_TRUE(did_watcher);
James Kuszmaulc79768b2019-02-18 15:08:44 -0800135}
Austin Schuh52d325c2019-06-23 18:59:06 -0700136
137// Tests that missing a deadline inside the function still results in PhasedLoop
138// running at the right offset.
139TEST(ShmEventLoopTest, DelayedPhasedLoop) {
140 ShmEventLoopTestFactory factory;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800141 auto loop1 = factory.MakePrimary("primary");
Austin Schuh52d325c2019-06-23 18:59:06 -0700142
143 ::std::vector<::aos::monotonic_clock::time_point> times;
144
145 constexpr chrono::milliseconds kOffset = chrono::milliseconds(400);
146
147 loop1->AddPhasedLoop(
Austin Schuh9fe68f72019-08-10 19:32:03 -0700148 [&times, &loop1, &kOffset, &factory](int count) {
Austin Schuh52d325c2019-06-23 18:59:06 -0700149 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 Schuhde8a8ff2019-11-30 15:25:36 -0800166 CHECK_EQ(count, 1);
Austin Schuh52d325c2019-06-23 18:59:06 -0700167 } else {
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800168 CHECK_EQ(count, 3);
Austin Schuh52d325c2019-06-23 18:59:06 -0700169 }
170
171 times.push_back(loop1->monotonic_now());
172 if (times.size() == 2) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700173 factory.Exit();
Austin Schuh52d325c2019-06-23 18:59:06 -0700174 }
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 Silverman5120afb2020-01-31 17:44:35 -0800186// Test GetWatcherSharedMemory in a few basic scenarios.
187TEST(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
204TEST(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 Schuh39788ff2019-12-01 18:22:57 -0800214// TODO(austin): Test that missing a deadline with a timer recovers as expected.
215
Parker Schuhe4a70d62017-12-27 20:10:20 -0800216} // namespace testing
217} // namespace aos