2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/portability/Time.h>
19 #include <folly/test/TestUtils.h>
23 #include <gtest/gtest.h>
25 static constexpr auto kAcceptableDeltaSecs = std::chrono::seconds(120);
27 using folly::test::AreWithinSecs;
31 TEST(Time, clockGettimeRealtimeAreWithin120SecsOfStdChronoSystemClock) {
33 auto ret = clock_gettime(CLOCK_REALTIME, &ts);
37 std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec);
38 auto stdChronoSystemClockNow =
39 std::chrono::system_clock::now().time_since_epoch();
40 ASSERT_TRUE(AreWithinSecs(
41 gettimeResult, stdChronoSystemClockNow, kAcceptableDeltaSecs));
44 #endif /* CLOCK_REALTIME */
46 #ifdef CLOCK_MONOTONIC
48 TEST(Time, clockGettimeMonotonicAreWithin120SecsOfStdChronoSteadyClock) {
50 auto ret = clock_gettime(CLOCK_MONOTONIC, &ts);
54 std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec);
55 auto stdChronoSteadyClockNow =
56 std::chrono::steady_clock::now().time_since_epoch();
57 ASSERT_TRUE(AreWithinSecs(
58 gettimeResult, stdChronoSteadyClockNow, kAcceptableDeltaSecs));
61 #endif /* CLOCK_MONOTONIC */
63 #ifdef CLOCK_PROCESS_CPUTIME_ID
65 TEST(Time, clockGettimeProcessCputimeIsGreaterThanZero) {
67 auto ret = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
71 std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec);
73 ASSERT_GT(gettimeResult, std::chrono::nanoseconds::zero());
76 #endif /* CLOCK_PROCESS_CPUTIME_ID */
78 #ifdef CLOCK_THREAD_CPUTIME_ID
80 TEST(Time, clockGettimeProcessThreadTimeIsGreaterThanZero) {
82 auto ret = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
86 std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec);
88 ASSERT_GT(gettimeResult, std::chrono::nanoseconds::zero());
91 #endif /* CLOCK_THREAD_CPUTIME_ID */