Chromium Code Reviews| Index: test/cctest/test-time.cc |
| diff --git a/test/cctest/test-time.cc b/test/cctest/test-time.cc |
| index 8b92d8d32ac0024409503c0030b95fba4a72c978..bb25b1e2267234a6c3b28d3889cb9f178f118429 100644 |
| --- a/test/cctest/test-time.cc |
| +++ b/test/cctest/test-time.cc |
| @@ -142,3 +142,54 @@ TEST(TimeTicksIsMonotonic) { |
| previous_highres_ticks = highres_ticks; |
| } |
| } |
| + |
| + |
| +template <typename T> |
| +static void ResolutionTest(T (*Now)(), TimeDelta target_granularity) { |
| + // We're trying to measure that intervals increment in a VERY small amount |
| + // of time -- according to the specified target granularity. Unfortunately, |
| + // if we happen to have a context switch in the middle of our test, the |
| + // context switch could easily exceed our limit. So, we iterate on this |
| + // several times. As long as we're able to detect the fine-granularity |
| + // timers at least once, then the test has succeeded. |
| + static const TimeDelta kExpirationTimeout = TimeDelta::FromSeconds(1); |
| + ElapsedTimer timer; |
| + timer.Start(); |
| + TimeDelta delta; |
| + do { |
| + T start = Now(); |
| + T now = start; |
| + // Loop until we can detect that the clock has changed. Non-HighRes timers |
| + // will increment in chunks, i.e. 15ms. By spinning until we see a clock |
| + // change, we detect the minimum time between measurements. |
| + do { |
| + now = Now(); |
| + delta = now - start; |
| + } while (now <= start); |
| + CHECK_NE(static_cast<int64_t>(0), delta.InMicroseconds()); |
| + } while (delta > target_granularity && !timer.HasExpired(kExpirationTimeout)); |
| + CHECK_LE(delta, target_granularity); |
| +} |
| + |
| + |
| +TEST(TimeNowResolution) { |
| + // We assume that Time::Now() has at least 16ms resolution. |
| + static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(16); |
| + ResolutionTest<Time>(&Time::Now, kTargetGranularity); |
| +} |
| + |
| + |
| +TEST(TimeTicksNowResolution) { |
| + // We assume that TimeTicks::Now() has at least 16ms resolution. |
| + static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(16); |
| + ResolutionTest<TimeTicks>(&TimeTicks::Now, kTargetGranularity); |
| +} |
| + |
| + |
| +TEST(TimeTicksHighResNowResolution) { |
|
Hannes Payer (out of office)
2013/10/02 11:27:25
What about merging the last two test cases and usi
Benedikt Meurer
2013/10/02 12:27:46
TimeTicks::Now() is different from TimeTicks::High
|
| + if (!TimeTicks::IsHighResClockWorking()) return; |
| + |
| + // We assume that TimeTicks::HighResNow() has sub-millisecond resolution. |
| + static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(1); |
| + ResolutionTest<TimeTicks>(&TimeTicks::HighResNow, kTargetGranularity); |
| +} |