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