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

Unified Diff: chrome/common/net/url_fetcher_unittest.cc

Issue 5276007: Implement exponential back-off mechanism. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: pull latest to merge Created 10 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/common/net/url_fetcher_protect.cc ('k') | chrome/service/cloud_print/cloud_print_consts.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/common/net/url_fetcher_unittest.cc
diff --git a/chrome/common/net/url_fetcher_unittest.cc b/chrome/common/net/url_fetcher_unittest.cc
index 65fb3c58577c70861c62391591555c0d00e11aa8..cd213e7a0d8176c0f544b423d3885ff781dec718 100644
--- a/chrome/common/net/url_fetcher_unittest.cc
+++ b/chrome/common/net/url_fetcher_unittest.cc
@@ -8,11 +8,11 @@
#include "build/build_config.h"
#include "chrome/common/chrome_plugin_lib.h"
#include "chrome/common/net/url_fetcher.h"
-#include "chrome/common/net/url_fetcher_protect.h"
#include "chrome/common/net/url_request_context_getter.h"
#include "net/http/http_response_headers.h"
-#include "net/url_request/url_request_unittest.h"
#include "net/test/test_server.h"
+#include "net/url_request/url_request_throttler_manager.h"
+#include "net/url_request/url_request_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(USE_NSS)
@@ -312,6 +312,7 @@ void URLFetcherProtectTest::CreateFetcher(const GURL& url) {
fetcher_->set_request_context(new TestURLRequestContextGetter(
io_message_loop_proxy()));
start_time_ = Time::Now();
+ fetcher_->set_max_retries(11);
fetcher_->Start();
}
@@ -338,7 +339,7 @@ void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source,
fetcher_->Start();
} else {
// We have already sent 20 requests continuously. And we expect that
- // it takes more than 1 second due to the overload pretection settings.
+ // it takes more than 1 second due to the overload protection settings.
EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
URLFetcherTest::OnURLFetchComplete(source, url, status, response_code,
cookies, data);
@@ -352,6 +353,7 @@ void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) {
io_message_loop_proxy()));
fetcher_->set_automatically_retry_on_5xx(false);
start_time_ = Time::Now();
+ fetcher_->set_max_retries(11);
fetcher_->Start();
}
@@ -419,6 +421,7 @@ void URLFetcherCancelTest::CreateFetcher(const GURL& url) {
CancelTestURLRequestContextGetter* context_getter =
new CancelTestURLRequestContextGetter(io_message_loop_proxy());
fetcher_->set_request_context(context_getter);
+ fetcher_->set_max_retries(2);
fetcher_->Start();
// We need to wait for the creation of the URLRequestContext, since we
// rely on it being destroyed as a signal to end the test.
@@ -500,14 +503,16 @@ TEST_F(URLFetcherProtectTest, Overload) {
// Registers an entry for test url. It only allows 3 requests to be sent
// in 200 milliseconds.
- URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance();
- URLFetcherProtectEntry* entry =
- new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256);
- manager->Register(url.host(), entry);
+ scoped_refptr<net::URLRequestThrottlerEntry> entry(
+ new net::URLRequestThrottlerEntry(200, 3, 1, 0, 2.0, 0.0, 256));
+ net::URLRequestThrottlerManager::GetInstance()->OverrideEntryForTests(
+ url, entry);
CreateFetcher(url);
MessageLoop::current()->Run();
+
+ net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
}
TEST_F(URLFetcherProtectTest, ServerUnavailable) {
@@ -520,14 +525,16 @@ TEST_F(URLFetcherProtectTest, ServerUnavailable) {
// new_backoff = 2.0 * old_backoff + 0
// and maximum backoff time is 256 milliseconds.
// Maximum retries allowed is set to 11.
- URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance();
- URLFetcherProtectEntry* entry =
- new URLFetcherProtectEntry(200, 3, 11, 1, 2.0, 0, 256);
- manager->Register(url.host(), entry);
+ scoped_refptr<net::URLRequestThrottlerEntry> entry(
+ new net::URLRequestThrottlerEntry(200, 3, 1, 0, 2.0, 0.0, 256));
+ net::URLRequestThrottlerManager::GetInstance()->OverrideEntryForTests(
+ url, entry);
CreateFetcher(url);
MessageLoop::current()->Run();
+
+ net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
}
TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) {
@@ -538,20 +545,21 @@ TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) {
// Registers an entry for test url. The backoff time is calculated by:
// new_backoff = 2.0 * old_backoff + 0
- // and maximum backoff time is 256 milliseconds.
+ // and maximum backoff time is 150000 milliseconds.
// Maximum retries allowed is set to 11.
- URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance();
+ scoped_refptr<net::URLRequestThrottlerEntry> entry(
+ new net::URLRequestThrottlerEntry(200, 3, 100, 0, 2.0, 0.0, 150000));
// Total time if *not* for not doing automatic backoff would be 150s.
// In reality it should be "as soon as server responds".
- URLFetcherProtectEntry* entry =
- new URLFetcherProtectEntry(200, 3, 11, 100, 2.0, 0, 150000);
- manager->Register(url.host(), entry);
+ net::URLRequestThrottlerManager::GetInstance()->OverrideEntryForTests(
+ url, entry);
CreateFetcher(url);
MessageLoop::current()->Run();
-}
+ net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
+}
TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) {
net::TestServer::HTTPSOptions https_options(
@@ -573,10 +581,10 @@ TEST_F(URLFetcherCancelTest, ReleasesContext) {
// new_backoff = 2.0 * old_backoff + 0
// The initial backoff is 2 seconds and maximum backoff is 4 seconds.
// Maximum retries allowed is set to 2.
- URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance();
- URLFetcherProtectEntry* entry =
- new URLFetcherProtectEntry(200, 3, 2, 2000, 2.0, 0, 4000);
- manager->Register(url.host(), entry);
+ scoped_refptr<net::URLRequestThrottlerEntry> entry(
+ new net::URLRequestThrottlerEntry(200, 3, 2000, 0, 2.0, 0.0, 4000));
+ net::URLRequestThrottlerManager::GetInstance()->OverrideEntryForTests(
+ url, entry);
// Create a separate thread that will create the URLFetcher. The current
// (main) thread will do the IO, and when the fetch is complete it will
@@ -588,6 +596,8 @@ TEST_F(URLFetcherCancelTest, ReleasesContext) {
t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url));
MessageLoop::current()->Run();
+
+ net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
}
TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) {
@@ -597,22 +607,14 @@ TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) {
GURL url(test_server.GetURL("files/server-unavailable.html"));
// Register an entry for test url.
- //
- // Ideally we would mock URLFetcherProtectEntry to return XXX seconds
- // in response to entry->UpdateBackoff(SEND).
- //
- // Unfortunately this function is time sensitive, so we fudge some numbers
- // to make it at least somewhat likely to have a non-zero deferred
- // delay when running.
- //
- // Using a sliding window of 2 seconds, and max of 1 request, under a fast
+ // Using a sliding window of 4 seconds, and max of 1 request, under a fast
// run we expect to have a 4 second delay when posting the Start task.
- URLFetcherProtectManager* manager = URLFetcherProtectManager::GetInstance();
- URLFetcherProtectEntry* entry =
- new URLFetcherProtectEntry(2000, 1, 2, 2000, 2.0, 0, 4000);
- EXPECT_EQ(0, entry->UpdateBackoff(URLFetcherProtectEntry::SEND));
- entry->UpdateBackoff(URLFetcherProtectEntry::SEND); // Returns about 2000.
- manager->Register(url.host(), entry);
+ scoped_refptr<net::URLRequestThrottlerEntry> entry(
+ new net::URLRequestThrottlerEntry(4000, 1, 2000, 0, 2.0, 0.0, 4000));
+ net::URLRequestThrottlerManager::GetInstance()->OverrideEntryForTests(
+ url, entry);
+ // Fake that a request has just started.
+ entry->ReserveSendingTimeForNextRequest(base::TimeTicks());
// The next request we try to send will be delayed by ~4 seconds.
// The slower the test runs, the less the delay will be (since it takes the
@@ -623,6 +625,8 @@ TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) {
t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url));
MessageLoop::current()->Run();
+
+ net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
}
} // namespace.
« no previous file with comments | « chrome/common/net/url_fetcher_protect.cc ('k') | chrome/service/cloud_print/cloud_print_consts.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698