metrics_utils_unittest.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Copyright (C) 2015 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 "update_engine/metrics_utils.h"
  17. #include <gtest/gtest.h>
  18. #include "update_engine/common/fake_clock.h"
  19. #include "update_engine/common/fake_prefs.h"
  20. #include "update_engine/fake_system_state.h"
  21. namespace chromeos_update_engine {
  22. namespace metrics_utils {
  23. class MetricsUtilsTest : public ::testing::Test {};
  24. TEST(MetricsUtilsTest, GetConnectionType) {
  25. // Check that expected combinations map to the right value.
  26. EXPECT_EQ(metrics::ConnectionType::kUnknown,
  27. GetConnectionType(ConnectionType::kUnknown,
  28. ConnectionTethering::kUnknown));
  29. EXPECT_EQ(metrics::ConnectionType::kDisconnected,
  30. GetConnectionType(ConnectionType::kDisconnected,
  31. ConnectionTethering::kUnknown));
  32. EXPECT_EQ(metrics::ConnectionType::kEthernet,
  33. GetConnectionType(ConnectionType::kEthernet,
  34. ConnectionTethering::kUnknown));
  35. EXPECT_EQ(
  36. metrics::ConnectionType::kWifi,
  37. GetConnectionType(ConnectionType::kWifi, ConnectionTethering::kUnknown));
  38. EXPECT_EQ(
  39. metrics::ConnectionType::kWimax,
  40. GetConnectionType(ConnectionType::kWimax, ConnectionTethering::kUnknown));
  41. EXPECT_EQ(metrics::ConnectionType::kBluetooth,
  42. GetConnectionType(ConnectionType::kBluetooth,
  43. ConnectionTethering::kUnknown));
  44. EXPECT_EQ(metrics::ConnectionType::kCellular,
  45. GetConnectionType(ConnectionType::kCellular,
  46. ConnectionTethering::kUnknown));
  47. EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
  48. GetConnectionType(ConnectionType::kEthernet,
  49. ConnectionTethering::kConfirmed));
  50. EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
  51. GetConnectionType(ConnectionType::kWifi,
  52. ConnectionTethering::kConfirmed));
  53. // Ensure that we don't report tethered ethernet unless it's confirmed.
  54. EXPECT_EQ(metrics::ConnectionType::kEthernet,
  55. GetConnectionType(ConnectionType::kEthernet,
  56. ConnectionTethering::kNotDetected));
  57. EXPECT_EQ(metrics::ConnectionType::kEthernet,
  58. GetConnectionType(ConnectionType::kEthernet,
  59. ConnectionTethering::kSuspected));
  60. EXPECT_EQ(metrics::ConnectionType::kEthernet,
  61. GetConnectionType(ConnectionType::kEthernet,
  62. ConnectionTethering::kUnknown));
  63. // Ditto for tethered wifi.
  64. EXPECT_EQ(metrics::ConnectionType::kWifi,
  65. GetConnectionType(ConnectionType::kWifi,
  66. ConnectionTethering::kNotDetected));
  67. EXPECT_EQ(metrics::ConnectionType::kWifi,
  68. GetConnectionType(ConnectionType::kWifi,
  69. ConnectionTethering::kSuspected));
  70. EXPECT_EQ(
  71. metrics::ConnectionType::kWifi,
  72. GetConnectionType(ConnectionType::kWifi, ConnectionTethering::kUnknown));
  73. }
  74. TEST(MetricsUtilsTest, WallclockDurationHelper) {
  75. FakeSystemState fake_system_state;
  76. FakeClock fake_clock;
  77. base::TimeDelta duration;
  78. const std::string state_variable_key = "test-prefs";
  79. FakePrefs fake_prefs;
  80. fake_system_state.set_clock(&fake_clock);
  81. fake_system_state.set_prefs(&fake_prefs);
  82. // Initialize wallclock to 1 sec.
  83. fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
  84. // First time called so no previous measurement available.
  85. EXPECT_FALSE(metrics_utils::WallclockDurationHelper(
  86. &fake_system_state, state_variable_key, &duration));
  87. // Next time, we should get zero since the clock didn't advance.
  88. EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
  89. &fake_system_state, state_variable_key, &duration));
  90. EXPECT_EQ(duration.InSeconds(), 0);
  91. // We can also call it as many times as we want with it being
  92. // considered a failure.
  93. EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
  94. &fake_system_state, state_variable_key, &duration));
  95. EXPECT_EQ(duration.InSeconds(), 0);
  96. EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
  97. &fake_system_state, state_variable_key, &duration));
  98. EXPECT_EQ(duration.InSeconds(), 0);
  99. // Advance the clock one second, then we should get 1 sec on the
  100. // next call and 0 sec on the subsequent call.
  101. fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
  102. EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
  103. &fake_system_state, state_variable_key, &duration));
  104. EXPECT_EQ(duration.InSeconds(), 1);
  105. EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
  106. &fake_system_state, state_variable_key, &duration));
  107. EXPECT_EQ(duration.InSeconds(), 0);
  108. // Advance clock two seconds and we should get 2 sec and then 0 sec.
  109. fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
  110. EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
  111. &fake_system_state, state_variable_key, &duration));
  112. EXPECT_EQ(duration.InSeconds(), 2);
  113. EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
  114. &fake_system_state, state_variable_key, &duration));
  115. EXPECT_EQ(duration.InSeconds(), 0);
  116. // There's a possibility that the wallclock can go backwards (NTP
  117. // adjustments, for example) so check that we properly handle this
  118. // case.
  119. fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
  120. EXPECT_FALSE(metrics_utils::WallclockDurationHelper(
  121. &fake_system_state, state_variable_key, &duration));
  122. fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
  123. EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
  124. &fake_system_state, state_variable_key, &duration));
  125. EXPECT_EQ(duration.InSeconds(), 1);
  126. }
  127. TEST(MetricsUtilsTest, MonotonicDurationHelper) {
  128. int64_t storage = 0;
  129. FakeSystemState fake_system_state;
  130. FakeClock fake_clock;
  131. base::TimeDelta duration;
  132. fake_system_state.set_clock(&fake_clock);
  133. // Initialize monotonic clock to 1 sec.
  134. fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
  135. // First time called so no previous measurement available.
  136. EXPECT_FALSE(metrics_utils::MonotonicDurationHelper(
  137. &fake_system_state, &storage, &duration));
  138. // Next time, we should get zero since the clock didn't advance.
  139. EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
  140. &fake_system_state, &storage, &duration));
  141. EXPECT_EQ(duration.InSeconds(), 0);
  142. // We can also call it as many times as we want with it being
  143. // considered a failure.
  144. EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
  145. &fake_system_state, &storage, &duration));
  146. EXPECT_EQ(duration.InSeconds(), 0);
  147. EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
  148. &fake_system_state, &storage, &duration));
  149. EXPECT_EQ(duration.InSeconds(), 0);
  150. // Advance the clock one second, then we should get 1 sec on the
  151. // next call and 0 sec on the subsequent call.
  152. fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
  153. EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
  154. &fake_system_state, &storage, &duration));
  155. EXPECT_EQ(duration.InSeconds(), 1);
  156. EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
  157. &fake_system_state, &storage, &duration));
  158. EXPECT_EQ(duration.InSeconds(), 0);
  159. // Advance clock two seconds and we should get 2 sec and then 0 sec.
  160. fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
  161. EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
  162. &fake_system_state, &storage, &duration));
  163. EXPECT_EQ(duration.InSeconds(), 2);
  164. EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
  165. &fake_system_state, &storage, &duration));
  166. EXPECT_EQ(duration.InSeconds(), 0);
  167. }
  168. } // namespace metrics_utils
  169. } // namespace chromeos_update_engine