blob: b74d614bdf3a5c68805c11f1e62efbf5084aa008 [file] [log] [blame]
[email protected]064107e2014-05-02 00:59:061// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]cac267c2011-09-29 15:18:102// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]064107e2014-05-02 00:59:065#include "components/metrics/metrics_log_manager.h"
[email protected]cfee9aa52013-10-19 17:53:056
[email protected]fe58acc22012-02-29 01:29:587#include <string>
8#include <utility>
9#include <vector>
10
[email protected]7f07db62014-05-15 01:12:4511#include "base/prefs/pref_registry_simple.h"
12#include "base/prefs/testing_pref_service.h"
[email protected]bfb77b52014-06-07 01:54:0113#include "components/metrics/metrics_log.h"
[email protected]7f07db62014-05-15 01:12:4514#include "components/metrics/metrics_pref_names.h"
[email protected]bfb77b52014-06-07 01:54:0115#include "components/metrics/test_metrics_service_client.h"
[email protected]cac267c2011-09-29 15:18:1016#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]064107e2014-05-02 00:59:0618namespace metrics {
19
[email protected]cac267c2011-09-29 15:18:1020namespace {
[email protected]fe58acc22012-02-29 01:29:5821
[email protected]cac267c2011-09-29 15:18:1022// Dummy serializer that just stores logs in memory.
[email protected]7f07db62014-05-15 01:12:4523class TestLogPrefService : public TestingPrefServiceSimple {
[email protected]cac267c2011-09-29 15:18:1024 public:
[email protected]7f07db62014-05-15 01:12:4525 TestLogPrefService() {
26 registry()->RegisterListPref(prefs::kMetricsInitialLogs);
[email protected]9706e1b2014-06-11 16:31:2427 registry()->RegisterListPref(prefs::kMetricsInitialLogsOld);
[email protected]7f07db62014-05-15 01:12:4528 registry()->RegisterListPref(prefs::kMetricsOngoingLogs);
[email protected]9706e1b2014-06-11 16:31:2429 registry()->RegisterListPref(prefs::kMetricsOngoingLogsOld);
[email protected]cac267c2011-09-29 15:18:1030 }
[email protected]bfb77b52014-06-07 01:54:0131
[email protected]cac267c2011-09-29 15:18:1032 // Returns the number of logs of the given type.
[email protected]bfb77b52014-06-07 01:54:0133 size_t TypeCount(MetricsLog::LogType log_type) {
[email protected]7f07db62014-05-15 01:12:4534 int list_length = 0;
[email protected]bfb77b52014-06-07 01:54:0135 if (log_type == MetricsLog::INITIAL_STABILITY_LOG)
[email protected]7f07db62014-05-15 01:12:4536 list_length = GetList(prefs::kMetricsInitialLogs)->GetSize();
37 else
38 list_length = GetList(prefs::kMetricsOngoingLogs)->GetSize();
[email protected]9706e1b2014-06-11 16:31:2439 return list_length / 2;
[email protected]cac267c2011-09-29 15:18:1040 }
[email protected]cac267c2011-09-29 15:18:1041};
[email protected]fe58acc22012-02-29 01:29:5842
43} // namespace
[email protected]cac267c2011-09-29 15:18:1044
45TEST(MetricsLogManagerTest, StandardFlow) {
[email protected]bfb77b52014-06-07 01:54:0146 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:4547 TestLogPrefService pref_service;
48 MetricsLogManager log_manager(&pref_service, 0);
[email protected]cac267c2011-09-29 15:18:1049
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]bfb77b52014-06-07 01:54:0156 MetricsLog* initial_log = new MetricsLog(
57 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service);
[email protected]09dee82d2014-05-22 14:00:5358 log_manager.BeginLoggingWithLog(make_scoped_ptr(initial_log));
[email protected]cac267c2011-09-29 15:18:1059 EXPECT_EQ(initial_log, log_manager.current_log());
60 EXPECT_FALSE(log_manager.has_staged_log());
61
[email protected]29948262012-03-01 12:15:0862 log_manager.FinishCurrentLog();
[email protected]cac267c2011-09-29 15:18:1063 EXPECT_EQ(NULL, log_manager.current_log());
[email protected]29948262012-03-01 12:15:0864 EXPECT_TRUE(log_manager.has_unsent_logs());
65 EXPECT_FALSE(log_manager.has_staged_log());
[email protected]cac267c2011-09-29 15:18:1066
[email protected]bfb77b52014-06-07 01:54:0167 MetricsLog* second_log =
68 new MetricsLog("id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service);
[email protected]09dee82d2014-05-22 14:00:5369 log_manager.BeginLoggingWithLog(make_scoped_ptr(second_log));
[email protected]cac267c2011-09-29 15:18:1070 EXPECT_EQ(second_log, log_manager.current_log());
[email protected]29948262012-03-01 12:15:0871
72 log_manager.StageNextLogForUpload();
[email protected]cac267c2011-09-29 15:18:1073 EXPECT_TRUE(log_manager.has_staged_log());
[email protected]7f07db62014-05-15 01:12:4574 EXPECT_FALSE(log_manager.staged_log().empty());
[email protected]cac267c2011-09-29 15:18:1075
[email protected]5f3e1642013-05-05 03:37:3476 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:1077 EXPECT_EQ(second_log, log_manager.current_log());
78 EXPECT_FALSE(log_manager.has_staged_log());
[email protected]29948262012-03-01 12:15:0879 EXPECT_FALSE(log_manager.has_unsent_logs());
[email protected]cac267c2011-09-29 15:18:1080
81 EXPECT_FALSE(log_manager.has_unsent_logs());
82}
83
84TEST(MetricsLogManagerTest, AbandonedLog) {
[email protected]bfb77b52014-06-07 01:54:0185 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:4586 TestLogPrefService pref_service;
87 MetricsLogManager log_manager(&pref_service, 0);
[email protected]cac267c2011-09-29 15:18:1088
[email protected]bfb77b52014-06-07 01:54:0189 MetricsLog* dummy_log = new MetricsLog(
90 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service);
[email protected]09dee82d2014-05-22 14:00:5391 log_manager.BeginLoggingWithLog(make_scoped_ptr(dummy_log));
[email protected]cac267c2011-09-29 15:18:1092 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
99TEST(MetricsLogManagerTest, InterjectedLog) {
[email protected]bfb77b52014-06-07 01:54:01100 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:45101 TestLogPrefService pref_service;
102 MetricsLogManager log_manager(&pref_service, 0);
[email protected]cac267c2011-09-29 15:18:10103
[email protected]bfb77b52014-06-07 01:54:01104 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]cac267c2011-09-29 15:18:10108
[email protected]09dee82d2014-05-22 14:00:53109 log_manager.BeginLoggingWithLog(make_scoped_ptr(ongoing_log));
[email protected]cac267c2011-09-29 15:18:10110 EXPECT_EQ(ongoing_log, log_manager.current_log());
111
112 log_manager.PauseCurrentLog();
113 EXPECT_EQ(NULL, log_manager.current_log());
114
[email protected]09dee82d2014-05-22 14:00:53115 log_manager.BeginLoggingWithLog(make_scoped_ptr(temp_log));
[email protected]cac267c2011-09-29 15:18:10116 EXPECT_EQ(temp_log, log_manager.current_log());
[email protected]29948262012-03-01 12:15:08117 log_manager.FinishCurrentLog();
[email protected]cac267c2011-09-29 15:18:10118 EXPECT_EQ(NULL, log_manager.current_log());
[email protected]cac267c2011-09-29 15:18:10119
120 log_manager.ResumePausedLog();
121 EXPECT_EQ(ongoing_log, log_manager.current_log());
[email protected]cac267c2011-09-29 15:18:10122
[email protected]29948262012-03-01 12:15:08123 EXPECT_FALSE(log_manager.has_staged_log());
124 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34125 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:10126 EXPECT_FALSE(log_manager.has_unsent_logs());
127}
128
[email protected]aa752c3f2012-04-27 22:31:51129TEST(MetricsLogManagerTest, InterjectedLogPreservesType) {
[email protected]bfb77b52014-06-07 01:54:01130 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:45131 TestLogPrefService pref_service;
132 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30133 log_manager.LoadPersistedUnsentLogs();
[email protected]aa752c3f2012-04-27 22:31:51134
[email protected]bfb77b52014-06-07 01:54:01135 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
136 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]aa752c3f2012-04-27 22:31:51137 log_manager.PauseCurrentLog();
[email protected]bfb77b52014-06-07 01:54:01138 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
139 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]aa752c3f2012-04-27 22:31:51140 log_manager.FinishCurrentLog();
141 log_manager.ResumePausedLog();
142 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34143 log_manager.DiscardStagedLog();
[email protected]aa752c3f2012-04-27 22:31:51144
145 // Verify that the remaining log (which is the original ongoing log) still
146 // has the right type.
[email protected]aa752c3f2012-04-27 22:31:51147 log_manager.FinishCurrentLog();
148 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01149 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
150 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]aa752c3f2012-04-27 22:31:51151}
152
[email protected]cac267c2011-09-29 15:18:10153TEST(MetricsLogManagerTest, StoreAndLoad) {
[email protected]bfb77b52014-06-07 01:54:01154 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:45155 TestLogPrefService pref_service;
[email protected]cac267c2011-09-29 15:18:10156 // 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]7f07db62014-05-15 01:12:45159 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30160 log_manager.LoadPersistedUnsentLogs();
161
[email protected]cac267c2011-09-29 15:18:10162 // Simulate a log having already been unsent from a previous session.
[email protected]7f07db62014-05-15 01:12:45163 {
164 std::string log("proto");
[email protected]9706e1b2014-06-11 16:31:24165 PersistedLogs ongoing_logs(&pref_service, prefs::kMetricsOngoingLogs,
166 prefs::kMetricsOngoingLogsOld, 1, 1, 0);
167 ongoing_logs.StoreLog(log);
[email protected]7f07db62014-05-15 01:12:45168 ongoing_logs.SerializeLogs();
169 }
[email protected]bfb77b52014-06-07 01:54:01170 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10171 EXPECT_FALSE(log_manager.has_unsent_logs());
172 log_manager.LoadPersistedUnsentLogs();
173 EXPECT_TRUE(log_manager.has_unsent_logs());
174
[email protected]bfb77b52014-06-07 01:54:01175 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
176 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]29948262012-03-01 12:15:08177 log_manager.FinishCurrentLog();
[email protected]bfb77b52014-06-07 01:54:01178 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
179 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]29948262012-03-01 12:15:08180 log_manager.StageNextLogForUpload();
[email protected]29948262012-03-01 12:15:08181 log_manager.FinishCurrentLog();
[email protected]cac267c2011-09-29 15:18:10182
183 // Nothing should be written out until PersistUnsentLogs is called.
[email protected]bfb77b52014-06-07 01:54:01184 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
185 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10186 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01187 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
188 EXPECT_EQ(2U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10189 }
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]7f07db62014-05-15 01:12:45195 MetricsLogManager log_manager(&pref_service, 0);
[email protected]cac267c2011-09-29 15:18:10196 log_manager.LoadPersistedUnsentLogs();
197 EXPECT_TRUE(log_manager.has_unsent_logs());
198
[email protected]29948262012-03-01 12:15:08199 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34200 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:10201 // The initial log should be sent first; update the persisted storage to
202 // verify.
203 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01204 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
205 EXPECT_EQ(2U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10206
207 // Handle the first ongoing log.
[email protected]29948262012-03-01 12:15:08208 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34209 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:10210 EXPECT_TRUE(log_manager.has_unsent_logs());
211
212 // Handle the last log.
[email protected]29948262012-03-01 12:15:08213 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34214 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:10215 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]bfb77b52014-06-07 01:54:01219 EXPECT_EQ(2U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10220 // Persist, and make sure nothing is left.
221 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01222 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
223 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10224 }
225}
226
[email protected]f829cc362012-03-10 10:09:16227TEST(MetricsLogManagerTest, StoreStagedLogTypes) {
[email protected]bfb77b52014-06-07 01:54:01228 TestMetricsServiceClient client;
229
[email protected]f829cc362012-03-10 10:09:16230 // Ensure that types are preserved when storing staged logs.
231 {
[email protected]7f07db62014-05-15 01:12:45232 TestLogPrefService pref_service;
233 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30234 log_manager.LoadPersistedUnsentLogs();
[email protected]f829cc362012-03-10 10:09:16235
[email protected]bfb77b52014-06-07 01:54:01236 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
237 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]f829cc362012-03-10 10:09:16238 log_manager.FinishCurrentLog();
239 log_manager.StageNextLogForUpload();
[email protected]f829cc362012-03-10 10:09:16240 log_manager.PersistUnsentLogs();
241
[email protected]bfb77b52014-06-07 01:54:01242 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
243 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]f829cc362012-03-10 10:09:16244 }
245
246 {
[email protected]7f07db62014-05-15 01:12:45247 TestLogPrefService pref_service;
248 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30249 log_manager.LoadPersistedUnsentLogs();
[email protected]f829cc362012-03-10 10:09:16250
[email protected]bfb77b52014-06-07 01:54:01251 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
252 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]f829cc362012-03-10 10:09:16253 log_manager.FinishCurrentLog();
254 log_manager.StageNextLogForUpload();
[email protected]f829cc362012-03-10 10:09:16255 log_manager.PersistUnsentLogs();
256
[email protected]bfb77b52014-06-07 01:54:01257 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
258 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]f829cc362012-03-10 10:09:16259 }
260}
261
[email protected]cac267c2011-09-29 15:18:10262TEST(MetricsLogManagerTest, LargeLogDiscarding) {
[email protected]bfb77b52014-06-07 01:54:01263 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:45264 TestLogPrefService pref_service;
[email protected]cac267c2011-09-29 15:18:10265 // Set the size threshold very low, to verify that it's honored.
[email protected]7f07db62014-05-15 01:12:45266 MetricsLogManager log_manager(&pref_service, 1);
267 log_manager.LoadPersistedUnsentLogs();
[email protected]cac267c2011-09-29 15:18:10268
[email protected]bfb77b52014-06-07 01:54:01269 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
270 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]29948262012-03-01 12:15:08271 log_manager.FinishCurrentLog();
[email protected]bfb77b52014-06-07 01:54:01272 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
273 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]29948262012-03-01 12:15:08274 log_manager.FinishCurrentLog();
[email protected]cac267c2011-09-29 15:18:10275
276 // Only the ongoing log should be written out, due to the threshold.
277 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01278 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
279 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10280}
[email protected]e7508d82012-05-03 15:59:53281
[email protected]faa8d222014-07-25 21:30:26282TEST(MetricsLogManagerTest, DiscardOrder) {
283 // Ensure that the correct log is discarded if new logs are pushed while
284 // a log is staged.
[email protected]bfb77b52014-06-07 01:54:01285 TestMetricsServiceClient client;
[email protected]e7508d82012-05-03 15:59:53286 {
[email protected]7f07db62014-05-15 01:12:45287 TestLogPrefService pref_service;
288 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30289 log_manager.LoadPersistedUnsentLogs();
290
[email protected]bfb77b52014-06-07 01:54:01291 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
292 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]e7508d82012-05-03 15:59:53293 log_manager.FinishCurrentLog();
[email protected]bfb77b52014-06-07 01:54:01294 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
295 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]e7508d82012-05-03 15:59:53296 log_manager.StageNextLogForUpload();
[email protected]e7508d82012-05-03 15:59:53297 log_manager.FinishCurrentLog();
[email protected]faa8d222014-07-25 21:30:26298 log_manager.DiscardStagedLog();
[email protected]e7508d82012-05-03 15:59:53299
[email protected]e7508d82012-05-03 15:59:53300 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01301 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
302 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]e7508d82012-05-03 15:59:53303 }
304}
305
[email protected]064107e2014-05-02 00:59:06306} // namespace metrics