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

Unified Diff: content/common/net/url_fetcher_impl_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 side-by-side diff with in-line comments
Download patch
Index: content/common/net/url_fetcher_impl_unittest.cc
diff --git a/content/common/net/url_fetcher_impl_unittest.cc b/content/common/net/url_fetcher_impl_unittest.cc
index ac5efa95026edfe4a6ad12be13c96c7585a88279..5af0e7c579ecb25508c5597111f73bbbe39e15a0 100644
--- a/content/common/net/url_fetcher_impl_unittest.cc
+++ b/content/common/net/url_fetcher_impl_unittest.cc
@@ -36,6 +36,34 @@ namespace {
const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data");
const char kTestServerFilePrefix[] = "files/";
+class ThrottlingTestURLRequestContextGetter
+ : public TestURLRequestContextGetter {
+ public:
+ explicit ThrottlingTestURLRequestContextGetter(
eroman 2012/04/24 19:05:57 no need for "explicit"
Jói 2012/04/27 13:20:26 Done.
+ base::MessageLoopProxy* io_message_loop_proxy,
+ net::URLRequestThrottlerManager* throttler_manager)
+ : TestURLRequestContextGetter(io_message_loop_proxy),
+ throttler_manager_(throttler_manager),
+ context_(NULL) {
+ }
+
+ virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE {
+ if (!context_) {
+ context_ = new TestURLRequestContext(true);
+ context_->set_throttler_manager(throttler_manager_);
+ context_->Init();
+ }
+ return context_.get();
+ }
+
+ protected:
+ virtual ~ThrottlingTestURLRequestContextGetter() {}
+
+ // Not owned here.
+ net::URLRequestThrottlerManager* throttler_manager_;
eroman 2012/04/24 19:05:57 Same issue -- kinda risky for a refcounted object
Jói 2012/04/27 13:20:26 Fixed by having URLFetcherTest own a TestURLReques
+ scoped_refptr<TestURLRequestContext> context_;
+};
+
} // namespace
class URLFetcherTest : public testing::Test,
@@ -57,6 +85,10 @@ class URLFetcherTest : public testing::Test,
return io_message_loop_proxy_;
}
+ net::URLRequestThrottlerManager* throttler_manager() {
+ return &throttler_manager_;
+ }
+
protected:
virtual void SetUp() OVERRIDE {
testing::Test::SetUp();
@@ -83,12 +115,13 @@ class URLFetcherTest : public testing::Test,
scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
URLFetcherImpl* fetcher_;
+ net::URLRequestThrottlerManager throttler_manager_;
};
void URLFetcherTest::CreateFetcher(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
- fetcher_->SetRequestContext(new TestURLRequestContextGetter(
- io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
fetcher_->Start();
}
@@ -229,6 +262,12 @@ class URLFetcherCancelTest : public URLFetcherTest {
// Version of TestURLRequestContext that posts a Quit task to the IO
// thread once it is deleted.
class CancelTestURLRequestContext : public TestURLRequestContext {
+ public:
+ explicit CancelTestURLRequestContext(bool defer_initialization)
+ : TestURLRequestContext(defer_initialization) {
+ }
+
+ private:
virtual ~CancelTestURLRequestContext() {
// The d'tor should execute in the IO thread. Post the quit task to the
// current thread.
@@ -236,16 +275,22 @@ class CancelTestURLRequestContext : public TestURLRequestContext {
}
};
-class CancelTestURLRequestContextGetter : public net::URLRequestContextGetter {
+class CancelTestURLRequestContextGetter
+ : public ThrottlingTestURLRequestContextGetter {
public:
explicit CancelTestURLRequestContextGetter(
eroman 2012/04/24 19:05:57 no need for "explicit" anymore
Jói 2012/04/27 13:20:26 Done.
- base::MessageLoopProxy* io_message_loop_proxy)
- : io_message_loop_proxy_(io_message_loop_proxy),
+ base::MessageLoopProxy* io_message_loop_proxy,
+ net::URLRequestThrottlerManager* throttler_manager)
+ : ThrottlingTestURLRequestContextGetter(io_message_loop_proxy,
+ throttler_manager),
+ io_message_loop_proxy_(io_message_loop_proxy),
context_created_(false, false) {
}
- virtual net::URLRequestContext* GetURLRequestContext() {
+ virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE {
if (!context_) {
- context_ = new CancelTestURLRequestContext();
+ context_ = new CancelTestURLRequestContext(true);
+ context_->set_throttler_manager(throttler_manager_);
+ context_->Init();
context_created_.Signal();
}
return context_;
@@ -263,7 +308,6 @@ class CancelTestURLRequestContextGetter : public net::URLRequestContextGetter {
private:
scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
base::WaitableEvent context_created_;
- scoped_refptr<net::URLRequestContext> context_;
};
// Version of URLFetcherTest that tests retying the same request twice.
@@ -302,8 +346,8 @@ class URLFetcherFileTest : public URLFetcherTest {
void URLFetcherPostTest::CreateFetcher(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this);
- fetcher_->SetRequestContext(new TestURLRequestContextGetter(
- io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
fetcher_->SetUploadData("application/x-www-form-urlencoded",
"bobsyeruncle");
fetcher_->Start();
@@ -318,8 +362,8 @@ void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) {
void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
- fetcher_->SetRequestContext(new TestURLRequestContextGetter(
- io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
previous_progress_ = 0;
fetcher_->Start();
}
@@ -336,8 +380,8 @@ void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress(
void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
- fetcher_->SetRequestContext(new TestURLRequestContextGetter(
- io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
cancelled_ = false;
fetcher_->Start();
}
@@ -362,8 +406,8 @@ void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete(
void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this);
- fetcher_->SetRequestContext(new TestURLRequestContextGetter(
- io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
previous_progress_ = 0;
// Large enough data to require more than one read from UploadDataStream.
chunk_.assign(1<<16, 'a');
@@ -408,8 +452,8 @@ void URLFetcherSocketAddressTest::OnURLFetchComplete(
void URLFetcherProtectTest::CreateFetcher(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
- fetcher_->SetRequestContext(new TestURLRequestContextGetter(
- io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
start_time_ = Time::Now();
fetcher_->SetMaxRetries(11);
fetcher_->Start();
@@ -434,7 +478,8 @@ void URLFetcherProtectTest::OnURLFetchComplete(
count++;
if (count < 20) {
fetcher_->SetRequestContext(
- new TestURLRequestContextGetter(io_message_loop_proxy()));
+ new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
fetcher_->Start();
} else {
// We have already sent 20 requests continuously. And we expect that
@@ -447,8 +492,8 @@ void URLFetcherProtectTest::OnURLFetchComplete(
void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
- fetcher_->SetRequestContext(new TestURLRequestContextGetter(
- io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
fetcher_->SetAutomaticallyRetryOn5xx(false);
start_time_ = Time::Now();
fetcher_->SetMaxRetries(11);
@@ -511,7 +556,8 @@ void URLFetcherBadHTTPSTest::OnURLFetchComplete(
void URLFetcherCancelTest::CreateFetcher(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
CancelTestURLRequestContextGetter* context_getter =
- new CancelTestURLRequestContextGetter(io_message_loop_proxy());
+ new CancelTestURLRequestContextGetter(io_message_loop_proxy(),
+ throttler_manager());
fetcher_->SetRequestContext(context_getter);
fetcher_->SetMaxRetries(2);
fetcher_->Start();
@@ -545,8 +591,8 @@ void URLFetcherMultipleAttemptTest::OnURLFetchComplete(
EXPECT_FALSE(data.empty());
if (!data.empty() && data_.empty()) {
data_ = data;
- fetcher_->SetRequestContext(
- new TestURLRequestContextGetter(io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
fetcher_->Start();
} else {
EXPECT_EQ(data, data_);
@@ -563,8 +609,8 @@ void URLFetcherMultipleAttemptTest::OnURLFetchComplete(
void URLFetcherFileTest::CreateFetcherForFile(const GURL& url,
const FilePath& file_path) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
- fetcher_->SetRequestContext(new TestURLRequestContextGetter(
- io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
// Use the IO message loop to do the file operations in this test.
fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy());
@@ -573,8 +619,8 @@ void URLFetcherFileTest::CreateFetcherForFile(const GURL& url,
void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) {
fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this);
- fetcher_->SetRequestContext(new TestURLRequestContextGetter(
- io_message_loop_proxy()));
+ fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter(
+ io_message_loop_proxy(), throttler_manager()));
// Use the IO message loop to do the file operations in this test.
fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy());
@@ -766,17 +812,14 @@ TEST_F(URLFetcherProtectTest, Overload) {
// Registers an entry for test url. It only allows 3 requests to be sent
// in 200 milliseconds.
- net::URLRequestThrottlerManager* manager =
- net::URLRequestThrottlerManager::GetInstance();
scoped_refptr<net::URLRequestThrottlerEntry> entry(
- new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256));
- manager->OverrideEntryForTests(url, entry);
+ new net::URLRequestThrottlerEntry(
+ throttler_manager(), "", 200, 3, 1, 2.0, 0.0, 256));
+ throttler_manager()->OverrideEntryForTests(url, entry);
CreateFetcher(url);
MessageLoop::current()->Run();
-
- net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
}
TEST_F(URLFetcherProtectTest, ServerUnavailable) {
@@ -791,17 +834,14 @@ 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.
- net::URLRequestThrottlerManager* manager =
- net::URLRequestThrottlerManager::GetInstance();
scoped_refptr<net::URLRequestThrottlerEntry> entry(
- new net::URLRequestThrottlerEntry(manager, "", 200, 3, 1, 2.0, 0.0, 256));
- manager->OverrideEntryForTests(url, entry);
+ new net::URLRequestThrottlerEntry(
+ throttler_manager(), "", 200, 3, 1, 2.0, 0.0, 256));
+ throttler_manager()->OverrideEntryForTests(url, entry);
CreateFetcher(url);
MessageLoop::current()->Run();
-
- net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
}
TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) {
@@ -816,20 +856,16 @@ TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) {
// new_backoff = 2.0 * old_backoff + 0
// and maximum backoff time is 150000 milliseconds.
// Maximum retries allowed is set to 11.
- net::URLRequestThrottlerManager* manager =
- net::URLRequestThrottlerManager::GetInstance();
scoped_refptr<net::URLRequestThrottlerEntry> entry(
new net::URLRequestThrottlerEntry(
- manager, "", 200, 3, 100, 2.0, 0.0, 150000));
+ throttler_manager(), "", 200, 3, 100, 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".
- manager->OverrideEntryForTests(url, entry);
+ throttler_manager()->OverrideEntryForTests(url, entry);
CreateFetcher(url);
MessageLoop::current()->Run();
-
- net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
}
#if defined(OS_MACOSX)
@@ -864,12 +900,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.
- net::URLRequestThrottlerManager* manager =
- net::URLRequestThrottlerManager::GetInstance();
scoped_refptr<net::URLRequestThrottlerEntry> entry(
new net::URLRequestThrottlerEntry(
- manager, "", 200, 3, 2000, 2.0, 0.0, 4000));
- manager->OverrideEntryForTests(url, entry);
+ throttler_manager(), "", 200, 3, 2000, 2.0, 0.0, 4000));
+ throttler_manager()->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
@@ -883,8 +917,6 @@ TEST_F(URLFetcherCancelTest, ReleasesContext) {
base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url));
MessageLoop::current()->Run();
-
- net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
}
TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) {
@@ -898,12 +930,10 @@ TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) {
// Register an entry for test url.
// 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.
- net::URLRequestThrottlerManager* manager =
- net::URLRequestThrottlerManager::GetInstance();
scoped_refptr<net::URLRequestThrottlerEntry> entry(
new net::URLRequestThrottlerEntry(
- manager, "", 4000, 1, 2000, 2.0, 0.0, 4000));
- manager->OverrideEntryForTests(url, entry);
+ throttler_manager(), "", 4000, 1, 2000, 2.0, 0.0, 4000));
+ throttler_manager()->OverrideEntryForTests(url, entry);
// Fake that a request has just started.
entry->ReserveSendingTimeForNextRequest(base::TimeTicks());
@@ -918,8 +948,6 @@ TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) {
base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url));
MessageLoop::current()->Run();
-
- net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url);
}
TEST_F(URLFetcherMultipleAttemptTest, SameData) {

Powered by Google App Engine
This is Rietveld 408576698