[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame^] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame^] | 5 | #include "components/metrics/metrics_log_manager.h" |
[email protected] | cfee9aa5 | 2013-10-19 17:53:05 | [diff] [blame] | 6 | |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
[email protected] | cfee9aa5 | 2013-10-19 17:53:05 | [diff] [blame] | 11 | #include "base/sha1.h" |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame^] | 12 | #include "components/metrics/metrics_log_base.h" |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame^] | 15 | namespace metrics { |
| 16 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 17 | namespace { |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 18 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 19 | // Dummy serializer that just stores logs in memory. |
| 20 | class DummyLogSerializer : public MetricsLogManager::LogSerializer { |
| 21 | public: |
[email protected] | cfee9aa5 | 2013-10-19 17:53:05 | [diff] [blame] | 22 | virtual void SerializeLogs( |
| 23 | const std::vector<MetricsLogManager::SerializedLog>& logs, |
| 24 | MetricsLogManager::LogType log_type) OVERRIDE { |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 25 | persisted_logs_[log_type] = logs; |
| 26 | } |
| 27 | |
[email protected] | cfee9aa5 | 2013-10-19 17:53:05 | [diff] [blame] | 28 | virtual void DeserializeLogs( |
| 29 | MetricsLogManager::LogType log_type, |
| 30 | std::vector<MetricsLogManager::SerializedLog>* logs) OVERRIDE { |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 31 | ASSERT_NE(static_cast<void*>(NULL), logs); |
| 32 | *logs = persisted_logs_[log_type]; |
| 33 | } |
| 34 | |
| 35 | // Returns the number of logs of the given type. |
| 36 | size_t TypeCount(MetricsLogManager::LogType log_type) { |
| 37 | return persisted_logs_[log_type].size(); |
| 38 | } |
| 39 | |
| 40 | // In-memory "persitent storage". |
[email protected] | cfee9aa5 | 2013-10-19 17:53:05 | [diff] [blame] | 41 | std::vector<MetricsLogManager::SerializedLog> persisted_logs_[2]; |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 42 | }; |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 43 | |
| 44 | } // namespace |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 45 | |
| 46 | TEST(MetricsLogManagerTest, StandardFlow) { |
| 47 | MetricsLogManager log_manager; |
| 48 | |
| 49 | // Make sure a new manager has a clean slate. |
| 50 | EXPECT_EQ(NULL, log_manager.current_log()); |
| 51 | EXPECT_FALSE(log_manager.has_staged_log()); |
| 52 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 53 | |
| 54 | // Check that the normal flow works. |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 55 | MetricsLogBase* initial_log = |
| 56 | new MetricsLogBase("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "v"); |
| 57 | log_manager.BeginLoggingWithLog(initial_log); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 58 | EXPECT_EQ(initial_log, log_manager.current_log()); |
| 59 | EXPECT_FALSE(log_manager.has_staged_log()); |
| 60 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 61 | log_manager.FinishCurrentLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 62 | EXPECT_EQ(NULL, log_manager.current_log()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 63 | EXPECT_TRUE(log_manager.has_unsent_logs()); |
| 64 | EXPECT_FALSE(log_manager.has_staged_log()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 65 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 66 | MetricsLogBase* second_log = |
| 67 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "version"); |
| 68 | log_manager.BeginLoggingWithLog(second_log); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 69 | EXPECT_EQ(second_log, log_manager.current_log()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 70 | |
| 71 | log_manager.StageNextLogForUpload(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 72 | EXPECT_TRUE(log_manager.has_staged_log()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 73 | EXPECT_FALSE(log_manager.staged_log_text().empty()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 74 | |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 75 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 76 | EXPECT_EQ(second_log, log_manager.current_log()); |
| 77 | EXPECT_FALSE(log_manager.has_staged_log()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 78 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 79 | EXPECT_TRUE(log_manager.staged_log_text().empty()); |
| 80 | |
| 81 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 82 | } |
| 83 | |
| 84 | TEST(MetricsLogManagerTest, AbandonedLog) { |
| 85 | MetricsLogManager log_manager; |
| 86 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 87 | MetricsLogBase* dummy_log = |
| 88 | new MetricsLogBase("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "v"); |
| 89 | log_manager.BeginLoggingWithLog(dummy_log); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 90 | EXPECT_EQ(dummy_log, log_manager.current_log()); |
| 91 | |
| 92 | log_manager.DiscardCurrentLog(); |
| 93 | EXPECT_EQ(NULL, log_manager.current_log()); |
| 94 | EXPECT_FALSE(log_manager.has_staged_log()); |
| 95 | } |
| 96 | |
| 97 | TEST(MetricsLogManagerTest, InterjectedLog) { |
| 98 | MetricsLogManager log_manager; |
| 99 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 100 | MetricsLogBase* ongoing_log = |
| 101 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "v"); |
| 102 | MetricsLogBase* temp_log = |
| 103 | new MetricsLogBase("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "v"); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 104 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 105 | log_manager.BeginLoggingWithLog(ongoing_log); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 106 | EXPECT_EQ(ongoing_log, log_manager.current_log()); |
| 107 | |
| 108 | log_manager.PauseCurrentLog(); |
| 109 | EXPECT_EQ(NULL, log_manager.current_log()); |
| 110 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 111 | log_manager.BeginLoggingWithLog(temp_log); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 112 | EXPECT_EQ(temp_log, log_manager.current_log()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 113 | log_manager.FinishCurrentLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 114 | EXPECT_EQ(NULL, log_manager.current_log()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 115 | |
| 116 | log_manager.ResumePausedLog(); |
| 117 | EXPECT_EQ(ongoing_log, log_manager.current_log()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 118 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 119 | EXPECT_FALSE(log_manager.has_staged_log()); |
| 120 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 121 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 122 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 123 | } |
| 124 | |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 125 | TEST(MetricsLogManagerTest, InterjectedLogPreservesType) { |
| 126 | MetricsLogManager log_manager; |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 127 | DummyLogSerializer* serializer = new DummyLogSerializer; |
| 128 | log_manager.set_log_serializer(serializer); |
| 129 | log_manager.LoadPersistedUnsentLogs(); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 130 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 131 | MetricsLogBase* ongoing_log = |
| 132 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "v"); |
| 133 | MetricsLogBase* temp_log = |
| 134 | new MetricsLogBase("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "v"); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 135 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 136 | log_manager.BeginLoggingWithLog(ongoing_log); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 137 | log_manager.PauseCurrentLog(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 138 | log_manager.BeginLoggingWithLog(temp_log); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 139 | log_manager.FinishCurrentLog(); |
| 140 | log_manager.ResumePausedLog(); |
| 141 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 142 | log_manager.DiscardStagedLog(); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 143 | |
| 144 | // Verify that the remaining log (which is the original ongoing log) still |
| 145 | // has the right type. |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 146 | log_manager.FinishCurrentLog(); |
| 147 | log_manager.PersistUnsentLogs(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 148 | EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_STABILITY_LOG)); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 149 | EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 152 | TEST(MetricsLogManagerTest, StoreAndLoad) { |
[email protected] | cfee9aa5 | 2013-10-19 17:53:05 | [diff] [blame] | 153 | std::vector<MetricsLogManager::SerializedLog> initial_logs; |
| 154 | std::vector<MetricsLogManager::SerializedLog> ongoing_logs; |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 155 | |
| 156 | // Set up some in-progress logging in a scoped log manager simulating the |
| 157 | // leadup to quitting, then persist as would be done on quit. |
| 158 | { |
| 159 | MetricsLogManager log_manager; |
| 160 | DummyLogSerializer* serializer = new DummyLogSerializer; |
| 161 | log_manager.set_log_serializer(serializer); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 162 | log_manager.LoadPersistedUnsentLogs(); |
| 163 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 164 | // Simulate a log having already been unsent from a previous session. |
[email protected] | cfee9aa5 | 2013-10-19 17:53:05 | [diff] [blame] | 165 | MetricsLogManager::SerializedLog log; |
| 166 | std::string text = "proto"; |
| 167 | log.SwapLogText(&text); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 168 | serializer->persisted_logs_[MetricsLogBase::ONGOING_LOG].push_back(log); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 169 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 170 | log_manager.LoadPersistedUnsentLogs(); |
| 171 | EXPECT_TRUE(log_manager.has_unsent_logs()); |
| 172 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 173 | MetricsLogBase* log1 = |
| 174 | new MetricsLogBase("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "v"); |
| 175 | MetricsLogBase* log2 = |
| 176 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "v"); |
| 177 | log_manager.BeginLoggingWithLog(log1); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 178 | log_manager.FinishCurrentLog(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 179 | log_manager.BeginLoggingWithLog(log2); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 180 | log_manager.StageNextLogForUpload(); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 181 | log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 182 | log_manager.FinishCurrentLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 183 | |
| 184 | // Nothing should be written out until PersistUnsentLogs is called. |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 185 | EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_STABILITY_LOG)); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 186 | EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 187 | log_manager.PersistUnsentLogs(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 188 | EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::INITIAL_STABILITY_LOG)); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 189 | EXPECT_EQ(2U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 190 | |
| 191 | // Save the logs to transfer over to a new serializer (since log_manager |
| 192 | // owns |serializer|, so it's about to go away. |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 193 | initial_logs = |
| 194 | serializer->persisted_logs_[MetricsLogBase::INITIAL_STABILITY_LOG]; |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 195 | ongoing_logs = serializer->persisted_logs_[MetricsLogBase::ONGOING_LOG]; |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Now simulate the relaunch, ensure that the log manager restores |
| 199 | // everything correctly, and verify that once the are handled they are not |
| 200 | // re-persisted. |
| 201 | { |
| 202 | MetricsLogManager log_manager; |
| 203 | |
| 204 | DummyLogSerializer* serializer = new DummyLogSerializer; |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 205 | serializer->persisted_logs_[MetricsLogBase::INITIAL_STABILITY_LOG] = |
| 206 | initial_logs; |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 207 | serializer->persisted_logs_[MetricsLogBase::ONGOING_LOG] = ongoing_logs; |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 208 | |
| 209 | log_manager.set_log_serializer(serializer); |
| 210 | log_manager.LoadPersistedUnsentLogs(); |
| 211 | EXPECT_TRUE(log_manager.has_unsent_logs()); |
| 212 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 213 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 214 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 215 | // The initial log should be sent first; update the persisted storage to |
| 216 | // verify. |
| 217 | log_manager.PersistUnsentLogs(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 218 | EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_STABILITY_LOG)); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 219 | EXPECT_EQ(2U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 220 | |
| 221 | // Handle the first ongoing log. |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 222 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 223 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 224 | EXPECT_TRUE(log_manager.has_unsent_logs()); |
| 225 | |
| 226 | // Handle the last log. |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 227 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 228 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 229 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 230 | |
| 231 | // Nothing should have changed "on disk" since PersistUnsentLogs hasn't been |
| 232 | // called again. |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 233 | EXPECT_EQ(2U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 234 | // Persist, and make sure nothing is left. |
| 235 | log_manager.PersistUnsentLogs(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 236 | EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_STABILITY_LOG)); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 237 | EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 241 | TEST(MetricsLogManagerTest, StoreStagedLogTypes) { |
| 242 | // Ensure that types are preserved when storing staged logs. |
| 243 | { |
| 244 | MetricsLogManager log_manager; |
| 245 | DummyLogSerializer* serializer = new DummyLogSerializer; |
| 246 | log_manager.set_log_serializer(serializer); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 247 | log_manager.LoadPersistedUnsentLogs(); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 248 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 249 | MetricsLogBase* log = |
| 250 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "version"); |
| 251 | log_manager.BeginLoggingWithLog(log); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 252 | log_manager.FinishCurrentLog(); |
| 253 | log_manager.StageNextLogForUpload(); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 254 | log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 255 | log_manager.PersistUnsentLogs(); |
| 256 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 257 | EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_STABILITY_LOG)); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 258 | EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | { |
| 262 | MetricsLogManager log_manager; |
| 263 | DummyLogSerializer* serializer = new DummyLogSerializer; |
| 264 | log_manager.set_log_serializer(serializer); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 265 | log_manager.LoadPersistedUnsentLogs(); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 266 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 267 | MetricsLogBase* log = |
| 268 | new MetricsLogBase("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "v"); |
| 269 | log_manager.BeginLoggingWithLog(log); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 270 | log_manager.FinishCurrentLog(); |
| 271 | log_manager.StageNextLogForUpload(); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 272 | log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 273 | log_manager.PersistUnsentLogs(); |
| 274 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 275 | EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::INITIAL_STABILITY_LOG)); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 276 | EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 280 | TEST(MetricsLogManagerTest, LargeLogDiscarding) { |
| 281 | MetricsLogManager log_manager; |
| 282 | DummyLogSerializer* serializer = new DummyLogSerializer; |
| 283 | log_manager.set_log_serializer(serializer); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 284 | log_manager.LoadPersistedUnsentLogs(); |
| 285 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 286 | // Set the size threshold very low, to verify that it's honored. |
| 287 | log_manager.set_max_ongoing_log_store_size(1); |
| 288 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 289 | MetricsLogBase* log1 = |
| 290 | new MetricsLogBase("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "v"); |
| 291 | MetricsLogBase* log2 = |
| 292 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "v"); |
| 293 | log_manager.BeginLoggingWithLog(log1); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 294 | log_manager.FinishCurrentLog(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 295 | log_manager.BeginLoggingWithLog(log2); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 296 | log_manager.FinishCurrentLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 297 | |
| 298 | // Only the ongoing log should be written out, due to the threshold. |
| 299 | log_manager.PersistUnsentLogs(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 300 | EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::INITIAL_STABILITY_LOG)); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 301 | EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 302 | } |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 303 | |
| 304 | TEST(MetricsLogManagerTest, ProvisionalStoreStandardFlow) { |
| 305 | // Ensure that provisional store works, and discards the correct log. |
| 306 | { |
| 307 | MetricsLogManager log_manager; |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 308 | DummyLogSerializer* serializer = new DummyLogSerializer; |
| 309 | log_manager.set_log_serializer(serializer); |
| 310 | log_manager.LoadPersistedUnsentLogs(); |
| 311 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 312 | MetricsLogBase* log1 = |
| 313 | new MetricsLogBase("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "v"); |
| 314 | MetricsLogBase* log2 = |
| 315 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "v"); |
| 316 | log_manager.BeginLoggingWithLog(log1); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 317 | log_manager.FinishCurrentLog(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 318 | log_manager.BeginLoggingWithLog(log2); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 319 | log_manager.StageNextLogForUpload(); |
| 320 | log_manager.StoreStagedLogAsUnsent(MetricsLogManager::PROVISIONAL_STORE); |
| 321 | log_manager.FinishCurrentLog(); |
| 322 | log_manager.DiscardLastProvisionalStore(); |
| 323 | |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 324 | log_manager.PersistUnsentLogs(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 325 | EXPECT_EQ(0U, serializer->TypeCount(MetricsLogBase::INITIAL_STABILITY_LOG)); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 326 | EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | TEST(MetricsLogManagerTest, ProvisionalStoreNoop) { |
| 331 | // Ensure that trying to drop a sent log is a no-op, even if another log has |
| 332 | // since been staged. |
| 333 | { |
| 334 | MetricsLogManager log_manager; |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 335 | DummyLogSerializer* serializer = new DummyLogSerializer; |
| 336 | log_manager.set_log_serializer(serializer); |
| 337 | log_manager.LoadPersistedUnsentLogs(); |
| 338 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 339 | MetricsLogBase* log1 = |
| 340 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "version"); |
| 341 | MetricsLogBase* log2 = |
| 342 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "version"); |
| 343 | log_manager.BeginLoggingWithLog(log1); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 344 | log_manager.FinishCurrentLog(); |
| 345 | log_manager.StageNextLogForUpload(); |
| 346 | log_manager.StoreStagedLogAsUnsent(MetricsLogManager::PROVISIONAL_STORE); |
| 347 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 348 | log_manager.DiscardStagedLog(); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 349 | log_manager.BeginLoggingWithLog(log2); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 350 | log_manager.FinishCurrentLog(); |
| 351 | log_manager.StageNextLogForUpload(); |
| 352 | log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE); |
| 353 | log_manager.DiscardLastProvisionalStore(); |
| 354 | |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 355 | log_manager.PersistUnsentLogs(); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 356 | EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // Ensure that trying to drop more than once is a no-op |
| 360 | { |
| 361 | MetricsLogManager log_manager; |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 362 | DummyLogSerializer* serializer = new DummyLogSerializer; |
| 363 | log_manager.set_log_serializer(serializer); |
| 364 | log_manager.LoadPersistedUnsentLogs(); |
| 365 | |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 366 | MetricsLogBase* log1 = |
| 367 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "version"); |
| 368 | MetricsLogBase* log2 = |
| 369 | new MetricsLogBase("id", 0, MetricsLogBase::ONGOING_LOG, "version"); |
| 370 | log_manager.BeginLoggingWithLog(log1); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 371 | log_manager.FinishCurrentLog(); |
| 372 | log_manager.StageNextLogForUpload(); |
| 373 | log_manager.StoreStagedLogAsUnsent(MetricsLogManager::NORMAL_STORE); |
[email protected] | 9eae403 | 2014-04-09 19:15:19 | [diff] [blame] | 374 | log_manager.BeginLoggingWithLog(log2); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 375 | log_manager.FinishCurrentLog(); |
| 376 | log_manager.StageNextLogForUpload(); |
| 377 | log_manager.StoreStagedLogAsUnsent(MetricsLogManager::PROVISIONAL_STORE); |
| 378 | log_manager.DiscardLastProvisionalStore(); |
| 379 | log_manager.DiscardLastProvisionalStore(); |
| 380 | |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 381 | log_manager.PersistUnsentLogs(); |
[email protected] | 0edf876 | 2013-11-21 18:33:30 | [diff] [blame] | 382 | EXPECT_EQ(1U, serializer->TypeCount(MetricsLogBase::ONGOING_LOG)); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 383 | } |
| 384 | } |
[email protected] | cfee9aa5 | 2013-10-19 17:53:05 | [diff] [blame] | 385 | |
| 386 | TEST(MetricsLogManagerTest, SerializedLog) { |
| 387 | const char kFooText[] = "foo"; |
| 388 | const std::string foo_hash = base::SHA1HashString(kFooText); |
| 389 | const char kBarText[] = "bar"; |
| 390 | const std::string bar_hash = base::SHA1HashString(kBarText); |
| 391 | |
| 392 | MetricsLogManager::SerializedLog log; |
| 393 | EXPECT_TRUE(log.log_text().empty()); |
| 394 | EXPECT_TRUE(log.log_hash().empty()); |
| 395 | |
| 396 | std::string foo = kFooText; |
| 397 | log.SwapLogText(&foo); |
| 398 | EXPECT_TRUE(foo.empty()); |
| 399 | EXPECT_FALSE(log.IsEmpty()); |
| 400 | EXPECT_EQ(kFooText, log.log_text()); |
| 401 | EXPECT_EQ(foo_hash, log.log_hash()); |
| 402 | |
| 403 | std::string bar = kBarText; |
| 404 | log.SwapLogText(&bar); |
| 405 | EXPECT_EQ(kFooText, bar); |
| 406 | EXPECT_FALSE(log.IsEmpty()); |
| 407 | EXPECT_EQ(kBarText, log.log_text()); |
| 408 | EXPECT_EQ(bar_hash, log.log_hash()); |
| 409 | |
| 410 | log.Clear(); |
| 411 | EXPECT_TRUE(log.IsEmpty()); |
| 412 | EXPECT_TRUE(log.log_text().empty()); |
| 413 | EXPECT_TRUE(log.log_hash().empty()); |
| 414 | |
| 415 | MetricsLogManager::SerializedLog log2; |
| 416 | foo = kFooText; |
| 417 | log2.SwapLogText(&foo); |
| 418 | log.Swap(&log2); |
| 419 | EXPECT_FALSE(log.IsEmpty()); |
| 420 | EXPECT_EQ(kFooText, log.log_text()); |
| 421 | EXPECT_EQ(foo_hash, log.log_hash()); |
| 422 | EXPECT_TRUE(log2.IsEmpty()); |
| 423 | EXPECT_TRUE(log2.log_text().empty()); |
| 424 | EXPECT_TRUE(log2.log_hash().empty()); |
| 425 | } |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame^] | 426 | |
| 427 | } // namespace metrics |