blob: 4f24d781079bca8c74c74cd4eaa8eb35576e67f3 [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
Austin Schuh217a9782019-12-21 23:02:50 -080057 void EnableNodes(std::string_view my_node) {
58 std::string json = std::string(R"config({
59 "channels": [
60 {
61 "name": "/aos",
62 "type": "aos.timing.Report",
63 "source_node": "me"
64 },
65 {
66 "name": "/test",
67 "type": "aos.TestMessage",
68 "source_node": "me"
69 },
70 {
71 "name": "/test1",
72 "type": "aos.TestMessage",
73 "source_node": "me"
74 },
75 {
76 "name": "/test2",
77 "type": "aos.TestMessage",
78 "source_node": "me"
79 }
80 ],
81 "nodes": [
82 {
83 "name": ")config") +
84 std::string(my_node) + R"config(",
85 "hostname": "myhostname"
86 }
87 ]
88})config";
89
90 flatbuffer_ = FlatbufferDetachedBuffer<Configuration>(
91 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable()));
92
Austin Schuhac0771c2020-01-07 18:36:30 -080093 my_node_ = configuration::GetNode(&flatbuffer_.message(), my_node);
Austin Schuh217a9782019-12-21 23:02:50 -080094 }
95
Austin Schuhac0771c2020-01-07 18:36:30 -080096 const Node *my_node() const { return my_node_; }
Austin Schuh217a9782019-12-21 23:02:50 -080097
Alex Perrycb7da4b2019-08-28 19:35:56 -070098 const Configuration *configuration() { return &flatbuffer_.message(); }
99
100 private:
101 FlatbufferDetachedBuffer<Configuration> flatbuffer_;
Austin Schuh217a9782019-12-21 23:02:50 -0800102
Austin Schuhac0771c2020-01-07 18:36:30 -0800103 const Node *my_node_ = nullptr;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800104};
105
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700106class AbstractEventLoopTestBase
Parker Schuhe4a70d62017-12-27 20:10:20 -0800107 : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> {
108 public:
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700109 AbstractEventLoopTestBase() { factory_.reset(GetParam()()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -0800110
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800111 ::std::unique_ptr<EventLoop> Make(std::string_view name = "") {
112 std::string name_copy(name);
113 if (name == "") {
114 name_copy = "loop";
115 name_copy += std::to_string(event_loop_count_);
116 }
117 ++event_loop_count_;
118 return factory_->Make(name_copy);
119 }
120 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") {
121 ++event_loop_count_;
122 return factory_->MakePrimary(name);
123 }
Austin Schuh44019f92019-05-19 19:58:27 -0700124
Austin Schuh217a9782019-12-21 23:02:50 -0800125 void EnableNodes(std::string_view my_node) { factory_->EnableNodes(my_node); }
126
Austin Schuh44019f92019-05-19 19:58:27 -0700127 void Run() { return factory_->Run(); }
Austin Schuh52d325c2019-06-23 18:59:06 -0700128
Austin Schuh9fe68f72019-08-10 19:32:03 -0700129 void Exit() { return factory_->Exit(); }
130
Austin Schuh52d325c2019-06-23 18:59:06 -0700131 void SleepFor(::std::chrono::nanoseconds duration) {
132 return factory_->SleepFor(duration);
133 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700134
Austin Schuh217a9782019-12-21 23:02:50 -0800135 const Configuration *configuration() { return factory_->configuration(); }
136
Austin Schuhac0771c2020-01-07 18:36:30 -0800137 const Node *my_node() const { return factory_->my_node(); }
Austin Schuh217a9782019-12-21 23:02:50 -0800138
Austin Schuh9fe68f72019-08-10 19:32:03 -0700139 // Ends the given event loop at the given time from now.
140 void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) {
141 auto end_timer = loop->AddTimer([this]() { this->Exit(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800142 end_timer->Setup(loop->monotonic_now() + duration);
Austin Schuh39788ff2019-12-01 18:22:57 -0800143 end_timer->set_name("end");
Austin Schuh9fe68f72019-08-10 19:32:03 -0700144 }
145
Parker Schuhe4a70d62017-12-27 20:10:20 -0800146 // You can implement all the usual fixture class members here.
147 // To access the test parameter, call GetParam() from class
148 // TestWithParam<T>.
149 private:
Austin Schuh44019f92019-05-19 19:58:27 -0700150 ::std::unique_ptr<EventLoopTestFactory> factory_;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800151
152 int event_loop_count_ = 0;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800153};
154
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700155typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest;
156typedef AbstractEventLoopTestBase AbstractEventLoopTest;
157
Parker Schuhe4a70d62017-12-27 20:10:20 -0800158} // namespace testing
159} // namespace aos
160
161#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_