Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: webrtc/base/timeutils_unittest.cc

Issue 2024813004: Improving the fake clock and using it to fix a flaky STUN timeout test. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fixing another TSan warning. WebRtcSession wasn't completely shut down. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 294 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
295 TimeDelta::FromMilliseconds(1000) /= 2); 295 TimeDelta::FromMilliseconds(1000) /= 2);
296 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 296 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
297 2 * TimeDelta::FromMilliseconds(1000)); 297 2 * TimeDelta::FromMilliseconds(1000));
298 } 298 }
299 299
300 // Test that all the time functions exposed by TimeUtils get time from the 300 // Test that all the time functions exposed by TimeUtils get time from the
301 // fake clock when it's set. 301 // fake clock when it's set.
302 TEST(FakeClock, TimeFunctionsUseFakeClock) { 302 TEST(FakeClock, TimeFunctionsUseFakeClock) {
303 FakeClock clock; 303 FakeClock clock;
304 SetClock(&clock); 304 SetClockForTesting(&clock);
305 305
306 clock.SetTimeNanos(987654321u); 306 clock.SetTimeNanos(987654321u);
307 EXPECT_EQ(987u, Time32()); 307 EXPECT_EQ(987u, Time32());
308 EXPECT_EQ(987, TimeMillis()); 308 EXPECT_EQ(987, TimeMillis());
309 EXPECT_EQ(987654u, TimeMicros()); 309 EXPECT_EQ(987654u, TimeMicros());
310 EXPECT_EQ(987654321u, TimeNanos()); 310 EXPECT_EQ(987654321u, TimeNanos());
311 EXPECT_EQ(1000u, TimeAfter(13)); 311 EXPECT_EQ(1000u, TimeAfter(13));
312 312
313 SetClock(nullptr); 313 SetClockForTesting(nullptr);
314 // After it's unset, we should get a normal time. 314 // After it's unset, we should get a normal time.
315 EXPECT_NE(987, TimeMillis()); 315 EXPECT_NE(987, TimeMillis());
316 } 316 }
317 317
318 TEST(FakeClock, InitialTime) { 318 TEST(FakeClock, InitialTime) {
319 FakeClock clock; 319 FakeClock clock;
320 EXPECT_EQ(0u, clock.TimeNanos()); 320 EXPECT_EQ(0u, clock.TimeNanos());
321 } 321 }
322 322
323 TEST(FakeClock, SetTimeNanos) { 323 TEST(FakeClock, SetTimeNanos) {
(...skipping 17 matching lines...) Expand all
341 } 341 }
342 342
343 // When the clock is advanced, threads that are waiting in a socket select 343 // When the clock is advanced, threads that are waiting in a socket select
344 // should wake up and look at the new time. This allows tests using the 344 // should wake up and look at the new time. This allows tests using the
345 // fake clock to run much faster, if the test is bound by time constraints 345 // fake clock to run much faster, if the test is bound by time constraints
346 // (such as a test for a STUN ping timeout). 346 // (such as a test for a STUN ping timeout).
347 TEST(FakeClock, SettingTimeWakesThreads) { 347 TEST(FakeClock, SettingTimeWakesThreads) {
348 int64_t real_start_time_ms = TimeMillis(); 348 int64_t real_start_time_ms = TimeMillis();
349 349
350 FakeClock clock; 350 FakeClock clock;
351 SetClock(&clock); 351 SetClockForTesting(&clock);
352 352
353 Thread worker; 353 Thread worker;
354 worker.Start(); 354 worker.Start();
355 355
356 // Post an event that won't be executed for 10 seconds. 356 // Post an event that won't be executed for 10 seconds.
357 Event message_handler_dispatched(false, false); 357 Event message_handler_dispatched(false, false);
358 auto functor = [&message_handler_dispatched] { 358 auto functor = [&message_handler_dispatched] {
359 message_handler_dispatched.Set(); 359 message_handler_dispatched.Set();
360 }; 360 };
361 FunctorMessageHandler<void, decltype(functor)> handler(functor); 361 FunctorMessageHandler<void, decltype(functor)> handler(functor);
362 worker.PostDelayed(10000, &handler); 362 worker.PostDelayed(60000, &handler);
363 363
364 // Wait for a bit for the worker thread to be started and enter its socket 364 // Wait for a bit for the worker thread to be started and enter its socket
365 // select(). 365 // select(). Otherwise this test would be trivial since the worker thread
366 // would process the event as soon as it was started.
366 Thread::Current()->SleepMs(1000); 367 Thread::Current()->SleepMs(1000);
367 368
368 // Advance the fake clock, expecting the worker thread to wake up 369 // Advance the fake clock, expecting the worker thread to wake up
369 // and dispatch the message quickly. 370 // and dispatch the message instantly.
370 clock.AdvanceTime(TimeDelta::FromSeconds(10u)); 371 clock.AdvanceTime(TimeDelta::FromSeconds(60u));
371 message_handler_dispatched.Wait(Event::kForever); 372 EXPECT_TRUE(message_handler_dispatched.Wait(0));
372 worker.Stop(); 373 worker.Stop();
373 374
374 SetClock(nullptr); 375 SetClockForTesting(nullptr);
375 376
376 // The message should have been dispatched long before the 10 seconds fully 377 // The message should have been dispatched long before the 60 seconds fully
377 // elapsed. 378 // elapsed (just a sanity check).
378 int64_t real_end_time_ms = TimeMillis(); 379 int64_t real_end_time_ms = TimeMillis();
379 EXPECT_LT(real_end_time_ms - real_start_time_ms, 2000); 380 EXPECT_LT(real_end_time_ms - real_start_time_ms, 10000);
380 } 381 }
381 382
382 } // namespace rtc 383 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698