Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/simulated_event_loop.h" |
| 2 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 3 | #include <string_view> |
| 4 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include "aos/events/event_loop_param_test.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 6 | #include "aos/events/test_message_generated.h" |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 7 | #include "gtest/gtest.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 12 | namespace chrono = ::std::chrono; |
| 13 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 14 | class SimulatedEventLoopTestFactory : public EventLoopTestFactory { |
| 15 | public: |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 16 | ::std::unique_ptr<EventLoop> Make(std::string_view name) override { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 17 | MaybeMake(); |
| 18 | return event_loop_factory_->MakeEventLoop(name); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 19 | } |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 20 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 21 | MaybeMake(); |
| 22 | return event_loop_factory_->MakeEventLoop(name); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 25 | void Run() override { event_loop_factory_->Run(); } |
| 26 | void Exit() override { event_loop_factory_->Exit(); } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 27 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 28 | // TODO(austin): Implement this. It's used currently for a phased loop test. |
| 29 | // I'm not sure how much that matters. |
| 30 | void SleepFor(::std::chrono::nanoseconds /*duration*/) override {} |
| 31 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 32 | void set_send_delay(std::chrono::nanoseconds send_delay) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 33 | MaybeMake(); |
| 34 | event_loop_factory_->set_send_delay(send_delay); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 37 | private: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 38 | void MaybeMake() { |
| 39 | if (!event_loop_factory_) { |
| 40 | if (configuration()->has_nodes()) { |
| 41 | event_loop_factory_ = std::make_unique<SimulatedEventLoopFactory>( |
| 42 | configuration(), my_node()); |
| 43 | } else { |
| 44 | event_loop_factory_ = |
| 45 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 52 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopDeathTest, AbstractEventLoopDeathTest, |
| 53 | ::testing::Values([]() { |
| 54 | return new SimulatedEventLoopTestFactory(); |
| 55 | })); |
| 56 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 57 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopTest, AbstractEventLoopTest, |
| 58 | ::testing::Values([]() { |
| 59 | return new SimulatedEventLoopTestFactory(); |
| 60 | })); |
| 61 | |
| 62 | // Test that creating an event and running the scheduler runs the event. |
| 63 | TEST(EventSchedulerTest, ScheduleEvent) { |
| 64 | int counter = 0; |
| 65 | EventScheduler scheduler; |
| 66 | |
| 67 | scheduler.Schedule(::aos::monotonic_clock::now(), |
| 68 | [&counter]() { counter += 1; }); |
| 69 | scheduler.Run(); |
| 70 | EXPECT_EQ(counter, 1); |
| 71 | auto token = scheduler.Schedule(::aos::monotonic_clock::now(), |
| 72 | [&counter]() { counter += 1; }); |
| 73 | scheduler.Deschedule(token); |
| 74 | scheduler.Run(); |
| 75 | EXPECT_EQ(counter, 1); |
| 76 | } |
| 77 | |
| 78 | // Test that descheduling an already scheduled event doesn't run the event. |
| 79 | TEST(EventSchedulerTest, DescheduleEvent) { |
| 80 | int counter = 0; |
| 81 | EventScheduler scheduler; |
| 82 | |
| 83 | auto token = scheduler.Schedule(::aos::monotonic_clock::now(), |
| 84 | [&counter]() { counter += 1; }); |
| 85 | scheduler.Deschedule(token); |
| 86 | scheduler.Run(); |
| 87 | EXPECT_EQ(counter, 0); |
| 88 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 89 | |
| 90 | // Test that running for a time period with no handlers causes time to progress |
| 91 | // correctly. |
| 92 | TEST(SimulatedEventLoopTest, RunForNoHandlers) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 93 | SimulatedEventLoopTestFactory factory; |
| 94 | |
| 95 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 96 | factory.configuration()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 97 | ::std::unique_ptr<EventLoop> event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 98 | simulated_event_loop_factory.MakeEventLoop("loop"); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 99 | |
| 100 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 101 | |
| 102 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
| 103 | simulated_event_loop_factory.monotonic_now()); |
| 104 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
| 105 | event_loop->monotonic_now()); |
| 106 | } |
| 107 | |
| 108 | // Test that running for a time with a periodic handler causes time to end |
| 109 | // correctly. |
| 110 | TEST(SimulatedEventLoopTest, RunForTimerHandler) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 111 | SimulatedEventLoopTestFactory factory; |
| 112 | |
| 113 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 114 | factory.configuration()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 115 | ::std::unique_ptr<EventLoop> event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 116 | simulated_event_loop_factory.MakeEventLoop("loop"); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 117 | |
| 118 | int counter = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 119 | auto timer = event_loop->AddTimer([&counter]() { ++counter; }); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 120 | event_loop->OnRun([&event_loop, &timer] { |
| 121 | timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50), |
| 122 | chrono::milliseconds(100)); |
| 123 | }); |
| 124 | |
| 125 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 126 | |
| 127 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
| 128 | simulated_event_loop_factory.monotonic_now()); |
| 129 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
| 130 | event_loop->monotonic_now()); |
| 131 | EXPECT_EQ(counter, 10); |
| 132 | } |
| 133 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 134 | // Tests that watchers have latency in simulation. |
| 135 | TEST(SimulatedEventLoopTest, WatcherTimingReport) { |
| 136 | SimulatedEventLoopTestFactory factory; |
| 137 | factory.set_send_delay(std::chrono::microseconds(50)); |
| 138 | |
| 139 | FLAGS_timing_report_ms = 1000; |
| 140 | auto loop1 = factory.MakePrimary("primary"); |
| 141 | loop1->MakeWatcher("/test", [](const TestMessage &) {}); |
| 142 | |
| 143 | auto loop2 = factory.Make("sender_loop"); |
| 144 | |
| 145 | auto loop3 = factory.Make("report_fetcher"); |
| 146 | |
| 147 | Fetcher<timing::Report> report_fetcher = |
| 148 | loop3->MakeFetcher<timing::Report>("/aos"); |
| 149 | EXPECT_FALSE(report_fetcher.Fetch()); |
| 150 | |
| 151 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 152 | |
| 153 | // Send 10 messages in the middle of a timing report period so we get |
| 154 | // something interesting back. |
| 155 | auto test_timer = loop2->AddTimer([&sender]() { |
| 156 | for (int i = 0; i < 10; ++i) { |
| 157 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 158 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 159 | builder.add_value(200 + i); |
| 160 | ASSERT_TRUE(msg.Send(builder.Finish())); |
| 161 | } |
| 162 | }); |
| 163 | |
| 164 | // Quit after 1 timing report, mid way through the next cycle. |
| 165 | { |
| 166 | auto end_timer = loop1->AddTimer([&factory]() { factory.Exit(); }); |
| 167 | end_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(2500)); |
| 168 | end_timer->set_name("end"); |
| 169 | } |
| 170 | |
| 171 | loop1->OnRun([&test_timer, &loop1]() { |
| 172 | test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500)); |
| 173 | }); |
| 174 | |
| 175 | factory.Run(); |
| 176 | |
| 177 | // And, since we are here, check that the timing report makes sense. |
| 178 | // Start by looking for our event loop's timing. |
| 179 | FlatbufferDetachedBuffer<timing::Report> primary_report = |
| 180 | FlatbufferDetachedBuffer<timing::Report>::Empty(); |
| 181 | while (report_fetcher.FetchNext()) { |
| 182 | LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get()); |
| 183 | if (report_fetcher->name()->string_view() == "primary") { |
| 184 | primary_report = CopyFlatBuffer(report_fetcher.get()); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Check the watcher report. |
| 189 | VLOG(1) << FlatbufferToJson(primary_report, true); |
| 190 | |
| 191 | EXPECT_EQ(primary_report.message().name()->string_view(), "primary"); |
| 192 | |
| 193 | // Just the timing report timer. |
| 194 | ASSERT_NE(primary_report.message().timers(), nullptr); |
| 195 | EXPECT_EQ(primary_report.message().timers()->size(), 2); |
| 196 | |
| 197 | // No phased loops |
| 198 | ASSERT_EQ(primary_report.message().phased_loops(), nullptr); |
| 199 | |
| 200 | // And now confirm that the watcher received all 10 messages, and has latency. |
| 201 | ASSERT_NE(primary_report.message().watchers(), nullptr); |
| 202 | ASSERT_EQ(primary_report.message().watchers()->size(), 1); |
| 203 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10); |
| 204 | EXPECT_NEAR( |
| 205 | primary_report.message().watchers()->Get(0)->wakeup_latency()->average(), |
| 206 | 0.00005, 1e-9); |
| 207 | EXPECT_NEAR( |
| 208 | primary_report.message().watchers()->Get(0)->wakeup_latency()->min(), |
| 209 | 0.00005, 1e-9); |
| 210 | EXPECT_NEAR( |
| 211 | primary_report.message().watchers()->Get(0)->wakeup_latency()->max(), |
| 212 | 0.00005, 1e-9); |
| 213 | EXPECT_EQ(primary_report.message() |
| 214 | .watchers() |
| 215 | ->Get(0) |
| 216 | ->wakeup_latency() |
| 217 | ->standard_deviation(), |
| 218 | 0.0); |
| 219 | |
| 220 | EXPECT_EQ( |
| 221 | primary_report.message().watchers()->Get(0)->handler_time()->average(), |
| 222 | 0.0); |
| 223 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->handler_time()->min(), |
| 224 | 0.0); |
| 225 | EXPECT_EQ(primary_report.message().watchers()->Get(0)->handler_time()->max(), |
| 226 | 0.0); |
| 227 | EXPECT_EQ(primary_report.message() |
| 228 | .watchers() |
| 229 | ->Get(0) |
| 230 | ->handler_time() |
| 231 | ->standard_deviation(), |
| 232 | 0.0); |
| 233 | } |
| 234 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 235 | } // namespace testing |
| 236 | } // namespace aos |