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 { |
| 13 | return event_loop.MakeEventLoop(); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 14 | } |
| 15 | private: |
| 16 | SimulatedEventLoopFactory event_loop; |
| 17 | }; |
| 18 | |
| 19 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopTest, AbstractEventLoopTest, |
| 20 | ::testing::Values([]() { |
| 21 | return new SimulatedEventLoopTestFactory(); |
| 22 | })); |
| 23 | |
| 24 | // Test that creating an event and running the scheduler runs the event. |
| 25 | TEST(EventSchedulerTest, ScheduleEvent) { |
| 26 | int counter = 0; |
| 27 | EventScheduler scheduler; |
| 28 | |
| 29 | scheduler.Schedule(::aos::monotonic_clock::now(), |
| 30 | [&counter]() { counter += 1; }); |
| 31 | scheduler.Run(); |
| 32 | EXPECT_EQ(counter, 1); |
| 33 | auto token = scheduler.Schedule(::aos::monotonic_clock::now(), |
| 34 | [&counter]() { counter += 1; }); |
| 35 | scheduler.Deschedule(token); |
| 36 | scheduler.Run(); |
| 37 | EXPECT_EQ(counter, 1); |
| 38 | } |
| 39 | |
| 40 | // Test that descheduling an already scheduled event doesn't run the event. |
| 41 | TEST(EventSchedulerTest, DescheduleEvent) { |
| 42 | int counter = 0; |
| 43 | EventScheduler scheduler; |
| 44 | |
| 45 | auto token = scheduler.Schedule(::aos::monotonic_clock::now(), |
| 46 | [&counter]() { counter += 1; }); |
| 47 | scheduler.Deschedule(token); |
| 48 | scheduler.Run(); |
| 49 | EXPECT_EQ(counter, 0); |
| 50 | } |
| 51 | } // namespace testing |
| 52 | } // namespace aos |