Chromium Code Reviews| Index: net/url_request/url_request_throttler_unittest.cc |
| diff --git a/net/url_request/url_request_throttler_unittest.cc b/net/url_request/url_request_throttler_unittest.cc |
| index e47da30ef9a507cec0d3bf9e5b6f8e7e4e212dcf..d4f31f5d3b6d3424cb8cb0f4e8da7c379df134e5 100644 |
| --- a/net/url_request/url_request_throttler_unittest.cc |
| +++ b/net/url_request/url_request_throttler_unittest.cc |
| @@ -1,3 +1,4 @@ |
| + |
|
wtc
2013/07/26 22:11:14
Nit: remove this blank line.
|
| // Copyright (c) 2012 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. |
| @@ -5,13 +6,12 @@ |
| #include "net/url_request/url_request_throttler_manager.h" |
| #include "base/memory/scoped_ptr.h" |
| -#include "base/metrics/histogram.h" |
| #include "base/metrics/histogram_samples.h" |
| -#include "base/metrics/statistics_recorder.h" |
| #include "base/pickle.h" |
| #include "base/stl_util.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/stringprintf.h" |
| +#include "base/test/histogram_recorder.h" |
| #include "base/time/time.h" |
| #include "net/base/load_flags.h" |
| #include "net/base/test_completion_callback.h" |
| @@ -28,10 +28,7 @@ namespace net { |
| namespace { |
| -using base::Histogram; |
| -using base::HistogramBase; |
| -using base::HistogramSamples; |
| -using base::StatisticsRecorder; |
| +const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled"; |
| class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
| public: |
| @@ -176,31 +173,17 @@ class URLRequestThrottlerEntryTest : public testing::Test { |
| } |
| virtual void SetUp(); |
| - virtual void TearDown(); |
| - |
| - // After calling this function, histogram snapshots in |samples_| contain |
| - // only the delta caused by the test case currently running. |
| - void CalculateHistogramDeltas(); |
| TimeTicks now_; |
| MockURLRequestThrottlerManager manager_; // Dummy object, not used. |
| scoped_refptr<MockURLRequestThrottlerEntry> entry_; |
| - std::map<std::string, HistogramSamples*> original_samples_; |
| - std::map<std::string, HistogramSamples*> samples_; |
| + scoped_ptr<base::HistogramRecorder> histogram_recorder_; |
| TestURLRequestContext context_; |
| TestURLRequest request_; |
| }; |
| -// List of all histograms we care about in these unit tests. |
| -const char* kHistogramNames[] = { |
| - "Throttling.FailureCountAtSuccess", |
| - "Throttling.PerceivedDowntime", |
| - "Throttling.RequestThrottled", |
| - "Throttling.SiteOptedOut", |
| -}; |
| - |
| void URLRequestThrottlerEntryTest::SetUp() { |
| request_.set_load_flags(0); |
| @@ -208,43 +191,7 @@ void URLRequestThrottlerEntryTest::SetUp() { |
| entry_ = new MockURLRequestThrottlerEntry(&manager_); |
| entry_->ResetToBlank(now_); |
| - for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| - // Must retrieve original samples for each histogram for comparison |
| - // as other tests may affect them. |
| - const char* name = kHistogramNames[i]; |
| - HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| - if (histogram) { |
| - original_samples_[name] = histogram->SnapshotSamples().release(); |
| - } else { |
| - original_samples_[name] = NULL; |
| - } |
| - } |
| -} |
| - |
| -void URLRequestThrottlerEntryTest::TearDown() { |
| - STLDeleteValues(&original_samples_); |
| - STLDeleteValues(&samples_); |
| -} |
| - |
| -void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() { |
| - for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| - const char* name = kHistogramNames[i]; |
| - HistogramSamples* original = original_samples_[name]; |
| - |
| - HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| - if (histogram) { |
| - ASSERT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| - |
| - scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); |
| - if (original) |
| - samples->Subtract(*original); |
| - samples_[name] = samples.release(); |
| - } |
| - } |
| - |
| - // Ensure we don't accidentally use the originals in our tests. |
| - STLDeleteValues(&original_samples_); |
| - original_samples_.clear(); |
| + histogram_recorder_.reset(new base::HistogramRecorder()); |
| } |
| std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
| @@ -260,9 +207,11 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
| request_.set_load_flags(LOAD_MAYBE_USER_GESTURE); |
| EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| - CalculateHistogramDeltas(); |
| - ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0)); |
| - ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1)); |
| + scoped_ptr<base::HistogramSamples> samples( |
| + histogram_recorder_->GetHistogramSamplesSinceCreation( |
| + kRequestThrottledHistogramName)); |
| + ASSERT_EQ(1, samples->GetCount(0)); |
| + ASSERT_EQ(1, samples->GetCount(1)); |
| } |
| TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| @@ -272,9 +221,11 @@ TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
| EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| - CalculateHistogramDeltas(); |
| - ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0)); |
| - ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1)); |
| + scoped_ptr<base::HistogramSamples> samples( |
| + histogram_recorder_->GetHistogramSamplesSinceCreation( |
| + kRequestThrottledHistogramName)); |
| + ASSERT_EQ(2, samples->GetCount(0)); |
| + ASSERT_EQ(0, samples->GetCount(1)); |
| } |
| TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |