proxygen
ClockGettimeWrappersTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <chrono>
20 
22 #include <folly/test/TestUtils.h>
23 
24 static constexpr auto kAcceptableDeltaSecs = std::chrono::seconds(120);
25 
27 
28 #ifdef CLOCK_REALTIME
29 
30 TEST(ClockGettimeWrappers, clockGettimeWrapperIsWithin120SecsOfSystemClock) {
31  struct timespec ts;
32  auto ret = folly::chrono::clock_gettime(CLOCK_REALTIME, &ts);
33  ASSERT_EQ(0, ret);
34 
35  auto gettimeResult =
36  std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec);
37  auto stdChronoSystemClockNow =
38  std::chrono::system_clock::now().time_since_epoch();
40  gettimeResult, stdChronoSystemClockNow, kAcceptableDeltaSecs));
41 }
42 
43 TEST(ClockGettimeWrappers, clockGettimeNsWrapperIsWithin120SecsOfSystemClock) {
44  auto now_ns = folly::chrono::clock_gettime_ns(CLOCK_REALTIME);
45  auto stdChronoSystemClockNow =
46  std::chrono::system_clock::now().time_since_epoch();
48  std::chrono::nanoseconds(now_ns),
49  stdChronoSystemClockNow,
51 }
52 
53 #endif /* CLOCK_REALTIME */
::testing::AssertionResult AreWithinSecs(T1 val1, T2 val2, std::chrono::seconds acceptableDeltaSecs)
Definition: TestUtils.h:129
#define TEST(test_case_name, test_name)
Definition: gtest.h:2187
int(* clock_gettime)(clockid_t, timespec *ts)
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
int64_t(* clock_gettime_ns)(clockid_t)
std::chrono::steady_clock::time_point now()
static constexpr auto kAcceptableDeltaSecs
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865