Add RunFor to SimulatedEventLoopFactory
Turns out we really want to be able to run for short periods of time in
simulation. So add support and tests for it.
Change-Id: I9276e1330a0d4eaea717f949516b290b1a01ed89
diff --git a/aos/events/simulated-event-loop_test.cc b/aos/events/simulated-event-loop_test.cc
index f652fd9..41d4dde 100644
--- a/aos/events/simulated-event-loop_test.cc
+++ b/aos/events/simulated-event-loop_test.cc
@@ -10,10 +10,16 @@
class SimulatedEventLoopTestFactory : public EventLoopTestFactory {
public:
::std::unique_ptr<EventLoop> Make() override {
- return event_loop.MakeEventLoop();
+ return event_loop_factory_.MakeEventLoop();
}
+ ::std::unique_ptr<EventLoop> MakePrimary() override {
+ return event_loop_factory_.MakeEventLoop();
+ }
+
+ void Run() override { event_loop_factory_.Run(); }
+
private:
- SimulatedEventLoopFactory event_loop;
+ SimulatedEventLoopFactory event_loop_factory_;
};
INSTANTIATE_TEST_CASE_P(SimulatedEventLoopTest, AbstractEventLoopTest,
@@ -48,5 +54,44 @@
scheduler.Run();
EXPECT_EQ(counter, 0);
}
+
+// Test that running for a time period with no handlers causes time to progress
+// correctly.
+TEST(SimulatedEventLoopTest, RunForNoHandlers) {
+ SimulatedEventLoopFactory simulated_event_loop_factory;
+ ::std::unique_ptr<EventLoop> event_loop =
+ simulated_event_loop_factory.MakeEventLoop();
+
+ simulated_event_loop_factory.RunFor(chrono::seconds(1));
+
+ EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
+ simulated_event_loop_factory.monotonic_now());
+ EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
+ event_loop->monotonic_now());
+}
+
+// Test that running for a time with a periodic handler causes time to end
+// correctly.
+TEST(SimulatedEventLoopTest, RunForTimerHandler) {
+ SimulatedEventLoopFactory simulated_event_loop_factory;
+ ::std::unique_ptr<EventLoop> event_loop =
+ simulated_event_loop_factory.MakeEventLoop();
+
+ int counter = 0;
+ auto timer = event_loop->AddTimer([&counter, &event_loop]() { ++counter; });
+ event_loop->OnRun([&event_loop, &timer] {
+ timer->Setup(event_loop->monotonic_now() + chrono::milliseconds(50),
+ chrono::milliseconds(100));
+ });
+
+ simulated_event_loop_factory.RunFor(chrono::seconds(1));
+
+ EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
+ simulated_event_loop_factory.monotonic_now());
+ EXPECT_EQ(::aos::monotonic_clock::epoch() + chrono::seconds(1),
+ event_loop->monotonic_now());
+ EXPECT_EQ(counter, 10);
+}
+
} // namespace testing
} // namespace aos