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 | |
17 | #include <folly/portability/Time.h> |
18 | |
19 | #include <folly/portability/GTest.h> |
20 | #include <folly/test/TestUtils.h> |
21 | |
22 | #include <chrono> |
23 | |
24 | static constexpr auto kAcceptableDeltaSecs = std::chrono::seconds(120); |
25 | |
26 | using folly::test::AreWithinSecs; |
27 | |
28 | #ifdef CLOCK_REALTIME |
29 | |
30 | TEST(Time, clockGettimeRealtimeAreWithin120SecsOfStdChronoSystemClock) { |
31 | struct timespec ts; |
32 | auto ret = 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(); |
39 | ASSERT_TRUE(AreWithinSecs( |
40 | gettimeResult, stdChronoSystemClockNow, kAcceptableDeltaSecs)); |
41 | } |
42 | |
43 | #endif /* CLOCK_REALTIME */ |
44 | |
45 | #ifdef CLOCK_MONOTONIC |
46 | |
47 | TEST(Time, clockGettimeMonotonicAreWithin120SecsOfStdChronoSteadyClock) { |
48 | struct timespec ts; |
49 | auto ret = clock_gettime(CLOCK_MONOTONIC, &ts); |
50 | ASSERT_EQ(0, ret); |
51 | |
52 | auto gettimeResult = |
53 | std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec); |
54 | auto stdChronoSteadyClockNow = |
55 | std::chrono::steady_clock::now().time_since_epoch(); |
56 | ASSERT_TRUE(AreWithinSecs( |
57 | gettimeResult, stdChronoSteadyClockNow, kAcceptableDeltaSecs)); |
58 | } |
59 | |
60 | #endif /* CLOCK_MONOTONIC */ |
61 | |
62 | #ifdef CLOCK_PROCESS_CPUTIME_ID |
63 | |
64 | TEST(Time, clockGettimeProcessCputimeIsGreaterThanZero) { |
65 | struct timespec ts; |
66 | auto ret = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); |
67 | ASSERT_EQ(0, ret); |
68 | |
69 | auto gettimeResult = |
70 | std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec); |
71 | |
72 | ASSERT_GT(gettimeResult, std::chrono::nanoseconds::zero()); |
73 | } |
74 | |
75 | #endif /* CLOCK_PROCESS_CPUTIME_ID */ |
76 | |
77 | #ifdef CLOCK_THREAD_CPUTIME_ID |
78 | |
79 | TEST(Time, clockGettimeProcessThreadTimeIsGreaterThanZero) { |
80 | struct timespec ts; |
81 | auto ret = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); |
82 | ASSERT_EQ(0, ret); |
83 | |
84 | auto gettimeResult = |
85 | std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec); |
86 | |
87 | ASSERT_GT(gettimeResult, std::chrono::nanoseconds::zero()); |
88 | } |
89 | |
90 | #endif /* CLOCK_THREAD_CPUTIME_ID */ |
91 | |