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