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 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 4 | #include <initializer_list> |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 5 | #include <string_view> |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 6 | #include <vector> |
| 7 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "aos/events/event_loop.h" |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 9 | #include "aos/events/test_message_generated.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "aos/flatbuffers.h" |
| 11 | #include "aos/json_to_flatbuffer.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | namespace aos { |
| 15 | namespace testing { |
| 16 | |
| 17 | class EventLoopTestFactory { |
| 18 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | EventLoopTestFactory() |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 20 | : flatbuffer_(JsonToFlatbuffer<Configuration>(R"config({ |
| 21 | "channels": [ |
| 22 | { |
| 23 | "name": "/aos", |
| 24 | "type": "aos.logging.LogMessageFbs" |
| 25 | }, |
| 26 | { |
| 27 | "name": "/aos", |
| 28 | "type": "aos.timing.Report" |
| 29 | }, |
| 30 | { |
| 31 | "name": "/test", |
| 32 | "type": "aos.TestMessage" |
| 33 | }, |
| 34 | { |
| 35 | "name": "/test1", |
| 36 | "type": "aos.TestMessage" |
| 37 | }, |
| 38 | { |
| 39 | "name": "/test2", |
| 40 | "type": "aos.TestMessage" |
| 41 | } |
| 42 | ] |
| 43 | })config")) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 45 | virtual ~EventLoopTestFactory() {} |
| 46 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 47 | // Makes a connected event loop. |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 48 | virtual std::unique_ptr<EventLoop> Make(std::string_view name) = 0; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 49 | // Makes a primary event loop. This is the one the tests will try to use for |
| 50 | // anything blocking. |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 51 | virtual std::unique_ptr<EventLoop> MakePrimary(std::string_view name) = 0; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 52 | |
| 53 | // Runs the loops until they quit. |
| 54 | virtual void Run() = 0; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 55 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 56 | // Quits the loops. |
| 57 | virtual void Exit() = 0; |
| 58 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 59 | // Advances time by sleeping. Can't be called from inside a loop. |
| 60 | virtual void SleepFor(::std::chrono::nanoseconds duration) = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 61 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 62 | void PinReads() { |
| 63 | static const std::string kJson = R"config({ |
| 64 | "channels": [ |
| 65 | { |
| 66 | "name": "/aos", |
| 67 | "type": "aos.logging.LogMessageFbs", |
| 68 | "read_method": "PIN", |
| 69 | "num_readers": 10 |
| 70 | }, |
| 71 | { |
| 72 | "name": "/aos", |
| 73 | "type": "aos.timing.Report", |
| 74 | "read_method": "PIN", |
| 75 | "num_readers": 10 |
| 76 | }, |
| 77 | { |
| 78 | "name": "/test", |
| 79 | "type": "aos.TestMessage", |
| 80 | "read_method": "PIN", |
| 81 | "num_readers": 10 |
| 82 | }, |
| 83 | { |
| 84 | "name": "/test1", |
| 85 | "type": "aos.TestMessage", |
| 86 | "read_method": "PIN", |
| 87 | "num_readers": 10 |
| 88 | }, |
| 89 | { |
| 90 | "name": "/test2", |
| 91 | "type": "aos.TestMessage", |
| 92 | "read_method": "PIN", |
| 93 | "num_readers": 10 |
| 94 | } |
| 95 | ] |
| 96 | })config"; |
| 97 | |
| 98 | flatbuffer_ = FlatbufferDetachedBuffer<Configuration>( |
| 99 | JsonToFlatbuffer(kJson, Configuration::MiniReflectTypeTable())); |
| 100 | } |
| 101 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 102 | void EnableNodes(std::string_view my_node) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 103 | static const std::string kJson = R"config({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 104 | "channels": [ |
| 105 | { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 106 | "name": "/me/aos", |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 107 | "type": "aos.logging.LogMessageFbs", |
| 108 | "source_node": "me" |
| 109 | }, |
| 110 | { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 111 | "name": "/them/aos", |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 112 | "type": "aos.logging.LogMessageFbs", |
| 113 | "source_node": "them" |
| 114 | }, |
| 115 | { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 116 | "name": "/me/aos", |
| 117 | "type": "aos.message_bridge.Timestamp", |
| 118 | "source_node": "me", |
| 119 | "destination_nodes": [ |
| 120 | { |
| 121 | "name": "them" |
| 122 | } |
| 123 | ] |
| 124 | }, |
| 125 | { |
| 126 | "name": "/them/aos", |
| 127 | "type": "aos.message_bridge.Timestamp", |
| 128 | "source_node": "them", |
| 129 | "destination_nodes": [ |
| 130 | { |
| 131 | "name": "me" |
| 132 | } |
| 133 | ] |
| 134 | }, |
| 135 | { |
| 136 | "name": "/me/aos", |
| 137 | "type": "aos.message_bridge.ServerStatistics", |
| 138 | "source_node": "me", |
| 139 | "frequency": 2 |
| 140 | }, |
| 141 | { |
| 142 | "name": "/them/aos", |
| 143 | "type": "aos.message_bridge.ServerStatistics", |
| 144 | "source_node": "them", |
| 145 | "frequency": 2 |
| 146 | }, |
| 147 | { |
| 148 | "name": "/me/aos", |
| 149 | "type": "aos.message_bridge.ClientStatistics", |
| 150 | "source_node": "me", |
| 151 | "frequency": 2 |
| 152 | }, |
| 153 | { |
| 154 | "name": "/them/aos", |
| 155 | "type": "aos.message_bridge.ClientStatistics", |
| 156 | "source_node": "them", |
| 157 | "frequency": 2 |
| 158 | }, |
| 159 | { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 160 | "name": "/aos", |
| 161 | "type": "aos.timing.Report", |
| 162 | "source_node": "me" |
| 163 | }, |
| 164 | { |
| 165 | "name": "/test", |
| 166 | "type": "aos.TestMessage", |
| 167 | "source_node": "me" |
| 168 | }, |
| 169 | { |
| 170 | "name": "/test1", |
| 171 | "type": "aos.TestMessage", |
| 172 | "source_node": "me" |
| 173 | }, |
| 174 | { |
| 175 | "name": "/test2", |
| 176 | "type": "aos.TestMessage", |
| 177 | "source_node": "me" |
| 178 | } |
| 179 | ], |
| 180 | "nodes": [ |
| 181 | { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 182 | "name": "me", |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 183 | "hostname": "myhostname" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 184 | }, |
| 185 | { |
| 186 | "name": "them", |
| 187 | "hostname": "themhostname" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 188 | } |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 189 | ], |
| 190 | "maps": [ |
| 191 | { |
| 192 | "match": { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 193 | "name": "/aos*", |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 194 | "source_node": "me" |
| 195 | }, |
| 196 | "rename": { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 197 | "name": "/me/aos" |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 198 | } |
| 199 | }, |
| 200 | { |
| 201 | "match": { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 202 | "name": "/aos*", |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 203 | "source_node": "them" |
| 204 | }, |
| 205 | "rename": { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 206 | "name": "/them/aos" |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 209 | ] |
| 210 | })config"; |
| 211 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 212 | flatbuffer_ = configuration::MergeConfiguration( |
| 213 | FlatbufferDetachedBuffer<Configuration>( |
| 214 | JsonToFlatbuffer(kJson, Configuration::MiniReflectTypeTable()))); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 215 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 216 | my_node_ = configuration::GetNode(&flatbuffer_.message(), my_node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 219 | const Node *my_node() const { return my_node_; } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 220 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 221 | const Configuration *configuration() { return &flatbuffer_.message(); } |
| 222 | |
| 223 | private: |
| 224 | FlatbufferDetachedBuffer<Configuration> flatbuffer_; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 225 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 226 | const Node *my_node_ = nullptr; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 227 | }; |
| 228 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 229 | class AbstractEventLoopTest |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 230 | : public ::testing::TestWithParam< |
| 231 | std::tuple<std::function<EventLoopTestFactory *()>, ReadMethod>> { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 232 | public: |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 233 | AbstractEventLoopTest() : factory_(std::get<0>(GetParam())()) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 234 | if (read_method() == ReadMethod::PIN) { |
| 235 | factory_->PinReads(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | ReadMethod read_method() const { return std::get<1>(GetParam()); } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 240 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 241 | ::std::unique_ptr<EventLoop> Make(std::string_view name = ""); |
| 242 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 243 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") { |
| 244 | ++event_loop_count_; |
| 245 | return factory_->MakePrimary(name); |
| 246 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 247 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 248 | void EnableNodes(std::string_view my_node) { factory_->EnableNodes(my_node); } |
| 249 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 250 | void Run() { return factory_->Run(); } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 251 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 252 | void Exit() { return factory_->Exit(); } |
| 253 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 254 | void SleepFor(::std::chrono::nanoseconds duration) { |
| 255 | return factory_->SleepFor(duration); |
| 256 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 257 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 258 | const Configuration *configuration() { return factory_->configuration(); } |
| 259 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 260 | const Node *my_node() const { return factory_->my_node(); } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 261 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 262 | // Ends the given event loop at the given time from now. |
| 263 | void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) { |
| 264 | auto end_timer = loop->AddTimer([this]() { this->Exit(); }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 265 | end_timer->Setup(loop->monotonic_now() + duration); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 266 | end_timer->set_name("end"); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 269 | // Verifies that the buffer_index values for all of the given objects are |
| 270 | // consistent. |
| 271 | void VerifyBuffers( |
| 272 | int number_buffers, |
| 273 | std::vector<std::reference_wrapper<const Fetcher<TestMessage>>> fetchers, |
| 274 | std::vector<std::reference_wrapper<const Sender<TestMessage>>> senders); |
| 275 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 276 | private: |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 277 | const ::std::unique_ptr<EventLoopTestFactory> factory_; |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 278 | |
| 279 | int event_loop_count_ = 0; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 280 | }; |
| 281 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 282 | using AbstractEventLoopDeathTest = AbstractEventLoopTest; |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 283 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 284 | } // namespace testing |
| 285 | } // namespace aos |
| 286 | |
| 287 | #endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |