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" |
| 21 | " \"name\": \"/test\",\n" |
| 22 | " \"type\": \"aos.TestMessage\"\n" |
| 23 | " },\n" |
| 24 | " {\n" |
| 25 | " \"name\": \"/test1\",\n" |
| 26 | " \"type\": \"aos.TestMessage\"\n" |
| 27 | " },\n" |
| 28 | " {\n" |
| 29 | " \"name\": \"/test2\",\n" |
| 30 | " \"type\": \"aos.TestMessage\"\n" |
| 31 | " }\n" |
| 32 | " ]\n" |
| 33 | "}\n", |
| 34 | Configuration::MiniReflectTypeTable())) {} |
| 35 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 36 | virtual ~EventLoopTestFactory() {} |
| 37 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 38 | // Makes a connected event loop. |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 39 | virtual std::unique_ptr<EventLoop> Make(std::string_view name) = 0; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 40 | // Makes a primary event loop. This is the one the tests will try to use for |
| 41 | // anything blocking. |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 42 | virtual std::unique_ptr<EventLoop> MakePrimary(std::string_view name) = 0; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 43 | |
| 44 | // Runs the loops until they quit. |
| 45 | virtual void Run() = 0; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 46 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 47 | // Quits the loops. |
| 48 | virtual void Exit() = 0; |
| 49 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 50 | // Advances time by sleeping. Can't be called from inside a loop. |
| 51 | virtual void SleepFor(::std::chrono::nanoseconds duration) = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 52 | |
| 53 | const Configuration *configuration() { return &flatbuffer_.message(); } |
| 54 | |
| 55 | private: |
| 56 | FlatbufferDetachedBuffer<Configuration> flatbuffer_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 59 | class AbstractEventLoopTestBase |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 60 | : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> { |
| 61 | public: |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 62 | AbstractEventLoopTestBase() { factory_.reset(GetParam()()); } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 63 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 64 | ::std::unique_ptr<EventLoop> Make(std::string_view name = "") { |
| 65 | std::string name_copy(name); |
| 66 | if (name == "") { |
| 67 | name_copy = "loop"; |
| 68 | name_copy += std::to_string(event_loop_count_); |
| 69 | } |
| 70 | ++event_loop_count_; |
| 71 | return factory_->Make(name_copy); |
| 72 | } |
| 73 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") { |
| 74 | ++event_loop_count_; |
| 75 | return factory_->MakePrimary(name); |
| 76 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 77 | |
| 78 | void Run() { return factory_->Run(); } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 79 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 80 | void Exit() { return factory_->Exit(); } |
| 81 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 82 | void SleepFor(::std::chrono::nanoseconds duration) { |
| 83 | return factory_->SleepFor(duration); |
| 84 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 85 | |
| 86 | // Ends the given event loop at the given time from now. |
| 87 | void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) { |
| 88 | auto end_timer = loop->AddTimer([this]() { this->Exit(); }); |
| 89 | end_timer->Setup(loop->monotonic_now() + |
| 90 | ::std::chrono::milliseconds(duration)); |
| 91 | } |
| 92 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 93 | // You can implement all the usual fixture class members here. |
| 94 | // To access the test parameter, call GetParam() from class |
| 95 | // TestWithParam<T>. |
| 96 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 97 | ::std::unique_ptr<EventLoopTestFactory> factory_; |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 98 | |
| 99 | int event_loop_count_ = 0; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 102 | typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest; |
| 103 | typedef AbstractEventLoopTestBase AbstractEventLoopTest; |
| 104 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 105 | } // namespace testing |
| 106 | } // namespace aos |
| 107 | |
| 108 | #endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |