blob: 0b5dbec7f2941db64e956b5f0c2c2c7ebc66edb0 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include "aos/events/simulated_event_loop.h"
2
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08003#include <string_view>
4
Alex Perrycb7da4b2019-08-28 19:35:56 -07005#include "aos/events/event_loop_param_test.h"
Austin Schuh898f4972020-01-11 17:21:25 -08006#include "aos/events/ping_lib.h"
7#include "aos/events/pong_lib.h"
Austin Schuh7d87b672019-12-01 20:23:49 -08008#include "aos/events/test_message_generated.h"
Neil Balchc8f41ed2018-01-20 22:06:53 -08009#include "gtest/gtest.h"
10
11namespace aos {
12namespace testing {
13
Austin Schuh7267c532019-05-19 19:55:53 -070014namespace chrono = ::std::chrono;
15
Neil Balchc8f41ed2018-01-20 22:06:53 -080016class SimulatedEventLoopTestFactory : public EventLoopTestFactory {
17 public:
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080018 ::std::unique_ptr<EventLoop> Make(std::string_view name) override {
Austin Schuh217a9782019-12-21 23:02:50 -080019 MaybeMake();
Austin Schuhac0771c2020-01-07 18:36:30 -080020 return event_loop_factory_->MakeEventLoop(name, my_node());
Neil Balchc8f41ed2018-01-20 22:06:53 -080021 }
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080022 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override {
Austin Schuh217a9782019-12-21 23:02:50 -080023 MaybeMake();
Austin Schuhac0771c2020-01-07 18:36:30 -080024 return event_loop_factory_->MakeEventLoop(name, my_node());
Austin Schuh44019f92019-05-19 19:58:27 -070025 }
26
Austin Schuh217a9782019-12-21 23:02:50 -080027 void Run() override { event_loop_factory_->Run(); }
28 void Exit() override { event_loop_factory_->Exit(); }
Austin Schuh44019f92019-05-19 19:58:27 -070029
Austin Schuh52d325c2019-06-23 18:59:06 -070030 // 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 Schuh7d87b672019-12-01 20:23:49 -080034 void set_send_delay(std::chrono::nanoseconds send_delay) {
Austin Schuh217a9782019-12-21 23:02:50 -080035 MaybeMake();
36 event_loop_factory_->set_send_delay(send_delay);
Austin Schuh7d87b672019-12-01 20:23:49 -080037 }
38
Neil Balchc8f41ed2018-01-20 22:06:53 -080039 private:
Austin Schuh217a9782019-12-21 23:02:50 -080040 void MaybeMake() {
41 if (!event_loop_factory_) {
42 if (configuration()->has_nodes()) {
Austin Schuhac0771c2020-01-07 18:36:30 -080043 event_loop_factory_ =
44 std::make_unique<SimulatedEventLoopFactory>(configuration());
Austin Schuh217a9782019-12-21 23:02:50 -080045 } else {
46 event_loop_factory_ =
47 std::make_unique<SimulatedEventLoopFactory>(configuration());
48 }
49 }
50 }
51 std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_;
Neil Balchc8f41ed2018-01-20 22:06:53 -080052};
53
Austin Schuh6b6dfa52019-06-12 20:16:20 -070054INSTANTIATE_TEST_CASE_P(SimulatedEventLoopDeathTest, AbstractEventLoopDeathTest,
55 ::testing::Values([]() {
56 return new SimulatedEventLoopTestFactory();
57 }));
58
Neil Balchc8f41ed2018-01-20 22:06:53 -080059INSTANTIATE_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.
65TEST(EventSchedulerTest, ScheduleEvent) {
66 int counter = 0;
Austin Schuh8bd96322020-02-13 21:18:22 -080067 EventSchedulerScheduler scheduler_scheduler;
Neil Balchc8f41ed2018-01-20 22:06:53 -080068 EventScheduler scheduler;
Austin Schuh8bd96322020-02-13 21:18:22 -080069 scheduler_scheduler.AddEventScheduler(&scheduler);
Neil Balchc8f41ed2018-01-20 22:06:53 -080070
Austin Schuh8bd96322020-02-13 21:18:22 -080071 scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(1),
Austin Schuhac0771c2020-01-07 18:36:30 -080072 [&counter]() { counter += 1; });
Austin Schuh8bd96322020-02-13 21:18:22 -080073 scheduler_scheduler.Run();
Neil Balchc8f41ed2018-01-20 22:06:53 -080074 EXPECT_EQ(counter, 1);
Austin Schuhac0771c2020-01-07 18:36:30 -080075 auto token =
Austin Schuh8bd96322020-02-13 21:18:22 -080076 scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(2),
Austin Schuhac0771c2020-01-07 18:36:30 -080077 [&counter]() { counter += 1; });
Neil Balchc8f41ed2018-01-20 22:06:53 -080078 scheduler.Deschedule(token);
Austin Schuh8bd96322020-02-13 21:18:22 -080079 scheduler_scheduler.Run();
Neil Balchc8f41ed2018-01-20 22:06:53 -080080 EXPECT_EQ(counter, 1);
81}
82
83// Test that descheduling an already scheduled event doesn't run the event.
84TEST(EventSchedulerTest, DescheduleEvent) {
85 int counter = 0;
Austin Schuh8bd96322020-02-13 21:18:22 -080086 EventSchedulerScheduler scheduler_scheduler;
Neil Balchc8f41ed2018-01-20 22:06:53 -080087 EventScheduler scheduler;
Austin Schuh8bd96322020-02-13 21:18:22 -080088 scheduler_scheduler.AddEventScheduler(&scheduler);
Neil Balchc8f41ed2018-01-20 22:06:53 -080089
Austin Schuh8bd96322020-02-13 21:18:22 -080090 auto token = scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(1),
91 [&counter]() { counter += 1; });
Neil Balchc8f41ed2018-01-20 22:06:53 -080092 scheduler.Deschedule(token);
Austin Schuh8bd96322020-02-13 21:18:22 -080093 scheduler_scheduler.Run();
Neil Balchc8f41ed2018-01-20 22:06:53 -080094 EXPECT_EQ(counter, 0);
95}
Austin Schuh44019f92019-05-19 19:58:27 -070096
97// Test that running for a time period with no handlers causes time to progress
98// correctly.
99TEST(SimulatedEventLoopTest, RunForNoHandlers) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800100 SimulatedEventLoopTestFactory factory;
101
102 SimulatedEventLoopFactory simulated_event_loop_factory(
103 factory.configuration());
Austin Schuh44019f92019-05-19 19:58:27 -0700104 ::std::unique_ptr<EventLoop> event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800105 simulated_event_loop_factory.MakeEventLoop("loop");
Austin Schuh44019f92019-05-19 19:58:27 -0700106
107 simulated_event_loop_factory.RunFor(chrono::seconds(1));
108
109 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
Austin Schuh44019f92019-05-19 19:58:27 -0700110 event_loop->monotonic_now());
111}
112
113// Test that running for a time with a periodic handler causes time to end
114// correctly.
115TEST(SimulatedEventLoopTest, RunForTimerHandler) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800116 SimulatedEventLoopTestFactory factory;
117
118 SimulatedEventLoopFactory simulated_event_loop_factory(
119 factory.configuration());
Austin Schuh44019f92019-05-19 19:58:27 -0700120 ::std::unique_ptr<EventLoop> event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800121 simulated_event_loop_factory.MakeEventLoop("loop");
Austin Schuh44019f92019-05-19 19:58:27 -0700122
123 int counter = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700124 auto timer = event_loop->AddTimer([&counter]() { ++counter; });
Austin Schuh44019f92019-05-19 19:58:27 -0700125 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 Schuh44019f92019-05-19 19:58:27 -0700133 event_loop->monotonic_now());
134 EXPECT_EQ(counter, 10);
135}
136
Austin Schuh7d87b672019-12-01 20:23:49 -0800137// Tests that watchers have latency in simulation.
138TEST(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 Schuh898f4972020-01-11 17:21:25 -0800238// Tests that ping and pong work when on 2 different nodes.
239TEST(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 Balchc8f41ed2018-01-20 22:06:53 -0800278} // namespace testing
279} // namespace aos