OLD | NEW |
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 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 // When the clock is advanced, threads that are waiting in a socket select | 342 // When the clock is advanced, threads that are waiting in a socket select |
343 // should wake up and look at the new time. This allows tests using the | 343 // should wake up and look at the new time. This allows tests using the |
344 // fake clock to run much faster, if the test is bound by time constraints | 344 // fake clock to run much faster, if the test is bound by time constraints |
345 // (such as a test for a STUN ping timeout). | 345 // (such as a test for a STUN ping timeout). |
346 TEST(FakeClock, SettingTimeWakesThreads) { | 346 TEST(FakeClock, SettingTimeWakesThreads) { |
347 int64_t real_start_time_ms = TimeMillis(); | 347 int64_t real_start_time_ms = TimeMillis(); |
348 | 348 |
349 FakeClock clock; | 349 FakeClock clock; |
350 SetClockForTesting(&clock); | 350 SetClockForTesting(&clock); |
351 | 351 |
352 Thread worker; | 352 std::unique_ptr<Thread> worker(Thread::CreateWithSocketServer()); |
353 worker.Start(); | 353 worker->Start(); |
354 | 354 |
355 // Post an event that won't be executed for 10 seconds. | 355 // Post an event that won't be executed for 10 seconds. |
356 Event message_handler_dispatched(false, false); | 356 Event message_handler_dispatched(false, false); |
357 auto functor = [&message_handler_dispatched] { | 357 auto functor = [&message_handler_dispatched] { |
358 message_handler_dispatched.Set(); | 358 message_handler_dispatched.Set(); |
359 }; | 359 }; |
360 FunctorMessageHandler<void, decltype(functor)> handler(functor); | 360 FunctorMessageHandler<void, decltype(functor)> handler(functor); |
361 worker.PostDelayed(RTC_FROM_HERE, 60000, &handler); | 361 worker->PostDelayed(RTC_FROM_HERE, 60000, &handler); |
362 | 362 |
363 // Wait for a bit for the worker thread to be started and enter its socket | 363 // Wait for a bit for the worker thread to be started and enter its socket |
364 // select(). Otherwise this test would be trivial since the worker thread | 364 // select(). Otherwise this test would be trivial since the worker thread |
365 // would process the event as soon as it was started. | 365 // would process the event as soon as it was started. |
366 Thread::Current()->SleepMs(1000); | 366 Thread::Current()->SleepMs(1000); |
367 | 367 |
368 // Advance the fake clock, expecting the worker thread to wake up | 368 // Advance the fake clock, expecting the worker thread to wake up |
369 // and dispatch the message instantly. | 369 // and dispatch the message instantly. |
370 clock.AdvanceTime(TimeDelta::FromSeconds(60u)); | 370 clock.AdvanceTime(TimeDelta::FromSeconds(60u)); |
371 EXPECT_TRUE(message_handler_dispatched.Wait(0)); | 371 EXPECT_TRUE(message_handler_dispatched.Wait(0)); |
372 worker.Stop(); | 372 worker->Stop(); |
373 | 373 |
374 SetClockForTesting(nullptr); | 374 SetClockForTesting(nullptr); |
375 | 375 |
376 // The message should have been dispatched long before the 60 seconds fully | 376 // The message should have been dispatched long before the 60 seconds fully |
377 // elapsed (just a sanity check). | 377 // elapsed (just a sanity check). |
378 int64_t real_end_time_ms = TimeMillis(); | 378 int64_t real_end_time_ms = TimeMillis(); |
379 EXPECT_LT(real_end_time_ms - real_start_time_ms, 10000); | 379 EXPECT_LT(real_end_time_ms - real_start_time_ms, 10000); |
380 } | 380 } |
381 | 381 |
382 } // namespace rtc | 382 } // namespace rtc |
OLD | NEW |