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