Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 1 | #include "aos/events/simulated-event-loop.h" |
| 2 | #include "aos/events/event-loop_param_test.h" |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | namespace aos { |
| 6 | namespace testing { |
| 7 | |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 8 | namespace chrono = ::std::chrono; |
| 9 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 10 | class SimulatedEventLoopTestFactory : public EventLoopTestFactory { |
| 11 | public: |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 12 | ::std::unique_ptr<EventLoop> Make() override { |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 13 | return event_loop_factory_.MakeEventLoop(); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 14 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 15 | ::std::unique_ptr<EventLoop> MakePrimary() override { |
| 16 | return event_loop_factory_.MakeEventLoop(); |
| 17 | } |
| 18 | |
| 19 | void Run() override { event_loop_factory_.Run(); } |
| 20 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame^] | 21 | // 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 Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 25 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 26 | SimulatedEventLoopFactory event_loop_factory_; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 29 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopDeathTest, AbstractEventLoopDeathTest, |
| 30 | ::testing::Values([]() { |
| 31 | return new SimulatedEventLoopTestFactory(); |
| 32 | })); |
| 33 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 34 | INSTANTIATE_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. |
| 40 | TEST(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. |
| 56 | TEST(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 Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 66 | |
| 67 | // Test that running for a time period with no handlers causes time to progress |
| 68 | // correctly. |
| 69 | TEST(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. |
| 84 | TEST(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 Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 105 | } // namespace testing |
| 106 | } // namespace aos |