blob: 26d869dc34dd9a369800d1b30ac39cedd48c8a27 [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
25 // Advances time by sleeping. Can't be called from inside a loop.
26 virtual void SleepFor(::std::chrono::nanoseconds duration) = 0;
Parker Schuhe4a70d62017-12-27 20:10:20 -080027};
28
Austin Schuh6b6dfa52019-06-12 20:16:20 -070029class AbstractEventLoopTestBase
Parker Schuhe4a70d62017-12-27 20:10:20 -080030 : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> {
31 public:
Austin Schuh6b6dfa52019-06-12 20:16:20 -070032 AbstractEventLoopTestBase() { factory_.reset(GetParam()()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -080033
Austin Schuh44019f92019-05-19 19:58:27 -070034 ::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 Schuh52d325c2019-06-23 18:59:06 -070038
39 void SleepFor(::std::chrono::nanoseconds duration) {
40 return factory_->SleepFor(duration);
41 }
Parker Schuhe4a70d62017-12-27 20:10:20 -080042 // 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 Schuh44019f92019-05-19 19:58:27 -070046 ::std::unique_ptr<EventLoopTestFactory> factory_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080047};
48
Austin Schuh6b6dfa52019-06-12 20:16:20 -070049typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest;
50typedef AbstractEventLoopTestBase AbstractEventLoopTest;
51
Parker Schuhe4a70d62017-12-27 20:10:20 -080052} // namespace testing
53} // namespace aos
54
55#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_