blob: fe85732f25c1ddd1fac3ad73bd89cfaf4ca3707f [file] [log] [blame]
Parker Schuhe4a70d62017-12-27 20:10:20 -08001#ifndef _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_
2#define _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_
3
Brian Silverman4f4e0612020-08-12 19:54:41 -07004#include <initializer_list>
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08005#include <string_view>
Neil Balch229001a2018-01-07 18:22:52 -08006#include <vector>
7
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "gtest/gtest.h"
9
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "aos/events/event_loop.h"
Brian Silverman4f4e0612020-08-12 19:54:41 -070011#include "aos/events/test_message_generated.h"
Austin Schuha9df9ad2021-06-16 14:49:39 -070012#include "aos/events/test_message_schema.h"
13#include "aos/events/timing_report_schema.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "aos/flatbuffers.h"
15#include "aos/json_to_flatbuffer.h"
Austin Schuha9df9ad2021-06-16 14:49:39 -070016#include "aos/logging/log_message_schema.h"
17#include "aos/network/message_bridge_client_schema.h"
18#include "aos/network/message_bridge_server_schema.h"
19#include "aos/network/timestamp_schema.h"
Parker Schuhe4a70d62017-12-27 20:10:20 -080020
21namespace aos {
22namespace testing {
23
24class EventLoopTestFactory {
25 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070026 EventLoopTestFactory()
Austin Schuha9df9ad2021-06-16 14:49:39 -070027 : flatbuffer_(configuration::AddSchema(
28 R"config({
Brian Silverman77162972020-08-12 19:52:40 -070029 "channels": [
30 {
31 "name": "/aos",
32 "type": "aos.logging.LogMessageFbs"
33 },
34 {
35 "name": "/aos",
36 "type": "aos.timing.Report"
37 },
38 {
39 "name": "/test",
milind945708b2021-07-03 13:30:15 -070040 "type": "aos.TestMessage",
41 "frequency": 800
Brian Silverman77162972020-08-12 19:52:40 -070042 },
43 {
44 "name": "/test1",
45 "type": "aos.TestMessage"
46 },
47 {
48 "name": "/test2",
49 "type": "aos.TestMessage"
50 }
51 ]
Austin Schuha9df9ad2021-06-16 14:49:39 -070052})config",
53 {aos::FlatbufferSpan<reflection::Schema>(
54 logging::LogMessageFbsSchema()),
55 aos::FlatbufferSpan<reflection::Schema>(timing::ReportSchema()),
56 aos::FlatbufferSpan<reflection::Schema>(TestMessageSchema())})) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -070057
Parker Schuhe4a70d62017-12-27 20:10:20 -080058 virtual ~EventLoopTestFactory() {}
59
Austin Schuh44019f92019-05-19 19:58:27 -070060 // Makes a connected event loop.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080061 virtual std::unique_ptr<EventLoop> Make(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070062 // Makes a primary event loop. This is the one the tests will try to use for
63 // anything blocking.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080064 virtual std::unique_ptr<EventLoop> MakePrimary(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070065
66 // Runs the loops until they quit.
67 virtual void Run() = 0;
Austin Schuh52d325c2019-06-23 18:59:06 -070068
Austin Schuh9fe68f72019-08-10 19:32:03 -070069 // Quits the loops.
70 virtual void Exit() = 0;
71
Austin Schuh52d325c2019-06-23 18:59:06 -070072 // Advances time by sleeping. Can't be called from inside a loop.
73 virtual void SleepFor(::std::chrono::nanoseconds duration) = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -070074
Austin Schuhd54780b2020-10-03 16:26:02 -070075 // Sets the config to a config with a max size with an invalid alignment.
76 void InvalidChannelAlignment() {
Austin Schuha9df9ad2021-06-16 14:49:39 -070077 flatbuffer_ = configuration::AddSchema(
78 R"config({
Austin Schuhd54780b2020-10-03 16:26:02 -070079 "channels": [
80 {
81 "name": "/aos",
82 "type": "aos.logging.LogMessageFbs"
83 },
84 {
85 "name": "/aos",
86 "type": "aos.timing.Report"
87 },
88 {
89 "name": "/test",
90 "type": "aos.TestMessage",
91 "max_size": 13
92 },
93 {
94 "name": "/test1",
95 "type": "aos.TestMessage"
96 },
97 {
98 "name": "/test2",
99 "type": "aos.TestMessage"
100 }
101 ]
Austin Schuha9df9ad2021-06-16 14:49:39 -0700102})config",
103 {aos::FlatbufferSpan<reflection::Schema>(
104 logging::LogMessageFbsSchema()),
105 aos::FlatbufferSpan<reflection::Schema>(timing::ReportSchema()),
106 aos::FlatbufferSpan<reflection::Schema>(TestMessageSchema())});
Austin Schuhd54780b2020-10-03 16:26:02 -0700107 }
108
Brian Silverman77162972020-08-12 19:52:40 -0700109 void PinReads() {
110 static const std::string kJson = R"config({
111 "channels": [
112 {
113 "name": "/aos",
114 "type": "aos.logging.LogMessageFbs",
115 "read_method": "PIN",
116 "num_readers": 10
117 },
118 {
119 "name": "/aos",
120 "type": "aos.timing.Report",
121 "read_method": "PIN",
122 "num_readers": 10
123 },
124 {
125 "name": "/test",
126 "type": "aos.TestMessage",
127 "read_method": "PIN",
milind945708b2021-07-03 13:30:15 -0700128 "num_readers": 10,
129 "frequency": 800
Brian Silverman77162972020-08-12 19:52:40 -0700130 },
131 {
132 "name": "/test1",
133 "type": "aos.TestMessage",
134 "read_method": "PIN",
135 "num_readers": 10
136 },
137 {
138 "name": "/test2",
139 "type": "aos.TestMessage",
140 "read_method": "PIN",
141 "num_readers": 10
142 }
143 ]
144})config";
145
Austin Schuha9df9ad2021-06-16 14:49:39 -0700146 flatbuffer_ = configuration::AddSchema(
147 kJson, {aos::FlatbufferSpan<reflection::Schema>(
148 logging::LogMessageFbsSchema()),
149 aos::FlatbufferSpan<reflection::Schema>(timing::ReportSchema()),
150 aos::FlatbufferSpan<reflection::Schema>(TestMessageSchema())});
Brian Silverman77162972020-08-12 19:52:40 -0700151 }
152
Austin Schuh217a9782019-12-21 23:02:50 -0800153 void EnableNodes(std::string_view my_node) {
Brian Silverman77162972020-08-12 19:52:40 -0700154 static const std::string kJson = R"config({
Austin Schuh217a9782019-12-21 23:02:50 -0800155 "channels": [
156 {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700157 "name": "/me/aos",
Tyler Chatow67ddb032020-01-12 14:30:04 -0800158 "type": "aos.logging.LogMessageFbs",
159 "source_node": "me"
160 },
161 {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700162 "name": "/them/aos",
Tyler Chatow67ddb032020-01-12 14:30:04 -0800163 "type": "aos.logging.LogMessageFbs",
164 "source_node": "them"
165 },
166 {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700167 "name": "/me/aos",
168 "type": "aos.message_bridge.Timestamp",
169 "source_node": "me",
170 "destination_nodes": [
171 {
172 "name": "them"
173 }
174 ]
175 },
176 {
177 "name": "/them/aos",
178 "type": "aos.message_bridge.Timestamp",
179 "source_node": "them",
180 "destination_nodes": [
181 {
182 "name": "me"
183 }
184 ]
185 },
186 {
187 "name": "/me/aos",
188 "type": "aos.message_bridge.ServerStatistics",
189 "source_node": "me",
190 "frequency": 2
191 },
192 {
193 "name": "/them/aos",
194 "type": "aos.message_bridge.ServerStatistics",
195 "source_node": "them",
196 "frequency": 2
197 },
198 {
199 "name": "/me/aos",
200 "type": "aos.message_bridge.ClientStatistics",
201 "source_node": "me",
202 "frequency": 2
203 },
204 {
205 "name": "/them/aos",
206 "type": "aos.message_bridge.ClientStatistics",
207 "source_node": "them",
208 "frequency": 2
209 },
210 {
Austin Schuh217a9782019-12-21 23:02:50 -0800211 "name": "/aos",
212 "type": "aos.timing.Report",
213 "source_node": "me"
214 },
215 {
216 "name": "/test",
217 "type": "aos.TestMessage",
218 "source_node": "me"
219 },
220 {
221 "name": "/test1",
222 "type": "aos.TestMessage",
223 "source_node": "me"
224 },
225 {
226 "name": "/test2",
227 "type": "aos.TestMessage",
228 "source_node": "me"
Brian Silverman631b6262021-11-10 12:25:08 -0800229 },
230 {
231 "name": "/test_forward",
232 "type": "aos.TestMessage",
233 "source_node": "them",
234 "destination_nodes": [
235 {
236 "name": "me"
237 }
238 ]
239 },
240 {
241 "name": "/test_noforward",
242 "type": "aos.TestMessage",
243 "source_node": "them"
Austin Schuh217a9782019-12-21 23:02:50 -0800244 }
245 ],
246 "nodes": [
247 {
Austin Schuh898f4972020-01-11 17:21:25 -0800248 "name": "me",
Austin Schuh217a9782019-12-21 23:02:50 -0800249 "hostname": "myhostname"
Austin Schuh898f4972020-01-11 17:21:25 -0800250 },
251 {
252 "name": "them",
253 "hostname": "themhostname"
Austin Schuh217a9782019-12-21 23:02:50 -0800254 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800255 ],
256 "maps": [
257 {
258 "match": {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700259 "name": "/aos*",
Tyler Chatow67ddb032020-01-12 14:30:04 -0800260 "source_node": "me"
261 },
262 "rename": {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700263 "name": "/me/aos"
Tyler Chatow67ddb032020-01-12 14:30:04 -0800264 }
265 },
266 {
267 "match": {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700268 "name": "/aos*",
Tyler Chatow67ddb032020-01-12 14:30:04 -0800269 "source_node": "them"
270 },
271 "rename": {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700272 "name": "/them/aos"
Tyler Chatow67ddb032020-01-12 14:30:04 -0800273 }
274 }
Austin Schuh217a9782019-12-21 23:02:50 -0800275 ]
276})config";
277
Austin Schuh4c3b9702020-08-30 11:34:55 -0700278 flatbuffer_ = configuration::MergeConfiguration(
Austin Schuha9df9ad2021-06-16 14:49:39 -0700279 configuration::MergeConfiguration(
280 aos::FlatbufferDetachedBuffer<Configuration>(
281 JsonToFlatbuffer<Configuration>(kJson))),
282 {aos::FlatbufferSpan<reflection::Schema>(
283 logging::LogMessageFbsSchema()),
284 aos::FlatbufferSpan<reflection::Schema>(timing::ReportSchema()),
285 aos::FlatbufferSpan<reflection::Schema>(TestMessageSchema()),
286 aos::FlatbufferSpan<reflection::Schema>(
287 message_bridge::ClientStatisticsSchema()),
288 aos::FlatbufferSpan<reflection::Schema>(
289 message_bridge::ServerStatisticsSchema()),
290 aos::FlatbufferSpan<reflection::Schema>(
291 message_bridge::TimestampSchema())});
Austin Schuh217a9782019-12-21 23:02:50 -0800292
Austin Schuhac0771c2020-01-07 18:36:30 -0800293 my_node_ = configuration::GetNode(&flatbuffer_.message(), my_node);
Austin Schuh217a9782019-12-21 23:02:50 -0800294 }
295
Austin Schuhac0771c2020-01-07 18:36:30 -0800296 const Node *my_node() const { return my_node_; }
Austin Schuh217a9782019-12-21 23:02:50 -0800297
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298 const Configuration *configuration() { return &flatbuffer_.message(); }
299
300 private:
301 FlatbufferDetachedBuffer<Configuration> flatbuffer_;
Austin Schuh217a9782019-12-21 23:02:50 -0800302
Austin Schuhac0771c2020-01-07 18:36:30 -0800303 const Node *my_node_ = nullptr;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800304};
305
Austin Schuh6bae8252021-02-07 22:01:49 -0800306enum class DoTimingReports { kYes, kNo };
307
Brian Silverman4f4e0612020-08-12 19:54:41 -0700308class AbstractEventLoopTest
Brian Silverman77162972020-08-12 19:52:40 -0700309 : public ::testing::TestWithParam<
Austin Schuh6bae8252021-02-07 22:01:49 -0800310 std::tuple<std::function<EventLoopTestFactory *()>, ReadMethod,
311 DoTimingReports>> {
Parker Schuhe4a70d62017-12-27 20:10:20 -0800312 public:
Brian Silverman4f4e0612020-08-12 19:54:41 -0700313 AbstractEventLoopTest() : factory_(std::get<0>(GetParam())()) {
Brian Silverman77162972020-08-12 19:52:40 -0700314 if (read_method() == ReadMethod::PIN) {
315 factory_->PinReads();
316 }
317 }
318
319 ReadMethod read_method() const { return std::get<1>(GetParam()); }
Austin Schuh6bae8252021-02-07 22:01:49 -0800320 DoTimingReports do_timing_reports() const { return std::get<2>(GetParam()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -0800321
Brian Silverman4f4e0612020-08-12 19:54:41 -0700322 ::std::unique_ptr<EventLoop> Make(std::string_view name = "");
323
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800324 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") {
325 ++event_loop_count_;
Austin Schuh6bae8252021-02-07 22:01:49 -0800326 auto result = factory_->MakePrimary(name);
327 if (do_timing_reports() == DoTimingReports::kNo) {
328 result->SkipTimingReport();
329 }
330 return result;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800331 }
Austin Schuh44019f92019-05-19 19:58:27 -0700332
Austin Schuhd54780b2020-10-03 16:26:02 -0700333 void InvalidChannelAlignment() { factory_->InvalidChannelAlignment(); }
334
Austin Schuh217a9782019-12-21 23:02:50 -0800335 void EnableNodes(std::string_view my_node) { factory_->EnableNodes(my_node); }
336
Austin Schuh44019f92019-05-19 19:58:27 -0700337 void Run() { return factory_->Run(); }
Austin Schuh52d325c2019-06-23 18:59:06 -0700338
Austin Schuh9fe68f72019-08-10 19:32:03 -0700339 void Exit() { return factory_->Exit(); }
340
Austin Schuh52d325c2019-06-23 18:59:06 -0700341 void SleepFor(::std::chrono::nanoseconds duration) {
342 return factory_->SleepFor(duration);
343 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700344
Austin Schuh217a9782019-12-21 23:02:50 -0800345 const Configuration *configuration() { return factory_->configuration(); }
346
Austin Schuhac0771c2020-01-07 18:36:30 -0800347 const Node *my_node() const { return factory_->my_node(); }
Austin Schuh217a9782019-12-21 23:02:50 -0800348
Austin Schuh9fe68f72019-08-10 19:32:03 -0700349 // Ends the given event loop at the given time from now.
350 void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) {
351 auto end_timer = loop->AddTimer([this]() { this->Exit(); });
Philipp Schradera6712522023-07-05 20:25:11 -0700352 end_timer->Schedule(loop->monotonic_now() + duration);
Austin Schuh39788ff2019-12-01 18:22:57 -0800353 end_timer->set_name("end");
Austin Schuh9fe68f72019-08-10 19:32:03 -0700354 }
355
Brian Silverman4f4e0612020-08-12 19:54:41 -0700356 // Verifies that the buffer_index values for all of the given objects are
357 // consistent.
358 void VerifyBuffers(
359 int number_buffers,
360 std::vector<std::reference_wrapper<const Fetcher<TestMessage>>> fetchers,
361 std::vector<std::reference_wrapper<const Sender<TestMessage>>> senders);
362
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700363 // Helper function for testing the sent too fast check using a PhasedLoop with
364 // an interval that sends exactly at the frequency of the channel
365 void TestSentTooFastCheckEdgeCase(
366 const std::function<RawSender::Error(int, int)> expected_err,
367 const bool send_twice_at_end);
368
Parker Schuhe4a70d62017-12-27 20:10:20 -0800369 private:
Brian Silverman77162972020-08-12 19:52:40 -0700370 const ::std::unique_ptr<EventLoopTestFactory> factory_;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800371
372 int event_loop_count_ = 0;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800373};
374
Brian Silverman4f4e0612020-08-12 19:54:41 -0700375using AbstractEventLoopDeathTest = AbstractEventLoopTest;
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700376
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700377// Returns the frequency of the /test TestMessage channel
378int TestChannelFrequency(EventLoop *event_loop);
379// Returns the queue size of the /test TestMessage channel
380int TestChannelQueueSize(EventLoop *event_loop);
381// Sends a test message with value 0 with the given sender
382RawSender::Error SendTestMessage(aos::Sender<TestMessage> &sender);
383
Parker Schuhe4a70d62017-12-27 20:10:20 -0800384} // namespace testing
385} // namespace aos
386
387#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_