[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] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 11 | #include "base/prefs/pref_registry_simple.h" |
| 12 | #include "base/prefs/testing_pref_service.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 13 | #include "components/metrics/metrics_log.h" |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 14 | #include "components/metrics/metrics_pref_names.h" |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 15 | #include "components/metrics/test_metrics_service_client.h" |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 18 | namespace metrics { |
| 19 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 20 | namespace { |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 21 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 22 | // Dummy serializer that just stores logs in memory. |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 23 | class TestLogPrefService : public TestingPrefServiceSimple { |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 24 | public: |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 25 | TestLogPrefService() { |
| 26 | registry()->RegisterListPref(prefs::kMetricsInitialLogs); |
| 27 | registry()->RegisterListPref(prefs::kMetricsOngoingLogs); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 28 | } |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 29 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 30 | // Returns the number of logs of the given type. |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 31 | size_t TypeCount(MetricsLog::LogType log_type) { |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 32 | int list_length = 0; |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 33 | if (log_type == MetricsLog::INITIAL_STABILITY_LOG) |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 34 | list_length = GetList(prefs::kMetricsInitialLogs)->GetSize(); |
| 35 | else |
| 36 | list_length = GetList(prefs::kMetricsOngoingLogs)->GetSize(); |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 37 | return list_length / 2; |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 38 | } |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 39 | }; |
[email protected] | fe58acc2 | 2012-02-29 01:29:58 | [diff] [blame] | 40 | |
| 41 | } // namespace |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 42 | |
| 43 | TEST(MetricsLogManagerTest, StandardFlow) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 44 | TestMetricsServiceClient client; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 45 | TestLogPrefService pref_service; |
| 46 | MetricsLogManager log_manager(&pref_service, 0); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 47 | |
| 48 | // Make sure a new manager has a clean slate. |
| 49 | EXPECT_EQ(NULL, log_manager.current_log()); |
| 50 | EXPECT_FALSE(log_manager.has_staged_log()); |
| 51 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 52 | |
| 53 | // Check that the normal flow works. |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 54 | MetricsLog* initial_log = new MetricsLog( |
| 55 | "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service); |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 56 | log_manager.BeginLoggingWithLog(make_scoped_ptr(initial_log)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 57 | EXPECT_EQ(initial_log, log_manager.current_log()); |
| 58 | EXPECT_FALSE(log_manager.has_staged_log()); |
| 59 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 60 | log_manager.FinishCurrentLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 61 | EXPECT_EQ(NULL, log_manager.current_log()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 62 | EXPECT_TRUE(log_manager.has_unsent_logs()); |
| 63 | EXPECT_FALSE(log_manager.has_staged_log()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 64 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 65 | MetricsLog* second_log = |
| 66 | new MetricsLog("id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service); |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 67 | log_manager.BeginLoggingWithLog(make_scoped_ptr(second_log)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 68 | EXPECT_EQ(second_log, log_manager.current_log()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 69 | |
| 70 | log_manager.StageNextLogForUpload(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 71 | EXPECT_TRUE(log_manager.has_staged_log()); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 72 | EXPECT_FALSE(log_manager.staged_log().empty()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 73 | |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 74 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 75 | EXPECT_EQ(second_log, log_manager.current_log()); |
| 76 | EXPECT_FALSE(log_manager.has_staged_log()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 77 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 78 | |
| 79 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 80 | } |
| 81 | |
| 82 | TEST(MetricsLogManagerTest, AbandonedLog) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 83 | TestMetricsServiceClient client; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 84 | TestLogPrefService pref_service; |
| 85 | MetricsLogManager log_manager(&pref_service, 0); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 86 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 87 | MetricsLog* dummy_log = new MetricsLog( |
| 88 | "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service); |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 89 | log_manager.BeginLoggingWithLog(make_scoped_ptr(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) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 98 | TestMetricsServiceClient client; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 99 | TestLogPrefService pref_service; |
| 100 | MetricsLogManager log_manager(&pref_service, 0); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 101 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 102 | MetricsLog* ongoing_log = |
| 103 | new MetricsLog("id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service); |
| 104 | MetricsLog* temp_log = new MetricsLog( |
| 105 | "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 106 | |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 107 | log_manager.BeginLoggingWithLog(make_scoped_ptr(ongoing_log)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 108 | EXPECT_EQ(ongoing_log, log_manager.current_log()); |
| 109 | |
| 110 | log_manager.PauseCurrentLog(); |
| 111 | EXPECT_EQ(NULL, log_manager.current_log()); |
| 112 | |
[email protected] | 09dee82d | 2014-05-22 14:00:53 | [diff] [blame] | 113 | log_manager.BeginLoggingWithLog(make_scoped_ptr(temp_log)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 114 | EXPECT_EQ(temp_log, log_manager.current_log()); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 115 | log_manager.FinishCurrentLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 116 | EXPECT_EQ(NULL, log_manager.current_log()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 117 | |
| 118 | log_manager.ResumePausedLog(); |
| 119 | EXPECT_EQ(ongoing_log, log_manager.current_log()); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 120 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 121 | EXPECT_FALSE(log_manager.has_staged_log()); |
| 122 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 123 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 124 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 125 | } |
| 126 | |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 127 | TEST(MetricsLogManagerTest, InterjectedLogPreservesType) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 128 | TestMetricsServiceClient client; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 129 | TestLogPrefService pref_service; |
| 130 | MetricsLogManager log_manager(&pref_service, 0); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 131 | log_manager.LoadPersistedUnsentLogs(); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 132 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 133 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 134 | "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service))); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 135 | log_manager.PauseCurrentLog(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 136 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 137 | "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service))); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 138 | log_manager.FinishCurrentLog(); |
| 139 | log_manager.ResumePausedLog(); |
| 140 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 141 | log_manager.DiscardStagedLog(); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 142 | |
| 143 | // Verify that the remaining log (which is the original ongoing log) still |
| 144 | // has the right type. |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 145 | log_manager.FinishCurrentLog(); |
| 146 | log_manager.PersistUnsentLogs(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 147 | EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG)); |
| 148 | EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | aa752c3f | 2012-04-27 22:31:51 | [diff] [blame] | 149 | } |
| 150 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 151 | TEST(MetricsLogManagerTest, StoreAndLoad) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 152 | TestMetricsServiceClient client; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 153 | TestLogPrefService pref_service; |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 154 | // Set up some in-progress logging in a scoped log manager simulating the |
| 155 | // leadup to quitting, then persist as would be done on quit. |
| 156 | { |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 157 | MetricsLogManager log_manager(&pref_service, 0); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 158 | log_manager.LoadPersistedUnsentLogs(); |
| 159 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 160 | // Simulate a log having already been unsent from a previous session. |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 161 | { |
| 162 | std::string log("proto"); |
[email protected] | f61623e | 2014-08-19 16:25:57 | [diff] [blame] | 163 | PersistedLogs ongoing_logs(&pref_service, prefs::kMetricsOngoingLogs, 1, |
| 164 | 1, 0); |
[email protected] | 9706e1b | 2014-06-11 16:31:24 | [diff] [blame] | 165 | ongoing_logs.StoreLog(log); |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 166 | ongoing_logs.SerializeLogs(); |
| 167 | } |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 168 | EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_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] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 173 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 174 | "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service))); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 175 | log_manager.FinishCurrentLog(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 176 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 177 | "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service))); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 178 | log_manager.StageNextLogForUpload(); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 179 | log_manager.FinishCurrentLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 180 | |
| 181 | // Nothing should be written out until PersistUnsentLogs is called. |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 182 | EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG)); |
| 183 | EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 184 | log_manager.PersistUnsentLogs(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 185 | EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG)); |
| 186 | EXPECT_EQ(2U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Now simulate the relaunch, ensure that the log manager restores |
| 190 | // everything correctly, and verify that once the are handled they are not |
| 191 | // re-persisted. |
| 192 | { |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 193 | MetricsLogManager log_manager(&pref_service, 0); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 194 | log_manager.LoadPersistedUnsentLogs(); |
| 195 | EXPECT_TRUE(log_manager.has_unsent_logs()); |
| 196 | |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 197 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 198 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 199 | // The initial log should be sent first; update the persisted storage to |
| 200 | // verify. |
| 201 | log_manager.PersistUnsentLogs(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 202 | EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG)); |
| 203 | EXPECT_EQ(2U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 204 | |
| 205 | // Handle the first ongoing log. |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 206 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 207 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 208 | EXPECT_TRUE(log_manager.has_unsent_logs()); |
| 209 | |
| 210 | // Handle the last log. |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 211 | log_manager.StageNextLogForUpload(); |
[email protected] | 5f3e164 | 2013-05-05 03:37:34 | [diff] [blame] | 212 | log_manager.DiscardStagedLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 213 | EXPECT_FALSE(log_manager.has_unsent_logs()); |
| 214 | |
| 215 | // Nothing should have changed "on disk" since PersistUnsentLogs hasn't been |
| 216 | // called again. |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 217 | EXPECT_EQ(2U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 218 | // Persist, and make sure nothing is left. |
| 219 | log_manager.PersistUnsentLogs(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 220 | EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG)); |
| 221 | EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 225 | TEST(MetricsLogManagerTest, StoreStagedLogTypes) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 226 | TestMetricsServiceClient client; |
| 227 | |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 228 | // Ensure that types are preserved when storing staged logs. |
| 229 | { |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 230 | TestLogPrefService pref_service; |
| 231 | MetricsLogManager log_manager(&pref_service, 0); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 232 | log_manager.LoadPersistedUnsentLogs(); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 233 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 234 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 235 | "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service))); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 236 | log_manager.FinishCurrentLog(); |
| 237 | log_manager.StageNextLogForUpload(); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 238 | log_manager.PersistUnsentLogs(); |
| 239 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 240 | EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG)); |
| 241 | EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | { |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 245 | TestLogPrefService pref_service; |
| 246 | MetricsLogManager log_manager(&pref_service, 0); |
[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] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 249 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 250 | "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service))); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 251 | log_manager.FinishCurrentLog(); |
| 252 | log_manager.StageNextLogForUpload(); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 253 | log_manager.PersistUnsentLogs(); |
| 254 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 255 | EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG)); |
| 256 | EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | f829cc36 | 2012-03-10 10:09:16 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 260 | TEST(MetricsLogManagerTest, LargeLogDiscarding) { |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 261 | TestMetricsServiceClient client; |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 262 | TestLogPrefService pref_service; |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 263 | // Set the size threshold very low, to verify that it's honored. |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 264 | MetricsLogManager log_manager(&pref_service, 1); |
| 265 | log_manager.LoadPersistedUnsentLogs(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 266 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 267 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 268 | "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service))); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 269 | log_manager.FinishCurrentLog(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 270 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 271 | "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service))); |
[email protected] | 2994826 | 2012-03-01 12:15:08 | [diff] [blame] | 272 | log_manager.FinishCurrentLog(); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 273 | |
| 274 | // Only the ongoing log should be written out, due to the threshold. |
| 275 | log_manager.PersistUnsentLogs(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 276 | EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG)); |
| 277 | EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | cac267c | 2011-09-29 15:18:10 | [diff] [blame] | 278 | } |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 279 | |
[email protected] | faa8d22 | 2014-07-25 21:30:26 | [diff] [blame] | 280 | TEST(MetricsLogManagerTest, DiscardOrder) { |
| 281 | // Ensure that the correct log is discarded if new logs are pushed while |
| 282 | // a log is staged. |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 283 | TestMetricsServiceClient client; |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 284 | { |
[email protected] | 7f07db6 | 2014-05-15 01:12:45 | [diff] [blame] | 285 | TestLogPrefService pref_service; |
| 286 | MetricsLogManager log_manager(&pref_service, 0); |
[email protected] | 80a8f31 | 2013-12-16 18:00:30 | [diff] [blame] | 287 | log_manager.LoadPersistedUnsentLogs(); |
| 288 | |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 289 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 290 | "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service))); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 291 | log_manager.FinishCurrentLog(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 292 | log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog( |
| 293 | "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service))); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 294 | log_manager.StageNextLogForUpload(); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 295 | log_manager.FinishCurrentLog(); |
[email protected] | faa8d22 | 2014-07-25 21:30:26 | [diff] [blame] | 296 | log_manager.DiscardStagedLog(); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 297 | |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 298 | log_manager.PersistUnsentLogs(); |
[email protected] | bfb77b5 | 2014-06-07 01:54:01 | [diff] [blame] | 299 | EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG)); |
| 300 | EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG)); |
[email protected] | e7508d8 | 2012-05-03 15:59:53 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
[email protected] | 064107e | 2014-05-02 00:59:06 | [diff] [blame] | 304 | } // namespace metrics |