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