Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: components/metrics/metrics_log_store_unittest.cc

Issue 2689323010: Split a MetricsLogStore object out of MetricsLogManager. (Closed)
Patch Set: Ukm Updates Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/metrics/metrics_log_store.h"
6
7 #include "components/metrics/metrics_pref_names.h"
8 #include "components/metrics/test_metrics_service_client.h"
9 #include "components/prefs/testing_pref_service.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace metrics {
13
14 namespace {
15
16 class MetricsLogStoreTest : public testing::Test {
17 public:
18 MetricsLogStoreTest() {
19 MetricsLogStore::RegisterPrefs(pref_service_.registry());
20 }
21 ~MetricsLogStoreTest() override {}
22
23 MetricsLog* CreateLog(MetricsLog::LogType log_type) {
24 return new MetricsLog("id", 0, log_type, &client_, &pref_service_);
25 }
26
27 // Returns the stored number of logs of the given type.
28 size_t TypeCount(MetricsLog::LogType log_type) {
29 int list_length = 0;
30 if (log_type == MetricsLog::INITIAL_STABILITY_LOG)
31 list_length =
rkaplow 2017/02/20 21:24:13 I would just return pref_service_.Get(...) here, a
Steven Holte 2017/02/21 21:51:19 Done.
32 pref_service_.GetList(prefs::kMetricsInitialLogs)->GetSize();
33 else
34 list_length =
35 pref_service_.GetList(prefs::kMetricsOngoingLogs)->GetSize();
36 return list_length;
37 }
38
39 TestMetricsServiceClient client_;
40 TestingPrefServiceSimple pref_service_;
41
42 private:
43 DISALLOW_COPY_AND_ASSIGN(MetricsLogStoreTest);
44 };
45
46 } // namespace
47
48 TEST_F(MetricsLogStoreTest, StandardFlow) {
49 MetricsLogStore log_store(&pref_service_, 0);
50 log_store.LoadPersistedUnsentLogs();
51
52 // Make sure a new manager has a clean slate.
53 EXPECT_FALSE(log_store.has_staged_log());
54 EXPECT_FALSE(log_store.has_unsent_logs());
55
56 log_store.StoreLog("a", MetricsLog::ONGOING_LOG);
57 EXPECT_TRUE(log_store.has_unsent_logs());
58 EXPECT_FALSE(log_store.has_staged_log());
59
60 log_store.StageNextLog();
61 EXPECT_TRUE(log_store.has_staged_log());
62 EXPECT_FALSE(log_store.staged_log().empty());
63
64 log_store.DiscardStagedLog();
65 EXPECT_FALSE(log_store.has_staged_log());
66 EXPECT_FALSE(log_store.has_unsent_logs());
67 }
68
69 TEST_F(MetricsLogStoreTest, StoreAndLoad) {
70 // Set up some in-progress logging in a scoped log manager simulating the
71 // leadup to quitting, then persist as would be done on quit.
72 {
73 MetricsLogStore log_store(&pref_service_, 0);
74 log_store.LoadPersistedUnsentLogs();
75 EXPECT_FALSE(log_store.has_unsent_logs());
76 log_store.StoreLog("a", MetricsLog::ONGOING_LOG);
77 log_store.PersistUnsentLogs();
78 EXPECT_EQ(0U, TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
79 EXPECT_EQ(1U, TypeCount(MetricsLog::ONGOING_LOG));
80 }
81
82 // Relaunch load and store more logs.
83 {
84 MetricsLogStore log_store(&pref_service_, 0);
85 log_store.LoadPersistedUnsentLogs();
86 EXPECT_TRUE(log_store.has_unsent_logs());
87 EXPECT_EQ(0U, TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
88 EXPECT_EQ(1U, TypeCount(MetricsLog::ONGOING_LOG));
89 log_store.StoreLog("x", MetricsLog::INITIAL_STABILITY_LOG);
90 log_store.StageNextLog();
91 log_store.StoreLog("b", MetricsLog::ONGOING_LOG);
92
93 EXPECT_TRUE(log_store.has_unsent_logs());
94 EXPECT_TRUE(log_store.has_staged_log());
95 EXPECT_EQ(0U, TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
96 EXPECT_EQ(1U, TypeCount(MetricsLog::ONGOING_LOG));
97
98 log_store.PersistUnsentLogs();
99 EXPECT_EQ(1U, TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
100 EXPECT_EQ(2U, TypeCount(MetricsLog::ONGOING_LOG));
101 }
102
103 // Relaunch and verify that once logs are handled they are not re-persisted.
104 {
105 MetricsLogStore log_store(&pref_service_, 0);
106 log_store.LoadPersistedUnsentLogs();
107 EXPECT_TRUE(log_store.has_unsent_logs());
108
109 log_store.StageNextLog();
110 log_store.DiscardStagedLog();
111 // The initial log should be sent first; update the persisted storage to
112 // verify.
113 log_store.PersistUnsentLogs();
114 EXPECT_EQ(0U, TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
115 EXPECT_EQ(2U, TypeCount(MetricsLog::ONGOING_LOG));
116
117 // Handle the first ongoing log.
118 log_store.StageNextLog();
119 log_store.DiscardStagedLog();
120 EXPECT_TRUE(log_store.has_unsent_logs());
121
122 // Handle the last log.
123 log_store.StageNextLog();
124 log_store.DiscardStagedLog();
125 EXPECT_FALSE(log_store.has_unsent_logs());
126
127 // Nothing should have changed "on disk" since PersistUnsentLogs hasn't been
128 // called again.
129 EXPECT_EQ(2U, TypeCount(MetricsLog::ONGOING_LOG));
130 // Persist, and make sure nothing is left.
131 log_store.PersistUnsentLogs();
132 EXPECT_EQ(0U, TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
133 EXPECT_EQ(0U, TypeCount(MetricsLog::ONGOING_LOG));
134 }
135 }
136
137 TEST_F(MetricsLogStoreTest, StoreStagedOngoingLog) {
138 // Ensure that types are preserved when storing staged logs.
139 MetricsLogStore log_store(&pref_service_, 0);
140 log_store.LoadPersistedUnsentLogs();
141 log_store.StoreLog("a", MetricsLog::ONGOING_LOG);
142 log_store.StageNextLog();
143 log_store.PersistUnsentLogs();
144
145 EXPECT_EQ(0U, TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
146 EXPECT_EQ(1U, TypeCount(MetricsLog::ONGOING_LOG));
147 }
148
149 TEST_F(MetricsLogStoreTest, StoreStagedInitialLog) {
150 // Ensure that types are preserved when storing staged logs.
151 MetricsLogStore log_store(&pref_service_, 0);
152 log_store.LoadPersistedUnsentLogs();
153 log_store.StoreLog("b", MetricsLog::INITIAL_STABILITY_LOG);
154 log_store.StageNextLog();
155 log_store.PersistUnsentLogs();
156
157 EXPECT_EQ(1U, TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
158 EXPECT_EQ(0U, TypeCount(MetricsLog::ONGOING_LOG));
159 }
160
161 TEST_F(MetricsLogStoreTest, LargeLogDiscarding) {
162 // Set the size threshold very low, to verify that it's honored.
163 MetricsLogStore log_store(&pref_service_, 1);
164 log_store.LoadPersistedUnsentLogs();
165
166 log_store.StoreLog("persisted", MetricsLog::INITIAL_STABILITY_LOG);
167 log_store.StoreLog("not_persisted", MetricsLog::ONGOING_LOG);
168
169 // Only the stability log should be written out, due to the threshold.
170 log_store.PersistUnsentLogs();
171 EXPECT_EQ(1U, TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
172 EXPECT_EQ(0U, TypeCount(MetricsLog::ONGOING_LOG));
173 }
174
175 TEST_F(MetricsLogStoreTest, DiscardOrder) {
176 // Ensure that the correct log is discarded if new logs are pushed while
177 // a log is staged.
178 MetricsLogStore log_store(&pref_service_, 0);
179 log_store.LoadPersistedUnsentLogs();
180
181 log_store.StoreLog("a", MetricsLog::ONGOING_LOG);
182 log_store.StoreLog("b", MetricsLog::ONGOING_LOG);
183 log_store.StageNextLog();
184 log_store.StoreLog("c", MetricsLog::INITIAL_STABILITY_LOG);
185 EXPECT_EQ(2U, log_store.ongoing_log_count());
186 EXPECT_EQ(1U, log_store.initial_log_count());
187 // Should discard the ongoing log staged earlier.
188 log_store.DiscardStagedLog();
189 EXPECT_EQ(1U, log_store.ongoing_log_count());
190 EXPECT_EQ(1U, log_store.initial_log_count());
191 // Initial log should be staged next.
192 log_store.StageNextLog();
193 log_store.DiscardStagedLog();
194 EXPECT_EQ(1U, log_store.ongoing_log_count());
195 EXPECT_EQ(0U, log_store.initial_log_count());
196 }
197
198 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698