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() |
| 18 | : flatbuffer_(JsonToFlatbuffer("{\n" |
| 19 | " \"channels\": [ \n" |
| 20 | " {\n" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 21 | " \"name\": \"/aos\",\n" |
| 22 | " \"type\": \"aos.timing.Report\"\n" |
| 23 | " },\n" |
| 24 | " {\n" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | " \"name\": \"/test\",\n" |
| 26 | " \"type\": \"aos.TestMessage\"\n" |
| 27 | " },\n" |
| 28 | " {\n" |
| 29 | " \"name\": \"/test1\",\n" |
| 30 | " \"type\": \"aos.TestMessage\"\n" |
| 31 | " },\n" |
| 32 | " {\n" |
| 33 | " \"name\": \"/test2\",\n" |
| 34 | " \"type\": \"aos.TestMessage\"\n" |
| 35 | " }\n" |
| 36 | " ]\n" |
| 37 | "}\n", |
| 38 | Configuration::MiniReflectTypeTable())) {} |
| 39 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 40 | virtual ~EventLoopTestFactory() {} |
| 41 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 42 | // Makes a connected event loop. |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 43 | virtual std::unique_ptr<EventLoop> Make(std::string_view name) = 0; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 44 | // Makes a primary event loop. This is the one the tests will try to use for |
| 45 | // anything blocking. |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 46 | virtual std::unique_ptr<EventLoop> MakePrimary(std::string_view name) = 0; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 47 | |
| 48 | // Runs the loops until they quit. |
| 49 | virtual void Run() = 0; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 50 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 51 | // Quits the loops. |
| 52 | virtual void Exit() = 0; |
| 53 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 54 | // Advances time by sleeping. Can't be called from inside a loop. |
| 55 | virtual void SleepFor(::std::chrono::nanoseconds duration) = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 56 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 57 | void EnableNodes(std::string_view my_node) { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 58 | std::string json = R"config({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 59 | "channels": [ |
| 60 | { |
| 61 | "name": "/aos", |
| 62 | "type": "aos.timing.Report", |
| 63 | "source_node": "me" |
| 64 | }, |
| 65 | { |
| 66 | "name": "/test", |
| 67 | "type": "aos.TestMessage", |
| 68 | "source_node": "me" |
| 69 | }, |
| 70 | { |
| 71 | "name": "/test1", |
| 72 | "type": "aos.TestMessage", |
| 73 | "source_node": "me" |
| 74 | }, |
| 75 | { |
| 76 | "name": "/test2", |
| 77 | "type": "aos.TestMessage", |
| 78 | "source_node": "me" |
| 79 | } |
| 80 | ], |
| 81 | "nodes": [ |
| 82 | { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 83 | "name": "me", |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 84 | "hostname": "myhostname" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 85 | }, |
| 86 | { |
| 87 | "name": "them", |
| 88 | "hostname": "themhostname" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 89 | } |
| 90 | ] |
| 91 | })config"; |
| 92 | |
| 93 | flatbuffer_ = FlatbufferDetachedBuffer<Configuration>( |
| 94 | JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable())); |
| 95 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 96 | my_node_ = configuration::GetNode(&flatbuffer_.message(), my_node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 99 | const Node *my_node() const { return my_node_; } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 100 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 101 | const Configuration *configuration() { return &flatbuffer_.message(); } |
| 102 | |
| 103 | private: |
| 104 | FlatbufferDetachedBuffer<Configuration> flatbuffer_; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 105 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 106 | const Node *my_node_ = nullptr; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 109 | class AbstractEventLoopTestBase |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 110 | : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> { |
| 111 | public: |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 112 | AbstractEventLoopTestBase() { factory_.reset(GetParam()()); } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 113 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 114 | ::std::unique_ptr<EventLoop> Make(std::string_view name = "") { |
| 115 | std::string name_copy(name); |
| 116 | if (name == "") { |
| 117 | name_copy = "loop"; |
| 118 | name_copy += std::to_string(event_loop_count_); |
| 119 | } |
| 120 | ++event_loop_count_; |
| 121 | return factory_->Make(name_copy); |
| 122 | } |
| 123 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") { |
| 124 | ++event_loop_count_; |
| 125 | return factory_->MakePrimary(name); |
| 126 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 127 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 128 | void EnableNodes(std::string_view my_node) { factory_->EnableNodes(my_node); } |
| 129 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 130 | void Run() { return factory_->Run(); } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 131 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 132 | void Exit() { return factory_->Exit(); } |
| 133 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 134 | void SleepFor(::std::chrono::nanoseconds duration) { |
| 135 | return factory_->SleepFor(duration); |
| 136 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 137 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 138 | const Configuration *configuration() { return factory_->configuration(); } |
| 139 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 140 | const Node *my_node() const { return factory_->my_node(); } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 141 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 142 | // Ends the given event loop at the given time from now. |
| 143 | void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) { |
| 144 | auto end_timer = loop->AddTimer([this]() { this->Exit(); }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 145 | end_timer->Setup(loop->monotonic_now() + duration); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 146 | end_timer->set_name("end"); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 149 | // You can implement all the usual fixture class members here. |
| 150 | // To access the test parameter, call GetParam() from class |
| 151 | // TestWithParam<T>. |
| 152 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 153 | ::std::unique_ptr<EventLoopTestFactory> factory_; |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 154 | |
| 155 | int event_loop_count_ = 0; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 158 | typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest; |
| 159 | typedef AbstractEventLoopTestBase AbstractEventLoopTest; |
| 160 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 161 | } // namespace testing |
| 162 | } // namespace aos |
| 163 | |
| 164 | #endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |