blob: cbd5cd1862b5667b22fe49afac2120d540ff89a8 [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()
18 : flatbuffer_(JsonToFlatbuffer("{\n"
19 " \"channels\": [ \n"
20 " {\n"
Austin Schuh39788ff2019-12-01 18:22:57 -080021 " \"name\": \"/aos\",\n"
Tyler Chatow67ddb032020-01-12 14:30:04 -080022 " \"type\": \"aos.logging.LogMessageFbs\"\n"
23 " },\n"
24 " {\n"
25 " \"name\": \"/aos\",\n"
Austin Schuh39788ff2019-12-01 18:22:57 -080026 " \"type\": \"aos.timing.Report\"\n"
27 " },\n"
28 " {\n"
Alex Perrycb7da4b2019-08-28 19:35:56 -070029 " \"name\": \"/test\",\n"
30 " \"type\": \"aos.TestMessage\"\n"
31 " },\n"
32 " {\n"
33 " \"name\": \"/test1\",\n"
34 " \"type\": \"aos.TestMessage\"\n"
35 " },\n"
36 " {\n"
37 " \"name\": \"/test2\",\n"
38 " \"type\": \"aos.TestMessage\"\n"
39 " }\n"
40 " ]\n"
41 "}\n",
42 Configuration::MiniReflectTypeTable())) {}
43
Parker Schuhe4a70d62017-12-27 20:10:20 -080044 virtual ~EventLoopTestFactory() {}
45
Austin Schuh44019f92019-05-19 19:58:27 -070046 // Makes a connected event loop.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080047 virtual std::unique_ptr<EventLoop> Make(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070048 // Makes a primary event loop. This is the one the tests will try to use for
49 // anything blocking.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080050 virtual std::unique_ptr<EventLoop> MakePrimary(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070051
52 // Runs the loops until they quit.
53 virtual void Run() = 0;
Austin Schuh52d325c2019-06-23 18:59:06 -070054
Austin Schuh9fe68f72019-08-10 19:32:03 -070055 // Quits the loops.
56 virtual void Exit() = 0;
57
Austin Schuh52d325c2019-06-23 18:59:06 -070058 // Advances time by sleeping. Can't be called from inside a loop.
59 virtual void SleepFor(::std::chrono::nanoseconds duration) = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -070060
Austin Schuh217a9782019-12-21 23:02:50 -080061 void EnableNodes(std::string_view my_node) {
Austin Schuh898f4972020-01-11 17:21:25 -080062 std::string json = R"config({
Austin Schuh217a9782019-12-21 23:02:50 -080063 "channels": [
64 {
Tyler Chatow67ddb032020-01-12 14:30:04 -080065 "name": "/aos/me",
66 "type": "aos.logging.LogMessageFbs",
67 "source_node": "me"
68 },
69 {
70 "name": "/aos/them",
71 "type": "aos.logging.LogMessageFbs",
72 "source_node": "them"
73 },
74 {
Austin Schuh217a9782019-12-21 23:02:50 -080075 "name": "/aos",
76 "type": "aos.timing.Report",
77 "source_node": "me"
78 },
79 {
80 "name": "/test",
81 "type": "aos.TestMessage",
82 "source_node": "me"
83 },
84 {
85 "name": "/test1",
86 "type": "aos.TestMessage",
87 "source_node": "me"
88 },
89 {
90 "name": "/test2",
91 "type": "aos.TestMessage",
92 "source_node": "me"
93 }
94 ],
95 "nodes": [
96 {
Austin Schuh898f4972020-01-11 17:21:25 -080097 "name": "me",
Austin Schuh217a9782019-12-21 23:02:50 -080098 "hostname": "myhostname"
Austin Schuh898f4972020-01-11 17:21:25 -080099 },
100 {
101 "name": "them",
102 "hostname": "themhostname"
Austin Schuh217a9782019-12-21 23:02:50 -0800103 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800104 ],
105 "maps": [
106 {
107 "match": {
108 "name": "/aos",
109 "type": "aos.logging.LogMessageFbs",
110 "source_node": "me"
111 },
112 "rename": {
113 "name": "/aos/me"
114 }
115 },
116 {
117 "match": {
118 "name": "/aos",
119 "type": "aos.logging.LogMessageFbs",
120 "source_node": "them"
121 },
122 "rename": {
123 "name": "/aos/them"
124 }
125 }
Austin Schuh217a9782019-12-21 23:02:50 -0800126 ]
127})config";
128
129 flatbuffer_ = FlatbufferDetachedBuffer<Configuration>(
130 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable()));
131
Austin Schuhac0771c2020-01-07 18:36:30 -0800132 my_node_ = configuration::GetNode(&flatbuffer_.message(), my_node);
Austin Schuh217a9782019-12-21 23:02:50 -0800133 }
134
Austin Schuhac0771c2020-01-07 18:36:30 -0800135 const Node *my_node() const { return my_node_; }
Austin Schuh217a9782019-12-21 23:02:50 -0800136
Alex Perrycb7da4b2019-08-28 19:35:56 -0700137 const Configuration *configuration() { return &flatbuffer_.message(); }
138
139 private:
140 FlatbufferDetachedBuffer<Configuration> flatbuffer_;
Austin Schuh217a9782019-12-21 23:02:50 -0800141
Austin Schuhac0771c2020-01-07 18:36:30 -0800142 const Node *my_node_ = nullptr;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800143};
144
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700145class AbstractEventLoopTestBase
Parker Schuhe4a70d62017-12-27 20:10:20 -0800146 : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> {
147 public:
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700148 AbstractEventLoopTestBase() { factory_.reset(GetParam()()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -0800149
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800150 ::std::unique_ptr<EventLoop> Make(std::string_view name = "") {
151 std::string name_copy(name);
152 if (name == "") {
153 name_copy = "loop";
154 name_copy += std::to_string(event_loop_count_);
155 }
156 ++event_loop_count_;
157 return factory_->Make(name_copy);
158 }
159 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") {
160 ++event_loop_count_;
161 return factory_->MakePrimary(name);
162 }
Austin Schuh44019f92019-05-19 19:58:27 -0700163
Austin Schuh217a9782019-12-21 23:02:50 -0800164 void EnableNodes(std::string_view my_node) { factory_->EnableNodes(my_node); }
165
Austin Schuh44019f92019-05-19 19:58:27 -0700166 void Run() { return factory_->Run(); }
Austin Schuh52d325c2019-06-23 18:59:06 -0700167
Austin Schuh9fe68f72019-08-10 19:32:03 -0700168 void Exit() { return factory_->Exit(); }
169
Austin Schuh52d325c2019-06-23 18:59:06 -0700170 void SleepFor(::std::chrono::nanoseconds duration) {
171 return factory_->SleepFor(duration);
172 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700173
Austin Schuh217a9782019-12-21 23:02:50 -0800174 const Configuration *configuration() { return factory_->configuration(); }
175
Austin Schuhac0771c2020-01-07 18:36:30 -0800176 const Node *my_node() const { return factory_->my_node(); }
Austin Schuh217a9782019-12-21 23:02:50 -0800177
Austin Schuh9fe68f72019-08-10 19:32:03 -0700178 // Ends the given event loop at the given time from now.
179 void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) {
180 auto end_timer = loop->AddTimer([this]() { this->Exit(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800181 end_timer->Setup(loop->monotonic_now() + duration);
Austin Schuh39788ff2019-12-01 18:22:57 -0800182 end_timer->set_name("end");
Austin Schuh9fe68f72019-08-10 19:32:03 -0700183 }
184
Parker Schuhe4a70d62017-12-27 20:10:20 -0800185 // You can implement all the usual fixture class members here.
186 // To access the test parameter, call GetParam() from class
187 // TestWithParam<T>.
188 private:
Austin Schuh44019f92019-05-19 19:58:27 -0700189 ::std::unique_ptr<EventLoopTestFactory> factory_;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800190
191 int event_loop_count_ = 0;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800192};
193
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700194typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest;
195typedef AbstractEventLoopTestBase AbstractEventLoopTest;
196
Parker Schuhe4a70d62017-12-27 20:10:20 -0800197} // namespace testing
198} // namespace aos
199
200#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_