Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/time/clock.h"
00016
00017 #include "absl/base/config.h"
00018 #if defined(ABSL_HAVE_ALARM)
00019 #include <signal.h>
00020 #include <unistd.h>
00021 #elif defined(__linux__) || defined(__APPLE__)
00022 #error all known Linux and Apple targets have alarm
00023 #endif
00024
00025 #include "gtest/gtest.h"
00026 #include "absl/time/time.h"
00027
00028 namespace {
00029
00030 TEST(Time, Now) {
00031 const absl::Time before = absl::FromUnixNanos(absl::GetCurrentTimeNanos());
00032 const absl::Time now = absl::Now();
00033 const absl::Time after = absl::FromUnixNanos(absl::GetCurrentTimeNanos());
00034 EXPECT_GE(now, before);
00035 EXPECT_GE(after, now);
00036 }
00037
00038 enum class AlarmPolicy { kWithoutAlarm, kWithAlarm };
00039
00040 #if defined(ABSL_HAVE_ALARM)
00041 bool alarm_handler_invoked = false;
00042
00043 void AlarmHandler(int signo) {
00044 ASSERT_EQ(signo, SIGALRM);
00045 alarm_handler_invoked = true;
00046 }
00047 #endif
00048
00049
00050
00051
00052 bool SleepForBounded(absl::Duration d, absl::Duration lower_bound,
00053 absl::Duration upper_bound, absl::Duration timeout,
00054 AlarmPolicy alarm_policy, int* attempts) {
00055 const absl::Time deadline = absl::Now() + timeout;
00056 while (absl::Now() < deadline) {
00057 #if defined(ABSL_HAVE_ALARM)
00058 sig_t old_alarm = SIG_DFL;
00059 if (alarm_policy == AlarmPolicy::kWithAlarm) {
00060 alarm_handler_invoked = false;
00061 old_alarm = signal(SIGALRM, AlarmHandler);
00062 alarm(absl::ToInt64Seconds(d / 2));
00063 }
00064 #else
00065 EXPECT_EQ(alarm_policy, AlarmPolicy::kWithoutAlarm);
00066 #endif
00067 ++*attempts;
00068 absl::Time start = absl::Now();
00069 absl::SleepFor(d);
00070 absl::Duration actual = absl::Now() - start;
00071 #if defined(ABSL_HAVE_ALARM)
00072 if (alarm_policy == AlarmPolicy::kWithAlarm) {
00073 signal(SIGALRM, old_alarm);
00074 if (!alarm_handler_invoked) continue;
00075 }
00076 #endif
00077 if (lower_bound <= actual && actual <= upper_bound) {
00078 return true;
00079 }
00080 }
00081 return false;
00082 }
00083
00084 testing::AssertionResult AssertSleepForBounded(absl::Duration d,
00085 absl::Duration early,
00086 absl::Duration late,
00087 absl::Duration timeout,
00088 AlarmPolicy alarm_policy) {
00089 const absl::Duration lower_bound = d - early;
00090 const absl::Duration upper_bound = d + late;
00091 int attempts = 0;
00092 if (SleepForBounded(d, lower_bound, upper_bound, timeout, alarm_policy,
00093 &attempts)) {
00094 return testing::AssertionSuccess();
00095 }
00096 return testing::AssertionFailure()
00097 << "SleepFor(" << d << ") did not return within [" << lower_bound
00098 << ":" << upper_bound << "] in " << attempts << " attempt"
00099 << (attempts == 1 ? "" : "s") << " over " << timeout
00100 << (alarm_policy == AlarmPolicy::kWithAlarm ? " with" : " without")
00101 << " an alarm";
00102 }
00103
00104
00105 TEST(SleepFor, Bounded) {
00106 const absl::Duration d = absl::Milliseconds(2500);
00107 const absl::Duration early = absl::Milliseconds(100);
00108 const absl::Duration late = absl::Milliseconds(300);
00109 const absl::Duration timeout = 48 * d;
00110 EXPECT_TRUE(AssertSleepForBounded(d, early, late, timeout,
00111 AlarmPolicy::kWithoutAlarm));
00112 #if defined(ABSL_HAVE_ALARM)
00113 EXPECT_TRUE(AssertSleepForBounded(d, early, late, timeout,
00114 AlarmPolicy::kWithAlarm));
00115 #endif
00116 }
00117
00118 }