blob: de5cbae864862cfb99f4329595aa7df99ad3d59c [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;
67 EventScheduler scheduler;
68
Austin Schuhac0771c2020-01-07 18:36:30 -080069 scheduler.Schedule(distributed_clock::epoch() + chrono::seconds(1),
70 [&counter]() { counter += 1; });
Neil Balchc8f41ed2018-01-20 22:06:53 -080071 scheduler.Run();
72 EXPECT_EQ(counter, 1);
Austin Schuhac0771c2020-01-07 18:36:30 -080073 auto token =
74 scheduler.Schedule(distributed_clock::epoch() + chrono::seconds(2),
75 [&counter]() { counter += 1; });
Neil Balchc8f41ed2018-01-20 22:06:53 -080076 scheduler.Deschedule(token);
77 scheduler.Run();
78 EXPECT_EQ(counter, 1);
79}
80
81// Test that descheduling an already scheduled event doesn't run the event.
82TEST(EventSchedulerTest, DescheduleEvent) {
83 int counter = 0;
84 EventScheduler scheduler;
85
Austin Schuhac0771c2020-01-07 18:36:30 -080086 auto token =
87 scheduler.Schedule(distributed_clock::epoch() + chrono::seconds(1),
88 [&counter]() { counter += 1; });
Neil Balchc8f41ed2018-01-20 22:06:53 -080089 scheduler.Deschedule(token);
90 scheduler.Run();
91 EXPECT_EQ(counter, 0);
92}
Austin Schuh44019f92019-05-19 19:58:27 -070093
94// Test that running for a time period with no handlers causes time to progress
95// correctly.
96TEST(SimulatedEventLoopTest, RunForNoHandlers) {
Austin Schuh39788ff2019-12-01 18:22:57 -080097 SimulatedEventLoopTestFactory factory;
98
99 SimulatedEventLoopFactory simulated_event_loop_factory(
100 factory.configuration());
Austin Schuh44019f92019-05-19 19:58:27 -0700101 ::std::unique_ptr<EventLoop> event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800102 simulated_event_loop_factory.MakeEventLoop("loop");
Austin Schuh44019f92019-05-19 19:58:27 -0700103
104 simulated_event_loop_factory.RunFor(chrono::seconds(1));
105
106 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
Austin Schuh44019f92019-05-19 19:58:27 -0700107 event_loop->monotonic_now());
108}
109
110// Test that running for a time with a periodic handler causes time to end
111// correctly.
112TEST(SimulatedEventLoopTest, RunForTimerHandler) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800113 SimulatedEventLoopTestFactory factory;
114
115 SimulatedEventLoopFactory simulated_event_loop_factory(
116 factory.configuration());
Austin Schuh44019f92019-05-19 19:58:27 -0700117 ::std::unique_ptr<EventLoop> event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800118 simulated_event_loop_factory.MakeEventLoop("loop");
Austin Schuh44019f92019-05-19 19:58:27 -0700119
120 int counter = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700121 auto timer = event_loop->AddTimer([&counter]() { ++counter; });
Austin Schuh44019f92019-05-19 19:58:27 -0700122 event_loop->OnRun([&event_loop, &timer] {
123 timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50),
124 chrono::milliseconds(100));
125 });
126
127 simulated_event_loop_factory.RunFor(chrono::seconds(1));
128
129 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
Austin Schuh44019f92019-05-19 19:58:27 -0700130 event_loop->monotonic_now());
131 EXPECT_EQ(counter, 10);
132}
133
Austin Schuh7d87b672019-12-01 20:23:49 -0800134// Tests that watchers have latency in simulation.
135TEST(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
Austin Schuh898f4972020-01-11 17:21:25 -0800235// Tests that ping and pong work when on 2 different nodes.
236TEST(SimulatedEventLoopTest, MultinodePingPong) {
237 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
238 aos::configuration::ReadConfig(
239 "aos/events/multinode_pingpong_config.json");
240 const Node *pi1 = configuration::GetNode(&config.message(), "pi1");
241 const Node *pi2 = configuration::GetNode(&config.message(), "pi2");
242
243 SimulatedEventLoopFactory simulated_event_loop_factory(&config.message());
244
245 std::unique_ptr<EventLoop> ping_event_loop =
246 simulated_event_loop_factory.MakeEventLoop("ping", pi1);
247 Ping ping(ping_event_loop.get());
248
249 std::unique_ptr<EventLoop> pong_event_loop =
250 simulated_event_loop_factory.MakeEventLoop("pong", pi2);
251 Pong pong(pong_event_loop.get());
252
253 std::unique_ptr<EventLoop> pi2_pong_counter_event_loop =
254 simulated_event_loop_factory.MakeEventLoop("pi2_pong_counter", pi2);
255
256 int pi2_pong_count = 0;
257 pi2_pong_counter_event_loop->MakeWatcher(
258 "/test",
259 [&pi2_pong_count](const examples::Pong & /*pong*/) { ++pi2_pong_count; });
260
261 std::unique_ptr<EventLoop> pi1_pong_counter_event_loop =
262 simulated_event_loop_factory.MakeEventLoop("pi1_pong_counter", pi1);
263 int pi1_pong_count = 0;
264 pi1_pong_counter_event_loop->MakeWatcher(
265 "/test",
266 [&pi1_pong_count](const examples::Pong & /*pong*/) { ++pi1_pong_count; });
267
268 simulated_event_loop_factory.RunFor(chrono::seconds(10) +
269 chrono::milliseconds(5));
270
271 EXPECT_EQ(pi1_pong_count, 1001);
272 EXPECT_EQ(pi2_pong_count, 1001);
273}
274
Neil Balchc8f41ed2018-01-20 22:06:53 -0800275} // namespace testing
276} // namespace aos