blob: 83f0b372a28b3977bf3cfe4d52b6d606337ede96 [file] [log] [blame]
Parker Schuhe4a70d62017-12-27 20:10:20 -08001#ifndef _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_
2#define _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_
3
Neil Balch229001a2018-01-07 18:22:52 -08004#include <vector>
5
Parker Schuhe4a70d62017-12-27 20:10:20 -08006#include "aos/events/event-loop.h"
7#include "gtest/gtest.h"
8
9namespace aos {
10namespace testing {
11
12class EventLoopTestFactory {
13 public:
14 virtual ~EventLoopTestFactory() {}
15
Austin Schuh44019f92019-05-19 19:58:27 -070016 // Makes a connected event loop.
Parker Schuhe4a70d62017-12-27 20:10:20 -080017 virtual std::unique_ptr<EventLoop> Make() = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070018 // 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 Schuh52d325c2019-06-23 18:59:06 -070024
Austin Schuh9fe68f72019-08-10 19:32:03 -070025 // Quits the loops.
26 virtual void Exit() = 0;
27
Austin Schuh52d325c2019-06-23 18:59:06 -070028 // Advances time by sleeping. Can't be called from inside a loop.
29 virtual void SleepFor(::std::chrono::nanoseconds duration) = 0;
Parker Schuhe4a70d62017-12-27 20:10:20 -080030};
31
Austin Schuh6b6dfa52019-06-12 20:16:20 -070032class AbstractEventLoopTestBase
Parker Schuhe4a70d62017-12-27 20:10:20 -080033 : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> {
34 public:
Austin Schuh6b6dfa52019-06-12 20:16:20 -070035 AbstractEventLoopTestBase() { factory_.reset(GetParam()()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -080036
Austin Schuh44019f92019-05-19 19:58:27 -070037 ::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 Schuh52d325c2019-06-23 18:59:06 -070041
Austin Schuh9fe68f72019-08-10 19:32:03 -070042 void Exit() { return factory_->Exit(); }
43
Austin Schuh52d325c2019-06-23 18:59:06 -070044 void SleepFor(::std::chrono::nanoseconds duration) {
45 return factory_->SleepFor(duration);
46 }
Austin Schuh9fe68f72019-08-10 19:32:03 -070047
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 Schuhe4a70d62017-12-27 20:10:20 -080055 // 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 Schuh44019f92019-05-19 19:58:27 -070059 ::std::unique_ptr<EventLoopTestFactory> factory_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080060};
61
Austin Schuh6b6dfa52019-06-12 20:16:20 -070062typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest;
63typedef AbstractEventLoopTestBase AbstractEventLoopTest;
64
Parker Schuhe4a70d62017-12-27 20:10:20 -080065} // namespace testing
66} // namespace aos
67
68#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_