OLD | NEW |
(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/single_sample_metrics_factory_impl.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/gtest_util.h" |
| 10 #include "base/test/histogram_tester.h" |
| 11 #include "base/threading/thread.h" |
| 12 #include "components/metrics/single_sample_metrics_provider.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace metrics { |
| 16 |
| 17 namespace { |
| 18 |
| 19 const base::HistogramBase::Sample kMin = 1; |
| 20 const base::HistogramBase::Sample kMax = 10; |
| 21 const uint32_t kBucketCount = 10; |
| 22 const char kMetricName[] = "Single.Sample.Metric"; |
| 23 |
| 24 class SingleSampleMetricsFactoryImplTest : public testing::Test { |
| 25 public: |
| 26 SingleSampleMetricsFactoryImplTest() |
| 27 : factory_(base::BindRepeating( |
| 28 &SingleSampleMetricsFactoryImplTest::CreateProvider, |
| 29 base::Unretained(this))), |
| 30 thread_("TestThread") {} |
| 31 |
| 32 ~SingleSampleMetricsFactoryImplTest() override { |
| 33 factory_.DestroyProviderForTesting(); |
| 34 if (thread_.IsRunning()) { |
| 35 thread_.task_runner()->PostTask( |
| 36 FROM_HERE, |
| 37 base::Bind(&SingleSampleMetricsFactoryImpl::DestroyProviderForTesting, |
| 38 base::Unretained(&factory_))); |
| 39 thread_.Stop(); |
| 40 } |
| 41 } |
| 42 |
| 43 protected: |
| 44 void StartThread() { ASSERT_TRUE(thread_.Start()); } |
| 45 |
| 46 void CreateProvider(mojom::SingleSampleMetricsProviderRequest request) { |
| 47 SingleSampleMetricsProvider::Create(std::move(request)); |
| 48 provider_count_++; |
| 49 } |
| 50 |
| 51 std::unique_ptr<base::SingleSampleMetric> CreateMetricOnThread() { |
| 52 std::unique_ptr<base::SingleSampleMetric> metric; |
| 53 base::RunLoop run_loop; |
| 54 thread_.task_runner()->PostTaskAndReply( |
| 55 FROM_HERE, |
| 56 base::Bind(&SingleSampleMetricsFactoryImplTest::CreateAndStoreMetric, |
| 57 base::Unretained(this), &metric), |
| 58 run_loop.QuitClosure()); |
| 59 run_loop.Run(); |
| 60 return metric; |
| 61 } |
| 62 |
| 63 void CreateAndStoreMetric(std::unique_ptr<base::SingleSampleMetric>* metric) { |
| 64 *metric = factory_.CreateCustomCountsMetric(kMetricName, kMin, kMax, |
| 65 kBucketCount); |
| 66 } |
| 67 |
| 68 base::MessageLoop message_loop_; |
| 69 SingleSampleMetricsFactoryImpl factory_; |
| 70 base::Thread thread_; |
| 71 size_t provider_count_ = 0; |
| 72 |
| 73 private: |
| 74 DISALLOW_COPY_AND_ASSIGN(SingleSampleMetricsFactoryImplTest); |
| 75 }; |
| 76 |
| 77 } // namespace |
| 78 |
| 79 TEST_F(SingleSampleMetricsFactoryImplTest, SingleProvider) { |
| 80 std::unique_ptr<base::SingleSampleMetric> metric1 = |
| 81 factory_.CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount); |
| 82 |
| 83 std::unique_ptr<base::SingleSampleMetric> metric2 = |
| 84 factory_.CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount); |
| 85 |
| 86 // Verify that only a single provider is created for multiple metrics. |
| 87 base::RunLoop().RunUntilIdle(); |
| 88 EXPECT_EQ(1u, provider_count_); |
| 89 } |
| 90 |
| 91 TEST_F(SingleSampleMetricsFactoryImplTest, DoesNothing) { |
| 92 base::HistogramTester tester; |
| 93 |
| 94 std::unique_ptr<base::SingleSampleMetric> metric = |
| 95 factory_.CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount); |
| 96 metric.reset(); |
| 97 |
| 98 // Verify that no sample is recorded if SetSample() is never called. |
| 99 base::RunLoop().RunUntilIdle(); |
| 100 tester.ExpectTotalCount(kMetricName, 0); |
| 101 } |
| 102 |
| 103 TEST_F(SingleSampleMetricsFactoryImplTest, DefaultSingleSampleMetricWithValue) { |
| 104 base::HistogramTester tester; |
| 105 std::unique_ptr<base::SingleSampleMetric> metric = |
| 106 factory_.CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount); |
| 107 |
| 108 const base::HistogramBase::Sample kLastSample = 9; |
| 109 metric->SetSample(1); |
| 110 metric->SetSample(3); |
| 111 metric->SetSample(5); |
| 112 metric->SetSample(kLastSample); |
| 113 metric.reset(); |
| 114 |
| 115 // Verify only the last sample sent to SetSample() is recorded. |
| 116 base::RunLoop().RunUntilIdle(); |
| 117 tester.ExpectUniqueSample(kMetricName, kLastSample, 1); |
| 118 |
| 119 // Verify construction implicitly by requesting a histogram with the same |
| 120 // parameters; this test relies on the fact that histogram objects are unique |
| 121 // per name. Different parameters will result in a nullptr being returned. |
| 122 EXPECT_FALSE(base::Histogram::FactoryGet(kMetricName, 1, 3, 3, |
| 123 base::HistogramBase::kNoFlags)); |
| 124 EXPECT_TRUE(base::Histogram::FactoryGet( |
| 125 kMetricName, kMin, kMax, kBucketCount, |
| 126 base::HistogramBase::kUmaTargetedHistogramFlag)); |
| 127 } |
| 128 |
| 129 TEST_F(SingleSampleMetricsFactoryImplTest, MultithreadedMetrics) { |
| 130 base::HistogramTester tester; |
| 131 std::unique_ptr<base::SingleSampleMetric> metric = |
| 132 factory_.CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount); |
| 133 EXPECT_EQ(1u, provider_count_); |
| 134 |
| 135 StartThread(); |
| 136 |
| 137 std::unique_ptr<base::SingleSampleMetric> threaded_metric = |
| 138 CreateMetricOnThread(); |
| 139 ASSERT_TRUE(threaded_metric); |
| 140 |
| 141 // A second provider should be created to handle requests on our new thread. |
| 142 EXPECT_EQ(2u, provider_count_); |
| 143 |
| 144 // Calls from the wrong thread should DCHECK. |
| 145 EXPECT_DCHECK_DEATH(threaded_metric->SetSample(5)); |
| 146 EXPECT_DCHECK_DEATH(threaded_metric.reset()); |
| 147 |
| 148 // Test that samples are set on each thread correctly. |
| 149 const base::HistogramBase::Sample kSample = 7; |
| 150 |
| 151 { |
| 152 metric->SetSample(kSample); |
| 153 |
| 154 base::RunLoop run_loop; |
| 155 thread_.task_runner()->PostTaskAndReply( |
| 156 FROM_HERE, |
| 157 base::Bind(&base::SingleSampleMetric::SetSample, |
| 158 base::Unretained(threaded_metric.get()), kSample), |
| 159 run_loop.QuitClosure()); |
| 160 run_loop.Run(); |
| 161 } |
| 162 |
| 163 // Release metrics and cycle threads to ensure destruction completes. |
| 164 { |
| 165 thread_.task_runner()->DeleteSoon(FROM_HERE, threaded_metric.release()); |
| 166 |
| 167 base::RunLoop run_loop; |
| 168 thread_.task_runner()->PostTaskAndReply( |
| 169 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure()); |
| 170 run_loop.Run(); |
| 171 } |
| 172 |
| 173 metric.reset(); |
| 174 base::RunLoop().RunUntilIdle(); |
| 175 tester.ExpectUniqueSample(kMetricName, kSample, 2); |
| 176 } |
| 177 |
| 178 } // namespace metrics |
OLD | NEW |