Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1407)

Side by Side Diff: net/url_request/url_request_throttler_unittest.cc

Issue 6730034: Remove all "net::" prefixes under net/url_request for code that's (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed indentation Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/pickle.h" 5 #include "base/pickle.h"
6 #include "base/scoped_ptr.h" 6 #include "base/scoped_ptr.h"
7 #include "base/stringprintf.h" 7 #include "base/stringprintf.h"
8 #include "base/string_number_conversions.h" 8 #include "base/string_number_conversions.h"
9 #include "base/time.h" 9 #include "base/time.h"
10 #include "net/base/test_completion_callback.h" 10 #include "net/base/test_completion_callback.h"
11 #include "net/url_request/url_request_context.h" 11 #include "net/url_request/url_request_context.h"
12 #include "net/url_request/url_request_throttler_header_interface.h" 12 #include "net/url_request/url_request_throttler_header_interface.h"
13 #include "net/url_request/url_request_throttler_manager.h" 13 #include "net/url_request/url_request_throttler_manager.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 using base::TimeDelta; 16 using base::TimeDelta;
17 using base::TimeTicks; 17 using base::TimeTicks;
18 18
19 namespace net {
20
19 namespace { 21 namespace {
20 class MockURLRequestThrottlerManager; 22 class MockURLRequestThrottlerManager;
21 23
22 class MockBackoffEntry : public net::BackoffEntry { 24 class MockBackoffEntry : public BackoffEntry {
23 public: 25 public:
24 explicit MockBackoffEntry(const net::BackoffEntry::Policy* const policy) 26 explicit MockBackoffEntry(const BackoffEntry::Policy* const policy)
25 : net::BackoffEntry(policy), fake_now_(TimeTicks()) { 27 : BackoffEntry(policy), fake_now_(TimeTicks()) {
26 } 28 }
27 29
28 virtual ~MockBackoffEntry() {} 30 virtual ~MockBackoffEntry() {}
29 31
30 TimeTicks GetTimeNow() const { 32 TimeTicks GetTimeNow() const {
31 return fake_now_; 33 return fake_now_;
32 } 34 }
33 35
34 void SetFakeNow(TimeTicks now) { 36 void SetFakeNow(TimeTicks now) {
35 fake_now_ = now; 37 fake_now_ = now;
36 } 38 }
37 39
38 private: 40 private:
39 TimeTicks fake_now_; 41 TimeTicks fake_now_;
40 }; 42 };
41 43
42 class MockURLRequestThrottlerEntry : public net::URLRequestThrottlerEntry { 44 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry {
43 public : 45 public :
44 MockURLRequestThrottlerEntry() : mock_backoff_entry_(&backoff_policy_) { 46 MockURLRequestThrottlerEntry() : mock_backoff_entry_(&backoff_policy_) {
45 // Some tests become flaky if we have jitter. 47 // Some tests become flaky if we have jitter.
46 backoff_policy_.jitter_factor = 0.0; 48 backoff_policy_.jitter_factor = 0.0;
47 } 49 }
48 MockURLRequestThrottlerEntry( 50 MockURLRequestThrottlerEntry(
49 const TimeTicks& exponential_backoff_release_time, 51 const TimeTicks& exponential_backoff_release_time,
50 const TimeTicks& sliding_window_release_time, 52 const TimeTicks& sliding_window_release_time,
51 const TimeTicks& fake_now) 53 const TimeTicks& fake_now)
52 : fake_time_now_(fake_now), 54 : fake_time_now_(fake_now),
53 mock_backoff_entry_(&backoff_policy_) { 55 mock_backoff_entry_(&backoff_policy_) {
54 // Some tests become flaky if we have jitter. 56 // Some tests become flaky if we have jitter.
55 backoff_policy_.jitter_factor = 0.0; 57 backoff_policy_.jitter_factor = 0.0;
56 58
57 mock_backoff_entry_.SetFakeNow(fake_now); 59 mock_backoff_entry_.SetFakeNow(fake_now);
58 set_exponential_backoff_release_time(exponential_backoff_release_time); 60 set_exponential_backoff_release_time(exponential_backoff_release_time);
59 set_sliding_window_release_time(sliding_window_release_time); 61 set_sliding_window_release_time(sliding_window_release_time);
60 } 62 }
61 virtual ~MockURLRequestThrottlerEntry() {} 63 virtual ~MockURLRequestThrottlerEntry() {}
62 64
63 const net::BackoffEntry* GetBackoffEntry() const { 65 const BackoffEntry* GetBackoffEntry() const {
64 return &mock_backoff_entry_; 66 return &mock_backoff_entry_;
65 } 67 }
66 68
67 net::BackoffEntry* GetBackoffEntry() { 69 BackoffEntry* GetBackoffEntry() {
68 return &mock_backoff_entry_; 70 return &mock_backoff_entry_;
69 } 71 }
70 72
71 void ResetToBlank(const TimeTicks& time_now) { 73 void ResetToBlank(const TimeTicks& time_now) {
72 fake_time_now_ = time_now; 74 fake_time_now_ = time_now;
73 mock_backoff_entry_.SetFakeNow(time_now); 75 mock_backoff_entry_.SetFakeNow(time_now);
74 76
75 GetBackoffEntry()->InformOfRequest(true); // Sets failure count to 0. 77 GetBackoffEntry()->InformOfRequest(true); // Sets failure count to 0.
76 GetBackoffEntry()->SetCustomReleaseTime(time_now); 78 GetBackoffEntry()->SetCustomReleaseTime(time_now);
77 set_sliding_window_release_time(time_now); 79 set_sliding_window_release_time(time_now);
78 } 80 }
79 81
80 // Overridden for tests. 82 // Overridden for tests.
81 virtual TimeTicks GetTimeNow() const { return fake_time_now_; } 83 virtual TimeTicks GetTimeNow() const { return fake_time_now_; }
82 84
83 void set_exponential_backoff_release_time( 85 void set_exponential_backoff_release_time(
84 const base::TimeTicks& release_time) { 86 const base::TimeTicks& release_time) {
85 GetBackoffEntry()->SetCustomReleaseTime(release_time); 87 GetBackoffEntry()->SetCustomReleaseTime(release_time);
86 } 88 }
87 89
88 base::TimeTicks sliding_window_release_time() const { 90 base::TimeTicks sliding_window_release_time() const {
89 return net::URLRequestThrottlerEntry::sliding_window_release_time(); 91 return URLRequestThrottlerEntry::sliding_window_release_time();
90 } 92 }
91 93
92 void set_sliding_window_release_time( 94 void set_sliding_window_release_time(
93 const base::TimeTicks& release_time) { 95 const base::TimeTicks& release_time) {
94 net::URLRequestThrottlerEntry::set_sliding_window_release_time( 96 URLRequestThrottlerEntry::set_sliding_window_release_time(
95 release_time); 97 release_time);
96 } 98 }
97 99
98 TimeTicks fake_time_now_; 100 TimeTicks fake_time_now_;
99 MockBackoffEntry mock_backoff_entry_; 101 MockBackoffEntry mock_backoff_entry_;
100 }; 102 };
101 103
102 class MockURLRequestThrottlerHeaderAdapter 104 class MockURLRequestThrottlerHeaderAdapter
103 : public net::URLRequestThrottlerHeaderInterface { 105 : public URLRequestThrottlerHeaderInterface {
104 public: 106 public:
105 MockURLRequestThrottlerHeaderAdapter() 107 MockURLRequestThrottlerHeaderAdapter()
106 : fake_retry_value_("0.0"), 108 : fake_retry_value_("0.0"),
107 fake_response_code_(0) { 109 fake_response_code_(0) {
108 } 110 }
109 111
110 MockURLRequestThrottlerHeaderAdapter(const std::string& retry_value, 112 MockURLRequestThrottlerHeaderAdapter(const std::string& retry_value,
111 int response_code) 113 int response_code)
112 : fake_retry_value_(retry_value), 114 : fake_retry_value_(retry_value),
113 fake_response_code_(response_code) { 115 fake_response_code_(response_code) {
114 } 116 }
115 117
116 virtual ~MockURLRequestThrottlerHeaderAdapter() {} 118 virtual ~MockURLRequestThrottlerHeaderAdapter() {}
117 119
118 virtual std::string GetNormalizedValue(const std::string& key) const { 120 virtual std::string GetNormalizedValue(const std::string& key) const {
119 if (key == MockURLRequestThrottlerEntry::kRetryHeaderName) 121 if (key == MockURLRequestThrottlerEntry::kRetryHeaderName)
120 return fake_retry_value_; 122 return fake_retry_value_;
121 return ""; 123 return "";
122 } 124 }
123 125
124 virtual int GetResponseCode() const { return fake_response_code_; } 126 virtual int GetResponseCode() const { return fake_response_code_; }
125 127
126 std::string fake_retry_value_; 128 std::string fake_retry_value_;
127 int fake_response_code_; 129 int fake_response_code_;
128 }; 130 };
129 131
130 class MockURLRequestThrottlerManager : public net::URLRequestThrottlerManager { 132 class MockURLRequestThrottlerManager : public URLRequestThrottlerManager {
131 public: 133 public:
132 MockURLRequestThrottlerManager() : create_entry_index_(0) {} 134 MockURLRequestThrottlerManager() : create_entry_index_(0) {}
133 135
134 // Method to process the URL using URLRequestThrottlerManager protected 136 // Method to process the URL using URLRequestThrottlerManager protected
135 // method. 137 // method.
136 std::string DoGetUrlIdFromUrl(const GURL& url) { return GetIdFromUrl(url); } 138 std::string DoGetUrlIdFromUrl(const GURL& url) { return GetIdFromUrl(url); }
137 139
138 // Method to use the garbage collecting method of URLRequestThrottlerManager. 140 // Method to use the garbage collecting method of URLRequestThrottlerManager.
139 void DoGarbageCollectEntries() { GarbageCollectEntries(); } 141 void DoGarbageCollectEntries() { GarbageCollectEntries(); }
140 142
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 295
294 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime(); 296 TimeTicks release_after_failures = entry_->GetExponentialBackoffReleaseTime();
295 297
296 // Inform the entry that a response body was malformed, which is supposed to 298 // Inform the entry that a response body was malformed, which is supposed to
297 // increase the back-off time. 299 // increase the back-off time.
298 entry_->ReceivedContentWasMalformed(); 300 entry_->ReceivedContentWasMalformed();
299 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures); 301 EXPECT_GT(entry_->GetExponentialBackoffReleaseTime(), release_after_failures);
300 } 302 }
301 303
302 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) { 304 TEST_F(URLRequestThrottlerEntryTest, SlidingWindow) {
303 int max_send = net::URLRequestThrottlerEntry::kDefaultMaxSendThreshold; 305 int max_send = URLRequestThrottlerEntry::kDefaultMaxSendThreshold;
304 int sliding_window = 306 int sliding_window =
305 net::URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs; 307 URLRequestThrottlerEntry::kDefaultSlidingWindowPeriodMs;
306 308
307 TimeTicks time_1 = entry_->fake_time_now_ + 309 TimeTicks time_1 = entry_->fake_time_now_ +
308 TimeDelta::FromMilliseconds(sliding_window / 3); 310 TimeDelta::FromMilliseconds(sliding_window / 3);
309 TimeTicks time_2 = entry_->fake_time_now_ + 311 TimeTicks time_2 = entry_->fake_time_now_ +
310 TimeDelta::FromMilliseconds(2 * sliding_window / 3); 312 TimeDelta::FromMilliseconds(2 * sliding_window / 3);
311 TimeTicks time_3 = entry_->fake_time_now_ + 313 TimeTicks time_3 = entry_->fake_time_now_ +
312 TimeDelta::FromMilliseconds(sliding_window); 314 TimeDelta::FromMilliseconds(sliding_window);
313 TimeTicks time_4 = entry_->fake_time_now_ + 315 TimeTicks time_4 = entry_->fake_time_now_ +
314 TimeDelta::FromMilliseconds(sliding_window + 2 * sliding_window / 3); 316 TimeDelta::FromMilliseconds(sliding_window + 2 * sliding_window / 3);
315 317
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 MockURLRequestThrottlerManager manager; 384 MockURLRequestThrottlerManager manager;
383 385
384 manager.RegisterRequestUrl(GURL("http://www.example.com/")); 386 manager.RegisterRequestUrl(GURL("http://www.example.com/"));
385 manager.RegisterRequestUrl(GURL("http://www.google.com/")); 387 manager.RegisterRequestUrl(GURL("http://www.google.com/"));
386 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0")); 388 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0"));
387 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1")); 389 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0?code=1"));
388 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure")); 390 manager.RegisterRequestUrl(GURL("http://www.google.com/index/0#lolsaure"));
389 391
390 EXPECT_EQ(3, manager.GetNumberOfEntries()); 392 EXPECT_EQ(3, manager.GetNumberOfEntries());
391 } 393 }
394
395 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_throttler_header_adapter.cc ('k') | net/url_request/url_request_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698