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 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame^] | 25 | // Quits the loops. |
| 26 | virtual void Exit() = 0; |
| 27 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 28 | // Advances time by sleeping. Can't be called from inside a loop. |
| 29 | virtual void SleepFor(::std::chrono::nanoseconds duration) = 0; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 30 | }; |
| 31 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 32 | class AbstractEventLoopTestBase |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 33 | : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> { |
| 34 | public: |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 35 | AbstractEventLoopTestBase() { factory_.reset(GetParam()()); } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 37 | ::std::unique_ptr<EventLoop> Make() { return factory_->Make(); } |
| 38 | ::std::unique_ptr<EventLoop> MakePrimary() { return factory_->MakePrimary(); } |
| 39 | |
| 40 | void Run() { return factory_->Run(); } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 41 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame^] | 42 | void Exit() { return factory_->Exit(); } |
| 43 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 44 | void SleepFor(::std::chrono::nanoseconds duration) { |
| 45 | return factory_->SleepFor(duration); |
| 46 | } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame^] | 47 | |
| 48 | // Ends the given event loop at the given time from now. |
| 49 | void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) { |
| 50 | auto end_timer = loop->AddTimer([this]() { this->Exit(); }); |
| 51 | end_timer->Setup(loop->monotonic_now() + |
| 52 | ::std::chrono::milliseconds(duration)); |
| 53 | } |
| 54 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 55 | // You can implement all the usual fixture class members here. |
| 56 | // To access the test parameter, call GetParam() from class |
| 57 | // TestWithParam<T>. |
| 58 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 59 | ::std::unique_ptr<EventLoopTestFactory> factory_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 62 | typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest; |
| 63 | typedef AbstractEventLoopTestBase AbstractEventLoopTest; |
| 64 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 65 | } // namespace testing |
| 66 | } // namespace aos |
| 67 | |
| 68 | #endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_ |