| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/url_request/url_request_throttler_manager.h" | 5 #include "net/url_request/url_request_throttler_manager.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/metrics/histogram_samples.h" | 9 #include "base/metrics/histogram_samples.h" |
| 10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 using base::TimeDelta; | 24 using base::TimeDelta; |
| 25 using base::TimeTicks; | 25 using base::TimeTicks; |
| 26 | 26 |
| 27 namespace net { | 27 namespace net { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 using base::Histogram; | 31 using base::Histogram; |
| 32 using base::HistogramBase; |
| 32 using base::HistogramSamples; | 33 using base::HistogramSamples; |
| 33 using base::StatisticsRecorder; | 34 using base::StatisticsRecorder; |
| 34 | 35 |
| 35 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 36 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
| 36 public: | 37 public: |
| 37 explicit MockURLRequestThrottlerEntry( | 38 explicit MockURLRequestThrottlerEntry( |
| 38 net::URLRequestThrottlerManager* manager) | 39 net::URLRequestThrottlerManager* manager) |
| 39 : net::URLRequestThrottlerEntry(manager, ""), | 40 : net::URLRequestThrottlerEntry(manager, ""), |
| 40 mock_backoff_entry_(&backoff_policy_) { | 41 mock_backoff_entry_(&backoff_policy_) { |
| 41 InitPolicy(); | 42 InitPolicy(); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 request_.set_load_flags(0); | 205 request_.set_load_flags(0); |
| 205 | 206 |
| 206 now_ = TimeTicks::Now(); | 207 now_ = TimeTicks::Now(); |
| 207 entry_ = new MockURLRequestThrottlerEntry(&manager_); | 208 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
| 208 entry_->ResetToBlank(now_); | 209 entry_->ResetToBlank(now_); |
| 209 | 210 |
| 210 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | 211 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| 211 // Must retrieve original samples for each histogram for comparison | 212 // Must retrieve original samples for each histogram for comparison |
| 212 // as other tests may affect them. | 213 // as other tests may affect them. |
| 213 const char* name = kHistogramNames[i]; | 214 const char* name = kHistogramNames[i]; |
| 214 Histogram* histogram = StatisticsRecorder::FindHistogram(name); | 215 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 215 if (histogram) { | 216 if (histogram) { |
| 216 original_samples_[name] = histogram->SnapshotSamples().release(); | 217 original_samples_[name] = histogram->SnapshotSamples().release(); |
| 217 } else { | 218 } else { |
| 218 original_samples_[name] = NULL; | 219 original_samples_[name] = NULL; |
| 219 } | 220 } |
| 220 } | 221 } |
| 221 } | 222 } |
| 222 | 223 |
| 223 void URLRequestThrottlerEntryTest::TearDown() { | 224 void URLRequestThrottlerEntryTest::TearDown() { |
| 224 STLDeleteValues(&original_samples_); | 225 STLDeleteValues(&original_samples_); |
| 225 STLDeleteValues(&samples_); | 226 STLDeleteValues(&samples_); |
| 226 } | 227 } |
| 227 | 228 |
| 228 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() { | 229 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() { |
| 229 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | 230 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| 230 const char* name = kHistogramNames[i]; | 231 const char* name = kHistogramNames[i]; |
| 231 HistogramSamples* original = original_samples_[name]; | 232 HistogramSamples* original = original_samples_[name]; |
| 232 | 233 |
| 233 Histogram* histogram = StatisticsRecorder::FindHistogram(name); | 234 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 234 if (histogram) { | 235 if (histogram) { |
| 235 ASSERT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 236 ASSERT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 236 | 237 |
| 237 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); | 238 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); |
| 238 if (original) | 239 if (original) |
| 239 samples->Subtract(*original); | 240 samples->Subtract(*original); |
| 240 samples_[name] = samples.release(); | 241 samples_[name] = samples.release(); |
| 241 } | 242 } |
| 242 } | 243 } |
| 243 | 244 |
| 244 // Ensure we don't accidentally use the originals in our tests. | 245 // Ensure we don't accidentally use the originals in our tests. |
| 245 STLDeleteValues(&original_samples_); | 246 STLDeleteValues(&original_samples_); |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 FAIL(); | 555 FAIL(); |
| 555 } | 556 } |
| 556 | 557 |
| 557 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 558 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
| 558 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 559 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 559 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 560 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
| 560 } | 561 } |
| 561 } | 562 } |
| 562 | 563 |
| 563 } // namespace net | 564 } // namespace net |
| OLD | NEW |