blob: 513dc1b4ff63acadaa96769eb0617c71f3bbc051 [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);
Ravago Jonescf453ab2020-05-06 21:14:53 -070075 auto token = scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(2),
76 [&counter]() { counter += 1; });
Neil Balchc8f41ed2018-01-20 22:06:53 -080077 scheduler.Deschedule(token);
Austin Schuh8bd96322020-02-13 21:18:22 -080078 scheduler_scheduler.Run();
Neil Balchc8f41ed2018-01-20 22:06:53 -080079 EXPECT_EQ(counter, 1);
80}
81
82// Test that descheduling an already scheduled event doesn't run the event.
83TEST(EventSchedulerTest, DescheduleEvent) {
84 int counter = 0;
Austin Schuh8bd96322020-02-13 21:18:22 -080085 EventSchedulerScheduler scheduler_scheduler;
Neil Balchc8f41ed2018-01-20 22:06:53 -080086 EventScheduler scheduler;
Austin Schuh8bd96322020-02-13 21:18:22 -080087 scheduler_scheduler.AddEventScheduler(&scheduler);
Neil Balchc8f41ed2018-01-20 22:06:53 -080088
Austin Schuh8bd96322020-02-13 21:18:22 -080089 auto token = scheduler.Schedule(monotonic_clock::epoch() + chrono::seconds(1),
90 [&counter]() { counter += 1; });
Neil Balchc8f41ed2018-01-20 22:06:53 -080091 scheduler.Deschedule(token);
Austin Schuh8bd96322020-02-13 21:18:22 -080092 scheduler_scheduler.Run();
Neil Balchc8f41ed2018-01-20 22:06:53 -080093 EXPECT_EQ(counter, 0);
94}
Austin Schuh44019f92019-05-19 19:58:27 -070095
96// Test that running for a time period with no handlers causes time to progress
97// correctly.
98TEST(SimulatedEventLoopTest, RunForNoHandlers) {
Austin Schuh39788ff2019-12-01 18:22:57 -080099 SimulatedEventLoopTestFactory factory;
100
101 SimulatedEventLoopFactory simulated_event_loop_factory(
102 factory.configuration());
Austin Schuh44019f92019-05-19 19:58:27 -0700103 ::std::unique_ptr<EventLoop> event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800104 simulated_event_loop_factory.MakeEventLoop("loop");
Austin Schuh44019f92019-05-19 19:58:27 -0700105
106 simulated_event_loop_factory.RunFor(chrono::seconds(1));
107
108 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
Austin Schuh44019f92019-05-19 19:58:27 -0700109 event_loop->monotonic_now());
110}
111
112// Test that running for a time with a periodic handler causes time to end
113// correctly.
114TEST(SimulatedEventLoopTest, RunForTimerHandler) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800115 SimulatedEventLoopTestFactory factory;
116
117 SimulatedEventLoopFactory simulated_event_loop_factory(
118 factory.configuration());
Austin Schuh44019f92019-05-19 19:58:27 -0700119 ::std::unique_ptr<EventLoop> event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800120 simulated_event_loop_factory.MakeEventLoop("loop");
Austin Schuh44019f92019-05-19 19:58:27 -0700121
122 int counter = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123 auto timer = event_loop->AddTimer([&counter]() { ++counter; });
Austin Schuh44019f92019-05-19 19:58:27 -0700124 event_loop->OnRun([&event_loop, &timer] {
125 timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50),
126 chrono::milliseconds(100));
127 });
128
129 simulated_event_loop_factory.RunFor(chrono::seconds(1));
130
131 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
Austin Schuh44019f92019-05-19 19:58:27 -0700132 event_loop->monotonic_now());
133 EXPECT_EQ(counter, 10);
134}
135
Austin Schuh7d87b672019-12-01 20:23:49 -0800136// Tests that watchers have latency in simulation.
137TEST(SimulatedEventLoopTest, WatcherTimingReport) {
138 SimulatedEventLoopTestFactory factory;
139 factory.set_send_delay(std::chrono::microseconds(50));
140
141 FLAGS_timing_report_ms = 1000;
142 auto loop1 = factory.MakePrimary("primary");
143 loop1->MakeWatcher("/test", [](const TestMessage &) {});
144
145 auto loop2 = factory.Make("sender_loop");
146
147 auto loop3 = factory.Make("report_fetcher");
148
149 Fetcher<timing::Report> report_fetcher =
150 loop3->MakeFetcher<timing::Report>("/aos");
151 EXPECT_FALSE(report_fetcher.Fetch());
152
153 auto sender = loop2->MakeSender<TestMessage>("/test");
154
155 // Send 10 messages in the middle of a timing report period so we get
156 // something interesting back.
157 auto test_timer = loop2->AddTimer([&sender]() {
158 for (int i = 0; i < 10; ++i) {
159 aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder();
160 TestMessage::Builder builder = msg.MakeBuilder<TestMessage>();
161 builder.add_value(200 + i);
162 ASSERT_TRUE(msg.Send(builder.Finish()));
163 }
164 });
165
166 // Quit after 1 timing report, mid way through the next cycle.
167 {
168 auto end_timer = loop1->AddTimer([&factory]() { factory.Exit(); });
169 end_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(2500));
170 end_timer->set_name("end");
171 }
172
173 loop1->OnRun([&test_timer, &loop1]() {
174 test_timer->Setup(loop1->monotonic_now() + chrono::milliseconds(1500));
175 });
176
177 factory.Run();
178
179 // And, since we are here, check that the timing report makes sense.
180 // Start by looking for our event loop's timing.
181 FlatbufferDetachedBuffer<timing::Report> primary_report =
182 FlatbufferDetachedBuffer<timing::Report>::Empty();
183 while (report_fetcher.FetchNext()) {
184 LOG(INFO) << "Report " << FlatbufferToJson(report_fetcher.get());
185 if (report_fetcher->name()->string_view() == "primary") {
186 primary_report = CopyFlatBuffer(report_fetcher.get());
187 }
188 }
189
190 // Check the watcher report.
Ravago Jonescf453ab2020-05-06 21:14:53 -0700191 VLOG(1) << FlatbufferToJson(primary_report, {.multi_line = true});
Austin Schuh7d87b672019-12-01 20:23:49 -0800192
193 EXPECT_EQ(primary_report.message().name()->string_view(), "primary");
194
195 // Just the timing report timer.
196 ASSERT_NE(primary_report.message().timers(), nullptr);
197 EXPECT_EQ(primary_report.message().timers()->size(), 2);
198
199 // No phased loops
200 ASSERT_EQ(primary_report.message().phased_loops(), nullptr);
201
202 // And now confirm that the watcher received all 10 messages, and has latency.
203 ASSERT_NE(primary_report.message().watchers(), nullptr);
204 ASSERT_EQ(primary_report.message().watchers()->size(), 1);
205 EXPECT_EQ(primary_report.message().watchers()->Get(0)->count(), 10);
206 EXPECT_NEAR(
207 primary_report.message().watchers()->Get(0)->wakeup_latency()->average(),
208 0.00005, 1e-9);
209 EXPECT_NEAR(
210 primary_report.message().watchers()->Get(0)->wakeup_latency()->min(),
211 0.00005, 1e-9);
212 EXPECT_NEAR(
213 primary_report.message().watchers()->Get(0)->wakeup_latency()->max(),
214 0.00005, 1e-9);
215 EXPECT_EQ(primary_report.message()
216 .watchers()
217 ->Get(0)
218 ->wakeup_latency()
219 ->standard_deviation(),
220 0.0);
221
222 EXPECT_EQ(
223 primary_report.message().watchers()->Get(0)->handler_time()->average(),
224 0.0);
225 EXPECT_EQ(primary_report.message().watchers()->Get(0)->handler_time()->min(),
226 0.0);
227 EXPECT_EQ(primary_report.message().watchers()->Get(0)->handler_time()->max(),
228 0.0);
229 EXPECT_EQ(primary_report.message()
230 .watchers()
231 ->Get(0)
232 ->handler_time()
233 ->standard_deviation(),
234 0.0);
235}
236
Austin Schuh898f4972020-01-11 17:21:25 -0800237// Tests that ping and pong work when on 2 different nodes.
238TEST(SimulatedEventLoopTest, MultinodePingPong) {
239 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
240 aos::configuration::ReadConfig(
241 "aos/events/multinode_pingpong_config.json");
242 const Node *pi1 = configuration::GetNode(&config.message(), "pi1");
243 const Node *pi2 = configuration::GetNode(&config.message(), "pi2");
244
245 SimulatedEventLoopFactory simulated_event_loop_factory(&config.message());
246
247 std::unique_ptr<EventLoop> ping_event_loop =
248 simulated_event_loop_factory.MakeEventLoop("ping", pi1);
249 Ping ping(ping_event_loop.get());
250
251 std::unique_ptr<EventLoop> pong_event_loop =
252 simulated_event_loop_factory.MakeEventLoop("pong", pi2);
253 Pong pong(pong_event_loop.get());
254
255 std::unique_ptr<EventLoop> pi2_pong_counter_event_loop =
256 simulated_event_loop_factory.MakeEventLoop("pi2_pong_counter", pi2);
257
258 int pi2_pong_count = 0;
259 pi2_pong_counter_event_loop->MakeWatcher(
260 "/test",
261 [&pi2_pong_count](const examples::Pong & /*pong*/) { ++pi2_pong_count; });
262
263 std::unique_ptr<EventLoop> pi1_pong_counter_event_loop =
264 simulated_event_loop_factory.MakeEventLoop("pi1_pong_counter", pi1);
265 int pi1_pong_count = 0;
266 pi1_pong_counter_event_loop->MakeWatcher(
267 "/test",
268 [&pi1_pong_count](const examples::Pong & /*pong*/) { ++pi1_pong_count; });
269
270 simulated_event_loop_factory.RunFor(chrono::seconds(10) +
271 chrono::milliseconds(5));
272
273 EXPECT_EQ(pi1_pong_count, 1001);
274 EXPECT_EQ(pi2_pong_count, 1001);
275}
276
Neil Balchc8f41ed2018-01-20 22:06:53 -0800277} // namespace testing
278} // namespace aos