Telemetry: Refactor Sampler and MetricReportQueue fakes.
Move Sampler and MetricReportQueue to their own files so
they can be used by other tests.
Bug: b:177847653
Change-Id: Ic8e166dc298c423cfe2cc2795abbdc955e29138e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3262396
Commit-Queue: Ahmed Nasr <[email protected]>
Reviewed-by: Leonid Baraz <[email protected]>
Cr-Commit-Position: refs/heads/main@{#940520}
diff --git a/components/reporting/metrics/BUILD.gn b/components/reporting/metrics/BUILD.gn
index 2dd65b2b..08587e5 100644
--- a/components/reporting/metrics/BUILD.gn
+++ b/components/reporting/metrics/BUILD.gn
@@ -27,12 +27,20 @@
static_library("test_support") {
testonly = true
sources = [
+ "fake_metric_report_queue.cc",
+ "fake_metric_report_queue.h",
"fake_reporting_settings.cc",
"fake_reporting_settings.h",
+ "fake_sampler.cc",
+ "fake_sampler.h",
]
deps = [
":metrics_data_collection",
"//base",
+ "//components/reporting/client:report_queue",
+ "//components/reporting/proto:metric_data_proto",
+ "//components/reporting/proto:record_constants",
+ "//components/reporting/util:status",
]
}
diff --git a/components/reporting/metrics/fake_metric_report_queue.cc b/components/reporting/metrics/fake_metric_report_queue.cc
new file mode 100644
index 0000000..67f1b8aa
--- /dev/null
+++ b/components/reporting/metrics/fake_metric_report_queue.cc
@@ -0,0 +1,58 @@
+// Copyright 2021 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 "components/reporting/metrics/fake_metric_report_queue.h"
+
+#include "base/task/sequenced_task_runner.h"
+#include "base/threading/sequenced_task_runner_handle.h"
+#include "components/reporting/proto/synced/record_constants.pb.h"
+#include "components/reporting/util/status.h"
+
+namespace reporting {
+namespace test {
+
+FakeMetricReportQueue::FakeMetricReportQueue(Priority priority)
+ : MetricReportQueue(std::unique_ptr<ReportQueue, base::OnTaskRunnerDeleter>(
+ nullptr,
+ base::OnTaskRunnerDeleter(
+ base::SequencedTaskRunnerHandle::Get())),
+ priority) {}
+
+FakeMetricReportQueue::FakeMetricReportQueue(
+ Priority priority,
+ ReportingSettings* reporting_settings,
+ const std::string& rate_setting_path,
+ base::TimeDelta default_rate,
+ int rate_unit_to_ms)
+ : MetricReportQueue(std::unique_ptr<ReportQueue, base::OnTaskRunnerDeleter>(
+ nullptr,
+ base::OnTaskRunnerDeleter(
+ base::SequencedTaskRunnerHandle::Get())),
+ priority,
+ reporting_settings,
+ rate_setting_path,
+ default_rate,
+ rate_unit_to_ms) {}
+
+void FakeMetricReportQueue::Enqueue(const MetricData& metric_data,
+ ReportQueue::EnqueueCallback callback) {
+ reported_data_.emplace_back(metric_data);
+ std::move(callback).Run(Status());
+}
+
+FakeMetricReportQueue::~FakeMetricReportQueue() = default;
+
+void FakeMetricReportQueue::Flush() {
+ num_flush_++;
+}
+
+std::vector<MetricData> FakeMetricReportQueue::GetMetricDataReported() const {
+ return reported_data_;
+}
+
+int FakeMetricReportQueue::GetNumFlush() const {
+ return num_flush_;
+}
+} // namespace test
+} // namespace reporting
diff --git a/components/reporting/metrics/fake_metric_report_queue.h b/components/reporting/metrics/fake_metric_report_queue.h
new file mode 100644
index 0000000..8aa07c6f
--- /dev/null
+++ b/components/reporting/metrics/fake_metric_report_queue.h
@@ -0,0 +1,49 @@
+// Copyright 2021 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.
+
+#ifndef COMPONENTS_REPORTING_METRICS_FAKE_METRIC_REPORT_QUEUE_H_
+#define COMPONENTS_REPORTING_METRICS_FAKE_METRIC_REPORT_QUEUE_H_
+
+#include <vector>
+
+#include "components/reporting/client/report_queue.h"
+#include "components/reporting/metrics/metric_report_queue.h"
+#include "components/reporting/proto/metric_data.pb.h"
+
+namespace reporting {
+namespace test {
+
+class FakeMetricReportQueue : public MetricReportQueue {
+ public:
+ explicit FakeMetricReportQueue(Priority priority = Priority::IMMEDIATE);
+
+ FakeMetricReportQueue(Priority priority,
+ ReportingSettings* reporting_settings,
+ const std::string& rate_setting_path,
+ base::TimeDelta default_rate,
+ int rate_unit_to_ms = 1);
+
+ FakeMetricReportQueue(const FakeMetricReportQueue& other) = delete;
+ FakeMetricReportQueue& operator=(const FakeMetricReportQueue& other) = delete;
+
+ ~FakeMetricReportQueue() override;
+
+ void Enqueue(const MetricData& metric_data,
+ ReportQueue::EnqueueCallback callback) override;
+
+ void Flush() override;
+
+ std::vector<MetricData> GetMetricDataReported() const;
+
+ int GetNumFlush() const;
+
+ private:
+ std::vector<MetricData> reported_data_;
+
+ int num_flush_ = 0;
+};
+} // namespace test
+} // namespace reporting
+
+#endif // COMPONENTS_REPORTING_METRICS_FAKE_METRIC_REPORT_QUEUE_H_
diff --git a/components/reporting/metrics/fake_sampler.cc b/components/reporting/metrics/fake_sampler.cc
new file mode 100644
index 0000000..16c5d00
--- /dev/null
+++ b/components/reporting/metrics/fake_sampler.cc
@@ -0,0 +1,23 @@
+// Copyright 2021 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 "components/reporting/metrics/fake_sampler.h"
+
+namespace reporting {
+namespace test {
+
+void FakeSampler::Collect(MetricCallback cb) {
+ num_calls_++;
+ std::move(cb).Run(std::move(metric_data_));
+}
+
+void FakeSampler::SetMetricData(MetricData metric_data) {
+ metric_data_ = std::move(metric_data);
+}
+
+int FakeSampler::GetNumCollectCalls() const {
+ return num_calls_;
+}
+} // namespace test
+} // namespace reporting
diff --git a/components/reporting/metrics/fake_sampler.h b/components/reporting/metrics/fake_sampler.h
new file mode 100644
index 0000000..73b051af
--- /dev/null
+++ b/components/reporting/metrics/fake_sampler.h
@@ -0,0 +1,37 @@
+// Copyright 2021 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.
+
+#ifndef COMPONENTS_REPORTING_METRICS_FAKE_SAMPLER_H_
+#define COMPONENTS_REPORTING_METRICS_FAKE_SAMPLER_H_
+
+#include "components/reporting/metrics/sampler.h"
+#include "components/reporting/proto/metric_data.pb.h"
+
+namespace reporting {
+namespace test {
+
+class FakeSampler : public Sampler {
+ public:
+ FakeSampler() = default;
+
+ FakeSampler(const FakeSampler& other) = delete;
+ FakeSampler& operator=(const FakeSampler& other) = delete;
+
+ ~FakeSampler() override = default;
+
+ void Collect(MetricCallback cb) override;
+
+ void SetMetricData(MetricData metric_data);
+
+ int GetNumCollectCalls() const;
+
+ private:
+ MetricData metric_data_;
+
+ int num_calls_ = 0;
+};
+} // namespace test
+} // namespace reporting
+
+#endif // COMPONENTS_REPORTING_METRICS_FAKE_SAMPLER_H_
diff --git a/components/reporting/metrics/metric_data_collector_unittest.cc b/components/reporting/metrics/metric_data_collector_unittest.cc
index fa81305..8a738c9 100644
--- a/components/reporting/metrics/metric_data_collector_unittest.cc
+++ b/components/reporting/metrics/metric_data_collector_unittest.cc
@@ -10,76 +10,18 @@
#include "base/location.h"
#include "base/run_loop.h"
-#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
-#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h"
-#include "components/reporting/client/report_queue.h"
+#include "components/reporting/metrics/fake_metric_report_queue.h"
#include "components/reporting/metrics/fake_reporting_settings.h"
+#include "components/reporting/metrics/fake_sampler.h"
#include "components/reporting/metrics/metric_report_queue.h"
-#include "components/reporting/metrics/sampler.h"
#include "components/reporting/proto/metric_data.pb.h"
-#include "components/reporting/proto/synced/record_constants.pb.h"
-#include "components/reporting/util/status.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace reporting {
-
-class FakeSampler : public Sampler {
- public:
- FakeSampler() = default;
-
- FakeSampler(const FakeSampler& other) = delete;
- FakeSampler& operator=(const FakeSampler& other) = delete;
-
- ~FakeSampler() override = default;
-
- void Collect(MetricCallback cb) override {
- num_calls_++;
- std::move(cb).Run(std::move(metric_data_));
- }
-
- void SetMetricData(MetricData metric_data) {
- metric_data_ = std::move(metric_data);
- }
-
- int GetNumCollectCalls() { return num_calls_; }
-
- private:
- MetricData metric_data_;
-
- int num_calls_ = 0;
-};
-
-class FakeMetricReportQueue : public MetricReportQueue {
- public:
- FakeMetricReportQueue()
- : MetricReportQueue(
- std::unique_ptr<ReportQueue, base::OnTaskRunnerDeleter>(
- nullptr,
- base::OnTaskRunnerDeleter(
- base::SequencedTaskRunnerHandle::Get())),
- Priority::IMMEDIATE) {}
-
- FakeMetricReportQueue(const FakeMetricReportQueue& other) = delete;
- FakeMetricReportQueue& operator=(const FakeMetricReportQueue& other) = delete;
-
- ~FakeMetricReportQueue() override = default;
-
- void Enqueue(const MetricData& metric_data,
- ReportQueue::EnqueueCallback callback) override {
- reported_data_.emplace_back(metric_data);
- std::move(callback).Run(Status());
- }
-
- void Flush() override {}
-
- std::vector<MetricData> GetMetricDataReported() { return reported_data_; }
-
- private:
- std::vector<MetricData> reported_data_;
-};
+namespace test {
class FakeEventDetector : public EventDetector {
public:
@@ -111,13 +53,14 @@
std::vector<MetricData> previous_metric_list_;
};
+} // namespace test
class MetricDataCollectorTest : public ::testing::Test {
public:
void SetUp() override {
settings_ = std::make_unique<FakeReportingSettings>();
- sampler_ = std::make_unique<FakeSampler>();
- metric_report_queue_ = std::make_unique<FakeMetricReportQueue>();
+ sampler_ = std::make_unique<test::FakeSampler>();
+ metric_report_queue_ = std::make_unique<test::FakeMetricReportQueue>();
}
protected:
@@ -128,8 +71,8 @@
const std::string kRateSettingPath = "rate_path";
std::unique_ptr<FakeReportingSettings> settings_;
- std::unique_ptr<FakeSampler> sampler_;
- std::unique_ptr<FakeMetricReportQueue> metric_report_queue_;
+ std::unique_ptr<test::FakeSampler> sampler_;
+ std::unique_ptr<test::FakeMetricReportQueue> metric_report_queue_;
};
TEST_F(MetricDataCollectorTest, OneShotCollector_InitiallyEnabled) {
@@ -306,7 +249,7 @@
metric_data[0].mutable_info_data();
metric_data[1].mutable_telemetry_data();
- auto event_detector = std::make_unique<FakeEventDetector>();
+ auto event_detector = std::make_unique<test::FakeEventDetector>();
auto* event_detector_ptr = event_detector.get();
PeriodicEventCollector collector(sampler_.get(), std::move(event_detector),
@@ -377,14 +320,14 @@
->mutable_https_latency_data()
->set_latency_ms(1500);
- FakeSampler additional_samplers[3];
+ test::FakeSampler additional_samplers[3];
std::vector<Sampler*> additional_sampler_ptrs;
for (int i = 0; i < 3; ++i) {
additional_samplers[i].SetMetricData(additional_metric_data[i]);
additional_sampler_ptrs.emplace_back(additional_samplers + i);
}
- auto event_detector = std::make_unique<FakeEventDetector>();
+ auto event_detector = std::make_unique<test::FakeEventDetector>();
auto* event_detector_ptr = event_detector.get();
PeriodicEventCollector collector(sampler_.get(), std::move(event_detector),