SystemClock_test.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  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 <unistd.h>
  17. #include <utils/SystemClock.h>
  18. #include <gtest/gtest.h>
  19. static const auto MS_IN_NS = 1000000;
  20. static const int64_t SLEEP_MS = 500;
  21. static const int64_t SLEEP_NS = SLEEP_MS * MS_IN_NS;
  22. // Conservatively assume that we might be descheduled for up to 50 ms
  23. static const int64_t SLACK_MS = 50;
  24. static const int64_t SLACK_NS = SLACK_MS * MS_IN_NS;
  25. TEST(SystemClock, SystemClock) {
  26. auto startUptimeMs = android::uptimeMillis();
  27. auto startRealtimeMs = android::elapsedRealtime();
  28. auto startRealtimeNs = android::elapsedRealtimeNano();
  29. ASSERT_GT(startUptimeMs, 0)
  30. << "uptimeMillis() reported an impossible uptime";
  31. ASSERT_GE(startRealtimeMs, startUptimeMs)
  32. << "elapsedRealtime() thinks we've suspended for negative time";
  33. ASSERT_GE(startRealtimeNs, startUptimeMs * MS_IN_NS)
  34. << "elapsedRealtimeNano() thinks we've suspended for negative time";
  35. ASSERT_GE(startRealtimeNs, startRealtimeMs * MS_IN_NS)
  36. << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent";
  37. ASSERT_LT(startRealtimeNs, (startRealtimeMs + SLACK_MS) * MS_IN_NS)
  38. << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent";
  39. timespec ts;
  40. ts.tv_sec = 0;
  41. ts.tv_nsec = SLEEP_MS * MS_IN_NS;
  42. auto nanosleepErr = TEMP_FAILURE_RETRY(nanosleep(&ts, nullptr));
  43. ASSERT_EQ(nanosleepErr, 0) << "nanosleep() failed: " << strerror(errno);
  44. auto endUptimeMs = android::uptimeMillis();
  45. auto endRealtimeMs = android::elapsedRealtime();
  46. auto endRealtimeNs = android::elapsedRealtimeNano();
  47. EXPECT_GE(endUptimeMs - startUptimeMs, SLEEP_MS)
  48. << "uptimeMillis() advanced too little after nanosleep()";
  49. EXPECT_LT(endUptimeMs - startUptimeMs, SLEEP_MS + SLACK_MS)
  50. << "uptimeMillis() advanced too much after nanosleep()";
  51. EXPECT_GE(endRealtimeMs - startRealtimeMs, SLEEP_MS)
  52. << "elapsedRealtime() advanced too little after nanosleep()";
  53. EXPECT_LT(endRealtimeMs - startRealtimeMs, SLEEP_MS + SLACK_MS)
  54. << "elapsedRealtime() advanced too much after nanosleep()";
  55. EXPECT_GE(endRealtimeNs - startRealtimeNs, SLEEP_NS)
  56. << "elapsedRealtimeNano() advanced too little after nanosleep()";
  57. EXPECT_LT(endRealtimeNs - startRealtimeNs, SLEEP_NS + SLACK_NS)
  58. << "elapsedRealtimeNano() advanced too much after nanosleep()";
  59. EXPECT_GE(endRealtimeNs, endRealtimeMs * MS_IN_NS)
  60. << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent after nanosleep()";
  61. EXPECT_LT(endRealtimeNs, (endRealtimeMs + SLACK_MS) * MS_IN_NS)
  62. << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent after nanosleep()";
  63. }