blob: 41d4ddec550855b509def0618ae5a449cf913acb [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
Neil Balchc8f41ed2018-01-20 22:06:53 -080021 private:
Austin Schuh44019f92019-05-19 19:58:27 -070022 SimulatedEventLoopFactory event_loop_factory_;
Neil Balchc8f41ed2018-01-20 22:06:53 -080023};
24
25INSTANTIATE_TEST_CASE_P(SimulatedEventLoopTest, AbstractEventLoopTest,
26 ::testing::Values([]() {
27 return new SimulatedEventLoopTestFactory();
28 }));
29
30// Test that creating an event and running the scheduler runs the event.
31TEST(EventSchedulerTest, ScheduleEvent) {
32 int counter = 0;
33 EventScheduler scheduler;
34
35 scheduler.Schedule(::aos::monotonic_clock::now(),
36 [&counter]() { counter += 1; });
37 scheduler.Run();
38 EXPECT_EQ(counter, 1);
39 auto token = scheduler.Schedule(::aos::monotonic_clock::now(),
40 [&counter]() { counter += 1; });
41 scheduler.Deschedule(token);
42 scheduler.Run();
43 EXPECT_EQ(counter, 1);
44}
45
46// Test that descheduling an already scheduled event doesn't run the event.
47TEST(EventSchedulerTest, DescheduleEvent) {
48 int counter = 0;
49 EventScheduler scheduler;
50
51 auto token = scheduler.Schedule(::aos::monotonic_clock::now(),
52 [&counter]() { counter += 1; });
53 scheduler.Deschedule(token);
54 scheduler.Run();
55 EXPECT_EQ(counter, 0);
56}
Austin Schuh44019f92019-05-19 19:58:27 -070057
58// Test that running for a time period with no handlers causes time to progress
59// correctly.
60TEST(SimulatedEventLoopTest, RunForNoHandlers) {
61 SimulatedEventLoopFactory simulated_event_loop_factory;
62 ::std::unique_ptr<EventLoop> event_loop =
63 simulated_event_loop_factory.MakeEventLoop();
64
65 simulated_event_loop_factory.RunFor(chrono::seconds(1));
66
67 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
68 simulated_event_loop_factory.monotonic_now());
69 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
70 event_loop->monotonic_now());
71}
72
73// Test that running for a time with a periodic handler causes time to end
74// correctly.
75TEST(SimulatedEventLoopTest, RunForTimerHandler) {
76 SimulatedEventLoopFactory simulated_event_loop_factory;
77 ::std::unique_ptr<EventLoop> event_loop =
78 simulated_event_loop_factory.MakeEventLoop();
79
80 int counter = 0;
81 auto timer = event_loop->AddTimer([&counter, &event_loop]() { ++counter; });
82 event_loop->OnRun([&event_loop, &timer] {
83 timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50),
84 chrono::milliseconds(100));
85 });
86
87 simulated_event_loop_factory.RunFor(chrono::seconds(1));
88
89 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
90 simulated_event_loop_factory.monotonic_now());
91 EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
92 event_loop->monotonic_now());
93 EXPECT_EQ(counter, 10);
94}
95
Neil Balchc8f41ed2018-01-20 22:06:53 -080096} // namespace testing
97} // namespace aos