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 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 21 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 22 | SimulatedEventLoopFactory event_loop_factory_; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 23 | }; |
| 24 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame^] | 25 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopDeathTest, AbstractEventLoopDeathTest, |
| 26 | ::testing::Values([]() { |
| 27 | return new SimulatedEventLoopTestFactory(); |
| 28 | })); |
| 29 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 30 | INSTANTIATE_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. |
| 36 | TEST(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. |
| 52 | TEST(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 Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 62 | |
| 63 | // Test that running for a time period with no handlers causes time to progress |
| 64 | // correctly. |
| 65 | TEST(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. |
| 80 | TEST(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 Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 101 | } // namespace testing |
| 102 | } // namespace aos |