| 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 "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 Loading... |
| 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 : io_message_loop_proxy_(io_message_loop_proxy) { |
| 48 } | 66 } |
| 49 | 67 |
| 50 virtual net::URLRequestContextGetter* GetRequestContextGetter() { | 68 virtual net::URLRequestContextGetter* GetRequestContextGetter() { |
| 51 return new TrackingTestURLRequestContextGetter( | 69 return new TrackingTestURLRequestContextGetter( |
| 52 io_message_loop_proxy_.get()); | 70 io_message_loop_proxy_.get(), throttler_manager()); |
| 71 } |
| 72 |
| 73 net::URLRequestThrottlerManager* throttler_manager() { |
| 74 return &throttler_manager_; |
| 53 } | 75 } |
| 54 | 76 |
| 55 private: | 77 private: |
| 56 virtual ~TestCloudPrintURLFetcher() {} | 78 virtual ~TestCloudPrintURLFetcher() {} |
| 57 | 79 |
| 58 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 80 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 81 |
| 82 // We set this as the throttler manager for the |
| 83 // TestURLRequestContext we create. |
| 84 net::URLRequestThrottlerManager throttler_manager_; |
| 59 }; | 85 }; |
| 60 | 86 |
| 61 class CloudPrintURLFetcherTest : public testing::Test, | 87 class CloudPrintURLFetcherTest : public testing::Test, |
| 62 public CloudPrintURLFetcherDelegate { | 88 public CloudPrintURLFetcherDelegate { |
| 63 public: | 89 public: |
| 64 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { } | 90 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { } |
| 65 | 91 |
| 66 // Creates a URLFetcher, using the program's main thread to do IO. | 92 // Creates a URLFetcher, using the program's main thread to do IO. |
| 67 virtual void CreateFetcher(const GURL& url, int max_retries); | 93 virtual void CreateFetcher(const GURL& url, int max_retries); |
| 68 | 94 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } | 131 } |
| 106 | 132 |
| 107 // URLFetcher is designed to run on the main UI thread, but in our tests | 133 // URLFetcher is designed to run on the main UI thread, but in our tests |
| 108 // we assume that the current thread is the IO thread where the URLFetcher | 134 // we assume that the current thread is the IO thread where the URLFetcher |
| 109 // dispatches its requests to. When we wish to simulate being used from | 135 // dispatches its requests to. When we wish to simulate being used from |
| 110 // a UI thread, we dispatch a worker thread to do so. | 136 // a UI thread, we dispatch a worker thread to do so. |
| 111 MessageLoopForIO io_loop_; | 137 MessageLoopForIO io_loop_; |
| 112 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 138 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 113 int max_retries_; | 139 int max_retries_; |
| 114 Time start_time_; | 140 Time start_time_; |
| 115 scoped_refptr<CloudPrintURLFetcher> fetcher_; | 141 scoped_refptr<TestCloudPrintURLFetcher> fetcher_; |
| 116 }; | 142 }; |
| 117 | 143 |
| 118 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest { | 144 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest { |
| 119 public: | 145 public: |
| 120 CloudPrintURLFetcherBasicTest() | 146 CloudPrintURLFetcherBasicTest() |
| 121 : handle_raw_response_(false), handle_raw_data_(false) { } | 147 : handle_raw_response_(false), handle_raw_data_(false) { } |
| 122 // CloudPrintURLFetcher::Delegate | 148 // CloudPrintURLFetcher::Delegate |
| 123 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( | 149 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( |
| 124 const content::URLFetcher* source, | 150 const content::URLFetcher* source, |
| 125 const GURL& url, | 151 const GURL& url, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 206 |
| 181 virtual void OnRequestGiveUp(); | 207 virtual void OnRequestGiveUp(); |
| 182 | 208 |
| 183 private: | 209 private: |
| 184 int response_count_; | 210 int response_count_; |
| 185 }; | 211 }; |
| 186 | 212 |
| 187 | 213 |
| 188 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) { | 214 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) { |
| 189 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy()); | 215 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy()); |
| 216 |
| 217 // Registers an entry for test url. It only allows 3 requests to be sent |
| 218 // in 200 milliseconds. |
| 219 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
| 220 new net::URLRequestThrottlerEntry( |
| 221 fetcher_->throttler_manager(), "", 200, 3, 1, 2.0, 0.0, 256)); |
| 222 fetcher_->throttler_manager()->OverrideEntryForTests(url, entry); |
| 223 |
| 190 max_retries_ = max_retries; | 224 max_retries_ = max_retries; |
| 191 start_time_ = Time::Now(); | 225 start_time_ = Time::Now(); |
| 192 fetcher_->StartGetRequest(url, this, max_retries_, std::string()); | 226 fetcher_->StartGetRequest(url, this, max_retries_, std::string()); |
| 193 } | 227 } |
| 194 | 228 |
| 195 CloudPrintURLFetcher::ResponseAction | 229 CloudPrintURLFetcher::ResponseAction |
| 196 CloudPrintURLFetcherTest::HandleRawResponse( | 230 CloudPrintURLFetcherTest::HandleRawResponse( |
| 197 const content::URLFetcher* source, | 231 const content::URLFetcher* source, |
| 198 const GURL& url, | 232 const GURL& url, |
| 199 const net::URLRequestStatus& status, | 233 const net::URLRequestStatus& status, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 MessageLoop::current()->Run(); | 350 MessageLoop::current()->Run(); |
| 317 } | 351 } |
| 318 | 352 |
| 319 TEST_F(CloudPrintURLFetcherOverloadTest, Protect) { | 353 TEST_F(CloudPrintURLFetcherOverloadTest, Protect) { |
| 320 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 354 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 321 net::TestServer::kLocalhost, | 355 net::TestServer::kLocalhost, |
| 322 FilePath(kDocRoot)); | 356 FilePath(kDocRoot)); |
| 323 ASSERT_TRUE(test_server.Start()); | 357 ASSERT_TRUE(test_server.Start()); |
| 324 | 358 |
| 325 GURL url(test_server.GetURL("defaultresponse")); | 359 GURL url(test_server.GetURL("defaultresponse")); |
| 326 | |
| 327 // Registers an entry for test url. It only allows 3 requests to be sent | |
| 328 // in 200 milliseconds. | |
| 329 net::URLRequestThrottlerManager* manager = | |
| 330 net::URLRequestThrottlerManager::GetInstance(); | |
| 331 scoped_refptr<net::URLRequestThrottlerEntry> entry( | |
| 332 new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256)); | |
| 333 manager->OverrideEntryForTests(url, entry); | |
| 334 | |
| 335 CreateFetcher(url, 11); | 360 CreateFetcher(url, 11); |
| 336 | 361 |
| 337 MessageLoop::current()->Run(); | 362 MessageLoop::current()->Run(); |
| 338 | |
| 339 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); | |
| 340 } | 363 } |
| 341 | 364 |
| 342 // http://code.google.com/p/chromium/issues/detail?id=60426 | 365 // http://code.google.com/p/chromium/issues/detail?id=60426 |
| 343 TEST_F(CloudPrintURLFetcherRetryBackoffTest, DISABLED_GiveUp) { | 366 TEST_F(CloudPrintURLFetcherRetryBackoffTest, DISABLED_GiveUp) { |
| 344 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 367 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 345 net::TestServer::kLocalhost, | 368 net::TestServer::kLocalhost, |
| 346 FilePath(kDocRoot)); | 369 FilePath(kDocRoot)); |
| 347 ASSERT_TRUE(test_server.Start()); | 370 ASSERT_TRUE(test_server.Start()); |
| 348 | 371 |
| 349 GURL url(test_server.GetURL("defaultresponse")); | 372 GURL url(test_server.GetURL("defaultresponse")); |
| 350 | |
| 351 // Registers an entry for test url. The backoff time is calculated by: | |
| 352 // new_backoff = 2.0 * old_backoff + 0 | |
| 353 // and maximum backoff time is 256 milliseconds. | |
| 354 // Maximum retries allowed is set to 11. | |
| 355 net::URLRequestThrottlerManager* manager = | |
| 356 net::URLRequestThrottlerManager::GetInstance(); | |
| 357 scoped_refptr<net::URLRequestThrottlerEntry> entry( | |
| 358 new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256)); | |
| 359 manager->OverrideEntryForTests(url, entry); | |
| 360 | |
| 361 CreateFetcher(url, 11); | 373 CreateFetcher(url, 11); |
| 362 | 374 |
| 363 MessageLoop::current()->Run(); | 375 MessageLoop::current()->Run(); |
| 364 | |
| 365 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); | |
| 366 } | 376 } |
| 367 | 377 |
| 368 } // namespace. | 378 } // namespace. |
| OLD | NEW |