clock_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
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 // Does SleepFor(d) take between lower_bound and upper_bound at least
00050 // once between now and (now + timeout)?  If requested (and supported),
00051 // add an alarm for the middle of the sleep period and expect it to fire.
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;  // yes, the SleepFor() was correctly bounded
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 // Tests that SleepFor() returns neither too early nor too late.
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 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14