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 |
11 #include "webrtc/base/common.h" | 11 #include "webrtc/base/common.h" |
| 12 #include "webrtc/base/event.h" |
| 13 #include "webrtc/base/fakeclock.h" |
12 #include "webrtc/base/gunit.h" | 14 #include "webrtc/base/gunit.h" |
13 #include "webrtc/base/helpers.h" | 15 #include "webrtc/base/helpers.h" |
14 #include "webrtc/base/thread.h" | 16 #include "webrtc/base/thread.h" |
15 #include "webrtc/base/timeutils.h" | 17 #include "webrtc/base/timeutils.h" |
16 | 18 |
17 namespace rtc { | 19 namespace rtc { |
18 | 20 |
19 TEST(TimeTest, TimeInMs) { | 21 TEST(TimeTest, TimeInMs) { |
20 uint32_t ts_earlier = Time(); | 22 uint32_t ts_earlier = Time(); |
21 Thread::SleepMs(100); | 23 Thread::SleepMs(100); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 EXPECT_TRUE(rtc::TmToSeconds(tm) == t); | 265 EXPECT_TRUE(rtc::TmToSeconds(tm) == t); |
264 #endif | 266 #endif |
265 } | 267 } |
266 } | 268 } |
267 }; | 269 }; |
268 | 270 |
269 TEST_F(TmToSeconds, TestTmToSeconds) { | 271 TEST_F(TmToSeconds, TestTmToSeconds) { |
270 TestTmToSeconds(100000); | 272 TestTmToSeconds(100000); |
271 } | 273 } |
272 | 274 |
| 275 // Test that all the time functions exposed by TimeUtils get time from the |
| 276 // fake clock when it's set. |
| 277 TEST(FakeClockTest, TimeFunctionsUseFakeClock) { |
| 278 FakeClock clock; |
| 279 SetClock(&clock); |
| 280 |
| 281 clock.SetTimeNanos(987654321u); |
| 282 EXPECT_EQ(987u, Time32()); |
| 283 EXPECT_EQ(987, Time64()); |
| 284 EXPECT_EQ(987u, Time()); |
| 285 EXPECT_EQ(987654u, TimeMicros()); |
| 286 EXPECT_EQ(987654321u, TimeNanos()); |
| 287 EXPECT_EQ(1000u, TimeAfter(13)); |
| 288 |
| 289 SetClock(nullptr); |
| 290 // After it's unset, we should get a normal time. |
| 291 EXPECT_NE(987u, Time()); |
| 292 } |
| 293 |
| 294 TEST(FakeClockTest, InitialTime) { |
| 295 FakeClock clock; |
| 296 EXPECT_EQ(0u, clock.TimeNanos()); |
| 297 } |
| 298 |
| 299 TEST(FakeClockTest, SetTimeNanos) { |
| 300 FakeClock clock; |
| 301 clock.SetTimeNanos(123u); |
| 302 EXPECT_EQ(123u, clock.TimeNanos()); |
| 303 clock.SetTimeNanos(456u); |
| 304 EXPECT_EQ(456u, clock.TimeNanos()); |
| 305 } |
| 306 |
| 307 TEST(FakeClockTest, AdvanceTime) { |
| 308 FakeClock clock; |
| 309 clock.AdvanceTimeNanos(1111u); |
| 310 EXPECT_EQ(1111u, clock.TimeNanos()); |
| 311 clock.AdvanceTimeMicros(2222u); |
| 312 EXPECT_EQ(2223111u, clock.TimeNanos()); |
| 313 clock.AdvanceTimeMillis(3333u); |
| 314 EXPECT_EQ(3335223111u, clock.TimeNanos()); |
| 315 clock.AdvanceTimeSeconds(4444u); |
| 316 EXPECT_EQ(4447335223111u, clock.TimeNanos()); |
| 317 } |
| 318 |
| 319 // When the clock is advanced, threads that are waiting in a socket select |
| 320 // should wake up and look at the new time. This allows tests using the |
| 321 // fake clock to run much faster, if the test is bound by time constraints |
| 322 // (such as a test for a STUN ping timeout). |
| 323 TEST(FakeClockTest, SettingTimeWakesThreads) { |
| 324 int64_t real_start_time_ms = Time64(); |
| 325 |
| 326 FakeClock clock; |
| 327 SetClock(&clock); |
| 328 |
| 329 Thread worker; |
| 330 worker.Start(); |
| 331 |
| 332 // Post an event that won't be executed for 10 seconds. |
| 333 Event message_handler_dispatched(false, false); |
| 334 auto functor = [&message_handler_dispatched] { |
| 335 message_handler_dispatched.Set(); |
| 336 }; |
| 337 worker.PostDelayed( |
| 338 10000, new FunctorMessageHandler<void, decltype(functor)>(functor)); |
| 339 |
| 340 // Wait for a bit for the worker thread to be started and enter its socket |
| 341 // select(). |
| 342 Thread::Current()->SleepMs(1000); |
| 343 |
| 344 // Advance the fake clock, expecting the worker thread to wake up |
| 345 // and dispatch the message quickly. |
| 346 clock.AdvanceTimeSeconds(10u); |
| 347 message_handler_dispatched.Wait(Event::kForever); |
| 348 |
| 349 SetClock(nullptr); |
| 350 |
| 351 // The message should have been dispatched long before the 10 seconds fully |
| 352 // elapsed. |
| 353 int64_t real_end_time_ms = Time64(); |
| 354 EXPECT_LT(real_end_time_ms - real_start_time_ms, 2000); |
| 355 } |
| 356 |
273 } // namespace rtc | 357 } // namespace rtc |
OLD | NEW |