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/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
10 #include "base/pickle.h" | 11 #include "base/pickle.h" |
12 #include "base/stl_util.h" | |
11 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
12 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
13 #include "base/time.h" | 15 #include "base/time.h" |
14 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
15 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
16 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
17 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
18 #include "net/url_request/url_request_throttler_header_interface.h" | 20 #include "net/url_request/url_request_throttler_header_interface.h" |
19 #include "net/url_request/url_request_throttler_test_support.h" | 21 #include "net/url_request/url_request_throttler_test_support.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 | 23 |
22 using base::TimeDelta; | 24 using base::TimeDelta; |
23 using base::TimeTicks; | 25 using base::TimeTicks; |
24 | 26 |
25 namespace net { | 27 namespace net { |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 using base::Histogram; | 31 using base::Histogram; |
32 using base::HistogramSamples; | |
30 using base::StatisticsRecorder; | 33 using base::StatisticsRecorder; |
31 | 34 |
32 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { | 35 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry { |
33 public: | 36 public: |
34 explicit MockURLRequestThrottlerEntry( | 37 explicit MockURLRequestThrottlerEntry( |
35 net::URLRequestThrottlerManager* manager) | 38 net::URLRequestThrottlerManager* manager) |
36 : net::URLRequestThrottlerEntry(manager, ""), | 39 : net::URLRequestThrottlerEntry(manager, ""), |
37 mock_backoff_entry_(&backoff_policy_) { | 40 mock_backoff_entry_(&backoff_policy_) { |
38 InitPolicy(); | 41 InitPolicy(); |
39 } | 42 } |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 }; | 168 }; |
166 | 169 |
167 } // namespace | 170 } // namespace |
168 | 171 |
169 class URLRequestThrottlerEntryTest : public testing::Test { | 172 class URLRequestThrottlerEntryTest : public testing::Test { |
170 protected: | 173 protected: |
171 URLRequestThrottlerEntryTest() : request_(GURL(), NULL, &context_) { | 174 URLRequestThrottlerEntryTest() : request_(GURL(), NULL, &context_) { |
172 } | 175 } |
173 | 176 |
174 virtual void SetUp(); | 177 virtual void SetUp(); |
178 virtual void TearDown(); | |
175 | 179 |
176 // After calling this function, histogram snapshots in |samples_| contain | 180 // After calling this function, histogram snapshots in |samples_| contain |
177 // only the delta caused by the test case currently running. | 181 // only the delta caused by the test case currently running. |
178 void CalculateHistogramDeltas(); | 182 void CalculateHistogramDeltas(); |
179 | 183 |
180 TimeTicks now_; | 184 TimeTicks now_; |
181 MockURLRequestThrottlerManager manager_; // Dummy object, not used. | 185 MockURLRequestThrottlerManager manager_; // Dummy object, not used. |
182 scoped_refptr<MockURLRequestThrottlerEntry> entry_; | 186 scoped_refptr<MockURLRequestThrottlerEntry> entry_; |
183 | 187 |
184 std::map<std::string, Histogram::SampleSet> original_samples_; | 188 std::map<std::string, HistogramSamples*> original_samples_; |
185 std::map<std::string, Histogram::SampleSet> samples_; | 189 std::map<std::string, HistogramSamples*> samples_; |
186 | 190 |
187 TestURLRequestContext context_; | 191 TestURLRequestContext context_; |
188 TestURLRequest request_; | 192 TestURLRequest request_; |
189 }; | 193 }; |
190 | 194 |
191 // List of all histograms we care about in these unit tests. | 195 // List of all histograms we care about in these unit tests. |
192 const char* kHistogramNames[] = { | 196 const char* kHistogramNames[] = { |
193 "Throttling.FailureCountAtSuccess", | 197 "Throttling.FailureCountAtSuccess", |
194 "Throttling.PerceivedDowntime", | 198 "Throttling.PerceivedDowntime", |
195 "Throttling.RequestThrottled", | 199 "Throttling.RequestThrottled", |
196 "Throttling.SiteOptedOut", | 200 "Throttling.SiteOptedOut", |
197 }; | 201 }; |
198 | 202 |
199 void URLRequestThrottlerEntryTest::SetUp() { | 203 void URLRequestThrottlerEntryTest::SetUp() { |
200 request_.set_load_flags(0); | 204 request_.set_load_flags(0); |
201 | 205 |
202 now_ = TimeTicks::Now(); | 206 now_ = TimeTicks::Now(); |
203 entry_ = new MockURLRequestThrottlerEntry(&manager_); | 207 entry_ = new MockURLRequestThrottlerEntry(&manager_); |
204 entry_->ResetToBlank(now_); | 208 entry_->ResetToBlank(now_); |
205 | 209 |
206 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | 210 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
207 // Must retrieve original samples for each histogram for comparison | 211 // Must retrieve original samples for each histogram for comparison |
208 // as other tests may affect them. | 212 // as other tests may affect them. |
209 const char* name = kHistogramNames[i]; | 213 const char* name = kHistogramNames[i]; |
210 Histogram::SampleSet& original = original_samples_[name]; | |
211 Histogram* histogram = StatisticsRecorder::FindHistogram(name); | 214 Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
212 if (histogram) { | 215 if (histogram) { |
213 histogram->SnapshotSample(&original); | 216 original_samples_[name] = histogram->SnapshotSamples().release(); |
217 } else { | |
218 original_samples_[name] = NULL; | |
214 } | 219 } |
215 } | 220 } |
216 } | 221 } |
217 | 222 |
223 void URLRequestThrottlerEntryTest::TearDown() { | |
224 STLDeleteValues(&original_samples_); | |
225 STLDeleteValues(&samples_); | |
226 } | |
227 | |
218 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() { | 228 void URLRequestThrottlerEntryTest::CalculateHistogramDeltas() { |
219 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { | 229 for (size_t i = 0; i < arraysize(kHistogramNames); ++i) { |
220 const char* name = kHistogramNames[i]; | 230 const char* name = kHistogramNames[i]; |
221 Histogram::SampleSet& original = original_samples_[name]; | 231 HistogramSamples* original = original_samples_[name]; |
222 Histogram::SampleSet& sample = samples_[name]; | |
223 | 232 |
224 Histogram* histogram = StatisticsRecorder::FindHistogram(name); | 233 Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
225 if (histogram) { | 234 if (histogram) { |
226 ASSERT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 235 ASSERT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); |
227 | 236 |
228 histogram->SnapshotSample(&sample); | 237 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); |
229 // Ensure |original| size is same as |sample|, then subtract original | 238 if (original != NULL) |
Ilya Sherman
2012/09/12 03:20:58
nit: You can just write this as "if (original)"
kaiwang
2012/09/20 22:54:59
Done.
| |
230 // values. | 239 samples->Subtract(*original); |
231 original.Resize(histogram->bucket_count()); | 240 samples_[name] = samples.release(); |
Ilya Sherman
2012/09/12 03:20:58
Is |samples_[name]| guaranteed to be empty prior t
kaiwang
2012/09/20 22:54:59
it's guranteed to be empty
| |
232 sample.Subtract(original); | |
233 } | 241 } |
234 } | 242 } |
235 | 243 |
236 // Ensure we don't accidentally use the originals in our tests. | 244 // Ensure we don't accidentally use the originals in our tests. |
245 STLDeleteValues(&original_samples_); | |
237 original_samples_.clear(); | 246 original_samples_.clear(); |
238 } | 247 } |
239 | 248 |
240 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { | 249 std::ostream& operator<<(std::ostream& out, const base::TimeTicks& time) { |
241 return out << time.ToInternalValue(); | 250 return out << time.ToInternalValue(); |
242 } | 251 } |
243 | 252 |
244 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { | 253 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) { |
245 entry_->set_exponential_backoff_release_time( | 254 entry_->set_exponential_backoff_release_time( |
246 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); | 255 entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1)); |
247 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); | 256 EXPECT_TRUE(entry_->ShouldRejectRequest(request_)); |
248 | 257 |
249 // Also end-to-end test the load flags exceptions. | 258 // Also end-to-end test the load flags exceptions. |
250 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE); | 259 request_.set_load_flags(LOAD_MAYBE_USER_GESTURE); |
251 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 260 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
252 | 261 |
253 CalculateHistogramDeltas(); | 262 CalculateHistogramDeltas(); |
254 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(0)); | 263 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(0)); |
255 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"].counts(1)); | 264 ASSERT_EQ(1, samples_["Throttling.RequestThrottled"]->GetCount(1)); |
256 } | 265 } |
257 | 266 |
258 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { | 267 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) { |
259 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); | 268 entry_->set_exponential_backoff_release_time(entry_->fake_time_now_); |
260 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 269 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
261 entry_->set_exponential_backoff_release_time( | 270 entry_->set_exponential_backoff_release_time( |
262 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); | 271 entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1)); |
263 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); | 272 EXPECT_FALSE(entry_->ShouldRejectRequest(request_)); |
264 | 273 |
265 CalculateHistogramDeltas(); | 274 CalculateHistogramDeltas(); |
266 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"].counts(0)); | 275 ASSERT_EQ(2, samples_["Throttling.RequestThrottled"]->GetCount(0)); |
267 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"].counts(1)); | 276 ASSERT_EQ(0, samples_["Throttling.RequestThrottled"]->GetCount(1)); |
268 } | 277 } |
269 | 278 |
270 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { | 279 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) { |
271 MockURLRequestThrottlerHeaderAdapter failure_response(503); | 280 MockURLRequestThrottlerHeaderAdapter failure_response(503); |
272 entry_->UpdateWithResponse("", &failure_response); | 281 entry_->UpdateWithResponse("", &failure_response); |
273 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) | 282 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), entry_->fake_time_now_) |
274 << "A failure should increase the release_time"; | 283 << "A failure should increase the release_time"; |
275 } | 284 } |
276 | 285 |
277 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { | 286 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateSuccess) { |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
545 FAIL(); | 554 FAIL(); |
546 } | 555 } |
547 | 556 |
548 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = | 557 scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after = |
549 manager.RegisterRequestUrl(GURL("http://www.example.com/")); | 558 manager.RegisterRequestUrl(GURL("http://www.example.com/")); |
550 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); | 559 EXPECT_FALSE(entry_after->ShouldRejectRequest(request_)); |
551 } | 560 } |
552 } | 561 } |
553 | 562 |
554 } // namespace net | 563 } // namespace net |
OLD | NEW |