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

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

Issue 4194001: Implement exponential back-off mechanism and enforce it at the URLRequestHttpJob level. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years 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) 2010 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/message_loop_proxy.h" 6 #include "base/message_loop_proxy.h"
7 #include "base/ref_counted.h" 7 #include "base/ref_counted.h"
8 #include "base/thread.h" 8 #include "base/thread.h"
9 #include "base/waitable_event.h" 9 #include "base/waitable_event.h"
10 #include "chrome/common/net/url_fetcher_protect.h"
11 #include "chrome/common/net/url_request_context_getter.h" 10 #include "chrome/common/net/url_request_context_getter.h"
12 #include "chrome/service/service_process.h" 11 #include "chrome/service/service_process.h"
13 #include "chrome/service/cloud_print/cloud_print_url_fetcher.h" 12 #include "chrome/service/cloud_print/cloud_print_url_fetcher.h"
14 #include "googleurl/src/gurl.h" 13 #include "googleurl/src/gurl.h"
15 #include "net/test/test_server.h" 14 #include "net/test/test_server.h"
15 #include "net/url_request/url_request_status.h"
16 #include "net/url_request/url_request_throttler_manager.h"
16 #include "net/url_request/url_request_unittest.h" 17 #include "net/url_request/url_request_unittest.h"
17 #include "net/url_request/url_request_status.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 using base::Time; 20 using base::Time;
21 using base::TimeDelta; 21 using base::TimeDelta;
22 22
23 namespace { 23 namespace {
24 24
25 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); 25 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data");
26 26
27 int g_request_context_getter_instances = 0; 27 int g_request_context_getter_instances = 0;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 virtual URLRequestContextGetter* GetRequestContextGetter() { 62 virtual URLRequestContextGetter* GetRequestContextGetter() {
63 return new TestURLRequestContextGetter(io_message_loop_proxy_.get()); 63 return new TestURLRequestContextGetter(io_message_loop_proxy_.get());
64 } 64 }
65 private: 65 private:
66 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 66 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
67 }; 67 };
68 68
69 class CloudPrintURLFetcherTest : public testing::Test, 69 class CloudPrintURLFetcherTest : public testing::Test,
70 public CloudPrintURLFetcher::Delegate { 70 public CloudPrintURLFetcher::Delegate {
71 public: 71 public:
72 CloudPrintURLFetcherTest() : fetcher_(NULL) { } 72 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { }
73 73
74 // Creates a URLFetcher, using the program's main thread to do IO. 74 // Creates a URLFetcher, using the program's main thread to do IO.
75 virtual void CreateFetcher(const GURL& url, const std::string& retry_policy); 75 virtual void CreateFetcher(const GURL& url, int max_retries);
76 76
77 // CloudPrintURLFetcher::Delegate 77 // CloudPrintURLFetcher::Delegate
78 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( 78 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse(
79 const URLFetcher* source, 79 const URLFetcher* source,
80 const GURL& url, 80 const GURL& url,
81 const URLRequestStatus& status, 81 const URLRequestStatus& status,
82 int response_code, 82 int response_code,
83 const ResponseCookies& cookies, 83 const ResponseCookies& cookies,
84 const std::string& data); 84 const std::string& data);
85 85
(...skipping 20 matching lines...) Expand all
106 MessageLoop::current()->RunAllPending(); 106 MessageLoop::current()->RunAllPending();
107 EXPECT_EQ(0, g_request_context_getter_instances); 107 EXPECT_EQ(0, g_request_context_getter_instances);
108 } 108 }
109 109
110 // URLFetcher is designed to run on the main UI thread, but in our tests 110 // URLFetcher is designed to run on the main UI thread, but in our tests
111 // we assume that the current thread is the IO thread where the URLFetcher 111 // we assume that the current thread is the IO thread where the URLFetcher
112 // dispatches its requests to. When we wish to simulate being used from 112 // dispatches its requests to. When we wish to simulate being used from
113 // a UI thread, we dispatch a worker thread to do so. 113 // a UI thread, we dispatch a worker thread to do so.
114 MessageLoopForIO io_loop_; 114 MessageLoopForIO io_loop_;
115 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 115 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
116 std::string retry_policy_; 116 int max_retries_;
117 Time start_time_; 117 Time start_time_;
118 scoped_refptr<CloudPrintURLFetcher> fetcher_; 118 scoped_refptr<CloudPrintURLFetcher> fetcher_;
119 }; 119 };
120 120
121 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest { 121 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest {
122 public: 122 public:
123 CloudPrintURLFetcherBasicTest() 123 CloudPrintURLFetcherBasicTest()
124 : handle_raw_response_(false), handle_raw_data_(false) { } 124 : handle_raw_response_(false), handle_raw_data_(false) { }
125 // CloudPrintURLFetcher::Delegate 125 // CloudPrintURLFetcher::Delegate
126 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( 126 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse(
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 const GURL& url, 181 const GURL& url,
182 const std::string& data); 182 const std::string& data);
183 183
184 virtual void OnRequestGiveUp(); 184 virtual void OnRequestGiveUp();
185 185
186 private: 186 private:
187 int response_count_; 187 int response_count_;
188 }; 188 };
189 189
190 190
191 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, 191 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) {
192 const std::string& retry_policy) {
193 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy()); 192 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy());
194 retry_policy_ = retry_policy; 193 max_retries_ = max_retries;
195 start_time_ = Time::Now(); 194 start_time_ = Time::Now();
196 fetcher_->StartGetRequest(url, this, "", retry_policy_); 195 fetcher_->StartGetRequest(url, this, "", max_retries_);
197 } 196 }
198 197
199 CloudPrintURLFetcher::ResponseAction 198 CloudPrintURLFetcher::ResponseAction
200 CloudPrintURLFetcherTest::HandleRawResponse( 199 CloudPrintURLFetcherTest::HandleRawResponse(
201 const URLFetcher* source, 200 const URLFetcher* source,
202 const GURL& url, 201 const GURL& url,
203 const URLRequestStatus& status, 202 const URLRequestStatus& status,
204 int response_code, 203 int response_code,
205 const ResponseCookies& cookies, 204 const ResponseCookies& cookies,
206 const std::string& data) { 205 const std::string& data) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 return CloudPrintURLFetcher::STOP_PROCESSING; 257 return CloudPrintURLFetcher::STOP_PROCESSING;
259 } 258 }
260 259
261 CloudPrintURLFetcher::ResponseAction 260 CloudPrintURLFetcher::ResponseAction
262 CloudPrintURLFetcherOverloadTest::HandleRawData(const URLFetcher* source, 261 CloudPrintURLFetcherOverloadTest::HandleRawData(const URLFetcher* source,
263 const GURL& url, 262 const GURL& url,
264 const std::string& data) { 263 const std::string& data) {
265 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); 264 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000);
266 response_count_++; 265 response_count_++;
267 if (response_count_ < 20) { 266 if (response_count_ < 20) {
268 fetcher_->StartGetRequest(url, this, "", retry_policy_); 267 fetcher_->StartGetRequest(url, this, "", max_retries_);
269 } else { 268 } else {
270 // We have already sent 20 requests continuously. And we expect that 269 // We have already sent 20 requests continuously. And we expect that
271 // it takes more than 1 second due to the overload pretection settings. 270 // it takes more than 1 second due to the overload protection settings.
272 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); 271 EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
273 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 272 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
274 } 273 }
275 return CloudPrintURLFetcher::STOP_PROCESSING; 274 return CloudPrintURLFetcher::STOP_PROCESSING;
276 } 275 }
277 276
278 CloudPrintURLFetcher::ResponseAction 277 CloudPrintURLFetcher::ResponseAction
279 CloudPrintURLFetcherRetryBackoffTest::HandleRawData(const URLFetcher* source, 278 CloudPrintURLFetcherRetryBackoffTest::HandleRawData(const URLFetcher* source,
280 const GURL& url, 279 const GURL& url,
281 const std::string& data) { 280 const std::string& data) {
282 response_count_++; 281 response_count_++;
283 // First attempt + 11 retries = 12 total responses. 282 // First attempt + 11 retries = 12 total responses.
284 EXPECT_LE(response_count_, 12); 283 EXPECT_LE(response_count_, 12);
285 return CloudPrintURLFetcher::RETRY_REQUEST; 284 return CloudPrintURLFetcher::RETRY_REQUEST;
286 } 285 }
287 286
288 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() { 287 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() {
289 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); 288 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000);
290 // It takes more than 1 second to finish all 11 requests. 289 // It takes more than 1 second to finish all 11 requests.
291 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); 290 EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
292 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 291 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
293 } 292 }
294 293
295 TEST_F(CloudPrintURLFetcherBasicTest, HandleRawResponse) { 294 TEST_F(CloudPrintURLFetcherBasicTest, HandleRawResponse) {
296 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); 295 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
297 ASSERT_TRUE(test_server.Start()); 296 ASSERT_TRUE(test_server.Start());
298 SetHandleRawResponse(true); 297 SetHandleRawResponse(true);
299 298
300 CreateFetcher(test_server.GetURL("echo"), "DummyRetryPolicy"); 299 CreateFetcher(test_server.GetURL("echo"), 0);
301 MessageLoop::current()->Run(); 300 MessageLoop::current()->Run();
302 } 301 }
303 302
304 // http://code.google.com/p/chromium/issues/detail?id=62758 303 // http://code.google.com/p/chromium/issues/detail?id=62758
305 TEST_F(CloudPrintURLFetcherBasicTest, FLAKY_HandleRawData) { 304 TEST_F(CloudPrintURLFetcherBasicTest, FLAKY_HandleRawData) {
306 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); 305 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
307 ASSERT_TRUE(test_server.Start()); 306 ASSERT_TRUE(test_server.Start());
308 307
309 SetHandleRawData(true); 308 SetHandleRawData(true);
310 CreateFetcher(test_server.GetURL("echo"), "DummyRetryPolicy"); 309 CreateFetcher(test_server.GetURL("echo"), 0);
311 MessageLoop::current()->Run(); 310 MessageLoop::current()->Run();
312 } 311 }
313 312
314 TEST_F(CloudPrintURLFetcherOverloadTest, Protect) { 313 TEST_F(CloudPrintURLFetcherOverloadTest, Protect) {
315 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); 314 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
316 ASSERT_TRUE(test_server.Start()); 315 ASSERT_TRUE(test_server.Start());
317 316
318 GURL url(test_server.GetURL("defaultresponse")); 317 GURL url(test_server.GetURL("defaultresponse"));
319 318
320 // Registers an entry for test url. It only allows 3 requests to be sent 319 // Registers an entry for test url. It only allows 3 requests to be sent
321 // in 200 milliseconds. 320 // in 200 milliseconds.
322 std::string retry_policy = "OverloadTestPolicy"; 321 scoped_refptr<net::URLRequestThrottlerEntry> entry(
323 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); 322 new net::URLRequestThrottlerEntry(200, 3, 1, 0, 2.0, 0.0, 256));
324 URLFetcherProtectEntry* entry = 323 net::URLRequestThrottlerManager::GetInstance()->OverrideEntryForTests(
325 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); 324 url, entry);
326 manager->Register(retry_policy, entry);
327 325
328 CreateFetcher(url, retry_policy); 326 CreateFetcher(url, 11);
329 327
330 MessageLoop::current()->Run(); 328 MessageLoop::current()->Run();
329
330 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
331 } 331 }
332 332
333 // http://code.google.com/p/chromium/issues/detail?id=62758 333 // http://code.google.com/p/chromium/issues/detail?id=62758
334 TEST_F(CloudPrintURLFetcherRetryBackoffTest, FLAKY_GiveUp) { 334 TEST_F(CloudPrintURLFetcherRetryBackoffTest, FLAKY_GiveUp) {
335 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); 335 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot));
336 ASSERT_TRUE(test_server.Start()); 336 ASSERT_TRUE(test_server.Start());
337 337
338 GURL url(test_server.GetURL("defaultresponse")); 338 GURL url(test_server.GetURL("defaultresponse"));
339 339
340 // Registers an entry for test url. The backoff time is calculated by: 340 // Registers an entry for test url. The backoff time is calculated by:
341 // new_backoff = 2.0 * old_backoff + 0 341 // new_backoff = 2.0 * old_backoff + 0
342 // and maximum backoff time is 256 milliseconds. 342 // and maximum backoff time is 256 milliseconds.
343 // Maximum retries allowed is set to 11. 343 // Maximum retries allowed is set to 11.
344 std::string retry_policy = "BackoffTestPolicy"; 344 scoped_refptr<net::URLRequestThrottlerEntry> entry(
345 URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance(); 345 new net::URLRequestThrottlerEntry(200, 3, 1, 0, 2.0, 0.0, 256));
346 URLFetcherProtectEntry* entry = 346 net::URLRequestThrottlerManager::GetInstance()->OverrideEntryForTests(
347 new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256); 347 url, entry);
348 manager->Register(retry_policy, entry);
349 348
350 CreateFetcher(url, retry_policy); 349 CreateFetcher(url, 11);
351 350
352 MessageLoop::current()->Run(); 351 MessageLoop::current()->Run();
352
353 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
353 } 354 }
354 355
355 } // namespace. 356 } // namespace.
OLDNEW
« no previous file with comments | « chrome/service/cloud_print/cloud_print_url_fetcher.cc ('k') | chrome/service/cloud_print/job_status_updater.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698