blob: 4328d6ff254aca726cb21b7ac9c44b687a338d8b [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 Schuh7d87b672019-12-01 20:23:49 -08006#include "aos/events/test_message_generated.h"
Neil Balchc8f41ed2018-01-20 22:06:53 -08007#include "gtest/gtest.h"
8
9namespace aos {
10namespace testing {
11
Austin Schuh7267c532019-05-19 19:55:53 -070012namespace chrono = ::std::chrono;
13
Neil Balchc8f41ed2018-01-20 22:06:53 -080014class SimulatedEventLoopTestFactory : public EventLoopTestFactory {
15 public:
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080016 ::std::unique_ptr<EventLoop> Make(std::string_view name) override {
Austin Schuh217a9782019-12-21 23:02:50 -080017 MaybeMake();
18 return event_loop_factory_->MakeEventLoop(name);
Neil Balchc8f41ed2018-01-20 22:06:53 -080019 }
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080020 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override {
Austin Schuh217a9782019-12-21 23:02:50 -080021 MaybeMake();
22 return event_loop_factory_->MakeEventLoop(name);
Austin Schuh44019f92019-05-19 19:58:27 -070023 }
24
Austin Schuh217a9782019-12-21 23:02:50 -080025 void Run() override { event_loop_factory_->Run(); }
26 void Exit() override { event_loop_factory_->Exit(); }
Austin Schuh44019f92019-05-19 19:58:27 -070027
Austin Schuh52d325c2019-06-23 18:59:06 -070028 // 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 Schuh7d87b672019-12-01 20:23:49 -080032 void set_send_delay(std::chrono::nanoseconds send_delay) {
Austin Schuh217a9782019-12-21 23:02:50 -080033 MaybeMake();
34 event_loop_factory_->set_send_delay(send_delay);
Austin Schuh7d87b672019-12-01 20:23:49 -080035 }
36
Neil Balchc8f41ed2018-01-20 22:06:53 -080037 private:
Austin Schuh217a9782019-12-21 23:02:50 -080038 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 Balchc8f41ed2018-01-20 22:06:53 -080050};
51
Austin Schuh6b6dfa52019-06-12 20:16:20 -070052INSTANTIATE_TEST_CASE_P(SimulatedEventLoopDeathTest, AbstractEventLoopDeathTest,
53 ::testing::Values([]() {
54 return new SimulatedEventLoopTestFactory();
55 }));
56
Neil Balchc8f41ed2018-01-20 22:06:53 -080057INSTANTIATE_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.
63TEST(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.
79TEST(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 Schuh44019f92019-05-19 19:58:27 -070089
90// Test that running for a time period with no handlers causes time to progress
91// correctly.
92TEST(SimulatedEventLoopTest, RunForNoHandlers) {
Austin Schuh39788ff2019-12-01 18:22:57 -080093 SimulatedEventLoopTestFactory factory;
94
95 SimulatedEventLoopFactory simulated_event_loop_factory(
96 factory.configuration());
Austin Schuh44019f92019-05-19 19:58:27 -070097 ::std::unique_ptr<EventLoop> event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080098 simulated_event_loop_factory.MakeEventLoop("loop");
Austin Schuh44019f92019-05-19 19:58:27 -070099
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.
110TEST(SimulatedEventLoopTest, RunForTimerHandler) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800111 SimulatedEventLoopTestFactory factory;
112
113 SimulatedEventLoopFactory simulated_event_loop_factory(
114 factory.configuration());
Austin Schuh44019f92019-05-19 19:58:27 -0700115 ::std::unique_ptr<EventLoop> event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800116 simulated_event_loop_factory.MakeEventLoop("loop");
Austin Schuh44019f92019-05-19 19:58:27 -0700117
118 int counter = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700119 auto timer = event_loop->AddTimer([&counter]() { ++counter; });
Austin Schuh44019f92019-05-19 19:58:27 -0700120 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 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
Neil Balchc8f41ed2018-01-20 22:06:53 -0800235} // namespace testing
236} // namespace aos