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 | { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 106 | "name": "/aos/me", |
| 107 | "type": "aos.logging.LogMessageFbs", |
| 108 | "source_node": "me" |
| 109 | }, |
| 110 | { |
| 111 | "name": "/aos/them", |
| 112 | "type": "aos.logging.LogMessageFbs", |
| 113 | "source_node": "them" |
| 114 | }, |
| 115 | { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 116 | "name": "/aos", |
| 117 | "type": "aos.timing.Report", |
| 118 | "source_node": "me" |
| 119 | }, |
| 120 | { |
| 121 | "name": "/test", |
| 122 | "type": "aos.TestMessage", |
| 123 | "source_node": "me" |
| 124 | }, |
| 125 | { |
| 126 | "name": "/test1", |
| 127 | "type": "aos.TestMessage", |
| 128 | "source_node": "me" |
| 129 | }, |
| 130 | { |
| 131 | "name": "/test2", |
| 132 | "type": "aos.TestMessage", |
| 133 | "source_node": "me" |
| 134 | } |
| 135 | ], |
| 136 | "nodes": [ |
| 137 | { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 138 | "name": "me", |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 139 | "hostname": "myhostname" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 140 | }, |
| 141 | { |
| 142 | "name": "them", |
| 143 | "hostname": "themhostname" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 144 | } |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 145 | ], |
| 146 | "maps": [ |
| 147 | { |
| 148 | "match": { |
| 149 | "name": "/aos", |
| 150 | "type": "aos.logging.LogMessageFbs", |
| 151 | "source_node": "me" |
| 152 | }, |
| 153 | "rename": { |
| 154 | "name": "/aos/me" |
| 155 | } |
| 156 | }, |
| 157 | { |
| 158 | "match": { |
| 159 | "name": "/aos", |
| 160 | "type": "aos.logging.LogMessageFbs", |
| 161 | "source_node": "them" |
| 162 | }, |
| 163 | "rename": { |
| 164 | "name": "/aos/them" |
| 165 | } |
| 166 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 167 | ] |
| 168 | })config"; |
| 169 | |
| 170 | flatbuffer_ = FlatbufferDetachedBuffer<Configuration>( |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 171 | JsonToFlatbuffer(kJson, Configuration::MiniReflectTypeTable())); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 172 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 173 | my_node_ = configuration::GetNode(&flatbuffer_.message(), my_node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 176 | const Node *my_node() const { return my_node_; } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 177 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 178 | const Configuration *configuration() { return &flatbuffer_.message(); } |
| 179 | |
| 180 | private: |
| 181 | FlatbufferDetachedBuffer<Configuration> flatbuffer_; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 182 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 183 | const Node *my_node_ = nullptr; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 186 | class AbstractEventLoopTest |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 187 | : public ::testing::TestWithParam< |
| 188 | std::tuple<std::function<EventLoopTestFactory *()>, ReadMethod>> { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 189 | public: |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 190 | AbstractEventLoopTest() : factory_(std::get<0>(GetParam())()) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 191 | if (read_method() == ReadMethod::PIN) { |
| 192 | factory_->PinReads(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | ReadMethod read_method() const { return std::get<1>(GetParam()); } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 197 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 198 | ::std::unique_ptr<EventLoop> Make(std::string_view name = ""); |
| 199 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 200 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") { |
| 201 | ++event_loop_count_; |
| 202 | return factory_->MakePrimary(name); |
| 203 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 204 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 205 | void EnableNodes(std::string_view my_node) { factory_->EnableNodes(my_node); } |
| 206 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 207 | void Run() { return factory_->Run(); } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 208 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 209 | void Exit() { return factory_->Exit(); } |
| 210 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 211 | void SleepFor(::std::chrono::nanoseconds duration) { |
| 212 | return factory_->SleepFor(duration); |
| 213 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 214 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 215 | const Configuration *configuration() { return factory_->configuration(); } |
| 216 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 217 | const Node *my_node() const { return factory_->my_node(); } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 218 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 219 | // Ends the given event loop at the given time from now. |
| 220 | void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) { |
| 221 | auto end_timer = loop->AddTimer([this]() { this->Exit(); }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 222 | end_timer->Setup(loop->monotonic_now() + duration); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 223 | end_timer->set_name("end"); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 226 | // Verifies that the buffer_index values for all of the given objects are |
| 227 | // consistent. |
| 228 | void VerifyBuffers( |
| 229 | int number_buffers, |
| 230 | std::vector<std::reference_wrapper<const Fetcher<TestMessage>>> fetchers, |
| 231 | std::vector<std::reference_wrapper<const Sender<TestMessage>>> senders); |
| 232 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 233 | private: |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 234 | const ::std::unique_ptr<EventLoopTestFactory> factory_; |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 235 | |
| 236 | int event_loop_count_ = 0; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 237 | }; |
| 238 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 239 | using AbstractEventLoopDeathTest = AbstractEventLoopTest; |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 240 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 241 | } // namespace testing |
| 242 | } // namespace aos |
| 243 | |
| 244 | #endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |