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 | |
| 57 | const Configuration *configuration() { return &flatbuffer_.message(); } |
| 58 | |
| 59 | private: |
| 60 | FlatbufferDetachedBuffer<Configuration> flatbuffer_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 63 | class AbstractEventLoopTestBase |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 64 | : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> { |
| 65 | public: |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 66 | AbstractEventLoopTestBase() { factory_.reset(GetParam()()); } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 67 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 68 | ::std::unique_ptr<EventLoop> Make(std::string_view name = "") { |
| 69 | std::string name_copy(name); |
| 70 | if (name == "") { |
| 71 | name_copy = "loop"; |
| 72 | name_copy += std::to_string(event_loop_count_); |
| 73 | } |
| 74 | ++event_loop_count_; |
| 75 | return factory_->Make(name_copy); |
| 76 | } |
| 77 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") { |
| 78 | ++event_loop_count_; |
| 79 | return factory_->MakePrimary(name); |
| 80 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 81 | |
| 82 | void Run() { return factory_->Run(); } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 83 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 84 | void Exit() { return factory_->Exit(); } |
| 85 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 86 | void SleepFor(::std::chrono::nanoseconds duration) { |
| 87 | return factory_->SleepFor(duration); |
| 88 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 89 | |
| 90 | // Ends the given event loop at the given time from now. |
| 91 | void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) { |
| 92 | auto end_timer = loop->AddTimer([this]() { this->Exit(); }); |
| 93 | end_timer->Setup(loop->monotonic_now() + |
| 94 | ::std::chrono::milliseconds(duration)); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 95 | end_timer->set_name("end"); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 98 | // You can implement all the usual fixture class members here. |
| 99 | // To access the test parameter, call GetParam() from class |
| 100 | // TestWithParam<T>. |
| 101 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 102 | ::std::unique_ptr<EventLoopTestFactory> factory_; |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 103 | |
| 104 | int event_loop_count_ = 0; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 107 | typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest; |
| 108 | typedef AbstractEventLoopTestBase AbstractEventLoopTest; |
| 109 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 110 | } // namespace testing |
| 111 | } // namespace aos |
| 112 | |
| 113 | #endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |