Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | |
|
wtc
2013/07/26 22:11:14
Nit: remove this blank line.
| |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 // 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 | 3 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 4 // found in the LICENSE file. |
| 4 | 5 |
| 5 #include "net/url_request/url_request_throttler_manager.h" | 6 #include "net/url_request/url_request_throttler_manager.h" |
| 6 | 7 |
| 7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.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" | |
| 11 #include "base/pickle.h" | 10 #include "base/pickle.h" |
| 12 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/test/histogram_recorder.h" | |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 17 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
| 18 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 20 #include "net/url_request/url_request_throttler_header_interface.h" | 20 #include "net/url_request/url_request_throttler_header_interface.h" |
| 21 #include "net/url_request/url_request_throttler_test_support.h" | 21 #include "net/url_request/url_request_throttler_test_support.h" |
| 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 const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled"; |
| 32 using base::HistogramBase; | |
| 33 using base::HistogramSamples; | |
| 34 using base::StatisticsRecorder; | |
| 35 | 32 |
| 36 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 33 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
| 37 public: | 34 public: |
| 38 explicit MockURLRequestThrottlerEntry( | 35 explicit MockURLRequestThrottlerEntry( |
| 39 net::URLRequestThrottlerManager* manager) | 36 net::URLRequestThrottlerManager* manager) |
| 40 : net::URLRequestThrottlerEntry(manager, std::string()), | 37 : net::URLRequestThrottlerEntry(manager, std::string()), |
| 41 mock_backoff_entry_(&backoff_policy_) { | 38 mock_backoff_entry_(&backoff_policy_) { |
| 42 InitPolicy(); | 39 InitPolicy(); |
| 43 } | 40 } |
| 44 MockURLRequestThrottlerEntry( | 41 MockURLRequestThrottlerEntry( |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 }; | 166 }; |
| 170 | 167 |
| 171 } // namespace | 168 } // namespace |
| 172 | 169 |
| 173 class URLRequestThrottlerEntryTest : public testing::Test { | 170 class URLRequestThrottlerEntryTest : public testing::Test { |
| 174 protected: | 171 protected: |
| 175 URLRequestThrottlerEntryTest() : request_(GURL(), NULL, &context_, NULL) { | 172 URLRequestThrottlerEntryTest() : request_(GURL(), NULL, &context_, NULL) { |
| 176 } | 173 } |
| 177 | 174 |
| 178 virtual void SetUp(); | 175 virtual void SetUp(); |
| 179 virtual void TearDown(); | |
| 180 | |
| 181 // After calling this function, histogram snapshots in |samples_| contain | |
| 182 // only the delta caused by the test case currently running. | |
| 183 void CalculateHistogramDeltas(); | |
| 184 | 176 |
| 185 TimeTicks now_; | 177 TimeTicks now_; |
| 186 MockURLRequestThrottlerManager manager_; // Dummy object, not used. | 178 MockURLRequestThrottlerManager manager_; // Dummy object, not used. |
| 187 scoped_refptr<MockURLRequestThrottlerEntry> entry_; | 179 scoped_refptr<MockURLRequestThrottlerEntry> entry_; |
| 188 | 180 |
| 189 std::map<std::string, HistogramSamples*> original_samples_; | 181 scoped_ptr<base::HistogramRecorder> histogram_recorder_; |
| 190 std::map<std::string, HistogramSamples*> samples_; | |
| 191 | 182 |
| 192 TestURLRequestContext context_; | 183 TestURLRequestContext context_; |
| 193 TestURLRequest request_; | 184 TestURLRequest request_; |
| 194 }; | 185 }; |
| 195 | 186 |
| 196 // List of all histograms we care about in these unit tests. | |
| 197 const char* kHistogramNames[] = { | |
| 198 "Throttling.FailureCountAtSuccess", | |
| 199 "Throttling.PerceivedDowntime", | |
| 200 "Throttling.RequestThrottled", | |
| 201 "Throttling.SiteOptedOut", | |
| 202 }; | |
| 203 | |
| 204 void URLRequestThrottlerEntryTest::SetUp() { | 187 void URLRequestThrottlerEntryTest::SetUp() { |
| 205 request_.set_load_flags(0); | 188 request_.set_load_flags(0); |
| 206 | 189 |
| 207 now_ = TimeTicks::Now(); | 190 now_ = TimeTicks::Now(); |
| 208 entry_ = new MockURLRequestThrottlerEntry(&manager_); | 191 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
| 209 entry_->ResetToBlank(now_); | 192 entry_->ResetToBlank(now_); |
| 210 | 193 |
| 211 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | 194 histogram_recorder_.reset(new base::HistogramRecorder()); |
| 212 // Must retrieve original samples for each histogram for comparison | |
| 213 // as other tests may affect them. | |
| 214 const char* name = kHistogramNames[i]; | |
| 215 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | |
| 216 if (histogram) { | |
| 217 original_samples_[name] = histogram->SnapshotSamples().release(); | |
| 218 } else { | |
| 219 original_samples_[name] = NULL; | |
| 220 } | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 void URLRequestThrottlerEntryTest::TearDown() { | |
| 225 STLDeleteValues(&original_samples_); | |
| 226 STLDeleteValues(&samples_); | |
| 227 } | |
| 228 | |
| 229 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() { | |
| 230 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | |
| 231 const char* name = kHistogramNames[i]; | |
| 232 HistogramSamples* original = original_samples_[name]; | |
| 233 | |
| 234 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | |
| 235 if (histogram) { | |
| 236 ASSERT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); | |
| 237 | |
| 238 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); | |
| 239 if (original) | |
| 240 samples->Subtract(*original); | |
| 241 samples_[name] = samples.release(); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 // Ensure we don't accidentally use the originals in our tests. | |
| 246 STLDeleteValues(&original_samples_); | |
| 247 original_samples_.clear(); | |
| 248 } | 195 } |
| 249 | 196 |
| 250 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 197 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
| 251 return out << time.ToInternalValue(); | 198 return out << time.ToInternalValue(); |
| 252 } | 199 } |
| 253 | 200 |
| 254 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 201 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
| 255 entry_->set_exponential_backoff_release_time( | 202 entry_->set_exponential_backoff_release_time( |
| 256 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 203 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
| 257 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 204 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
| 258 | 205 |
| 259 // Also end-to-end test the load flags exceptions. | 206 // Also end-to-end test the load flags exceptions. |
| 260 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE); | 207 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE); |
| 261 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 208 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 262 | 209 |
| 263 CalculateHistogramDeltas(); | 210 scoped_ptr<base::HistogramSamples> samples( |
| 264 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0)); | 211 histogram_recorder_->GetHistogramSamplesSinceCreation( |
| 265 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1)); | 212 kRequestThrottledHistogramName)); |
| 213 ASSERT_EQ(1, samples->GetCount(0)); | |
| 214 ASSERT_EQ(1, samples->GetCount(1)); | |
| 266 } | 215 } |
| 267 | 216 |
| 268 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 217 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| 269 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 218 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
| 270 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 219 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 271 entry_->set_exponential_backoff_release_time( | 220 entry_->set_exponential_backoff_release_time( |
| 272 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 221 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
| 273 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 222 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 274 | 223 |
| 275 CalculateHistogramDeltas(); | 224 scoped_ptr<base::HistogramSamples> samples( |
| 276 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0)); | 225 histogram_recorder_->GetHistogramSamplesSinceCreation( |
| 277 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1)); | 226 kRequestThrottledHistogramName)); |
| 227 ASSERT_EQ(2, samples->GetCount(0)); | |
| 228 ASSERT_EQ(0, samples->GetCount(1)); | |
| 278 } | 229 } |
| 279 | 230 |
| 280 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 231 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
| 281 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 232 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 282 entry_->UpdateWithResponse(std::string(), &failure_response); | 233 entry_->UpdateWithResponse(std::string(), &failure_response); |
| 283 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 234 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 284 << "A failure should increase the release_time"; | 235 << "A failure should increase the release_time"; |
| 285 } | 236 } |
| 286 | 237 |
| 287 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 238 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 557 FAIL(); | 508 FAIL(); |
| 558 } | 509 } |
| 559 | 510 |
| 560 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 511 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
| 561 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 512 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 562 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 513 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
| 563 } | 514 } |
| 564 } | 515 } |
| 565 | 516 |
| 566 } // namespace net | 517 } // namespace net |
| OLD | NEW |