blob: be5224349bc2d129ef02ce95ae5e447c540350fb [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(); }
20
Austin Schuh52d325c2019-06-23 18:59:06 -070021 // TODO(austin): Implement this. It's used currently for a phased loop test.
22 // I'm not sure how much that matters.
23 void SleepFor(::std::chrono::nanoseconds /*duration*/) override {}
24
Neil Balchc8f41ed2018-01-20 22:06:53 -080025 private:
Austin Schuh44019f92019-05-19 19:58:27 -070026 SimulatedEventLoopFactory event_loop_factory_;
Neil Balchc8f41ed2018-01-20 22:06:53 -080027};
28
Austin Schuh6b6dfa52019-06-12 20:16:20 -070029INSTANTIATE_TEST_CASE_P(SimulatedEventLoopDeathTest, AbstractEventLoopDeathTest,
30 ::testing::Values([]() {
31 return new SimulatedEventLoopTestFactory();
32 }));
33
Neil Balchc8f41ed2018-01-20 22:06:53 -080034INSTANTIATE_TEST_CASE_P(SimulatedEventLoopTest, AbstractEventLoopTest,
35 ::testing::Values([]() {
36 return new SimulatedEventLoopTestFactory();
37 }));
38
39// Test that creating an event and running the scheduler runs the event.
40TEST(EventSchedulerTest, ScheduleEvent) {
41 int counter = 0;
42 EventScheduler scheduler;
43
44 scheduler.Schedule(::aos::monotonic_clock::now(),
45 [&counter]() { counter += 1; });
46 scheduler.Run();
47 EXPECT_EQ(counter, 1);
48 auto token = scheduler.Schedule(::aos::monotonic_clock::now(),
49 [&counter]() { counter += 1; });
50 scheduler.Deschedule(token);
51 scheduler.Run();
52 EXPECT_EQ(counter, 1);
53}
54
55// Test that descheduling an already scheduled event doesn't run the event.
56TEST(EventSchedulerTest, DescheduleEvent) {
57 int counter = 0;
58 EventScheduler scheduler;
59
60 auto token = scheduler.Schedule(::aos::monotonic_clock::now(),
61 [&counter]() { counter += 1; });
62 scheduler.Deschedule(token);
63 scheduler.Run();
64 EXPECT_EQ(counter, 0);
65}
Austin Schuh44019f92019-05-19 19:58:27 -070066
67// Test that running for a time period with no handlers causes time to progress
68// correctly.
69TEST(SimulatedEventLoopTest, RunForNoHandlers) {
70 SimulatedEventLoopFactory simulated_event_loop_factory;
71 ::std::unique_ptr<EventLoop> event_loop =
72 simulated_event_loop_factory.MakeEventLoop();
73
74 simulated_event_loop_factory.RunFor(chrono::seconds(1));
75
76 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
77 simulated_event_loop_factory.monotonic_now());
78 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
79 event_loop->monotonic_now());
80}
81
82// Test that running for a time with a periodic handler causes time to end
83// correctly.
84TEST(SimulatedEventLoopTest, RunForTimerHandler) {
85 SimulatedEventLoopFactory simulated_event_loop_factory;
86 ::std::unique_ptr<EventLoop> event_loop =
87 simulated_event_loop_factory.MakeEventLoop();
88
89 int counter = 0;
90 auto timer = event_loop->AddTimer([&counter, &event_loop]() { ++counter; });
91 event_loop->OnRun([&event_loop, &timer] {
92 timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50),
93 chrono::milliseconds(100));
94 });
95
96 simulated_event_loop_factory.RunFor(chrono::seconds(1));
97
98 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
99 simulated_event_loop_factory.monotonic_now());
100 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
101 event_loop->monotonic_now());
102 EXPECT_EQ(counter, 10);
103}
104
Neil Balchc8f41ed2018-01-20 22:06:53 -0800105} // namespace testing
106} // namespace aos