1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fuchsia/legacymetrics/cpp/fidl.h>
#include <fuchsia/legacymetrics/cpp/fidl_test_base.h>
#include <string>
#include <utility>
#include "base/fuchsia/scoped_service_binding.h"
#include "base/fuchsia/test_component_context_for_process.h"
#include "base/metrics/histogram_macros.h"
#include "base/test/task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "fuchsia/base/legacymetrics_client.h"
#include "fuchsia/base/legacymetrics_histogram_flattener.h"
#include "fuchsia/base/result_receiver.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cr_fuchsia {
namespace {
constexpr base::TimeDelta kReportInterval = base::TimeDelta::FromMinutes(1);
class TestMetricsRecorder
: public fuchsia::legacymetrics::testing::MetricsRecorder_TestBase {
public:
TestMetricsRecorder() = default;
~TestMetricsRecorder() override = default;
bool IsRecordInFlight() const { return ack_callback_.has_value(); }
std::vector<fuchsia::legacymetrics::Event> WaitForEvents() {
if (recorded_events_.empty()) {
base::RunLoop run_loop;
on_record_cb_ = run_loop.QuitClosure();
run_loop.Run();
}
return std::move(recorded_events_);
}
void DropAck() { ack_callback_ = base::nullopt; }
void SendAck() {
(*ack_callback_)();
ack_callback_ = base::nullopt;
}
void set_expect_ack_dropped(bool expect_dropped) {
expect_ack_dropped_ = expect_dropped;
}
// fuchsia::legacymetrics::MetricsRecorder implementation.
void Record(std::vector<fuchsia::legacymetrics::Event> events,
RecordCallback callback) override {
std::move(events.begin(), events.end(),
std::back_inserter(recorded_events_));
// Received a call to Record() before the previous one was acknowledged,
// which can happen in some cases (e.g. flushing).
if (ack_callback_)
EXPECT_TRUE(expect_ack_dropped_);
ack_callback_ = std::move(callback);
if (on_record_cb_)
std::move(on_record_cb_).Run();
}
void NotImplemented_(const std::string& name) override { FAIL() << name; }
private:
std::vector<fuchsia::legacymetrics::Event> recorded_events_;
base::OnceClosure on_record_cb_;
base::Optional<RecordCallback> ack_callback_;
bool expect_ack_dropped_ = false;
};
class LegacyMetricsClientTest : public testing::Test {
public:
LegacyMetricsClientTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME,
base::test::TaskEnvironment::MainThreadType::IO) {}
~LegacyMetricsClientTest() override = default;
void SetUp() override {
service_binding_ =
std::make_unique<base::fuchsia::ScopedSingleClientServiceBinding<
fuchsia::legacymetrics::MetricsRecorder>>(
test_context_.additional_services(), &test_recorder_);
base::SetRecordActionTaskRunner(base::ThreadTaskRunnerHandle::Get());
// Flush any dirty histograms from previous test runs in this process.
GetLegacyMetricsDeltas();
}
protected:
base::test::TaskEnvironment task_environment_;
base::TestComponentContextForProcess test_context_;
TestMetricsRecorder test_recorder_;
std::unique_ptr<base::fuchsia::ScopedSingleClientServiceBinding<
fuchsia::legacymetrics::MetricsRecorder>>
service_binding_;
LegacyMetricsClient client_;
};
TEST_F(LegacyMetricsClientTest, ReportIntervalBoundary) {
client_.Start(kReportInterval);
task_environment_.FastForwardBy(kReportInterval -
base::TimeDelta::FromSeconds(1));
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(1));
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
}
void PopulateAdditionalEvents(
base::OnceCallback<void(std::vector<fuchsia::legacymetrics::Event>)>
callback) {
fuchsia::legacymetrics::ImplementationDefinedEvent impl_event;
impl_event.set_name("baz");
fuchsia::legacymetrics::Event event;
event.set_impl_defined_event(std::move(impl_event));
std::vector<fuchsia::legacymetrics::Event> events;
events.push_back(std::move(event));
std::move(callback).Run(std::move(events));
}
TEST_F(LegacyMetricsClientTest, AllTypes) {
client_.SetReportAdditionalMetricsCallback(
base::BindRepeating(&PopulateAdditionalEvents));
client_.Start(kReportInterval);
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
base::RecordComputedAction("bar");
task_environment_.FastForwardBy(kReportInterval);
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
auto events = test_recorder_.WaitForEvents();
EXPECT_EQ(3u, events.size());
EXPECT_EQ("baz", events[0].impl_defined_event().name());
EXPECT_EQ("foo", events[1].histogram().name());
EXPECT_EQ("bar", events[2].user_action_event().name());
}
TEST_F(LegacyMetricsClientTest, ReportSkippedNoEvents) {
client_.Start(kReportInterval);
// Verify that Record() is not invoked if there is no data to report.
task_environment_.FastForwardBy(kReportInterval);
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
// Add some events and allow the interval to lapse. Verify that the data is
// reported.
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
task_environment_.FastForwardBy(kReportInterval);
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
test_recorder_.SendAck();
// Verify that Record() is skipped again for no-data.
task_environment_.FastForwardBy(kReportInterval);
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
}
TEST_F(LegacyMetricsClientTest, MultipleReports) {
client_.Start(kReportInterval);
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
task_environment_.FastForwardBy(kReportInterval);
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
test_recorder_.SendAck();
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
task_environment_.FastForwardBy(kReportInterval);
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
test_recorder_.SendAck();
}
TEST_F(LegacyMetricsClientTest, NoReportIfNeverAcked) {
client_.Start(kReportInterval);
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
task_environment_.FastForwardBy(kReportInterval);
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
test_recorder_.DropAck();
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
task_environment_.FastForwardBy(kReportInterval);
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
}
TEST_F(LegacyMetricsClientTest, MetricsChannelDisconnected) {
client_.Start(kReportInterval);
service_binding_.reset();
task_environment_.FastForwardBy(kReportInterval);
}
TEST_F(LegacyMetricsClientTest, Batching) {
client_.Start(kReportInterval);
// Log enough actions that the list will be split across multiple batches.
// Batches are read out in reverse order, so even though it is being logged
// first, it will be emitted in the final batch.
base::RecordComputedAction("batch2");
for (size_t i = 0; i < LegacyMetricsClient::kMaxBatchSize; ++i)
base::RecordComputedAction("batch1");
task_environment_.FastForwardBy(kReportInterval);
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
// First batch.
auto events = test_recorder_.WaitForEvents();
EXPECT_EQ(LegacyMetricsClient::kMaxBatchSize, events.size());
for (const auto& event : events)
EXPECT_EQ(event.user_action_event().name(), "batch1");
test_recorder_.SendAck();
// Second batch (remainder).
events = test_recorder_.WaitForEvents();
EXPECT_EQ(1u, events.size());
for (const auto& event : events)
EXPECT_EQ(event.user_action_event().name(), "batch2");
test_recorder_.SendAck();
}
TEST_F(LegacyMetricsClientTest, FlushWithPending) {
client_.Start(kReportInterval);
base::RunLoop().RunUntilIdle();
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
service_binding_->events().OnCloseSoon();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
// The service should be unbound once all data is drained.
EXPECT_TRUE(service_binding_->has_clients());
auto events = test_recorder_.WaitForEvents();
test_recorder_.SendAck();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, events.size());
EXPECT_EQ("foo", events[0].histogram().name());
EXPECT_FALSE(service_binding_->has_clients());
}
TEST_F(LegacyMetricsClientTest, FlushNoData) {
client_.Start(kReportInterval);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(service_binding_->has_clients());
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
service_binding_->events().OnCloseSoon();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(service_binding_->has_clients());
}
TEST_F(LegacyMetricsClientTest, FlushWithOutstandingAck) {
client_.Start(kReportInterval);
base::RunLoop().RunUntilIdle();
// Send "foo", but don't ack.
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
task_environment_.FastForwardBy(kReportInterval);
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
// Allow the flush operation to call Record() without waiting for a prior ack.
test_recorder_.set_expect_ack_dropped(true);
// Buffer another event and trigger a flush.
UMA_HISTOGRAM_COUNTS_1M("bar", 20);
EXPECT_TRUE(service_binding_->has_clients());
service_binding_->events().OnCloseSoon();
// Simulate an asynchronous ack from the recorder, which be delivered around
// the same time as the flush's Record() call. The ack should be gracefully
// ignored by the client.
test_recorder_.SendAck();
base::RunLoop().RunUntilIdle();
auto events = test_recorder_.WaitForEvents();
test_recorder_.SendAck();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2u, events.size());
EXPECT_EQ("foo", events[0].histogram().name());
EXPECT_EQ("bar", events[1].histogram().name());
EXPECT_FALSE(service_binding_->has_clients());
}
TEST_F(LegacyMetricsClientTest, ExternalFlushSignal) {
ResultReceiver<base::OnceClosure> flush_receiver;
client_.SetNotifyFlushCallback(flush_receiver.GetReceiveCallback());
client_.Start(kReportInterval);
base::RunLoop().RunUntilIdle();
UMA_HISTOGRAM_COUNTS_1M("foo", 20);
// Verify that reporting does not start until the flush completion callback is
// run.
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
service_binding_->events().OnCloseSoon();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(test_recorder_.IsRecordInFlight());
// Verify that invoking the completion callback unblocks reporting.
EXPECT_TRUE(flush_receiver.has_value());
std::move(*flush_receiver).Run();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(test_recorder_.IsRecordInFlight());
}
} // namespace
} // namespace cr_fuchsia
|