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