Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1 | #include "aos/events/event-loop_param_test.h" |
| 2 | |
| 3 | namespace aos { |
| 4 | namespace testing { |
| 5 | |
| 6 | struct TestMessage : public ::aos::Message { |
| 7 | enum { kQueueLength = 100, kHash = 0x696c0cdc }; |
| 8 | int msg_value; |
| 9 | |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 10 | void Zero() { |
| 11 | ::aos::Message::Zero(); |
| 12 | msg_value = 0; |
| 13 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 14 | static size_t Size() { return 1 + ::aos::Message::Size(); } |
| 15 | size_t Print(char *buffer, size_t length) const; |
| 16 | TestMessage() { Zero(); } |
| 17 | }; |
| 18 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 19 | // Ends the given event loop at the given time from now. |
| 20 | void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) { |
| 21 | auto end_timer = loop->AddTimer([loop]() { loop->Exit(); }); |
| 22 | end_timer->Setup(loop->monotonic_now() + |
| 23 | ::std::chrono::milliseconds(duration)); |
| 24 | } |
| 25 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 26 | // Tests that watcher and fetcher can fetch from a sender. |
| 27 | // Also tests that OnRun() works. |
| 28 | TEST_P(AbstractEventLoopTest, Basic) { |
| 29 | auto loop1 = Make(); |
| 30 | auto loop2 = Make(); |
| 31 | auto loop3 = Make(); |
| 32 | |
| 33 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 34 | |
| 35 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 36 | |
| 37 | auto msg = sender.MakeMessage(); |
| 38 | |
| 39 | msg->msg_value = 200; |
| 40 | |
| 41 | msg.Send(); |
| 42 | |
| 43 | EXPECT_TRUE(fetcher.Fetch()); |
| 44 | ASSERT_FALSE(fetcher.get() == nullptr); |
| 45 | EXPECT_EQ(fetcher->msg_value, 200); |
| 46 | |
| 47 | bool happened = false; |
| 48 | |
| 49 | loop3->OnRun([&]() { happened = true; }); |
| 50 | |
| 51 | loop3->MakeWatcher("/test", [&](const TestMessage &message) { |
| 52 | EXPECT_EQ(message.msg_value, 200); |
| 53 | loop3->Exit(); |
| 54 | }); |
| 55 | |
| 56 | EXPECT_FALSE(happened); |
| 57 | loop3->Run(); |
| 58 | EXPECT_TRUE(happened); |
| 59 | } |
| 60 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 61 | // Verify that making a fetcher and watcher for "/test" succeeds. |
| 62 | TEST_P(AbstractEventLoopTest, FetcherAndWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 63 | auto loop = Make(); |
| 64 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 65 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 68 | // Verify that making 2 fetchers for "/test" succeeds. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 69 | TEST_P(AbstractEventLoopTest, TwoFetcher) { |
| 70 | auto loop = Make(); |
| 71 | auto fetcher = loop->MakeFetcher<TestMessage>("/test"); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 72 | auto fetcher2 = loop->MakeFetcher<TestMessage>("/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 75 | // Verify that registering a watcher twice for "/test" fails. |
| 76 | TEST_P(AbstractEventLoopTest, TwoWatcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 77 | auto loop = Make(); |
| 78 | loop->MakeWatcher("/test", [&](const TestMessage &) {}); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 79 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 80 | "/test"); |
| 81 | } |
| 82 | |
| 83 | // Verify that registering a watcher and a sender for "/test" fails. |
| 84 | TEST_P(AbstractEventLoopTest, WatcherAndSender) { |
| 85 | auto loop = Make(); |
| 86 | auto sender = loop->MakeSender<TestMessage>("/test"); |
| 87 | EXPECT_DEATH(loop->MakeWatcher("/test", [&](const TestMessage &) {}), |
| 88 | "/test"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Verify that Quit() works when there are multiple watchers. |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 92 | TEST_P(AbstractEventLoopTest, MultipleWatcherQuit) { |
| 93 | auto loop1 = Make(); |
| 94 | auto loop2 = Make(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 95 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 96 | auto sender = loop1->MakeSender<TestMessage>("/test2"); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 97 | { |
| 98 | auto msg = sender.MakeMessage(); |
| 99 | msg->msg_value = 200; |
| 100 | msg.Send(); |
| 101 | } |
| 102 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 103 | loop2->MakeWatcher("/test1", [&](const TestMessage &) {}); |
| 104 | loop2->MakeWatcher("/test2", [&](const TestMessage &message) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 105 | EXPECT_EQ(message.msg_value, 200); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 106 | loop2->Exit(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 107 | }); |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 108 | loop2->Run(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 111 | // Verify that timer intervals and duration function properly. |
| 112 | TEST_P(AbstractEventLoopTest, TimerIntervalAndDuration) { |
| 113 | auto loop = Make(); |
| 114 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 115 | |
| 116 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
| 117 | iteration_list.push_back(loop->monotonic_now()); |
| 118 | }); |
| 119 | |
| 120 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
| 121 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(150)); |
| 122 | // Testing that the timer thread waits for the event loop to start before |
| 123 | // running |
| 124 | ::std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 125 | loop->Run(); |
| 126 | |
| 127 | EXPECT_EQ(iteration_list.size(), 8); |
| 128 | } |
| 129 | |
| 130 | // Verify that we can change a timer's parameters during execution. |
| 131 | TEST_P(AbstractEventLoopTest, TimerChangeParameters) { |
| 132 | auto loop = Make(); |
| 133 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 134 | |
| 135 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
| 136 | iteration_list.push_back(loop->monotonic_now()); |
| 137 | }); |
| 138 | |
| 139 | auto modifier_timer = loop->AddTimer([&loop, &test_timer]() { |
| 140 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(30)); |
| 141 | }); |
| 142 | |
| 143 | |
| 144 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
| 145 | modifier_timer->Setup(loop->monotonic_now() + |
| 146 | ::std::chrono::milliseconds(45)); |
| 147 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(150)); |
| 148 | loop->Run(); |
| 149 | |
| 150 | EXPECT_EQ(iteration_list.size(), 7); |
| 151 | } |
| 152 | |
| 153 | // Verify that we can disable a timer during execution. |
| 154 | TEST_P(AbstractEventLoopTest, TimerDisable) { |
| 155 | auto loop = Make(); |
| 156 | ::std::vector<::aos::monotonic_clock::time_point> iteration_list; |
| 157 | |
| 158 | auto test_timer = loop->AddTimer([&iteration_list, &loop]() { |
| 159 | iteration_list.push_back(loop->monotonic_now()); |
| 160 | }); |
| 161 | |
| 162 | auto ender_timer = loop->AddTimer([&test_timer]() { |
| 163 | test_timer->Disable(); |
| 164 | }); |
| 165 | |
| 166 | test_timer->Setup(loop->monotonic_now(), ::std::chrono::milliseconds(20)); |
| 167 | ender_timer->Setup(loop->monotonic_now() + |
| 168 | ::std::chrono::milliseconds(45)); |
| 169 | EndEventLoop(loop.get(), ::std::chrono::milliseconds(150)); |
| 170 | loop->Run(); |
| 171 | |
| 172 | EXPECT_EQ(iteration_list.size(), 3); |
| 173 | } |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 174 | |
| 175 | // Verify that the send time on a message is roughly right. |
| 176 | TEST_P(AbstractEventLoopTest, MessageSendTime) { |
| 177 | auto loop1 = Make(); |
| 178 | auto loop2 = Make(); |
| 179 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
| 180 | auto fetcher = loop2->MakeFetcher<TestMessage>("/test"); |
| 181 | |
| 182 | auto test_timer = loop1->AddTimer([&sender]() { |
| 183 | auto msg = sender.MakeMessage(); |
| 184 | msg->msg_value = 200; |
| 185 | msg.Send(); |
| 186 | }); |
| 187 | |
| 188 | test_timer->Setup(loop1->monotonic_now() + ::std::chrono::seconds(1)); |
| 189 | |
| 190 | EndEventLoop(loop1.get(), ::std::chrono::seconds(2)); |
| 191 | loop1->Run(); |
| 192 | |
| 193 | EXPECT_TRUE(fetcher.Fetch()); |
| 194 | |
| 195 | monotonic_clock::duration time_offset = |
| 196 | fetcher->sent_time - (loop1->monotonic_now() - ::std::chrono::seconds(1)); |
| 197 | |
| 198 | EXPECT_TRUE(time_offset > ::std::chrono::milliseconds(-500)) |
| 199 | << ": Got " << fetcher->sent_time.time_since_epoch().count() << " expected " |
| 200 | << loop1->monotonic_now().time_since_epoch().count(); |
| 201 | EXPECT_TRUE(time_offset < ::std::chrono::milliseconds(500)) |
| 202 | << ": Got " << fetcher->sent_time.time_since_epoch().count() |
| 203 | << " expected " << loop1->monotonic_now().time_since_epoch().count(); |
| 204 | } |
| 205 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 206 | } // namespace testing |
| 207 | } // namespace aos |