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