blob: fb08ac34fbf23bbdb8d395b50d1f2599ab20e8e3 [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
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include "aos/events/event_loop.h"
7#include "aos/flatbuffers.h"
8#include "aos/json_to_flatbuffer.h"
Parker Schuhe4a70d62017-12-27 20:10:20 -08009#include "gtest/gtest.h"
10
11namespace aos {
12namespace testing {
13
14class EventLoopTestFactory {
15 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070016 EventLoopTestFactory()
17 : flatbuffer_(JsonToFlatbuffer("{\n"
18 " \"channels\": [ \n"
19 " {\n"
20 " \"name\": \"/test\",\n"
21 " \"type\": \"aos.TestMessage\"\n"
22 " },\n"
23 " {\n"
24 " \"name\": \"/test1\",\n"
25 " \"type\": \"aos.TestMessage\"\n"
26 " },\n"
27 " {\n"
28 " \"name\": \"/test2\",\n"
29 " \"type\": \"aos.TestMessage\"\n"
30 " }\n"
31 " ]\n"
32 "}\n",
33 Configuration::MiniReflectTypeTable())) {}
34
Parker Schuhe4a70d62017-12-27 20:10:20 -080035 virtual ~EventLoopTestFactory() {}
36
Austin Schuh44019f92019-05-19 19:58:27 -070037 // Makes a connected event loop.
Parker Schuhe4a70d62017-12-27 20:10:20 -080038 virtual std::unique_ptr<EventLoop> Make() = 0;
Austin Schuh44019f92019-05-19 19:58:27 -070039 // Makes a primary event loop. This is the one the tests will try to use for
40 // anything blocking.
41 virtual std::unique_ptr<EventLoop> MakePrimary() = 0;
42
43 // Runs the loops until they quit.
44 virtual void Run() = 0;
Austin Schuh52d325c2019-06-23 18:59:06 -070045
Austin Schuh9fe68f72019-08-10 19:32:03 -070046 // Quits the loops.
47 virtual void Exit() = 0;
48
Austin Schuh52d325c2019-06-23 18:59:06 -070049 // Advances time by sleeping. Can't be called from inside a loop.
50 virtual void SleepFor(::std::chrono::nanoseconds duration) = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -070051
52 const Configuration *configuration() { return &flatbuffer_.message(); }
53
54 private:
55 FlatbufferDetachedBuffer<Configuration> flatbuffer_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080056};
57
Austin Schuh6b6dfa52019-06-12 20:16:20 -070058class AbstractEventLoopTestBase
Parker Schuhe4a70d62017-12-27 20:10:20 -080059 : public ::testing::TestWithParam<std::function<EventLoopTestFactory *()>> {
60 public:
Austin Schuh6b6dfa52019-06-12 20:16:20 -070061 AbstractEventLoopTestBase() { factory_.reset(GetParam()()); }
Parker Schuhe4a70d62017-12-27 20:10:20 -080062
Austin Schuh44019f92019-05-19 19:58:27 -070063 ::std::unique_ptr<EventLoop> Make() { return factory_->Make(); }
64 ::std::unique_ptr<EventLoop> MakePrimary() { return factory_->MakePrimary(); }
65
66 void Run() { return factory_->Run(); }
Austin Schuh52d325c2019-06-23 18:59:06 -070067
Austin Schuh9fe68f72019-08-10 19:32:03 -070068 void Exit() { return factory_->Exit(); }
69
Austin Schuh52d325c2019-06-23 18:59:06 -070070 void SleepFor(::std::chrono::nanoseconds duration) {
71 return factory_->SleepFor(duration);
72 }
Austin Schuh9fe68f72019-08-10 19:32:03 -070073
74 // Ends the given event loop at the given time from now.
75 void EndEventLoop(EventLoop *loop, ::std::chrono::milliseconds duration) {
76 auto end_timer = loop->AddTimer([this]() { this->Exit(); });
77 end_timer->Setup(loop->monotonic_now() +
78 ::std::chrono::milliseconds(duration));
79 }
80
Parker Schuhe4a70d62017-12-27 20:10:20 -080081 // You can implement all the usual fixture class members here.
82 // To access the test parameter, call GetParam() from class
83 // TestWithParam<T>.
84 private:
Austin Schuh44019f92019-05-19 19:58:27 -070085 ::std::unique_ptr<EventLoopTestFactory> factory_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080086};
87
Austin Schuh6b6dfa52019-06-12 20:16:20 -070088typedef AbstractEventLoopTestBase AbstractEventLoopDeathTest;
89typedef AbstractEventLoopTestBase AbstractEventLoopTest;
90
Parker Schuhe4a70d62017-12-27 20:10:20 -080091} // namespace testing
92} // namespace aos
93
94#endif // _AOS_EVENTS_EVENT_LOOP_PARAM_TEST_H_