blob: 4406e01901c9156bcb81d791dfdffed1c0940342 [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
Alex Perrycb7da4b2019-08-28 19:35:56 -07003#include "aos/events/event_loop_param_test.h"
4#include "glog/logging.h"
Parker Schuhe4a70d62017-12-27 20:10:20 -08005#include "gtest/gtest.h"
6
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/events/test_message_generated.h"
8
9DECLARE_string(shm_base);
10
Parker Schuhe4a70d62017-12-27 20:10:20 -080011namespace aos {
12namespace testing {
13namespace {
Austin Schuh3115a202019-05-27 21:02:14 -070014namespace chrono = ::std::chrono;
Parker Schuhe4a70d62017-12-27 20:10:20 -080015
16class ShmEventLoopTestFactory : public EventLoopTestFactory {
17 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070018 ShmEventLoopTestFactory() {
19 // Put all the queue files in ${TEST_TMPDIR} if it is set, otherwise
20 // everything will be reusing /dev/shm when sharded.
21 char *test_tmpdir = getenv("TEST_TMPDIR");
22 if (test_tmpdir != nullptr) {
23 FLAGS_shm_base = std::string(test_tmpdir) + "/aos";
24 }
25
26 // Clean up anything left there before.
27 unlink((FLAGS_shm_base + "test/aos.TestMessage.v0").c_str());
28 unlink((FLAGS_shm_base + "test1/aos.TestMessage.v0").c_str());
29 unlink((FLAGS_shm_base + "test2/aos.TestMessage.v0").c_str());
30 }
31
Austin Schuh44019f92019-05-19 19:58:27 -070032 ::std::unique_ptr<EventLoop> Make() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 return ::std::unique_ptr<EventLoop>(new ShmEventLoop(configuration()));
Parker Schuhe4a70d62017-12-27 20:10:20 -080034 }
35
Austin Schuh44019f92019-05-19 19:58:27 -070036 ::std::unique_ptr<EventLoop> MakePrimary() override {
37 ::std::unique_ptr<ShmEventLoop> loop =
Alex Perrycb7da4b2019-08-28 19:35:56 -070038 ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop(configuration()));
Austin Schuh44019f92019-05-19 19:58:27 -070039 primary_event_loop_ = loop.get();
40 return ::std::move(loop);
41 }
42
Alex Perrycb7da4b2019-08-28 19:35:56 -070043 void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); }
Austin Schuh44019f92019-05-19 19:58:27 -070044
Alex Perrycb7da4b2019-08-28 19:35:56 -070045 void Exit() override { CHECK_NOTNULL(primary_event_loop_)->Exit(); }
Austin Schuh9fe68f72019-08-10 19:32:03 -070046
Austin Schuh52d325c2019-06-23 18:59:06 -070047 void SleepFor(::std::chrono::nanoseconds duration) override {
48 ::std::this_thread::sleep_for(duration);
49 }
50
Austin Schuh44019f92019-05-19 19:58:27 -070051 private:
Austin Schuh44019f92019-05-19 19:58:27 -070052 ::aos::ShmEventLoop *primary_event_loop_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080053};
54
55INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest,
56 ::testing::Values([]() {
57 return new ShmEventLoopTestFactory();
58 }));
59
Austin Schuh6b6dfa52019-06-12 20:16:20 -070060INSTANTIATE_TEST_CASE_P(ShmEventLoopDeathTest, AbstractEventLoopDeathTest,
61 ::testing::Values([]() {
62 return new ShmEventLoopTestFactory();
63 }));
64
Parker Schuhe4a70d62017-12-27 20:10:20 -080065} // namespace
James Kuszmaulc79768b2019-02-18 15:08:44 -080066
Austin Schuh3115a202019-05-27 21:02:14 -070067bool IsRealtime() {
68 int scheduler;
Alex Perrycb7da4b2019-08-28 19:35:56 -070069 PCHECK((scheduler = sched_getscheduler(0)) != -1);
70
71 LOG(INFO) << "scheduler is " << scheduler;
Austin Schuh3115a202019-05-27 21:02:14 -070072 return scheduler == SCHED_FIFO || scheduler == SCHED_RR;
73}
James Kuszmaulc79768b2019-02-18 15:08:44 -080074
Austin Schuh3115a202019-05-27 21:02:14 -070075// Tests that every handler type is realtime and runs. There are threads
76// involved and it's easy to miss one.
77TEST(ShmEventLoopTest, AllHandlersAreRealtime) {
78 ShmEventLoopTestFactory factory;
79 auto loop = factory.MakePrimary();
80 auto loop2 = factory.Make();
81
82 loop->SetRuntimeRealtimePriority(1);
83
84 auto sender = loop2->MakeSender<TestMessage>("/test");
85
86 bool did_onrun = false;
87 bool did_timer = false;
88 bool did_watcher = false;
89
Alex Perrycb7da4b2019-08-28 19:35:56 -070090 auto timer = loop->AddTimer([&did_timer, &factory]() {
Austin Schuh3115a202019-05-27 21:02:14 -070091 EXPECT_TRUE(IsRealtime());
92 did_timer = true;
Austin Schuh9fe68f72019-08-10 19:32:03 -070093 factory.Exit();
Austin Schuh3115a202019-05-27 21:02:14 -070094 });
95
96 loop->MakeWatcher("/test", [&did_watcher](const TestMessage &) {
97 EXPECT_TRUE(IsRealtime());
98 did_watcher = true;
99 });
100
101 loop->OnRun([&loop, &did_onrun, &sender, timer]() {
102 EXPECT_TRUE(IsRealtime());
103 did_onrun = true;
104 timer->Setup(loop->monotonic_now() + chrono::milliseconds(100));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700105
106 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
107 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
108 builder.add_value(200);
109 msg.Send(builder.Finish());
Austin Schuh3115a202019-05-27 21:02:14 -0700110 });
James Kuszmaulc79768b2019-02-18 15:08:44 -0800111
Austin Schuh3115a202019-05-27 21:02:14 -0700112 factory.Run();
James Kuszmaulc79768b2019-02-18 15:08:44 -0800113
Austin Schuh3115a202019-05-27 21:02:14 -0700114 EXPECT_TRUE(did_onrun);
115 EXPECT_TRUE(did_timer);
116 EXPECT_TRUE(did_watcher);
James Kuszmaulc79768b2019-02-18 15:08:44 -0800117}
Austin Schuh52d325c2019-06-23 18:59:06 -0700118
119// Tests that missing a deadline inside the function still results in PhasedLoop
120// running at the right offset.
121TEST(ShmEventLoopTest, DelayedPhasedLoop) {
122 ShmEventLoopTestFactory factory;
123 auto loop1 = factory.MakePrimary();
124
125 ::std::vector<::aos::monotonic_clock::time_point> times;
126
127 constexpr chrono::milliseconds kOffset = chrono::milliseconds(400);
128
129 loop1->AddPhasedLoop(
Austin Schuh9fe68f72019-08-10 19:32:03 -0700130 [&times, &loop1, &kOffset, &factory](int count) {
Austin Schuh52d325c2019-06-23 18:59:06 -0700131 const ::aos::monotonic_clock::time_point monotonic_now =
132 loop1->monotonic_now();
133
134 // Compute our offset.
135 const ::aos::monotonic_clock::duration remainder =
136 monotonic_now.time_since_epoch() -
137 chrono::duration_cast<chrono::seconds>(
138 monotonic_now.time_since_epoch());
139
140 // Make sure we we are called near where we should be even when we
141 // delay.
142 constexpr chrono::milliseconds kEpsilon(200);
143 EXPECT_LT(remainder, kOffset + kEpsilon);
144 EXPECT_GT(remainder, kOffset - kEpsilon);
145
146 // Confirm that we see the missed count when we sleep.
147 if (times.size() == 0) {
148 EXPECT_EQ(count, 1);
149 } else {
150 EXPECT_EQ(count, 3);
151 }
152
153 times.push_back(loop1->monotonic_now());
154 if (times.size() == 2) {
Austin Schuh9fe68f72019-08-10 19:32:03 -0700155 factory.Exit();
Austin Schuh52d325c2019-06-23 18:59:06 -0700156 }
157
158 // Now, add a large delay. This should push us up to 3 cycles.
159 ::std::this_thread::sleep_for(chrono::milliseconds(2500));
160 },
161 chrono::seconds(1), kOffset);
162
163 factory.Run();
164
165 EXPECT_EQ(times.size(), 2u);
166}
167
Parker Schuhe4a70d62017-12-27 20:10:20 -0800168} // namespace testing
169} // namespace aos