blob: efa85459a5463e112b5dac675360fcd92d6f9e36 [file] [log] [blame]
Parker Schuhe4a70d62017-12-27 20:10:20 -08001#include "aos/events/shm-event-loop.h"
2
3#include "aos/events/event-loop_param_test.h"
4#include "aos/testing/test_shm.h"
5#include "gtest/gtest.h"
6
7namespace aos {
8namespace testing {
9namespace {
Austin Schuh3115a202019-05-27 21:02:14 -070010namespace chrono = ::std::chrono;
Parker Schuhe4a70d62017-12-27 20:10:20 -080011
12class ShmEventLoopTestFactory : public EventLoopTestFactory {
13 public:
Austin Schuh44019f92019-05-19 19:58:27 -070014 ::std::unique_ptr<EventLoop> Make() override {
15 return ::std::unique_ptr<EventLoop>(new ShmEventLoop());
Parker Schuhe4a70d62017-12-27 20:10:20 -080016 }
17
Austin Schuh44019f92019-05-19 19:58:27 -070018 ::std::unique_ptr<EventLoop> MakePrimary() override {
19 ::std::unique_ptr<ShmEventLoop> loop =
20 ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop());
21 primary_event_loop_ = loop.get();
22 return ::std::move(loop);
23 }
24
25 void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); }
26
27 private:
Parker Schuhe4a70d62017-12-27 20:10:20 -080028 ::aos::testing::TestSharedMemory my_shm_;
Austin Schuh44019f92019-05-19 19:58:27 -070029
30 ::aos::ShmEventLoop *primary_event_loop_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080031};
32
33INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest,
34 ::testing::Values([]() {
35 return new ShmEventLoopTestFactory();
36 }));
37
James Kuszmaulc79768b2019-02-18 15:08:44 -080038struct TestMessage : public ::aos::Message {
39 enum { kQueueLength = 100, kHash = 0x696c0cdc };
40 int msg_value;
41
42 void Zero() { msg_value = 0; }
43 static size_t Size() { return 1 + ::aos::Message::Size(); }
44 size_t Print(char *buffer, size_t length) const;
45 TestMessage() { Zero(); }
46};
47
Parker Schuhe4a70d62017-12-27 20:10:20 -080048} // namespace
James Kuszmaulc79768b2019-02-18 15:08:44 -080049
Austin Schuh3115a202019-05-27 21:02:14 -070050bool IsRealtime() {
51 int scheduler;
52 if ((scheduler = sched_getscheduler(0)) == -1) {
53 PLOG(FATAL, "sched_getscheduler(0) failed\n");
James Kuszmaulc79768b2019-02-18 15:08:44 -080054 }
Austin Schuh3115a202019-05-27 21:02:14 -070055 LOG(INFO, "scheduler is %d\n", scheduler);
56 return scheduler == SCHED_FIFO || scheduler == SCHED_RR;
57}
James Kuszmaulc79768b2019-02-18 15:08:44 -080058
Austin Schuh3115a202019-05-27 21:02:14 -070059// Tests that every handler type is realtime and runs. There are threads
60// involved and it's easy to miss one.
61TEST(ShmEventLoopTest, AllHandlersAreRealtime) {
62 ShmEventLoopTestFactory factory;
63 auto loop = factory.MakePrimary();
64 auto loop2 = factory.Make();
65
66 loop->SetRuntimeRealtimePriority(1);
67
68 auto sender = loop2->MakeSender<TestMessage>("/test");
69
70 bool did_onrun = false;
71 bool did_timer = false;
72 bool did_watcher = false;
73
74 auto timer = loop->AddTimer([&did_timer, &loop]() {
75 EXPECT_TRUE(IsRealtime());
76 did_timer = true;
77 loop->Exit();
78 });
79
80 loop->MakeWatcher("/test", [&did_watcher](const TestMessage &) {
81 EXPECT_TRUE(IsRealtime());
82 did_watcher = true;
83 });
84
85 loop->OnRun([&loop, &did_onrun, &sender, timer]() {
86 EXPECT_TRUE(IsRealtime());
87 did_onrun = true;
88 timer->Setup(loop->monotonic_now() + chrono::milliseconds(100));
James Kuszmaulc79768b2019-02-18 15:08:44 -080089 auto msg = sender.MakeMessage();
90 msg->msg_value = 200;
Austin Schuh3115a202019-05-27 21:02:14 -070091 msg.Send();
92 });
James Kuszmaulc79768b2019-02-18 15:08:44 -080093
Austin Schuh3115a202019-05-27 21:02:14 -070094 factory.Run();
James Kuszmaulc79768b2019-02-18 15:08:44 -080095
Austin Schuh3115a202019-05-27 21:02:14 -070096 EXPECT_TRUE(did_onrun);
97 EXPECT_TRUE(did_timer);
98 EXPECT_TRUE(did_watcher);
James Kuszmaulc79768b2019-02-18 15:08:44 -080099}
Parker Schuhe4a70d62017-12-27 20:10:20 -0800100} // namespace testing
101} // namespace aos