ClockGettimeWrappersTest.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include <folly/ClockGettimeWrappers.h>
  17. #include <chrono>
  18. #include <folly/portability/GTest.h>
  19. #include <folly/test/TestUtils.h>
  20. static constexpr auto kAcceptableDeltaSecs = std::chrono::seconds(120);
  21. using folly::test::AreWithinSecs;
  22. #ifdef CLOCK_REALTIME
  23. TEST(ClockGettimeWrappers, clockGettimeWrapperIsWithin120SecsOfSystemClock) {
  24. struct timespec ts;
  25. auto ret = folly::chrono::clock_gettime(CLOCK_REALTIME, &ts);
  26. ASSERT_EQ(0, ret);
  27. auto gettimeResult =
  28. std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec);
  29. auto stdChronoSystemClockNow =
  30. std::chrono::system_clock::now().time_since_epoch();
  31. ASSERT_TRUE(AreWithinSecs(
  32. gettimeResult, stdChronoSystemClockNow, kAcceptableDeltaSecs));
  33. }
  34. TEST(ClockGettimeWrappers, clockGettimeNsWrapperIsWithin120SecsOfSystemClock) {
  35. auto now_ns = folly::chrono::clock_gettime_ns(CLOCK_REALTIME);
  36. auto stdChronoSystemClockNow =
  37. std::chrono::system_clock::now().time_since_epoch();
  38. ASSERT_TRUE(AreWithinSecs(
  39. std::chrono::nanoseconds(now_ns),
  40. stdChronoSystemClockNow,
  41. kAcceptableDeltaSecs));
  42. }
  43. #endif /* CLOCK_REALTIME */