| 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_samples.h" | 9 #include "base/metrics/histogram_samples.h" |
| 10 #include "base/metrics/statistics_recorder.h" |
| 9 #include "base/pickle.h" | 11 #include "base/pickle.h" |
| 10 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 13 #include "base/test/histogram_recorder.h" | |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 16 #include "net/base/request_priority.h" | 17 #include "net/base/request_priority.h" |
| 17 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
| 18 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 20 #include "net/url_request/url_request_throttler_header_interface.h" | 21 #include "net/url_request/url_request_throttler_header_interface.h" |
| 21 #include "net/url_request/url_request_throttler_test_support.h" | 22 #include "net/url_request/url_request_throttler_test_support.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 using base::TimeDelta; | 25 using base::TimeDelta; |
| 25 using base::TimeTicks; | 26 using base::TimeTicks; |
| 26 | 27 |
| 27 namespace net { | 28 namespace net { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled"; | 32 using base::Histogram; |
| 33 using base::HistogramBase; |
| 34 using base::HistogramSamples; |
| 35 using base::StatisticsRecorder; |
| 32 | 36 |
| 33 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 37 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
| 34 public: | 38 public: |
| 35 explicit MockURLRequestThrottlerEntry( | 39 explicit MockURLRequestThrottlerEntry( |
| 36 net::URLRequestThrottlerManager* manager) | 40 net::URLRequestThrottlerManager* manager) |
| 37 : net::URLRequestThrottlerEntry(manager, std::string()), | 41 : net::URLRequestThrottlerEntry(manager, std::string()), |
| 38 mock_backoff_entry_(&backoff_policy_) { | 42 mock_backoff_entry_(&backoff_policy_) { |
| 39 InitPolicy(); | 43 InitPolicy(); |
| 40 } | 44 } |
| 41 MockURLRequestThrottlerEntry( | 45 MockURLRequestThrottlerEntry( |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 int line; | 169 int line; |
| 166 }; | 170 }; |
| 167 | 171 |
| 168 } // namespace | 172 } // namespace |
| 169 | 173 |
| 170 class URLRequestThrottlerEntryTest : public testing::Test { | 174 class URLRequestThrottlerEntryTest : public testing::Test { |
| 171 protected: | 175 protected: |
| 172 URLRequestThrottlerEntryTest() | 176 URLRequestThrottlerEntryTest() |
| 173 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} | 177 : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {} |
| 174 | 178 |
| 175 static void SetUpTestCase() { | 179 virtual void SetUp(); |
| 176 base::HistogramRecorder::Initialize(); | 180 virtual void TearDown(); |
| 177 } | |
| 178 | 181 |
| 179 virtual void SetUp(); | 182 // After calling this function, histogram snapshots in |samples_| contain |
| 183 // only the delta caused by the test case currently running. |
| 184 void CalculateHistogramDeltas(); |
| 180 | 185 |
| 181 TimeTicks now_; | 186 TimeTicks now_; |
| 182 MockURLRequestThrottlerManager manager_; // Dummy object, not used. | 187 MockURLRequestThrottlerManager manager_; // Dummy object, not used. |
| 183 scoped_refptr<MockURLRequestThrottlerEntry> entry_; | 188 scoped_refptr<MockURLRequestThrottlerEntry> entry_; |
| 184 | 189 |
| 185 scoped_ptr<base::HistogramRecorder> histogram_recorder_; | 190 std::map<std::string, HistogramSamples*> original_samples_; |
| 191 std::map<std::string, HistogramSamples*> samples_; |
| 186 | 192 |
| 187 TestURLRequestContext context_; | 193 TestURLRequestContext context_; |
| 188 TestURLRequest request_; | 194 TestURLRequest request_; |
| 189 }; | 195 }; |
| 190 | 196 |
| 197 // List of all histograms we care about in these unit tests. |
| 198 const char* kHistogramNames[] = { |
| 199 "Throttling.FailureCountAtSuccess", |
| 200 "Throttling.PerceivedDowntime", |
| 201 "Throttling.RequestThrottled", |
| 202 "Throttling.SiteOptedOut", |
| 203 }; |
| 204 |
| 191 void URLRequestThrottlerEntryTest::SetUp() { | 205 void URLRequestThrottlerEntryTest::SetUp() { |
| 192 request_.SetLoadFlags(0); | 206 request_.SetLoadFlags(0); |
| 193 | 207 |
| 194 now_ = TimeTicks::Now(); | 208 now_ = TimeTicks::Now(); |
| 195 entry_ = new MockURLRequestThrottlerEntry(&manager_); | 209 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
| 196 entry_->ResetToBlank(now_); | 210 entry_->ResetToBlank(now_); |
| 197 | 211 |
| 198 histogram_recorder_.reset(new base::HistogramRecorder()); | 212 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| 213 // Must retrieve original samples for each histogram for comparison |
| 214 // as other tests may affect them. |
| 215 const char* name = kHistogramNames[i]; |
| 216 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 217 if (histogram) { |
| 218 original_samples_[name] = histogram->SnapshotSamples().release(); |
| 219 } else { |
| 220 original_samples_[name] = NULL; |
| 221 } |
| 222 } |
| 223 } |
| 224 |
| 225 void URLRequestThrottlerEntryTest::TearDown() { |
| 226 STLDeleteValues(&original_samples_); |
| 227 STLDeleteValues(&samples_); |
| 228 } |
| 229 |
| 230 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() { |
| 231 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
| 232 const char* name = kHistogramNames[i]; |
| 233 HistogramSamples* original = original_samples_[name]; |
| 234 |
| 235 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 236 if (histogram) { |
| 237 ASSERT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 238 |
| 239 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); |
| 240 if (original) |
| 241 samples->Subtract(*original); |
| 242 samples_[name] = samples.release(); |
| 243 } |
| 244 } |
| 245 |
| 246 // Ensure we don't accidentally use the originals in our tests. |
| 247 STLDeleteValues(&original_samples_); |
| 248 original_samples_.clear(); |
| 199 } | 249 } |
| 200 | 250 |
| 201 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 251 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
| 202 return out << time.ToInternalValue(); | 252 return out << time.ToInternalValue(); |
| 203 } | 253 } |
| 204 | 254 |
| 205 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) { | 255 TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) { |
| 206 TestNetworkDelegate d; | 256 TestNetworkDelegate d; |
| 207 context_.set_network_delegate(&d); | 257 context_.set_network_delegate(&d); |
| 208 entry_->set_exponential_backoff_release_time( | 258 entry_->set_exponential_backoff_release_time( |
| 209 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 259 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
| 210 | 260 |
| 211 d.set_can_throttle_requests(false); | 261 d.set_can_throttle_requests(false); |
| 212 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 262 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 213 d.set_can_throttle_requests(true); | 263 d.set_can_throttle_requests(true); |
| 214 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 264 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
| 215 } | 265 } |
| 216 | 266 |
| 217 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 267 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
| 218 entry_->set_exponential_backoff_release_time( | 268 entry_->set_exponential_backoff_release_time( |
| 219 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 269 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
| 220 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 270 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
| 221 | 271 |
| 222 // Also end-to-end test the load flags exceptions. | 272 // Also end-to-end test the load flags exceptions. |
| 223 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); | 273 request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE); |
| 224 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 274 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 225 | 275 |
| 226 scoped_ptr<base::HistogramSamples> samples( | 276 CalculateHistogramDeltas(); |
| 227 histogram_recorder_->GetHistogramSamplesSinceCreation( | 277 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0)); |
| 228 kRequestThrottledHistogramName)); | 278 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1)); |
| 229 ASSERT_EQ(1, samples->GetCount(0)); | |
| 230 ASSERT_EQ(1, samples->GetCount(1)); | |
| 231 } | 279 } |
| 232 | 280 |
| 233 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 281 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
| 234 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 282 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
| 235 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 283 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 236 entry_->set_exponential_backoff_release_time( | 284 entry_->set_exponential_backoff_release_time( |
| 237 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 285 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
| 238 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 286 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
| 239 | 287 |
| 240 scoped_ptr<base::HistogramSamples> samples( | 288 CalculateHistogramDeltas(); |
| 241 histogram_recorder_->GetHistogramSamplesSinceCreation( | 289 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0)); |
| 242 kRequestThrottledHistogramName)); | 290 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1)); |
| 243 ASSERT_EQ(2, samples->GetCount(0)); | |
| 244 ASSERT_EQ(0, samples->GetCount(1)); | |
| 245 } | 291 } |
| 246 | 292 |
| 247 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 293 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
| 248 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 294 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
| 249 entry_->UpdateWithResponse(std::string(), &failure_response); | 295 entry_->UpdateWithResponse(std::string(), &failure_response); |
| 250 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 296 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
| 251 << "A failure should increase the release_time"; | 297 << "A failure should increase the release_time"; |
| 252 } | 298 } |
| 253 | 299 |
| 254 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 300 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 FAIL(); | 569 FAIL(); |
| 524 } | 570 } |
| 525 | 571 |
| 526 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 572 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
| 527 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 573 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
| 528 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 574 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
| 529 } | 575 } |
| 530 } | 576 } |
| 531 | 577 |
| 532 } // namespace net | 578 } // namespace net |
| OLD | NEW |