blob: f158e02e43f69c6a01a8bf3ac242f4ed35b05e97 [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
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08004#include <string_view>
Neil Balch229001a2018-01-07 18:22:52 -08005#include <vector>
6
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/events/event_loop.h"
8#include "aos/flatbuffers.h"
9#include "aos/json_to_flatbuffer.h"
Parker Schuhe4a70d62017-12-27 20:10:20 -080010#include "gtest/gtest.h"
11
12namespace aos {
13namespace testing {
14
15class EventLoopTestFactory {
16 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070017 EventLoopTestFactory()
Brian Silverman77162972020-08-12 19:52:40 -070018 : flatbuffer_(JsonToFlatbuffer<Configuration>(R"config({
19 "channels": [
20 {
21 "name": "/aos",
22 "type": "aos.logging.LogMessageFbs"
23 },
24 {
25 "name": "/aos",
26 "type": "aos.timing.Report"
27 },
28 {
29 "name": "/test",
30 "type": "aos.TestMessage"
31 },
32 {
33 "name": "/test1",
34 "type": "aos.TestMessage"
35 },
36 {
37 "name": "/test2",
38 "type": "aos.TestMessage"
39 }
40 ]
41})config")) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -070042
Parker Schuhe4a70d62017-12-27 20:10:20 -080043 virtual ~EventLoopTestFactory() {}
44
Austin Schuh44019f92019-05-19 19:58:27 -070045 // Makes a connected event loop.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080046 virtual std::unique_ptr<EventLoop> Make(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070047 // Makes a primary event loop. This is the one the tests will try to use for
48 // anything blocking.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080049 virtual std::unique_ptr<EventLoop> MakePrimary(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070050
51 // Runs the loops until they quit.
52 virtual void Run() = 0;
Austin Schuh52d325c2019-06-23 18:59:06 -070053
Austin Schuh9fe68f72019-08-10 19:32:03 -070054 // Quits the loops.
55 virtual void Exit() = 0;
56
Austin Schuh52d325c2019-06-23 18:59:06 -070057 // Advances time by sleeping. Can't be called from inside a loop.
58 virtual void SleepFor(::std::chrono::nanoseconds duration) = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -070059
Brian Silverman77162972020-08-12 19:52:40 -070060 void PinReads() {
61 static const std::string kJson = R"config({
62 "channels": [
63 {
64 "name": "/aos",
65 "type": "aos.logging.LogMessageFbs",
66 "read_method": "PIN",
67 "num_readers": 10
68 },
69 {
70 "name": "/aos",
71 "type": "aos.timing.Report",
72 "read_method": "PIN",
73 "num_readers": 10
74 },
75 {
76 "name": "/test",
77 "type": "aos.TestMessage",
78 "read_method": "PIN",
79 "num_readers": 10
80 },
81 {
82 "name": "/test1",
83 "type": "aos.TestMessage",
84 "read_method": "PIN",
85 "num_readers": 10
86 },
87 {
88 "name": "/test2",
89 "type": "aos.TestMessage",
90 "read_method": "PIN",
91 "num_readers": 10
92 }
93 ]
94})config";
95
96 flatbuffer_ = FlatbufferDetachedBuffer<Configuration>(
97 JsonToFlatbuffer(kJson, Configuration::MiniReflectTypeTable()));
98 }
99
Austin Schuh217a9782019-12-21 23:02:50 -0800100 void EnableNodes(std::string_view my_node) {
Brian Silverman77162972020-08-12 19:52:40 -0700101 static const std::string kJson = R"config({
Austin Schuh217a9782019-12-21 23:02:50 -0800102 "channels": [
103 {
Tyler Chatow67ddb032020-01-12 14:30:04 -0800104 "name": "/aos/me",
105 "type": "aos.logging.LogMessageFbs",
106 "source_node": "me"
107 },
108 {
109 "name": "/aos/them",
110 "type": "aos.logging.LogMessageFbs",
111 "source_node": "them"
112 },
113 {
Austin Schuh217a9782019-12-21 23:02:50 -0800114 "name": "/aos",
115 "type": "aos.timing.Report",
116 "source_node": "me"
117 },
118 {
119 "name": "/test",
120 "type": "aos.TestMessage",
121 "source_node": "me"
122 },
123 {
124 "name": "/test1",
125 "type": "aos.TestMessage",
126 "source_node": "me"
127 },
128 {
129 "name": "/test2",
130 "type": "aos.TestMessage",
131 "source_node": "me"
132 }
133 ],
134 "nodes": [
135 {
Austin Schuh898f4972020-01-11 17:21:25 -0800136 "name": "me",
Austin Schuh217a9782019-12-21 23:02:50 -0800137 "hostname": "myhostname"
Austin Schuh898f4972020-01-11 17:21:25 -0800138 },
139 {
140 "name": "them",
141 "hostname": "themhostname"
Austin Schuh217a9782019-12-21 23:02:50 -0800142 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800143 ],
144 "maps": [
145 {
146 "match": {
147 "name": "/aos",
148 "type": "aos.logging.LogMessageFbs",
149 "source_node": "me"
150 },
151 "rename": {
152 "name": "/aos/me"
153 }
154 },
155 {
156 "match": {
157 "name": "/aos",
158 "type": "aos.logging.LogMessageFbs",
159 "source_node": "them"
160 },
161 "rename": {
162 "name": "/aos/them"
163 }
164 }
Austin Schuh217a9782019-12-21 23:02:50 -0800165 ]
166})config";
167
168 flatbuffer_ = FlatbufferDetachedBuffer<Configuration>(
Brian Silverman77162972020-08-12 19:52:40 -0700169 JsonToFlatbuffer(kJson, Configuration::MiniReflectTypeTable()));
Austin Schuh217a9782019-12-21 23:02:50 -0800170
Austin Schuhac0771c2020-01-07 18:36:30 -0800171 my_node_ = configuration::GetNode(&flatbuffer_.message(), my_node);
Austin Schuh217a9782019-12-21 23:02:50 -0800172 }
173
Austin Schuhac0771c2020-01-07 18:36:30 -0800174 const Node *my_node() const { return my_node_; }
Austin Schuh217a9782019-12-21 23:02:50 -0800175
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176 const Configuration *configuration() { return &flatbuffer_.message(); }
177
178 private:
179 FlatbufferDetachedBuffer<Configuration> flatbuffer_;
Austin Schuh217a9782019-12-21 23:02:50 -0800180
Austin Schuhac0771c2020-01-07 18:36:30 -0800181 const Node *my_node_ = nullptr;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800182};
183
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700184class AbstractEventLoopTestBase
Brian Silverman77162972020-08-12 19:52:40 -0700185 : public ::testing::TestWithParam<
186 std::tuple<std::function<EventLoopTestFactory *()>, ReadMethod>> {
Parker Schuhe4a70d62017-12-27 20:10:20 -0800187 public:
Brian Silverman77162972020-08-12 19:52:40 -0700188 AbstractEventLoopTestBase() : factory_(std::get<0>(GetParam())()) {
189 if (read_method() == ReadMethod::PIN) {
190 factory_->PinReads();
191 }
192 }
193
194 ReadMethod read_method() const { return std::get<1>(GetParam()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -0800195
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800196 ::std::unique_ptr<EventLoop> Make(std::string_view name = "") {
197 std::string name_copy(name);
198 if (name == "") {
199 name_copy = "loop";
200 name_copy += std::to_string(event_loop_count_);
201 }
202 ++event_loop_count_;
203 return factory_->Make(name_copy);
204 }
205 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") {
206 ++event_loop_count_;
207 return factory_->MakePrimary(name);
208 }
Austin Schuh44019f92019-05-19 19:58:27 -0700209
Austin Schuh217a9782019-12-21 23:02:50 -0800210 void EnableNodes(std::string_view my_node) { factory_->EnableNodes(my_node); }
211
Austin Schuh44019f92019-05-19 19:58:27 -0700212 void Run() { return factory_->Run(); }
Austin Schuh52d325c2019-06-23 18:59:06 -0700213
Austin Schuh9fe68f72019-08-10 19:32:03 -0700214 void Exit() { return factory_->Exit(); }
215
Austin Schuh52d325c2019-06-23 18:59:06 -0700216 void SleepFor(::std::chrono::nanoseconds duration) {
217 return factory_->SleepFor(duration);
218 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700219
Austin Schuh217a9782019-12-21 23:02:50 -0800220 const Configuration *configuration() { return factory_->configuration(); }
221
Austin Schuhac0771c2020-01-07 18:36:30 -0800222 const Node *my_node() const { return factory_->my_node(); }
Austin Schuh217a9782019-12-21 23:02:50 -0800223
Austin Schuh9fe68f72019-08-10 19:32:03 -0700224 // Ends the given event loop at the given time from now.
225 void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) {
226 auto end_timer = loop->AddTimer([this]() { this->Exit(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800227 end_timer->Setup(loop->monotonic_now() + duration);
Austin Schuh39788ff2019-12-01 18:22:57 -0800228 end_timer->set_name("end");
Austin Schuh9fe68f72019-08-10 19:32:03 -0700229 }
230
Parker Schuhe4a70d62017-12-27 20:10:20 -0800231 private:
Brian Silverman77162972020-08-12 19:52:40 -0700232 const ::std::unique_ptr<EventLoopTestFactory> factory_;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800233
234 int event_loop_count_ = 0;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800235};
236
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700237typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest;
238typedef AbstractEventLoopTestBase AbstractEventLoopTest;
239
Parker Schuhe4a70d62017-12-27 20:10:20 -0800240} // namespace testing
241} // namespace aos
242
243#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_