bloaty/third_party/abseil-cpp/absl/time/clock_test.cc
Go to the documentation of this file.
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 
30 TEST(Time, Now) {
32  const absl::Time now = absl::Now();
36 }
37 
38 enum class AlarmPolicy { kWithoutAlarm, kWithAlarm };
39 
40 #if defined(ABSL_HAVE_ALARM)
41 bool alarm_handler_invoked = false;
42 
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.
52 bool SleepForBounded(absl::Duration d, absl::Duration lower_bound,
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;
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 
84 testing::AssertionResult AssertSleepForBounded(absl::Duration d,
85  absl::Duration early,
86  absl::Duration late,
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)) {
95  }
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.
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
testing::AssertionFailure
AssertionResult AssertionFailure()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:1028
now
static double now(void)
Definition: test/core/fling/client.cc:130
absl::Time
Definition: third_party/abseil-cpp/absl/time/time.h:642
absl::FromUnixNanos
constexpr Time FromUnixNanos(int64_t ns)
Definition: third_party/abseil-cpp/absl/time/time.h:1597
absl::SleepFor
void SleepFor(absl::Duration duration)
Definition: abseil-cpp/absl/time/clock.h:70
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
before
IntBeforeRegisterTypedTestSuiteP before
Definition: googletest/googletest/test/gtest-typed-test_test.cc:376
absl::GetCurrentTimeNanos
ABSL_NAMESPACE_BEGIN int64_t GetCurrentTimeNanos()
Definition: abseil-cpp/absl/time/clock.cc:78
absl::Milliseconds
constexpr Duration Milliseconds(T n)
Definition: third_party/abseil-cpp/absl/time/time.h:415
start
static uint64_t start
Definition: benchmark-pound.c:74
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
signal
static void signal(notification *n)
Definition: alts_tsi_handshaker_test.cc:107
testing::AssertionResult
Definition: cares/cares/test/gmock-1.8.0/gtest/gtest.h:18855
absl::Duration
Definition: third_party/abseil-cpp/absl/time/time.h:159
testing::AssertionSuccess
AssertionResult AssertionSuccess()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:1023
d
static const fe d
Definition: curve25519_tables.h:19
after
IntAfterTypedTestSuiteP after
Definition: googletest/googletest/test/gtest-typed-test_test.cc:375
absl::Now
ABSL_NAMESPACE_BEGIN Time Now()
Definition: abseil-cpp/absl/time/clock.cc:39
EXPECT_GE
#define EXPECT_GE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2034
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
absl::ToInt64Seconds
int64_t ToInt64Seconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:568


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:56