blob: 8a8f33ae7f75bb5db2eae107686f713cdefbc206 [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
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08004#include <string_view>
Neil Balch229001a2018-01-07 18:22:52 -08005#include <vector>
6
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/events/event_loop.h"
8#include "aos/flatbuffers.h"
9#include "aos/json_to_flatbuffer.h"
Parker Schuhe4a70d62017-12-27 20:10:20 -080010#include "gtest/gtest.h"
11
12namespace aos {
13namespace testing {
14
15class EventLoopTestFactory {
16 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070017 EventLoopTestFactory()
18 : flatbuffer_(JsonToFlatbuffer("{\n"
19 " \"channels\": [ \n"
20 " {\n"
21 " \"name\": \"/test\",\n"
22 " \"type\": \"aos.TestMessage\"\n"
23 " },\n"
24 " {\n"
25 " \"name\": \"/test1\",\n"
26 " \"type\": \"aos.TestMessage\"\n"
27 " },\n"
28 " {\n"
29 " \"name\": \"/test2\",\n"
30 " \"type\": \"aos.TestMessage\"\n"
31 " }\n"
32 " ]\n"
33 "}\n",
34 Configuration::MiniReflectTypeTable())) {}
35
Parker Schuhe4a70d62017-12-27 20:10:20 -080036 virtual ~EventLoopTestFactory() {}
37
Austin Schuh44019f92019-05-19 19:58:27 -070038 // Makes a connected event loop.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080039 virtual std::unique_ptr<EventLoop> Make(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070040 // Makes a primary event loop. This is the one the tests will try to use for
41 // anything blocking.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080042 virtual std::unique_ptr<EventLoop> MakePrimary(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070043
44 // Runs the loops until they quit.
45 virtual void Run() = 0;
Austin Schuh52d325c2019-06-23 18:59:06 -070046
Austin Schuh9fe68f72019-08-10 19:32:03 -070047 // Quits the loops.
48 virtual void Exit() = 0;
49
Austin Schuh52d325c2019-06-23 18:59:06 -070050 // Advances time by sleeping. Can't be called from inside a loop.
51 virtual void SleepFor(::std::chrono::nanoseconds duration) = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -070052
53 const Configuration *configuration() { return &flatbuffer_.message(); }
54
55 private:
56 FlatbufferDetachedBuffer<Configuration> flatbuffer_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080057};
58
Austin Schuh6b6dfa52019-06-12 20:16:20 -070059class AbstractEventLoopTestBase
Parker Schuhe4a70d62017-12-27 20:10:20 -080060 : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> {
61 public:
Austin Schuh6b6dfa52019-06-12 20:16:20 -070062 AbstractEventLoopTestBase() { factory_.reset(GetParam()()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -080063
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080064 ::std::unique_ptr<EventLoop> Make(std::string_view name = "") {
65 std::string name_copy(name);
66 if (name == "") {
67 name_copy = "loop";
68 name_copy += std::to_string(event_loop_count_);
69 }
70 ++event_loop_count_;
71 return factory_->Make(name_copy);
72 }
73 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") {
74 ++event_loop_count_;
75 return factory_->MakePrimary(name);
76 }
Austin Schuh44019f92019-05-19 19:58:27 -070077
78 void Run() { return factory_->Run(); }
Austin Schuh52d325c2019-06-23 18:59:06 -070079
Austin Schuh9fe68f72019-08-10 19:32:03 -070080 void Exit() { return factory_->Exit(); }
81
Austin Schuh52d325c2019-06-23 18:59:06 -070082 void SleepFor(::std::chrono::nanoseconds duration) {
83 return factory_->SleepFor(duration);
84 }
Austin Schuh9fe68f72019-08-10 19:32:03 -070085
86 // Ends the given event loop at the given time from now.
87 void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) {
88 auto end_timer = loop->AddTimer([this]() { this->Exit(); });
89 end_timer->Setup(loop->monotonic_now() +
90 ::std::chrono::milliseconds(duration));
91 }
92
Parker Schuhe4a70d62017-12-27 20:10:20 -080093 // You can implement all the usual fixture class members here.
94 // To access the test parameter, call GetParam() from class
95 // TestWithParam<T>.
96 private:
Austin Schuh44019f92019-05-19 19:58:27 -070097 ::std::unique_ptr<EventLoopTestFactory> factory_;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080098
99 int event_loop_count_ = 0;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800100};
101
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700102typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest;
103typedef AbstractEventLoopTestBase AbstractEventLoopTest;
104
Parker Schuhe4a70d62017-12-27 20:10:20 -0800105} // namespace testing
106} // namespace aos
107
108#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_