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

Side by Side Diff: chrome/service/cloud_print/cloud_print_url_fetcher_unittest.cc

Issue 10203002: Make URLRequestThrottlerManager a member of URLRequestContext. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Ready for review. Created 8 years, 8 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) 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 "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/message_loop_proxy.h" 7 #include "base/message_loop_proxy.h"
8 #include "base/synchronization/waitable_event.h" 8 #include "base/synchronization/waitable_event.h"
9 #include "base/threading/thread.h" 9 #include "base/threading/thread.h"
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 12 matching lines...) Expand all
23 23
24 namespace { 24 namespace {
25 25
26 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); 26 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data");
27 27
28 int g_request_context_getter_instances = 0; 28 int g_request_context_getter_instances = 0;
29 class TrackingTestURLRequestContextGetter 29 class TrackingTestURLRequestContextGetter
30 : public TestURLRequestContextGetter { 30 : public TestURLRequestContextGetter {
31 public: 31 public:
32 explicit TrackingTestURLRequestContextGetter( 32 explicit TrackingTestURLRequestContextGetter(
33 base::MessageLoopProxy* io_message_loop_proxy) 33 base::MessageLoopProxy* io_message_loop_proxy,
34 : TestURLRequestContextGetter(io_message_loop_proxy) { 34 net::URLRequestThrottlerManager* throttler_manager)
35 : TestURLRequestContextGetter(io_message_loop_proxy),
36 throttler_manager_(throttler_manager),
37 context_(NULL) {
35 g_request_context_getter_instances++; 38 g_request_context_getter_instances++;
36 } 39 }
40
41 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE {
42 if (!context_) {
43 context_ = new TestURLRequestContext(true);
44 context_->set_throttler_manager(throttler_manager_);
45 context_->Init();
46 }
47 return context_.get();
48 }
49
37 protected: 50 protected:
38 virtual ~TrackingTestURLRequestContextGetter() { 51 virtual ~TrackingTestURLRequestContextGetter() {
39 g_request_context_getter_instances--; 52 g_request_context_getter_instances--;
40 } 53 }
54
55 private:
56 // Not owned here.
57 net::URLRequestThrottlerManager* throttler_manager_;
58 scoped_refptr<TestURLRequestContext> context_;
41 }; 59 };
42 60
43 class TestCloudPrintURLFetcher : public CloudPrintURLFetcher { 61 class TestCloudPrintURLFetcher : public CloudPrintURLFetcher {
44 public: 62 public:
45 explicit TestCloudPrintURLFetcher( 63 explicit TestCloudPrintURLFetcher(
46 base::MessageLoopProxy* io_message_loop_proxy) 64 base::MessageLoopProxy* io_message_loop_proxy,
47 : io_message_loop_proxy_(io_message_loop_proxy) { 65 net::URLRequestThrottlerManager* throttler_manager)
66 : io_message_loop_proxy_(io_message_loop_proxy),
67 throttler_manager_(throttler_manager) {
48 } 68 }
49 69
50 virtual net::URLRequestContextGetter* GetRequestContextGetter() { 70 virtual net::URLRequestContextGetter* GetRequestContextGetter() {
51 return new TrackingTestURLRequestContextGetter( 71 return new TrackingTestURLRequestContextGetter(
52 io_message_loop_proxy_.get()); 72 io_message_loop_proxy_.get(), throttler_manager_);
53 } 73 }
54 private: 74 private:
55 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 75 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
76
77 // Not owned here.
78 net::URLRequestThrottlerManager* throttler_manager_;
56 }; 79 };
57 80
58 class CloudPrintURLFetcherTest : public testing::Test, 81 class CloudPrintURLFetcherTest : public testing::Test,
59 public CloudPrintURLFetcherDelegate { 82 public CloudPrintURLFetcherDelegate {
60 public: 83 public:
61 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { } 84 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { }
62 85
63 // Creates a URLFetcher, using the program's main thread to do IO. 86 // Creates a URLFetcher, using the program's main thread to do IO.
64 virtual void CreateFetcher(const GURL& url, int max_retries); 87 virtual void CreateFetcher(const GURL& url, int max_retries);
65 88
(...skipping 12 matching lines...) Expand all
78 } 101 }
79 102
80 virtual std::string GetAuthHeader() { 103 virtual std::string GetAuthHeader() {
81 return std::string(); 104 return std::string();
82 } 105 }
83 106
84 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { 107 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() {
85 return io_message_loop_proxy_; 108 return io_message_loop_proxy_;
86 } 109 }
87 110
111 net::URLRequestThrottlerManager* throttler_manager() {
112 return &throttler_manager_;
113 }
114
88 protected: 115 protected:
89 virtual void SetUp() { 116 virtual void SetUp() {
90 testing::Test::SetUp(); 117 testing::Test::SetUp();
91 118
92 io_message_loop_proxy_ = base::MessageLoopProxy::current(); 119 io_message_loop_proxy_ = base::MessageLoopProxy::current();
93 } 120 }
94 121
95 virtual void TearDown() { 122 virtual void TearDown() {
96 fetcher_ = NULL; 123 fetcher_ = NULL;
97 // Deleting the fetcher causes a task to be posted to the IO thread to 124 // Deleting the fetcher causes a task to be posted to the IO thread to
98 // release references to the URLRequestContextGetter. We need to run all 125 // release references to the URLRequestContextGetter. We need to run all
99 // pending tasks to execute that (this is the IO thread). 126 // pending tasks to execute that (this is the IO thread).
100 MessageLoop::current()->RunAllPending(); 127 MessageLoop::current()->RunAllPending();
101 EXPECT_EQ(0, g_request_context_getter_instances); 128 EXPECT_EQ(0, g_request_context_getter_instances);
102 } 129 }
103 130
104 // URLFetcher is designed to run on the main UI thread, but in our tests 131 // URLFetcher is designed to run on the main UI thread, but in our tests
105 // we assume that the current thread is the IO thread where the URLFetcher 132 // we assume that the current thread is the IO thread where the URLFetcher
106 // dispatches its requests to. When we wish to simulate being used from 133 // dispatches its requests to. When we wish to simulate being used from
107 // a UI thread, we dispatch a worker thread to do so. 134 // a UI thread, we dispatch a worker thread to do so.
108 MessageLoopForIO io_loop_; 135 MessageLoopForIO io_loop_;
109 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 136 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
110 int max_retries_; 137 int max_retries_;
111 Time start_time_; 138 Time start_time_;
112 scoped_refptr<CloudPrintURLFetcher> fetcher_; 139 scoped_refptr<CloudPrintURLFetcher> fetcher_;
140
141 // We set this as the throttler manager for the
142 // TestURLRequestContext we create.
143 net::URLRequestThrottlerManager throttler_manager_;
eroman 2012/04/24 19:05:57 I wonder if it wouldn't be better to declare this
Jói 2012/04/27 13:20:26 Made it a member of the TestCloudPrintURLFetcher.
113 }; 144 };
114 145
115 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest { 146 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest {
116 public: 147 public:
117 CloudPrintURLFetcherBasicTest() 148 CloudPrintURLFetcherBasicTest()
118 : handle_raw_response_(false), handle_raw_data_(false) { } 149 : handle_raw_response_(false), handle_raw_data_(false) { }
119 // CloudPrintURLFetcher::Delegate 150 // CloudPrintURLFetcher::Delegate
120 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( 151 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse(
121 const content::URLFetcher* source, 152 const content::URLFetcher* source,
122 const GURL& url, 153 const GURL& url,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 const std::string& data); 207 const std::string& data);
177 208
178 virtual void OnRequestGiveUp(); 209 virtual void OnRequestGiveUp();
179 210
180 private: 211 private:
181 int response_count_; 212 int response_count_;
182 }; 213 };
183 214
184 215
185 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) { 216 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) {
186 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy()); 217 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy(),
218 throttler_manager());
187 max_retries_ = max_retries; 219 max_retries_ = max_retries;
188 start_time_ = Time::Now(); 220 start_time_ = Time::Now();
189 fetcher_->StartGetRequest(url, this, max_retries_, std::string()); 221 fetcher_->StartGetRequest(url, this, max_retries_, std::string());
190 } 222 }
191 223
192 CloudPrintURLFetcher::ResponseAction 224 CloudPrintURLFetcher::ResponseAction
193 CloudPrintURLFetcherTest::HandleRawResponse( 225 CloudPrintURLFetcherTest::HandleRawResponse(
194 const content::URLFetcher* source, 226 const content::URLFetcher* source,
195 const GURL& url, 227 const GURL& url,
196 const net::URLRequestStatus& status, 228 const net::URLRequestStatus& status,
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 TEST_F(CloudPrintURLFetcherOverloadTest, Protect) { 348 TEST_F(CloudPrintURLFetcherOverloadTest, Protect) {
317 net::TestServer test_server(net::TestServer::TYPE_HTTP, 349 net::TestServer test_server(net::TestServer::TYPE_HTTP,
318 net::TestServer::kLocalhost, 350 net::TestServer::kLocalhost,
319 FilePath(kDocRoot)); 351 FilePath(kDocRoot));
320 ASSERT_TRUE(test_server.Start()); 352 ASSERT_TRUE(test_server.Start());
321 353
322 GURL url(test_server.GetURL("defaultresponse")); 354 GURL url(test_server.GetURL("defaultresponse"));
323 355
324 // Registers an entry for test url. It only allows 3 requests to be sent 356 // Registers an entry for test url. It only allows 3 requests to be sent
325 // in 200 milliseconds. 357 // in 200 milliseconds.
326 net::URLRequestThrottlerManager* manager =
327 net::URLRequestThrottlerManager::GetInstance();
328 scoped_refptr<net::URLRequestThrottlerEntry> entry( 358 scoped_refptr<net::URLRequestThrottlerEntry> entry(
329 new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256)); 359 new net::URLRequestThrottlerEntry(
330 manager->OverrideEntryForTests(url, entry); 360 throttler_manager(), "", 200, 3, 1, 2.0, 0.0, 256));
361 throttler_manager()->OverrideEntryForTests(url, entry);
331 362
332 CreateFetcher(url, 11); 363 CreateFetcher(url, 11);
333 364
334 MessageLoop::current()->Run(); 365 MessageLoop::current()->Run();
335
336 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
337 } 366 }
338 367
339 // http://code.google.com/p/chromium/issues/detail?id=60426 368 // http://code.google.com/p/chromium/issues/detail?id=60426
340 TEST_F(CloudPrintURLFetcherRetryBackoffTest, DISABLED_GiveUp) { 369 TEST_F(CloudPrintURLFetcherRetryBackoffTest, DISABLED_GiveUp) {
341 net::TestServer test_server(net::TestServer::TYPE_HTTP, 370 net::TestServer test_server(net::TestServer::TYPE_HTTP,
342 net::TestServer::kLocalhost, 371 net::TestServer::kLocalhost,
343 FilePath(kDocRoot)); 372 FilePath(kDocRoot));
344 ASSERT_TRUE(test_server.Start()); 373 ASSERT_TRUE(test_server.Start());
345 374
346 GURL url(test_server.GetURL("defaultresponse")); 375 GURL url(test_server.GetURL("defaultresponse"));
347 376
348 // Registers an entry for test url. The backoff time is calculated by: 377 // Registers an entry for test url. The backoff time is calculated by:
349 // new_backoff = 2.0 * old_backoff + 0 378 // new_backoff = 2.0 * old_backoff + 0
350 // and maximum backoff time is 256 milliseconds. 379 // and maximum backoff time is 256 milliseconds.
351 // Maximum retries allowed is set to 11. 380 // Maximum retries allowed is set to 11.
352 net::URLRequestThrottlerManager* manager =
353 net::URLRequestThrottlerManager::GetInstance();
354 scoped_refptr<net::URLRequestThrottlerEntry> entry( 381 scoped_refptr<net::URLRequestThrottlerEntry> entry(
355 new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256)); 382 new net::URLRequestThrottlerEntry(
356 manager->OverrideEntryForTests(url, entry); 383 throttler_manager(), "", 200, 3, 1, 2.0, 0.0, 256));
384 throttler_manager()->OverrideEntryForTests(url, entry);
357 385
358 CreateFetcher(url, 11); 386 CreateFetcher(url, 11);
359 387
360 MessageLoop::current()->Run(); 388 MessageLoop::current()->Run();
361
362 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
363 } 389 }
364 390
365 } // namespace. 391 } // namespace.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698