Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "platform/api/time.h" |
| 6 | |
Jordan Bayles | c9201dd | 2020-06-02 15:51:31 -0700 | [diff] [blame^] | 7 | #include <chrono> |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 8 | #include <thread> |
| 9 | |
Jordan Bayles | 2d01f17 | 2019-06-07 11:11:50 -0700 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
Jordan Bayles | c9201dd | 2020-06-02 15:51:31 -0700 | [diff] [blame^] | 11 | #include "util/chrono_helpers.h" |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 12 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 13 | namespace openscreen { |
Yuri Wiitala | f162a61 | 2019-11-22 22:46:04 -0800 | [diff] [blame] | 14 | namespace { |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 15 | |
Yuri Wiitala | f162a61 | 2019-11-22 22:46:04 -0800 | [diff] [blame] | 16 | // Tests that the clock always seems to tick forward. If this test is broken, or |
| 17 | // is flaky, the time source is probably not monotonic. |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 18 | TEST(TimeTest, PlatformClockIsMonotonic) { |
| 19 | constexpr auto kSleepPeriod = milliseconds(2); |
| 20 | for (int i = 0; i < 50; ++i) { |
| 21 | const auto start = Clock::now(); |
| 22 | std::this_thread::sleep_for(kSleepPeriod); |
| 23 | const auto stop = Clock::now(); |
| 24 | EXPECT_GE(stop - start, kSleepPeriod / 2); |
| 25 | } |
| 26 | } |
| 27 | |
Yuri Wiitala | f162a61 | 2019-11-22 22:46:04 -0800 | [diff] [blame] | 28 | // Tests that the clock ticks at least 10000 times per second, a requirement |
| 29 | // specified in the API header comments. |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 30 | TEST(TimeTest, PlatformClockHasSufficientResolution) { |
Yuri Wiitala | f162a61 | 2019-11-22 22:46:04 -0800 | [diff] [blame] | 31 | constexpr std::chrono::duration<int, Clock::kRequiredResolution> |
| 32 | kMaxAllowedDurationBetweenTicks(1); |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 33 | constexpr int kMaxRetries = 100; |
| 34 | |
| 35 | // Loop until a small-enough clock change is observed. The platform is given |
| 36 | // multiple chances because unpredictable events, like thread context |
| 37 | // switches, could suspend the current thread for a "long" time, masking what |
| 38 | // the clock is actually capable of. |
| 39 | Clock::duration delta = microseconds(0); |
| 40 | for (int i = 0; i < kMaxRetries; ++i) { |
| 41 | const auto start = Clock::now(); |
| 42 | // Loop until the clock changes. |
| 43 | do { |
| 44 | delta = Clock::now() - start; |
| 45 | ASSERT_LE(microseconds(0), delta); |
| 46 | } while (delta == microseconds(0)); |
| 47 | |
Yuri Wiitala | f162a61 | 2019-11-22 22:46:04 -0800 | [diff] [blame] | 48 | if (delta <= kMaxAllowedDurationBetweenTicks) { |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 49 | break; |
| 50 | } |
| 51 | } |
| 52 | |
Yuri Wiitala | f162a61 | 2019-11-22 22:46:04 -0800 | [diff] [blame] | 53 | EXPECT_LE(delta, kMaxAllowedDurationBetweenTicks); |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Yuri Wiitala | f162a61 | 2019-11-22 22:46:04 -0800 | [diff] [blame] | 56 | } // namespace |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 57 | } // namespace openscreen |