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