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/ClockGettimeWrappers.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(ClockGettimeWrappers, clockGettimeWrapperIsWithin120SecsOfSystemClock) {
33 auto ret = folly::chrono::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 TEST(ClockGettimeWrappers, clockGettimeNsWrapperIsWithin120SecsOfSystemClock) {
45 auto now_ns = folly::chrono::clock_gettime_ns(CLOCK_REALTIME);
46 auto stdChronoSystemClockNow =
47 std::chrono::system_clock::now().time_since_epoch();
48 ASSERT_TRUE(AreWithinSecs(
49 std::chrono::nanoseconds(now_ns),
50 stdChronoSystemClockNow,
51 kAcceptableDeltaSecs));
54 #endif /* CLOCK_REALTIME */