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 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 4 | #include <vector> |
| 5 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 6 | #include "aos/events/event-loop.h" |
| 7 | #include "gtest/gtest.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | |
| 12 | class EventLoopTestFactory { |
| 13 | public: |
| 14 | virtual ~EventLoopTestFactory() {} |
| 15 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 16 | // Makes a connected event loop. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 17 | virtual std::unique_ptr<EventLoop> Make() = 0; |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 18 | // Makes a primary event loop. This is the one the tests will try to use for |
| 19 | // anything blocking. |
| 20 | virtual std::unique_ptr<EventLoop> MakePrimary() = 0; |
| 21 | |
| 22 | // Runs the loops until they quit. |
| 23 | virtual void Run() = 0; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame^] | 24 | |
| 25 | // Advances time by sleeping. Can't be called from inside a loop. |
| 26 | virtual void SleepFor(::std::chrono::nanoseconds duration) = 0; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 29 | class AbstractEventLoopTestBase |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 30 | : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> { |
| 31 | public: |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 32 | AbstractEventLoopTestBase() { factory_.reset(GetParam()()); } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 34 | ::std::unique_ptr<EventLoop> Make() { return factory_->Make(); } |
| 35 | ::std::unique_ptr<EventLoop> MakePrimary() { return factory_->MakePrimary(); } |
| 36 | |
| 37 | void Run() { return factory_->Run(); } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame^] | 38 | |
| 39 | void SleepFor(::std::chrono::nanoseconds duration) { |
| 40 | return factory_->SleepFor(duration); |
| 41 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 42 | // You can implement all the usual fixture class members here. |
| 43 | // To access the test parameter, call GetParam() from class |
| 44 | // TestWithParam<T>. |
| 45 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 46 | ::std::unique_ptr<EventLoopTestFactory> factory_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 49 | typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest; |
| 50 | typedef AbstractEventLoopTestBase AbstractEventLoopTest; |
| 51 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 52 | } // namespace testing |
| 53 | } // namespace aos |
| 54 | |
| 55 | #endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |