clock_test.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2014 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "util/misc/clock.h"
  15. #include <sys/types.h>
  16. #include <algorithm>
  17. #include "base/format_macros.h"
  18. #include "base/logging.h"
  19. #include "base/macros.h"
  20. #include "base/strings/stringprintf.h"
  21. #include "gtest/gtest.h"
  22. namespace crashpad {
  23. namespace test {
  24. namespace {
  25. TEST(Clock, ClockMonotonicNanoseconds) {
  26. uint64_t start = ClockMonotonicNanoseconds();
  27. EXPECT_GT(start, 0u);
  28. uint64_t now = start;
  29. for (size_t iteration = 0; iteration < 10; ++iteration) {
  30. uint64_t last = now;
  31. now = ClockMonotonicNanoseconds();
  32. // Use EXPECT_GE instead of EXPECT_GT, because there are no guarantees about
  33. // the clock’s resolution.
  34. EXPECT_GE(now, last);
  35. }
  36. #if !defined(OS_WIN) // No SleepNanoseconds implemented on Windows.
  37. // SleepNanoseconds() should sleep for at least the value of the clock’s
  38. // resolution, so the clock’s value should definitely increase after a sleep.
  39. // EXPECT_GT can be used instead of EXPECT_GE after the sleep.
  40. SleepNanoseconds(1);
  41. now = ClockMonotonicNanoseconds();
  42. EXPECT_GT(now, start);
  43. #endif // OS_WIN
  44. }
  45. #if !defined(OS_WIN) // No SleepNanoseconds implemented on Windows.
  46. void TestSleepNanoseconds(uint64_t nanoseconds) {
  47. uint64_t start = ClockMonotonicNanoseconds();
  48. SleepNanoseconds(nanoseconds);
  49. uint64_t end = ClockMonotonicNanoseconds();
  50. uint64_t diff = end - start;
  51. // |nanoseconds| is the lower bound for the actual amount of time spent
  52. // sleeping.
  53. EXPECT_GE(diff, nanoseconds);
  54. // It’s difficult to set an upper bound for the time spent sleeping, and
  55. // attempting to do so results in a flaky test.
  56. }
  57. TEST(Clock, SleepNanoseconds) {
  58. static constexpr uint64_t kTestData[] = {
  59. 0,
  60. 1,
  61. static_cast<uint64_t>(1E3), // 1 microsecond
  62. static_cast<uint64_t>(1E4), // 10 microseconds
  63. static_cast<uint64_t>(1E5), // 100 microseconds
  64. static_cast<uint64_t>(1E6), // 1 millisecond
  65. static_cast<uint64_t>(1E7), // 10 milliseconds
  66. static_cast<uint64_t>(2E7), // 20 milliseconds
  67. static_cast<uint64_t>(5E7), // 50 milliseconds
  68. };
  69. for (size_t index = 0; index < arraysize(kTestData); ++index) {
  70. const uint64_t nanoseconds = kTestData[index];
  71. SCOPED_TRACE(base::StringPrintf(
  72. "index %zu, nanoseconds %" PRIu64, index, nanoseconds));
  73. TestSleepNanoseconds(nanoseconds);
  74. }
  75. }
  76. #endif // OS_WIN
  77. } // namespace
  78. } // namespace test
  79. } // namespace crashpad