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 | |
| 8 | class SimulatedEventLoopTestFactory : public EventLoopTestFactory { |
| 9 | public: |
| 10 | std::unique_ptr<EventLoop> Make() override { |
| 11 | return event_loop.CreateEventLoop(); |
| 12 | } |
| 13 | private: |
| 14 | SimulatedEventLoopFactory event_loop; |
| 15 | }; |
| 16 | |
| 17 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopTest, AbstractEventLoopTest, |
| 18 | ::testing::Values([]() { |
| 19 | return new SimulatedEventLoopTestFactory(); |
| 20 | })); |
| 21 | |
| 22 | // Test that creating an event and running the scheduler runs the event. |
| 23 | TEST(EventSchedulerTest, ScheduleEvent) { |
| 24 | int counter = 0; |
| 25 | EventScheduler scheduler; |
| 26 | |
| 27 | scheduler.Schedule(::aos::monotonic_clock::now(), |
| 28 | [&counter]() { counter += 1; }); |
| 29 | scheduler.Run(); |
| 30 | EXPECT_EQ(counter, 1); |
| 31 | auto token = scheduler.Schedule(::aos::monotonic_clock::now(), |
| 32 | [&counter]() { counter += 1; }); |
| 33 | scheduler.Deschedule(token); |
| 34 | scheduler.Run(); |
| 35 | EXPECT_EQ(counter, 1); |
| 36 | } |
| 37 | |
| 38 | // Test that descheduling an already scheduled event doesn't run the event. |
| 39 | TEST(EventSchedulerTest, DescheduleEvent) { |
| 40 | int counter = 0; |
| 41 | EventScheduler scheduler; |
| 42 | |
| 43 | auto token = scheduler.Schedule(::aos::monotonic_clock::now(), |
| 44 | [&counter]() { counter += 1; }); |
| 45 | scheduler.Deschedule(token); |
| 46 | scheduler.Run(); |
| 47 | EXPECT_EQ(counter, 0); |
| 48 | } |
| 49 | } // namespace testing |
| 50 | } // namespace aos |