blob: 61adbe36725a236cf50ae832b9cebebf222eb542 [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
Austin Schuh6b6dfa52019-06-12 20:16:20 -070038INSTANTIATE_TEST_CASE_P(ShmEventLoopDeathTest, AbstractEventLoopDeathTest,
39 ::testing::Values([]() {
40 return new ShmEventLoopTestFactory();
41 }));
42
James Kuszmaulc79768b2019-02-18 15:08:44 -080043struct TestMessage : public ::aos::Message {
44 enum { kQueueLength = 100, kHash = 0x696c0cdc };
45 int msg_value;
46
47 void Zero() { msg_value = 0; }
48 static size_t Size() { return 1 + ::aos::Message::Size(); }
49 size_t Print(char *buffer, size_t length) const;
50 TestMessage() { Zero(); }
51};
52
Parker Schuhe4a70d62017-12-27 20:10:20 -080053} // namespace
James Kuszmaulc79768b2019-02-18 15:08:44 -080054
Austin Schuh3115a202019-05-27 21:02:14 -070055bool IsRealtime() {
56 int scheduler;
57 if ((scheduler = sched_getscheduler(0)) == -1) {
58 PLOG(FATAL, "sched_getscheduler(0) failed\n");
James Kuszmaulc79768b2019-02-18 15:08:44 -080059 }
Austin Schuh3115a202019-05-27 21:02:14 -070060 LOG(INFO, "scheduler is %d\n", scheduler);
61 return scheduler == SCHED_FIFO || scheduler == SCHED_RR;
62}
James Kuszmaulc79768b2019-02-18 15:08:44 -080063
Austin Schuh3115a202019-05-27 21:02:14 -070064// Tests that every handler type is realtime and runs. There are threads
65// involved and it's easy to miss one.
66TEST(ShmEventLoopTest, AllHandlersAreRealtime) {
67 ShmEventLoopTestFactory factory;
68 auto loop = factory.MakePrimary();
69 auto loop2 = factory.Make();
70
71 loop->SetRuntimeRealtimePriority(1);
72
73 auto sender = loop2->MakeSender<TestMessage>("/test");
74
75 bool did_onrun = false;
76 bool did_timer = false;
77 bool did_watcher = false;
78
79 auto timer = loop->AddTimer([&did_timer, &loop]() {
80 EXPECT_TRUE(IsRealtime());
81 did_timer = true;
82 loop->Exit();
83 });
84
85 loop->MakeWatcher("/test", [&did_watcher](const TestMessage &) {
86 EXPECT_TRUE(IsRealtime());
87 did_watcher = true;
88 });
89
90 loop->OnRun([&loop, &did_onrun, &sender, timer]() {
91 EXPECT_TRUE(IsRealtime());
92 did_onrun = true;
93 timer->Setup(loop->monotonic_now() + chrono::milliseconds(100));
James Kuszmaulc79768b2019-02-18 15:08:44 -080094 auto msg = sender.MakeMessage();
95 msg->msg_value = 200;
Austin Schuh3115a202019-05-27 21:02:14 -070096 msg.Send();
97 });
James Kuszmaulc79768b2019-02-18 15:08:44 -080098
Austin Schuh3115a202019-05-27 21:02:14 -070099 factory.Run();
James Kuszmaulc79768b2019-02-18 15:08:44 -0800100
Austin Schuh3115a202019-05-27 21:02:14 -0700101 EXPECT_TRUE(did_onrun);
102 EXPECT_TRUE(did_timer);
103 EXPECT_TRUE(did_watcher);
James Kuszmaulc79768b2019-02-18 15:08:44 -0800104}
Parker Schuhe4a70d62017-12-27 20:10:20 -0800105} // namespace testing
106} // namespace aos