Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/simulated_event_loop.h" |
| 2 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 3 | #include <string_view> |
| 4 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include "aos/events/event_loop_param_test.h" |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 6 | #include "gtest/gtest.h" |
| 7 | |
| 8 | namespace aos { |
| 9 | namespace testing { |
| 10 | |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 11 | namespace chrono = ::std::chrono; |
| 12 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 13 | class SimulatedEventLoopTestFactory : public EventLoopTestFactory { |
| 14 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 15 | SimulatedEventLoopTestFactory() : event_loop_factory_(configuration()) {} |
| 16 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 17 | ::std::unique_ptr<EventLoop> Make(std::string_view name) override { |
| 18 | return event_loop_factory_.MakeEventLoop(name); |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 19 | } |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 20 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override { |
| 21 | return event_loop_factory_.MakeEventLoop(name); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | void Run() override { event_loop_factory_.Run(); } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 25 | void Exit() override { event_loop_factory_.Exit(); } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 26 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 27 | // TODO(austin): Implement this. It's used currently for a phased loop test. |
| 28 | // I'm not sure how much that matters. |
| 29 | void SleepFor(::std::chrono::nanoseconds /*duration*/) override {} |
| 30 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 31 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 32 | SimulatedEventLoopFactory event_loop_factory_; |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 33 | }; |
| 34 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 35 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopDeathTest, AbstractEventLoopDeathTest, |
| 36 | ::testing::Values([]() { |
| 37 | return new SimulatedEventLoopTestFactory(); |
| 38 | })); |
| 39 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 40 | INSTANTIATE_TEST_CASE_P(SimulatedEventLoopTest, AbstractEventLoopTest, |
| 41 | ::testing::Values([]() { |
| 42 | return new SimulatedEventLoopTestFactory(); |
| 43 | })); |
| 44 | |
| 45 | // Test that creating an event and running the scheduler runs the event. |
| 46 | TEST(EventSchedulerTest, ScheduleEvent) { |
| 47 | int counter = 0; |
| 48 | EventScheduler scheduler; |
| 49 | |
| 50 | scheduler.Schedule(::aos::monotonic_clock::now(), |
| 51 | [&counter]() { counter += 1; }); |
| 52 | scheduler.Run(); |
| 53 | EXPECT_EQ(counter, 1); |
| 54 | auto token = scheduler.Schedule(::aos::monotonic_clock::now(), |
| 55 | [&counter]() { counter += 1; }); |
| 56 | scheduler.Deschedule(token); |
| 57 | scheduler.Run(); |
| 58 | EXPECT_EQ(counter, 1); |
| 59 | } |
| 60 | |
| 61 | // Test that descheduling an already scheduled event doesn't run the event. |
| 62 | TEST(EventSchedulerTest, DescheduleEvent) { |
| 63 | int counter = 0; |
| 64 | EventScheduler scheduler; |
| 65 | |
| 66 | auto token = scheduler.Schedule(::aos::monotonic_clock::now(), |
| 67 | [&counter]() { counter += 1; }); |
| 68 | scheduler.Deschedule(token); |
| 69 | scheduler.Run(); |
| 70 | EXPECT_EQ(counter, 0); |
| 71 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 72 | |
| 73 | // Test that running for a time period with no handlers causes time to progress |
| 74 | // correctly. |
| 75 | TEST(SimulatedEventLoopTest, RunForNoHandlers) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 76 | SimulatedEventLoopTestFactory factory; |
| 77 | |
| 78 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 79 | factory.configuration()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 80 | ::std::unique_ptr<EventLoop> event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 81 | simulated_event_loop_factory.MakeEventLoop("loop"); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 82 | |
| 83 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 84 | |
| 85 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
| 86 | simulated_event_loop_factory.monotonic_now()); |
| 87 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
| 88 | event_loop->monotonic_now()); |
| 89 | } |
| 90 | |
| 91 | // Test that running for a time with a periodic handler causes time to end |
| 92 | // correctly. |
| 93 | TEST(SimulatedEventLoopTest, RunForTimerHandler) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 94 | SimulatedEventLoopTestFactory factory; |
| 95 | |
| 96 | SimulatedEventLoopFactory simulated_event_loop_factory( |
| 97 | factory.configuration()); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 98 | ::std::unique_ptr<EventLoop> event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 99 | simulated_event_loop_factory.MakeEventLoop("loop"); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 100 | |
| 101 | int counter = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 102 | auto timer = event_loop->AddTimer([&counter]() { ++counter; }); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 103 | event_loop->OnRun([&event_loop, &timer] { |
| 104 | timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50), |
| 105 | chrono::milliseconds(100)); |
| 106 | }); |
| 107 | |
| 108 | simulated_event_loop_factory.RunFor(chrono::seconds(1)); |
| 109 | |
| 110 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
| 111 | simulated_event_loop_factory.monotonic_now()); |
| 112 | EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1), |
| 113 | event_loop->monotonic_now()); |
| 114 | EXPECT_EQ(counter, 10); |
| 115 | } |
| 116 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 117 | } // namespace testing |
| 118 | } // namespace aos |