clock_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2017 The Abseil Authors.
  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. // https://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 "absl/time/clock.h"
  15. #include "absl/base/config.h"
  16. #if defined(ABSL_HAVE_ALARM)
  17. #include <signal.h>
  18. #include <unistd.h>
  19. #ifdef _AIX
  20. // sig_t is not defined in AIX.
  21. typedef void (*sig_t)(int);
  22. #endif
  23. #elif defined(__linux__) || defined(__APPLE__)
  24. #error all known Linux and Apple targets have alarm
  25. #endif
  26. #include "gtest/gtest.h"
  27. #include "absl/time/time.h"
  28. namespace {
  29. TEST(Time, Now) {
  30. const absl::Time before = absl::FromUnixNanos(absl::GetCurrentTimeNanos());
  31. const absl::Time now = absl::Now();
  32. const absl::Time after = absl::FromUnixNanos(absl::GetCurrentTimeNanos());
  33. EXPECT_GE(now, before);
  34. EXPECT_GE(after, now);
  35. }
  36. enum class AlarmPolicy { kWithoutAlarm, kWithAlarm };
  37. #if defined(ABSL_HAVE_ALARM)
  38. bool alarm_handler_invoked = false;
  39. void AlarmHandler(int signo) {
  40. ASSERT_EQ(signo, SIGALRM);
  41. alarm_handler_invoked = true;
  42. }
  43. #endif
  44. // Does SleepFor(d) take between lower_bound and upper_bound at least
  45. // once between now and (now + timeout)? If requested (and supported),
  46. // add an alarm for the middle of the sleep period and expect it to fire.
  47. bool SleepForBounded(absl::Duration d, absl::Duration lower_bound,
  48. absl::Duration upper_bound, absl::Duration timeout,
  49. AlarmPolicy alarm_policy, int* attempts) {
  50. const absl::Time deadline = absl::Now() + timeout;
  51. while (absl::Now() < deadline) {
  52. #if defined(ABSL_HAVE_ALARM)
  53. sig_t old_alarm = SIG_DFL;
  54. if (alarm_policy == AlarmPolicy::kWithAlarm) {
  55. alarm_handler_invoked = false;
  56. old_alarm = signal(SIGALRM, AlarmHandler);
  57. alarm(absl::ToInt64Seconds(d / 2));
  58. }
  59. #else
  60. EXPECT_EQ(alarm_policy, AlarmPolicy::kWithoutAlarm);
  61. #endif
  62. ++*attempts;
  63. absl::Time start = absl::Now();
  64. absl::SleepFor(d);
  65. absl::Duration actual = absl::Now() - start;
  66. #if defined(ABSL_HAVE_ALARM)
  67. if (alarm_policy == AlarmPolicy::kWithAlarm) {
  68. signal(SIGALRM, old_alarm);
  69. if (!alarm_handler_invoked) continue;
  70. }
  71. #endif
  72. if (lower_bound <= actual && actual <= upper_bound) {
  73. return true; // yes, the SleepFor() was correctly bounded
  74. }
  75. }
  76. return false;
  77. }
  78. testing::AssertionResult AssertSleepForBounded(absl::Duration d,
  79. absl::Duration early,
  80. absl::Duration late,
  81. absl::Duration timeout,
  82. AlarmPolicy alarm_policy) {
  83. const absl::Duration lower_bound = d - early;
  84. const absl::Duration upper_bound = d + late;
  85. int attempts = 0;
  86. if (SleepForBounded(d, lower_bound, upper_bound, timeout, alarm_policy,
  87. &attempts)) {
  88. return testing::AssertionSuccess();
  89. }
  90. return testing::AssertionFailure()
  91. << "SleepFor(" << d << ") did not return within [" << lower_bound
  92. << ":" << upper_bound << "] in " << attempts << " attempt"
  93. << (attempts == 1 ? "" : "s") << " over " << timeout
  94. << (alarm_policy == AlarmPolicy::kWithAlarm ? " with" : " without")
  95. << " an alarm";
  96. }
  97. // Tests that SleepFor() returns neither too early nor too late.
  98. TEST(SleepFor, Bounded) {
  99. const absl::Duration d = absl::Milliseconds(2500);
  100. const absl::Duration early = absl::Milliseconds(100);
  101. const absl::Duration late = absl::Milliseconds(300);
  102. const absl::Duration timeout = 48 * d;
  103. EXPECT_TRUE(AssertSleepForBounded(d, early, late, timeout,
  104. AlarmPolicy::kWithoutAlarm));
  105. #if defined(ABSL_HAVE_ALARM)
  106. EXPECT_TRUE(AssertSleepForBounded(d, early, late, timeout,
  107. AlarmPolicy::kWithAlarm));
  108. #endif
  109. }
  110. } // namespace