blob: ba0b80da12c8fd8bf9448f6054e96f7bb290ba72 [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);
12
Parker Schuhe4a70d62017-12-27 20:10:20 -080013namespace aos {
14namespace testing {
15namespace {
Austin Schuh3115a202019-05-27 21:02:14 -070016namespace chrono = ::std::chrono;
Parker Schuhe4a70d62017-12-27 20:10:20 -080017
18class ShmEventLoopTestFactory : public EventLoopTestFactory {
19 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070020 ShmEventLoopTestFactory() {
21 // Put all the queue files in ${TEST_TMPDIR} if it is set, otherwise
22 // everything will be reusing /dev/shm when sharded.
23 char *test_tmpdir = getenv("TEST_TMPDIR");
24 if (test_tmpdir != nullptr) {
25 FLAGS_shm_base = std::string(test_tmpdir) + "/aos";
26 }
27
28 // Clean up anything left there before.
Austin Schuhde8a8ff2019-11-30 15:25:36 -080029 unlink((FLAGS_shm_base + "/test/aos.TestMessage.v0").c_str());
30 unlink((FLAGS_shm_base + "/test1/aos.TestMessage.v0").c_str());
31 unlink((FLAGS_shm_base + "/test2/aos.TestMessage.v0").c_str());
32 unlink((FLAGS_shm_base + "/test2/aos.TestMessage.v0").c_str());
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 }
34
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080035 ::std::unique_ptr<EventLoop> Make(std::string_view name) override {
36 ::std::unique_ptr<EventLoop> loop(new ShmEventLoop(configuration()));
37 loop->set_name(name);
38 return loop;
Parker Schuhe4a70d62017-12-27 20:10:20 -080039 }
40
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080041 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override {
Austin Schuh44019f92019-05-19 19:58:27 -070042 ::std::unique_ptr<ShmEventLoop> loop =
Alex Perrycb7da4b2019-08-28 19:35:56 -070043 ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop(configuration()));
Austin Schuh44019f92019-05-19 19:58:27 -070044 primary_event_loop_ = loop.get();
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080045 loop->set_name(name);
46 return std::move(loop);
Austin Schuh44019f92019-05-19 19:58:27 -070047 }
48
Alex Perrycb7da4b2019-08-28 19:35:56 -070049 void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); }
Austin Schuh44019f92019-05-19 19:58:27 -070050
Alex Perrycb7da4b2019-08-28 19:35:56 -070051 void Exit() override { CHECK_NOTNULL(primary_event_loop_)->Exit(); }
Austin Schuh9fe68f72019-08-10 19:32:03 -070052
Austin Schuh52d325c2019-06-23 18:59:06 -070053 void SleepFor(::std::chrono::nanoseconds duration) override {
54 ::std::this_thread::sleep_for(duration);
55 }
56
Austin Schuh44019f92019-05-19 19:58:27 -070057 private:
Austin Schuh44019f92019-05-19 19:58:27 -070058 ::aos::ShmEventLoop *primary_event_loop_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080059};
60
61INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest,
62 ::testing::Values([]() {
63 return new ShmEventLoopTestFactory();
64 }));
65
Austin Schuh6b6dfa52019-06-12 20:16:20 -070066INSTANTIATE_TEST_CASE_P(ShmEventLoopDeathTest, AbstractEventLoopDeathTest,
67 ::testing::Values([]() {
68 return new ShmEventLoopTestFactory();
69 }));
70
Parker Schuhe4a70d62017-12-27 20:10:20 -080071} // namespace
James Kuszmaulc79768b2019-02-18 15:08:44 -080072
Austin Schuh3115a202019-05-27 21:02:14 -070073bool IsRealtime() {
74 int scheduler;
Alex Perrycb7da4b2019-08-28 19:35:56 -070075 PCHECK((scheduler = sched_getscheduler(0)) != -1);
76
77 LOG(INFO) << "scheduler is " << scheduler;
Austin Schuh3115a202019-05-27 21:02:14 -070078 return scheduler == SCHED_FIFO || scheduler == SCHED_RR;
79}
James Kuszmaulc79768b2019-02-18 15:08:44 -080080
Austin Schuh3115a202019-05-27 21:02:14 -070081// Tests that every handler type is realtime and runs. There are threads
82// involved and it's easy to miss one.
83TEST(ShmEventLoopTest, AllHandlersAreRealtime) {
84 ShmEventLoopTestFactory factory;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080085 auto loop = factory.MakePrimary("primary");
86 auto loop2 = factory.Make("loop2");
Austin Schuh3115a202019-05-27 21:02:14 -070087
88 loop->SetRuntimeRealtimePriority(1);
89
90 auto sender = loop2->MakeSender<TestMessage>("/test");
91
92 bool did_onrun = false;
93 bool did_timer = false;
94 bool did_watcher = false;
95
Alex Perrycb7da4b2019-08-28 19:35:56 -070096 auto timer = loop->AddTimer([&did_timer, &factory]() {
Austin Schuh3115a202019-05-27 21:02:14 -070097 EXPECT_TRUE(IsRealtime());
98 did_timer = true;
Austin Schuh9fe68f72019-08-10 19:32:03 -070099 factory.Exit();
Austin Schuh3115a202019-05-27 21:02:14 -0700100 });
101
102 loop->MakeWatcher("/test", [&did_watcher](const TestMessage &) {
103 EXPECT_TRUE(IsRealtime());
104 did_watcher = true;
105 });
106
107 loop->OnRun([&loop, &did_onrun, &sender, timer]() {
108 EXPECT_TRUE(IsRealtime());
109 did_onrun = true;
110 timer->Setup(loop->monotonic_now() + chrono::milliseconds(100));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700111
112 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
113 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
114 builder.add_value(200);
115 msg.Send(builder.Finish());
Austin Schuh3115a202019-05-27 21:02:14 -0700116 });
James Kuszmaulc79768b2019-02-18 15:08:44 -0800117
Austin Schuh3115a202019-05-27 21:02:14 -0700118 factory.Run();
James Kuszmaulc79768b2019-02-18 15:08:44 -0800119
Austin Schuh3115a202019-05-27 21:02:14 -0700120 EXPECT_TRUE(did_onrun);
121 EXPECT_TRUE(did_timer);
122 EXPECT_TRUE(did_watcher);
James Kuszmaulc79768b2019-02-18 15:08:44 -0800123}
Austin Schuh52d325c2019-06-23 18:59:06 -0700124
125// Tests that missing a deadline inside the function still results in PhasedLoop
126// running at the right offset.
127TEST(ShmEventLoopTest, DelayedPhasedLoop) {
128 ShmEventLoopTestFactory factory;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800129 auto loop1 = factory.MakePrimary("primary");
Austin Schuh52d325c2019-06-23 18:59:06 -0700130
131 ::std::vector<::aos::monotonic_clock::time_point> times;
132
133 constexpr chrono::milliseconds kOffset = chrono::milliseconds(400);
134
135 loop1->AddPhasedLoop(
Austin Schuh9fe68f72019-08-10 19:32:03 -0700136 [&times, &loop1, &kOffset, &factory](int count) {
Austin Schuh52d325c2019-06-23 18:59:06 -0700137 const ::aos::monotonic_clock::time_point monotonic_now =
138 loop1->monotonic_now();
139
140 // Compute our offset.
141 const ::aos::monotonic_clock::duration remainder =
142 monotonic_now.time_since_epoch() -
143 chrono::duration_cast<chrono::seconds>(
144 monotonic_now.time_since_epoch());
145
146 // Make sure we we are called near where we should be even when we
147 // delay.
148 constexpr chrono::milliseconds kEpsilon(200);
149 EXPECT_LT(remainder, kOffset + kEpsilon);
150 EXPECT_GT(remainder, kOffset - kEpsilon);
151
152 // Confirm that we see the missed count when we sleep.
153 if (times.size() == 0) {
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800154 CHECK_EQ(count, 1);
Austin Schuh52d325c2019-06-23 18:59:06 -0700155 } else {
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800156 CHECK_EQ(count, 3);
Austin Schuh52d325c2019-06-23 18:59:06 -0700157 }
158
159 times.push_back(loop1->monotonic_now());
160 if (times.size() == 2) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700161 factory.Exit();
Austin Schuh52d325c2019-06-23 18:59:06 -0700162 }
163
164 // Now, add a large delay. This should push us up to 3 cycles.
165 ::std::this_thread::sleep_for(chrono::milliseconds(2500));
166 },
167 chrono::seconds(1), kOffset);
168
169 factory.Run();
170
171 EXPECT_EQ(times.size(), 2u);
172}
173
Parker Schuhe4a70d62017-12-27 20:10:20 -0800174} // namespace testing
175} // namespace aos