blob: 89e80aea27afa5c4b6ce04daa6c203359a249efd [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) {
Austin Schuh898f4972020-01-11 17:21:25 -080058 std::string json = R"config({
Austin Schuh217a9782019-12-21 23:02:50 -080059 "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 {
Austin Schuh898f4972020-01-11 17:21:25 -080083 "name": "me",
Austin Schuh217a9782019-12-21 23:02:50 -080084 "hostname": "myhostname"
Austin Schuh898f4972020-01-11 17:21:25 -080085 },
86 {
87 "name": "them",
88 "hostname": "themhostname"
Austin Schuh217a9782019-12-21 23:02:50 -080089 }
90 ]
91})config";
92
93 flatbuffer_ = FlatbufferDetachedBuffer<Configuration>(
94 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable()));
95
Austin Schuhac0771c2020-01-07 18:36:30 -080096 my_node_ = configuration::GetNode(&flatbuffer_.message(), my_node);
Austin Schuh217a9782019-12-21 23:02:50 -080097 }
98
Austin Schuhac0771c2020-01-07 18:36:30 -080099 const Node *my_node() const { return my_node_; }
Austin Schuh217a9782019-12-21 23:02:50 -0800100
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 const Configuration *configuration() { return &flatbuffer_.message(); }
102
103 private:
104 FlatbufferDetachedBuffer<Configuration> flatbuffer_;
Austin Schuh217a9782019-12-21 23:02:50 -0800105
Austin Schuhac0771c2020-01-07 18:36:30 -0800106 const Node *my_node_ = nullptr;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800107};
108
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700109class AbstractEventLoopTestBase
Parker Schuhe4a70d62017-12-27 20:10:20 -0800110 : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> {
111 public:
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700112 AbstractEventLoopTestBase() { factory_.reset(GetParam()()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -0800113
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800114 ::std::unique_ptr<EventLoop> Make(std::string_view name = "") {
115 std::string name_copy(name);
116 if (name == "") {
117 name_copy = "loop";
118 name_copy += std::to_string(event_loop_count_);
119 }
120 ++event_loop_count_;
121 return factory_->Make(name_copy);
122 }
123 ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name = "primary") {
124 ++event_loop_count_;
125 return factory_->MakePrimary(name);
126 }
Austin Schuh44019f92019-05-19 19:58:27 -0700127
Austin Schuh217a9782019-12-21 23:02:50 -0800128 void EnableNodes(std::string_view my_node) { factory_->EnableNodes(my_node); }
129
Austin Schuh44019f92019-05-19 19:58:27 -0700130 void Run() { return factory_->Run(); }
Austin Schuh52d325c2019-06-23 18:59:06 -0700131
Austin Schuh9fe68f72019-08-10 19:32:03 -0700132 void Exit() { return factory_->Exit(); }
133
Austin Schuh52d325c2019-06-23 18:59:06 -0700134 void SleepFor(::std::chrono::nanoseconds duration) {
135 return factory_->SleepFor(duration);
136 }
Austin Schuh9fe68f72019-08-10 19:32:03 -0700137
Austin Schuh217a9782019-12-21 23:02:50 -0800138 const Configuration *configuration() { return factory_->configuration(); }
139
Austin Schuhac0771c2020-01-07 18:36:30 -0800140 const Node *my_node() const { return factory_->my_node(); }
Austin Schuh217a9782019-12-21 23:02:50 -0800141
Austin Schuh9fe68f72019-08-10 19:32:03 -0700142 // Ends the given event loop at the given time from now.
143 void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) {
144 auto end_timer = loop->AddTimer([this]() { this->Exit(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800145 end_timer->Setup(loop->monotonic_now() + duration);
Austin Schuh39788ff2019-12-01 18:22:57 -0800146 end_timer->set_name("end");
Austin Schuh9fe68f72019-08-10 19:32:03 -0700147 }
148
Parker Schuhe4a70d62017-12-27 20:10:20 -0800149 // You can implement all the usual fixture class members here.
150 // To access the test parameter, call GetParam() from class
151 // TestWithParam<T>.
152 private:
Austin Schuh44019f92019-05-19 19:58:27 -0700153 ::std::unique_ptr<EventLoopTestFactory> factory_;
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800154
155 int event_loop_count_ = 0;
Parker Schuhe4a70d62017-12-27 20:10:20 -0800156};
157
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700158typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest;
159typedef AbstractEventLoopTestBase AbstractEventLoopTest;
160
Parker Schuhe4a70d62017-12-27 20:10:20 -0800161} // namespace testing
162} // namespace aos
163
164#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_