Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/metrics/single_sample_metrics_factory_impl.h" | 5 #include "components/metrics/single_sample_metrics_factory_impl.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/test/gtest_util.h" | 9 #include "base/test/gtest_util.h" |
| 10 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 SingleSampleMetricsFactoryImplTest() : thread_("TestThread") { | 27 SingleSampleMetricsFactoryImplTest() : thread_("TestThread") { |
| 28 InitializeSingleSampleMetricsFactory( | 28 InitializeSingleSampleMetricsFactory( |
| 29 base::BindRepeating(&SingleSampleMetricsFactoryImplTest::CreateProvider, | 29 base::BindRepeating(&SingleSampleMetricsFactoryImplTest::CreateProvider, |
| 30 base::Unretained(this))); | 30 base::Unretained(this))); |
| 31 factory_ = static_cast<SingleSampleMetricsFactoryImpl*>( | 31 factory_ = static_cast<SingleSampleMetricsFactoryImpl*>( |
| 32 base::SingleSampleMetricsFactory::Get()); | 32 base::SingleSampleMetricsFactory::Get()); |
| 33 } | 33 } |
| 34 | 34 |
| 35 ~SingleSampleMetricsFactoryImplTest() override { | 35 ~SingleSampleMetricsFactoryImplTest() override { |
| 36 factory_->DestroyProviderForTesting(); | 36 factory_->DestroyProviderForTesting(); |
| 37 if (thread_.IsRunning()) { | 37 if (thread_.IsRunning()) |
| 38 thread_.task_runner()->PostTask( | 38 ShutdownThread(); |
| 39 FROM_HERE, | |
| 40 base::Bind(&SingleSampleMetricsFactoryImpl::DestroyProviderForTesting, | |
| 41 base::Unretained(factory_))); | |
| 42 thread_.Stop(); | |
| 43 } | |
| 44 base::SingleSampleMetricsFactory::DeleteFactoryForTesting(); | 39 base::SingleSampleMetricsFactory::DeleteFactoryForTesting(); |
| 45 } | 40 } |
| 46 | 41 |
| 47 protected: | 42 protected: |
| 48 void StartThread() { ASSERT_TRUE(thread_.Start()); } | 43 void StartThread() { ASSERT_TRUE(thread_.Start()); } |
| 49 | 44 |
| 45 void ShutdownThread() { | |
| 46 thread_.task_runner()->PostTask( | |
| 47 FROM_HERE, | |
| 48 base::Bind(&SingleSampleMetricsFactoryImpl::DestroyProviderForTesting, | |
| 49 base::Unretained(factory_))); | |
| 50 thread_.Stop(); | |
|
Ilya Sherman
2017/05/19 00:12:41
I'm honestly a bit confused: I'd expect PostTask()
DaleCurtis
2017/05/19 00:13:29
Thread::Stop() joins, so all posted tasks are wait
Ilya Sherman
2017/05/19 00:18:55
Ah, great! =) LGTM in that case -- thanks!
| |
| 51 } | |
| 52 | |
| 50 void CreateProvider(mojom::SingleSampleMetricsProviderRequest request) { | 53 void CreateProvider(mojom::SingleSampleMetricsProviderRequest request) { |
| 51 CreateSingleSampleMetricsProvider(service_manager::BindSourceInfo(), | 54 CreateSingleSampleMetricsProvider(service_manager::BindSourceInfo(), |
| 52 std::move(request)); | 55 std::move(request)); |
| 53 provider_count_++; | 56 provider_count_++; |
| 54 } | 57 } |
| 55 | 58 |
| 56 std::unique_ptr<base::SingleSampleMetric> CreateMetricOnThread() { | 59 std::unique_ptr<base::SingleSampleMetric> CreateMetricOnThread() { |
| 57 std::unique_ptr<base::SingleSampleMetric> metric; | 60 std::unique_ptr<base::SingleSampleMetric> metric; |
| 58 base::RunLoop run_loop; | 61 base::RunLoop run_loop; |
| 59 thread_.task_runner()->PostTaskAndReply( | 62 thread_.task_runner()->PostTaskAndReply( |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 // Verify construction implicitly by requesting a histogram with the same | 127 // Verify construction implicitly by requesting a histogram with the same |
| 125 // parameters; this test relies on the fact that histogram objects are unique | 128 // parameters; this test relies on the fact that histogram objects are unique |
| 126 // per name. Different parameters will result in a nullptr being returned. | 129 // per name. Different parameters will result in a nullptr being returned. |
| 127 EXPECT_FALSE(base::Histogram::FactoryGet(kMetricName, 1, 3, 3, | 130 EXPECT_FALSE(base::Histogram::FactoryGet(kMetricName, 1, 3, 3, |
| 128 base::HistogramBase::kNoFlags)); | 131 base::HistogramBase::kNoFlags)); |
| 129 EXPECT_TRUE(base::Histogram::FactoryGet( | 132 EXPECT_TRUE(base::Histogram::FactoryGet( |
| 130 kMetricName, kMin, kMax, kBucketCount, | 133 kMetricName, kMin, kMax, kBucketCount, |
| 131 base::HistogramBase::kUmaTargetedHistogramFlag)); | 134 base::HistogramBase::kUmaTargetedHistogramFlag)); |
| 132 } | 135 } |
| 133 | 136 |
| 134 // Flaky on Android N5X builders. https://crbug.com/719497 | 137 TEST_F(SingleSampleMetricsFactoryImplTest, MultithreadedMetrics) { |
| 135 #if defined(OS_ANDROID) | |
| 136 #define MAYBE_MultithreadedMetrics DISABLED_MultithreadedMetrics | |
| 137 #else | |
| 138 #define MAYBE_MultithreadedMetrics MultithreadedMetrics | |
| 139 #endif | |
| 140 TEST_F(SingleSampleMetricsFactoryImplTest, MAYBE_MultithreadedMetrics) { | |
| 141 base::HistogramTester tester; | 138 base::HistogramTester tester; |
| 142 std::unique_ptr<base::SingleSampleMetric> metric = | 139 std::unique_ptr<base::SingleSampleMetric> metric = |
| 143 factory_->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount); | 140 factory_->CreateCustomCountsMetric(kMetricName, kMin, kMax, kBucketCount); |
| 144 EXPECT_EQ(1u, provider_count_); | 141 EXPECT_EQ(1u, provider_count_); |
| 145 | 142 |
| 146 StartThread(); | 143 StartThread(); |
| 147 | 144 |
| 148 std::unique_ptr<base::SingleSampleMetric> threaded_metric = | 145 std::unique_ptr<base::SingleSampleMetric> threaded_metric = |
| 149 CreateMetricOnThread(); | 146 CreateMetricOnThread(); |
| 150 ASSERT_TRUE(threaded_metric); | 147 ASSERT_TRUE(threaded_metric); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 164 | 161 |
| 165 base::RunLoop run_loop; | 162 base::RunLoop run_loop; |
| 166 thread_.task_runner()->PostTaskAndReply( | 163 thread_.task_runner()->PostTaskAndReply( |
| 167 FROM_HERE, | 164 FROM_HERE, |
| 168 base::Bind(&base::SingleSampleMetric::SetSample, | 165 base::Bind(&base::SingleSampleMetric::SetSample, |
| 169 base::Unretained(threaded_metric.get()), kSample), | 166 base::Unretained(threaded_metric.get()), kSample), |
| 170 run_loop.QuitClosure()); | 167 run_loop.QuitClosure()); |
| 171 run_loop.Run(); | 168 run_loop.Run(); |
| 172 } | 169 } |
| 173 | 170 |
| 174 // Release metrics and cycle threads to ensure destruction completes. | 171 // Release metrics and shutdown thread to ensure destruction completes. |
| 175 { | 172 thread_.task_runner()->DeleteSoon(FROM_HERE, threaded_metric.release()); |
| 176 thread_.task_runner()->DeleteSoon(FROM_HERE, threaded_metric.release()); | 173 ShutdownThread(); |
| 177 | |
| 178 base::RunLoop run_loop; | |
| 179 thread_.task_runner()->PostTaskAndReply( | |
| 180 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure()); | |
| 181 run_loop.Run(); | |
| 182 } | |
| 183 | 174 |
| 184 metric.reset(); | 175 metric.reset(); |
| 185 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
| 177 | |
| 186 tester.ExpectUniqueSample(kMetricName, kSample, 2); | 178 tester.ExpectUniqueSample(kMetricName, kSample, 2); |
| 187 } | 179 } |
| 188 | 180 |
| 189 } // namespace metrics | 181 } // namespace metrics |
| OLD | NEW |