Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1 | #ifndef _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |
| 2 | #define _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |
| 3 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 4 | #include <string_view> |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/events/event_loop.h" |
| 8 | #include "aos/flatbuffers.h" |
| 9 | #include "aos/json_to_flatbuffer.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | namespace aos { |
| 13 | namespace testing { |
| 14 | |
| 15 | class EventLoopTestFactory { |
| 16 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | EventLoopTestFactory() |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame^] | 18 | : 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 42 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 43 | virtual ~EventLoopTestFactory() {} |
| 44 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 45 | // Makes a connected event loop. |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 46 | virtual std::unique_ptr<EventLoop> Make(std::string_view name) = 0; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 47 | // Makes a primary event loop. This is the one the tests will try to use for |
| 48 | // anything blocking. |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 49 | virtual std::unique_ptr<EventLoop> MakePrimary(std::string_view name) = 0; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 50 | |
| 51 | // Runs the loops until they quit. |
| 52 | virtual void Run() = 0; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 53 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 54 | // Quits the loops. |
| 55 | virtual void Exit() = 0; |
| 56 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 57 | // Advances time by sleeping. Can't be called from inside a loop. |
| 58 | virtual void SleepFor(::std::chrono::nanoseconds duration) = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 59 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame^] | 60 | 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 Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 100 | void EnableNodes(std::string_view my_node) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame^] | 101 | static const std::string kJson = R"config({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 102 | "channels": [ |
| 103 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 104 | "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 Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 114 | "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 Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 136 | "name": "me", |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 137 | "hostname": "myhostname" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 138 | }, |
| 139 | { |
| 140 | "name": "them", |
| 141 | "hostname": "themhostname" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 142 | } |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 143 | ], |
| 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 Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 165 | ] |
| 166 | })config"; |
| 167 | |
| 168 | flatbuffer_ = FlatbufferDetachedBuffer<Configuration>( |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame^] | 169 | JsonToFlatbuffer(kJson, Configuration::MiniReflectTypeTable())); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 170 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 171 | my_node_ = configuration::GetNode(&flatbuffer_.message(), my_node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 174 | const Node *my_node() const { return my_node_; } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 175 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 176 | const Configuration *configuration() { return &flatbuffer_.message(); } |
| 177 | |
| 178 | private: |
| 179 | FlatbufferDetachedBuffer<Configuration> flatbuffer_; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 180 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 181 | const Node *my_node_ = nullptr; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 184 | class AbstractEventLoopTestBase |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame^] | 185 | : public ::testing::TestWithParam< |
| 186 | std::tuple<std::function<EventLoopTestFactory *()>, ReadMethod>> { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 187 | public: |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame^] | 188 | 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 Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 195 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 196 | ::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 Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 209 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 210 | void EnableNodes(std::string_view my_node) { factory_->EnableNodes(my_node); } |
| 211 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 212 | void Run() { return factory_->Run(); } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 213 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 214 | void Exit() { return factory_->Exit(); } |
| 215 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 216 | void SleepFor(::std::chrono::nanoseconds duration) { |
| 217 | return factory_->SleepFor(duration); |
| 218 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 219 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 220 | const Configuration *configuration() { return factory_->configuration(); } |
| 221 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 222 | const Node *my_node() const { return factory_->my_node(); } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 223 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 224 | // 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 Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 227 | end_timer->Setup(loop->monotonic_now() + duration); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 228 | end_timer->set_name("end"); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 231 | private: |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame^] | 232 | const ::std::unique_ptr<EventLoopTestFactory> factory_; |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 233 | |
| 234 | int event_loop_count_ = 0; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 235 | }; |
| 236 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 237 | typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest; |
| 238 | typedef AbstractEventLoopTestBase AbstractEventLoopTest; |
| 239 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 240 | } // namespace testing |
| 241 | } // namespace aos |
| 242 | |
| 243 | #endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |