blob: b69f28a57215597a729a3754fb1f209f36ccc8b5 [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"
Austin Schuh39788ff2019-12-01 18:22:57 -080021 " \"name\": \"/aos\",\n"
22 " \"type\": \"aos.timing.Report\"\n"
23 " },\n"
24 " {\n"
Alex Perrycb7da4b2019-08-28 19:35:56 -070025 " \"name\": \"/test\",\n"
26 " \"type\": \"aos.TestMessage\"\n"
27 " },\n"
28 " {\n"
29 " \"name\": \"/test1\",\n"
30 " \"type\": \"aos.TestMessage\"\n"
31 " },\n"
32 " {\n"
33 " \"name\": \"/test2\",\n"
34 " \"type\": \"aos.TestMessage\"\n"
35 " }\n"
36 " ]\n"
37 "}\n",
38 Configuration::MiniReflectTypeTable())) {}
39
Parker Schuhe4a70d62017-12-27 20:10:20 -080040 virtual ~EventLoopTestFactory() {}
41
Austin Schuh44019f92019-05-19 19:58:27 -070042 // Makes a connected event loop.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080043 virtual std::unique_ptr<EventLoop> Make(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070044 // Makes a primary event loop. This is the one the tests will try to use for
45 // anything blocking.
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080046 virtual std::unique_ptr<EventLoop> MakePrimary(std::string_view name) = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070047
48 // Runs the loops until they quit.
49 virtual void Run() = 0;
Austin Schuh52d325c2019-06-23 18:59:06 -070050
Austin Schuh9fe68f72019-08-10 19:32:03 -070051 // Quits the loops.
52 virtual void Exit() = 0;
53
Austin Schuh52d325c2019-06-23 18:59:06 -070054 // Advances time by sleeping. Can't be called from inside a loop.
55 virtual void SleepFor(::std::chrono::nanoseconds duration) = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -070056
57 const Configuration *configuration() { return &flatbuffer_.message(); }
58
59 private:
60 FlatbufferDetachedBuffer<Configuration> flatbuffer_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080061};
62
Austin Schuh6b6dfa52019-06-12 20:16:20 -070063class AbstractEventLoopTestBase
Parker Schuhe4a70d62017-12-27 20:10:20 -080064 : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> {
65 public:
Austin Schuh6b6dfa52019-06-12 20:16:20 -070066 AbstractEventLoopTestBase() { factory_.reset(GetParam()()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -080067
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080068 ::std::unique_ptr<EventLoop> Make(std::string_view name = "") {
69 std::string name_copy(name);
70 if (name == "") {
71 name_copy = "loop";
72 name_copy += std::to_string(event_loop_count_);
73 }
74 ++event_loop_count_;
75 return factory_->Make(name_copy);
76 }
77 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") {
78 ++event_loop_count_;
79 return factory_->MakePrimary(name);
80 }
Austin Schuh44019f92019-05-19 19:58:27 -070081
82 void Run() { return factory_->Run(); }
Austin Schuh52d325c2019-06-23 18:59:06 -070083
Austin Schuh9fe68f72019-08-10 19:32:03 -070084 void Exit() { return factory_->Exit(); }
85
Austin Schuh52d325c2019-06-23 18:59:06 -070086 void SleepFor(::std::chrono::nanoseconds duration) {
87 return factory_->SleepFor(duration);
88 }
Austin Schuh9fe68f72019-08-10 19:32:03 -070089
90 // Ends the given event loop at the given time from now.
91 void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) {
92 auto end_timer = loop->AddTimer([this]() { this->Exit(); });
93 end_timer->Setup(loop->monotonic_now() +
94 ::std::chrono::milliseconds(duration));
Austin Schuh39788ff2019-12-01 18:22:57 -080095 end_timer->set_name("end");
Austin Schuh9fe68f72019-08-10 19:32:03 -070096 }
97
Parker Schuhe4a70d62017-12-27 20:10:20 -080098 // You can implement all the usual fixture class members here.
99 // To access the test parameter, call GetParam() from class
100 // TestWithParam<T>.
101 private:
Austin Schuh44019f92019-05-19 19:58:27 -0700102 ::std::unique_ptr<EventLoopTestFactory> factory_;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800103
104 int event_loop_count_ = 0;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800105};
106
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700107typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest;
108typedef AbstractEventLoopTestBase AbstractEventLoopTest;
109
Parker Schuhe4a70d62017-12-27 20:10:20 -0800110} // namespace testing
111} // namespace aos
112
113#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_