Reland of: Improving the fake clock and using it to fix a flaky STUN timeout test.
When the fake clock's time is advanced, it now ensures all pending
queued messages have been dispatched. This allows us to write a
"SIMULATED_WAIT" macro that ticks the simulated clock by milliseconds up
until the target time.
Useful in this case, where we know the STUN timeout should take a total
of 9500ms, but it would be overly complex to write test code that waits
for each individual timeout, ensures a STUN packet has been
retransmited, etc.
(The test described above *should* be written, but it belongs in
p2ptransportchannel_unittest.cc, not webrtcsession_unittest.cc).
Review-Url: https://codereview.webrtc.org/2024813004
Cr-Commit-Position: refs/heads/master@{#13052}
diff --git a/webrtc/base/timeutils_unittest.cc b/webrtc/base/timeutils_unittest.cc
index f183684..d1cfe23 100644
--- a/webrtc/base/timeutils_unittest.cc
+++ b/webrtc/base/timeutils_unittest.cc
@@ -301,7 +301,7 @@
// fake clock when it's set.
TEST(FakeClock, TimeFunctionsUseFakeClock) {
FakeClock clock;
- SetClock(&clock);
+ SetClockForTesting(&clock);
clock.SetTimeNanos(987654321u);
EXPECT_EQ(987u, Time32());
@@ -310,7 +310,7 @@
EXPECT_EQ(987654321u, TimeNanos());
EXPECT_EQ(1000u, TimeAfter(13));
- SetClock(nullptr);
+ SetClockForTesting(nullptr);
// After it's unset, we should get a normal time.
EXPECT_NE(987, TimeMillis());
}
@@ -348,7 +348,7 @@
int64_t real_start_time_ms = TimeMillis();
FakeClock clock;
- SetClock(&clock);
+ SetClockForTesting(&clock);
Thread worker;
worker.Start();
@@ -359,24 +359,25 @@
message_handler_dispatched.Set();
};
FunctorMessageHandler<void, decltype(functor)> handler(functor);
- worker.PostDelayed(10000, &handler);
+ worker.PostDelayed(60000, &handler);
// Wait for a bit for the worker thread to be started and enter its socket
- // select().
+ // select(). Otherwise this test would be trivial since the worker thread
+ // would process the event as soon as it was started.
Thread::Current()->SleepMs(1000);
// Advance the fake clock, expecting the worker thread to wake up
- // and dispatch the message quickly.
- clock.AdvanceTime(TimeDelta::FromSeconds(10u));
- message_handler_dispatched.Wait(Event::kForever);
+ // and dispatch the message instantly.
+ clock.AdvanceTime(TimeDelta::FromSeconds(60u));
+ EXPECT_TRUE(message_handler_dispatched.Wait(0));
worker.Stop();
- SetClock(nullptr);
+ SetClockForTesting(nullptr);
- // The message should have been dispatched long before the 10 seconds fully
- // elapsed.
+ // The message should have been dispatched long before the 60 seconds fully
+ // elapsed (just a sanity check).
int64_t real_end_time_ms = TimeMillis();
- EXPECT_LT(real_end_time_ms - real_start_time_ms, 2000);
+ EXPECT_LT(real_end_time_ms - real_start_time_ms, 10000);
}
} // namespace rtc