blob: e987a11e6f33c2a9d193b660404c291b69fab7e7 [file] [log] [blame]
Neil Balchc8f41ed2018-01-20 22:06:53 -08001#include "aos/events/simulated-event-loop.h"
2#include "aos/events/event-loop_param_test.h"
3#include "gtest/gtest.h"
4
5namespace aos {
6namespace testing {
7
Austin Schuh7267c532019-05-19 19:55:53 -07008namespace chrono = ::std::chrono;
9
Neil Balchc8f41ed2018-01-20 22:06:53 -080010class SimulatedEventLoopTestFactory : public EventLoopTestFactory {
11 public:
Austin Schuh7267c532019-05-19 19:55:53 -070012 ::std::unique_ptr<EventLoop> Make() override {
Austin Schuh44019f92019-05-19 19:58:27 -070013 return event_loop_factory_.MakeEventLoop();
Neil Balchc8f41ed2018-01-20 22:06:53 -080014 }
Austin Schuh44019f92019-05-19 19:58:27 -070015 ::std::unique_ptr<EventLoop> MakePrimary() override {
16 return event_loop_factory_.MakeEventLoop();
17 }
18
19 void Run() override { event_loop_factory_.Run(); }
Austin Schuh9fe68f72019-08-10 19:32:03 -070020 void Exit() override { event_loop_factory_.Exit(); }
Austin Schuh44019f92019-05-19 19:58:27 -070021
Austin Schuh52d325c2019-06-23 18:59:06 -070022 // TODO(austin): Implement this. It's used currently for a phased loop test.
23 // I'm not sure how much that matters.
24 void SleepFor(::std::chrono::nanoseconds /*duration*/) override {}
25
Neil Balchc8f41ed2018-01-20 22:06:53 -080026 private:
Austin Schuh44019f92019-05-19 19:58:27 -070027 SimulatedEventLoopFactory event_loop_factory_;
Neil Balchc8f41ed2018-01-20 22:06:53 -080028};
29
Austin Schuh6b6dfa52019-06-12 20:16:20 -070030INSTANTIATE_TEST_CASE_P(SimulatedEventLoopDeathTest, AbstractEventLoopDeathTest,
31 ::testing::Values([]() {
32 return new SimulatedEventLoopTestFactory();
33 }));
34
Neil Balchc8f41ed2018-01-20 22:06:53 -080035INSTANTIATE_TEST_CASE_P(SimulatedEventLoopTest, AbstractEventLoopTest,
36 ::testing::Values([]() {
37 return new SimulatedEventLoopTestFactory();
38 }));
39
40// Test that creating an event and running the scheduler runs the event.
41TEST(EventSchedulerTest, ScheduleEvent) {
42 int counter = 0;
43 EventScheduler scheduler;
44
45 scheduler.Schedule(::aos::monotonic_clock::now(),
46 [&counter]() { counter += 1; });
47 scheduler.Run();
48 EXPECT_EQ(counter, 1);
49 auto token = scheduler.Schedule(::aos::monotonic_clock::now(),
50 [&counter]() { counter += 1; });
51 scheduler.Deschedule(token);
52 scheduler.Run();
53 EXPECT_EQ(counter, 1);
54}
55
56// Test that descheduling an already scheduled event doesn't run the event.
57TEST(EventSchedulerTest, DescheduleEvent) {
58 int counter = 0;
59 EventScheduler scheduler;
60
61 auto token = scheduler.Schedule(::aos::monotonic_clock::now(),
62 [&counter]() { counter += 1; });
63 scheduler.Deschedule(token);
64 scheduler.Run();
65 EXPECT_EQ(counter, 0);
66}
Austin Schuh44019f92019-05-19 19:58:27 -070067
68// Test that running for a time period with no handlers causes time to progress
69// correctly.
70TEST(SimulatedEventLoopTest, RunForNoHandlers) {
71 SimulatedEventLoopFactory simulated_event_loop_factory;
72 ::std::unique_ptr<EventLoop> event_loop =
73 simulated_event_loop_factory.MakeEventLoop();
74
75 simulated_event_loop_factory.RunFor(chrono::seconds(1));
76
77 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
78 simulated_event_loop_factory.monotonic_now());
79 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
80 event_loop->monotonic_now());
81}
82
83// Test that running for a time with a periodic handler causes time to end
84// correctly.
85TEST(SimulatedEventLoopTest, RunForTimerHandler) {
86 SimulatedEventLoopFactory simulated_event_loop_factory;
87 ::std::unique_ptr<EventLoop> event_loop =
88 simulated_event_loop_factory.MakeEventLoop();
89
90 int counter = 0;
91 auto timer = event_loop->AddTimer([&counter, &event_loop]() { ++counter; });
92 event_loop->OnRun([&event_loop, &timer] {
93 timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50),
94 chrono::milliseconds(100));
95 });
96
97 simulated_event_loop_factory.RunFor(chrono::seconds(1));
98
99 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
100 simulated_event_loop_factory.monotonic_now());
101 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
102 event_loop->monotonic_now());
103 EXPECT_EQ(counter, 10);
104}
105
Neil Balchc8f41ed2018-01-20 22:06:53 -0800106} // namespace testing
107} // namespace aos