Index: webrtc/base/timeutils_unittest.cc |
diff --git a/webrtc/base/timeutils_unittest.cc b/webrtc/base/timeutils_unittest.cc |
index 61e41b7c644919070ee84822622c9fc4b7807bfd..4cde6a4c681f085a165ab455d3157f30fcc4d65c 100644 |
--- a/webrtc/base/timeutils_unittest.cc |
+++ b/webrtc/base/timeutils_unittest.cc |
@@ -9,6 +9,8 @@ |
*/ |
#include "webrtc/base/common.h" |
+#include "webrtc/base/event.h" |
+#include "webrtc/base/fakeclock.h" |
#include "webrtc/base/gunit.h" |
#include "webrtc/base/helpers.h" |
#include "webrtc/base/thread.h" |
@@ -270,4 +272,86 @@ TEST_F(TmToSeconds, TestTmToSeconds) { |
TestTmToSeconds(100000); |
} |
+// Test that all the time functions exposed by TimeUtils get time from the |
+// fake clock when it's set. |
+TEST(FakeClockTest, TimeFunctionsUseFakeClock) { |
+ FakeClock clock; |
+ SetClock(&clock); |
+ |
+ clock.SetTimeNanos(987654321u); |
+ EXPECT_EQ(987u, Time32()); |
+ EXPECT_EQ(987, Time64()); |
+ EXPECT_EQ(987u, Time()); |
+ EXPECT_EQ(987654u, TimeMicros()); |
+ EXPECT_EQ(987654321u, TimeNanos()); |
+ EXPECT_EQ(1000u, TimeAfter(13)); |
+ |
+ SetClock(nullptr); |
+ // After it's unset, we should get a normal time. |
+ EXPECT_NE(987u, Time()); |
+} |
+ |
+TEST(FakeClockTest, InitialTime) { |
+ FakeClock clock; |
+ EXPECT_EQ(0u, clock.TimeNanos()); |
+} |
+ |
+TEST(FakeClockTest, SetTimeNanos) { |
+ FakeClock clock; |
+ clock.SetTimeNanos(123u); |
+ EXPECT_EQ(123u, clock.TimeNanos()); |
+ clock.SetTimeNanos(456u); |
+ EXPECT_EQ(456u, clock.TimeNanos()); |
+} |
+ |
+TEST(FakeClockTest, AdvanceTime) { |
+ FakeClock clock; |
+ clock.AdvanceTimeNanos(1111u); |
+ EXPECT_EQ(1111u, clock.TimeNanos()); |
+ clock.AdvanceTimeMicros(2222u); |
+ EXPECT_EQ(2223111u, clock.TimeNanos()); |
+ clock.AdvanceTimeMillis(3333u); |
+ EXPECT_EQ(3335223111u, clock.TimeNanos()); |
+ clock.AdvanceTimeSeconds(4444u); |
+ EXPECT_EQ(4447335223111u, clock.TimeNanos()); |
+} |
+ |
+// When the clock is advanced, threads that are waiting in a socket select |
+// should wake up and look at the new time. This allows tests using the |
+// fake clock to run much faster, if the test is bound by time constraints |
+// (such as a test for a STUN ping timeout). |
+TEST(FakeClockTest, SettingTimeWakesThreads) { |
+ int64_t real_start_time_ms = Time64(); |
+ |
+ FakeClock clock; |
+ SetClock(&clock); |
+ |
+ Thread worker; |
+ worker.Start(); |
+ |
+ // Post an event that won't be executed for 10 seconds. |
+ Event message_handler_dispatched(false, false); |
+ auto functor = [&message_handler_dispatched] { |
+ message_handler_dispatched.Set(); |
+ }; |
+ worker.PostDelayed( |
+ 10000, new FunctorMessageHandler<void, decltype(functor)>(functor)); |
+ |
+ // Wait for a bit for the worker thread to be started and enter its socket |
+ // select(). |
+ Thread::Current()->SleepMs(1000); |
+ |
+ // Advance the fake clock, expecting the worker thread to wake up |
+ // and dispatch the message quickly. |
+ clock.AdvanceTimeSeconds(10u); |
+ message_handler_dispatched.Wait(Event::kForever); |
+ |
+ SetClock(nullptr); |
+ |
+ // The message should have been dispatched long before the 10 seconds fully |
+ // elapsed. |
+ int64_t real_end_time_ms = Time64(); |
+ EXPECT_LT(real_end_time_ms - real_start_time_ms, 2000); |
+} |
+ |
} // namespace rtc |