| Index: webrtc/base/timeutils_unittest.cc
|
| diff --git a/webrtc/base/timeutils_unittest.cc b/webrtc/base/timeutils_unittest.cc
|
| index 61e41b7c644919070ee84822622c9fc4b7807bfd..b08a696c3872ae1df83449618f0c460762f8abd5 100644
|
| --- a/webrtc/base/timeutils_unittest.cc
|
| +++ b/webrtc/base/timeutils_unittest.cc
|
| @@ -9,8 +9,10 @@
|
| */
|
|
|
| #include "webrtc/base/common.h"
|
| +#include "webrtc/base/event.h"
|
| #include "webrtc/base/gunit.h"
|
| #include "webrtc/base/helpers.h"
|
| +#include "webrtc/base/simulatedclock.h"
|
| #include "webrtc/base/thread.h"
|
| #include "webrtc/base/timeutils.h"
|
|
|
| @@ -270,4 +272,86 @@ TEST_F(TmToSeconds, TestTmToSeconds) {
|
| TestTmToSeconds(100000);
|
| }
|
|
|
| +// Test that all the time functions exposed by TimeUtils get time from the
|
| +// simulated clock when it's set.
|
| +TEST(SimulatedClockTest, TimeFunctionsUseSimulatedClock) {
|
| + SimulatedClock 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(SimulatedClockTest, InitialTime) {
|
| + SimulatedClock clock;
|
| + EXPECT_EQ(0u, clock.TimeNanos());
|
| +}
|
| +
|
| +TEST(SimulatedClockTest, SetTimeNanos) {
|
| + SimulatedClock clock;
|
| + clock.SetTimeNanos(123u);
|
| + EXPECT_EQ(123u, clock.TimeNanos());
|
| + clock.SetTimeNanos(456u);
|
| + EXPECT_EQ(456u, clock.TimeNanos());
|
| +}
|
| +
|
| +TEST(SimulatedClockTest, AdvanceTime) {
|
| + SimulatedClock 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
|
| +// simulated clock to run much faster, if the test is bound by time constraints
|
| +// (such as a test for a STUN ping timeout).
|
| +TEST(SimulatedClockTest, SettingTimeWakesThreads) {
|
| + int64_t real_start_time_ms = Time64();
|
| +
|
| + SimulatedClock 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 simulated 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
|
|
|