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

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

Issue 1167163002: chrome: Remove use of MessageLoopProxy and deprecated MessageLoop APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added some missing message_loop.h includes. Created 5 years, 6 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
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/location.h"
6 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
7 #include "base/message_loop/message_loop_proxy.h" 8 #include "base/single_thread_task_runner.h"
8 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/thread_task_runner_handle.h"
9 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
10 #include "base/values.h" 12 #include "base/values.h"
11 #include "chrome/service/cloud_print/cloud_print_url_fetcher.h" 13 #include "chrome/service/cloud_print/cloud_print_url_fetcher.h"
12 #include "chrome/service/service_process.h" 14 #include "chrome/service/service_process.h"
13 #include "net/test/spawned_test_server/spawned_test_server.h" 15 #include "net/test/spawned_test_server/spawned_test_server.h"
14 #include "net/url_request/url_request_context_getter.h" 16 #include "net/url_request/url_request_context_getter.h"
15 #include "net/url_request/url_request_status.h" 17 #include "net/url_request/url_request_status.h"
16 #include "net/url_request/url_request_test_util.h" 18 #include "net/url_request/url_request_test_util.h"
17 #include "net/url_request/url_request_throttler_manager.h" 19 #include "net/url_request/url_request_throttler_manager.h"
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 #include "url/gurl.h" 21 #include "url/gurl.h"
20 22
21 using base::Time; 23 using base::Time;
22 using base::TimeDelta; 24 using base::TimeDelta;
23 25
24 namespace cloud_print { 26 namespace cloud_print {
25 27
26 const base::FilePath::CharType kDocRoot[] = 28 const base::FilePath::CharType kDocRoot[] =
27 FILE_PATH_LITERAL("chrome/test/data"); 29 FILE_PATH_LITERAL("chrome/test/data");
28 30
29 int g_request_context_getter_instances = 0; 31 int g_request_context_getter_instances = 0;
30 class TrackingTestURLRequestContextGetter 32 class TrackingTestURLRequestContextGetter
31 : public net::TestURLRequestContextGetter { 33 : public net::TestURLRequestContextGetter {
32 public: 34 public:
33 explicit TrackingTestURLRequestContextGetter( 35 explicit TrackingTestURLRequestContextGetter(
34 base::MessageLoopProxy* io_message_loop_proxy, 36 base::SingleThreadTaskRunner* io_task_runner,
35 net::URLRequestThrottlerManager* throttler_manager) 37 net::URLRequestThrottlerManager* throttler_manager)
36 : TestURLRequestContextGetter(io_message_loop_proxy), 38 : TestURLRequestContextGetter(io_task_runner),
37 throttler_manager_(throttler_manager) { 39 throttler_manager_(throttler_manager) {
38 g_request_context_getter_instances++; 40 g_request_context_getter_instances++;
39 } 41 }
40 42
41 net::TestURLRequestContext* GetURLRequestContext() override { 43 net::TestURLRequestContext* GetURLRequestContext() override {
42 if (!context_.get()) { 44 if (!context_.get()) {
43 context_.reset(new net::TestURLRequestContext(true)); 45 context_.reset(new net::TestURLRequestContext(true));
44 context_->set_throttler_manager(throttler_manager_); 46 context_->set_throttler_manager(throttler_manager_);
45 context_->Init(); 47 context_->Init();
46 } 48 }
47 return context_.get(); 49 return context_.get();
48 } 50 }
49 51
50 protected: 52 protected:
51 ~TrackingTestURLRequestContextGetter() override { 53 ~TrackingTestURLRequestContextGetter() override {
52 g_request_context_getter_instances--; 54 g_request_context_getter_instances--;
53 } 55 }
54 56
55 private: 57 private:
56 // Not owned here. 58 // Not owned here.
57 net::URLRequestThrottlerManager* throttler_manager_; 59 net::URLRequestThrottlerManager* throttler_manager_;
58 scoped_ptr<net::TestURLRequestContext> context_; 60 scoped_ptr<net::TestURLRequestContext> context_;
59 }; 61 };
60 62
61 class TestCloudPrintURLFetcher : public CloudPrintURLFetcher { 63 class TestCloudPrintURLFetcher : public CloudPrintURLFetcher {
62 public: 64 public:
63 explicit TestCloudPrintURLFetcher( 65 explicit TestCloudPrintURLFetcher(
64 base::MessageLoopProxy* io_message_loop_proxy) 66 base::SingleThreadTaskRunner* io_task_runner)
65 : io_message_loop_proxy_(io_message_loop_proxy) { 67 : io_task_runner_(io_task_runner) {}
66 }
67 68
68 net::URLRequestContextGetter* GetRequestContextGetter() override { 69 net::URLRequestContextGetter* GetRequestContextGetter() override {
69 return new TrackingTestURLRequestContextGetter( 70 return new TrackingTestURLRequestContextGetter(io_task_runner_.get(),
70 io_message_loop_proxy_.get(), throttler_manager()); 71 throttler_manager());
71 } 72 }
72 73
73 net::URLRequestThrottlerManager* throttler_manager() { 74 net::URLRequestThrottlerManager* throttler_manager() {
74 return &throttler_manager_; 75 return &throttler_manager_;
75 } 76 }
76 77
77 private: 78 private:
78 ~TestCloudPrintURLFetcher() override {} 79 ~TestCloudPrintURLFetcher() override {}
79 80
80 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 81 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
81 82
82 // We set this as the throttler manager for the 83 // We set this as the throttler manager for the
83 // TestURLRequestContext we create. 84 // TestURLRequestContext we create.
84 net::URLRequestThrottlerManager throttler_manager_; 85 net::URLRequestThrottlerManager throttler_manager_;
85 }; 86 };
86 87
87 class CloudPrintURLFetcherTest : public testing::Test, 88 class CloudPrintURLFetcherTest : public testing::Test,
88 public CloudPrintURLFetcherDelegate { 89 public CloudPrintURLFetcherDelegate {
89 public: 90 public:
90 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { } 91 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { }
(...skipping 10 matching lines...) Expand all
101 const net::ResponseCookies& cookies, 102 const net::ResponseCookies& cookies,
102 const std::string& data) override; 103 const std::string& data) override;
103 104
104 CloudPrintURLFetcher::ResponseAction OnRequestAuthError() override { 105 CloudPrintURLFetcher::ResponseAction OnRequestAuthError() override {
105 ADD_FAILURE(); 106 ADD_FAILURE();
106 return CloudPrintURLFetcher::STOP_PROCESSING; 107 return CloudPrintURLFetcher::STOP_PROCESSING;
107 } 108 }
108 109
109 std::string GetAuthHeader() override { return std::string(); } 110 std::string GetAuthHeader() override { return std::string(); }
110 111
111 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { 112 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner() {
112 return io_message_loop_proxy_; 113 return io_task_runner_;
113 } 114 }
114 115
115 protected: 116 protected:
116 void SetUp() override { 117 void SetUp() override {
117 testing::Test::SetUp(); 118 testing::Test::SetUp();
118 119
119 io_message_loop_proxy_ = base::MessageLoopProxy::current(); 120 io_task_runner_ = base::ThreadTaskRunnerHandle::Get();
120 } 121 }
121 122
122 void TearDown() override { 123 void TearDown() override {
123 fetcher_ = NULL; 124 fetcher_ = NULL;
124 // Deleting the fetcher causes a task to be posted to the IO thread to 125 // Deleting the fetcher causes a task to be posted to the IO thread to
125 // release references to the URLRequestContextGetter. We need to run all 126 // release references to the URLRequestContextGetter. We need to run all
126 // pending tasks to execute that (this is the IO thread). 127 // pending tasks to execute that (this is the IO thread).
127 base::MessageLoop::current()->RunUntilIdle(); 128 base::MessageLoop::current()->RunUntilIdle();
128 EXPECT_EQ(0, g_request_context_getter_instances); 129 EXPECT_EQ(0, g_request_context_getter_instances);
129 } 130 }
130 131
131 // URLFetcher is designed to run on the main UI thread, but in our tests 132 // URLFetcher is designed to run on the main UI thread, but in our tests
132 // we assume that the current thread is the IO thread where the URLFetcher 133 // we assume that the current thread is the IO thread where the URLFetcher
133 // dispatches its requests to. When we wish to simulate being used from 134 // dispatches its requests to. When we wish to simulate being used from
134 // a UI thread, we dispatch a worker thread to do so. 135 // a UI thread, we dispatch a worker thread to do so.
135 base::MessageLoopForIO io_loop_; 136 base::MessageLoopForIO io_loop_;
136 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 137 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
137 int max_retries_; 138 int max_retries_;
138 Time start_time_; 139 Time start_time_;
139 scoped_refptr<TestCloudPrintURLFetcher> fetcher_; 140 scoped_refptr<TestCloudPrintURLFetcher> fetcher_;
140 }; 141 };
141 142
142 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest { 143 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest {
143 public: 144 public:
144 CloudPrintURLFetcherBasicTest() 145 CloudPrintURLFetcherBasicTest()
145 : handle_raw_response_(false), handle_raw_data_(false) { } 146 : handle_raw_response_(false), handle_raw_data_(false) { }
146 // CloudPrintURLFetcher::Delegate 147 // CloudPrintURLFetcher::Delegate
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 const std::string& data) override; 204 const std::string& data) override;
204 205
205 void OnRequestGiveUp() override; 206 void OnRequestGiveUp() override;
206 207
207 private: 208 private:
208 int response_count_; 209 int response_count_;
209 }; 210 };
210 211
211 212
212 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) { 213 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) {
213 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy().get()); 214 fetcher_ = new TestCloudPrintURLFetcher(io_task_runner().get());
214 215
215 // Registers an entry for test url. It only allows 3 requests to be sent 216 // Registers an entry for test url. It only allows 3 requests to be sent
216 // in 200 milliseconds. 217 // in 200 milliseconds.
217 scoped_refptr<net::URLRequestThrottlerEntry> 218 scoped_refptr<net::URLRequestThrottlerEntry>
218 entry(new net::URLRequestThrottlerEntry( 219 entry(new net::URLRequestThrottlerEntry(
219 fetcher_->throttler_manager(), std::string(), 200, 3, 1, 2.0, 0.0, 256)); 220 fetcher_->throttler_manager(), std::string(), 200, 3, 1, 2.0, 0.0, 256));
220 fetcher_->throttler_manager()->OverrideEntryForTests(url, entry.get()); 221 fetcher_->throttler_manager()->OverrideEntryForTests(url, entry.get());
221 222
222 max_retries_ = max_retries; 223 max_retries_ = max_retries;
223 start_time_ = Time::Now(); 224 start_time_ = Time::Now();
(...skipping 23 matching lines...) Expand all
247 int response_code, 248 int response_code,
248 const net::ResponseCookies& cookies, 249 const net::ResponseCookies& cookies,
249 const std::string& data) { 250 const std::string& data) {
250 EXPECT_TRUE(status.is_success()); 251 EXPECT_TRUE(status.is_success());
251 EXPECT_EQ(200, response_code); // HTTP OK 252 EXPECT_EQ(200, response_code); // HTTP OK
252 EXPECT_FALSE(data.empty()); 253 EXPECT_FALSE(data.empty());
253 254
254 if (handle_raw_response_) { 255 if (handle_raw_response_) {
255 // If the current message loop is not the IO loop, it will be shut down when 256 // If the current message loop is not the IO loop, it will be shut down when
256 // the main loop returns and this thread subsequently goes out of scope. 257 // the main loop returns and this thread subsequently goes out of scope.
257 io_message_loop_proxy()->PostTask(FROM_HERE, 258 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
258 base::MessageLoop::QuitClosure());
259 return CloudPrintURLFetcher::STOP_PROCESSING; 259 return CloudPrintURLFetcher::STOP_PROCESSING;
260 } 260 }
261 return CloudPrintURLFetcher::CONTINUE_PROCESSING; 261 return CloudPrintURLFetcher::CONTINUE_PROCESSING;
262 } 262 }
263 263
264 CloudPrintURLFetcher::ResponseAction 264 CloudPrintURLFetcher::ResponseAction
265 CloudPrintURLFetcherBasicTest::HandleRawData( 265 CloudPrintURLFetcherBasicTest::HandleRawData(
266 const net::URLFetcher* source, 266 const net::URLFetcher* source,
267 const GURL& url, 267 const GURL& url,
268 const std::string& data) { 268 const std::string& data) {
269 // We should never get here if we returned true in HandleRawResponse 269 // We should never get here if we returned true in HandleRawResponse
270 EXPECT_FALSE(handle_raw_response_); 270 EXPECT_FALSE(handle_raw_response_);
271 if (handle_raw_data_) { 271 if (handle_raw_data_) {
272 io_message_loop_proxy()->PostTask(FROM_HERE, 272 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
273 base::MessageLoop::QuitClosure());
274 return CloudPrintURLFetcher::STOP_PROCESSING; 273 return CloudPrintURLFetcher::STOP_PROCESSING;
275 } 274 }
276 return CloudPrintURLFetcher::CONTINUE_PROCESSING; 275 return CloudPrintURLFetcher::CONTINUE_PROCESSING;
277 } 276 }
278 277
279 CloudPrintURLFetcher::ResponseAction 278 CloudPrintURLFetcher::ResponseAction
280 CloudPrintURLFetcherBasicTest::HandleJSONData( 279 CloudPrintURLFetcherBasicTest::HandleJSONData(
281 const net::URLFetcher* source, 280 const net::URLFetcher* source,
282 const GURL& url, 281 const GURL& url,
283 base::DictionaryValue* json_data, 282 base::DictionaryValue* json_data,
284 bool succeeded) { 283 bool succeeded) {
285 // We should never get here if we returned true in one of the above methods. 284 // We should never get here if we returned true in one of the above methods.
286 EXPECT_FALSE(handle_raw_response_); 285 EXPECT_FALSE(handle_raw_response_);
287 EXPECT_FALSE(handle_raw_data_); 286 EXPECT_FALSE(handle_raw_data_);
288 io_message_loop_proxy()->PostTask(FROM_HERE, 287 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
289 base::MessageLoop::QuitClosure());
290 return CloudPrintURLFetcher::STOP_PROCESSING; 288 return CloudPrintURLFetcher::STOP_PROCESSING;
291 } 289 }
292 290
293 CloudPrintURLFetcher::ResponseAction 291 CloudPrintURLFetcher::ResponseAction
294 CloudPrintURLFetcherOverloadTest::HandleRawData( 292 CloudPrintURLFetcherOverloadTest::HandleRawData(
295 const net::URLFetcher* source, 293 const net::URLFetcher* source,
296 const GURL& url, 294 const GURL& url,
297 const std::string& data) { 295 const std::string& data) {
298 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); 296 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000);
299 response_count_++; 297 response_count_++;
300 if (response_count_ < 20) { 298 if (response_count_ < 20) {
301 fetcher_->StartGetRequest(CloudPrintURLFetcher::REQUEST_MAX, url, this, 299 fetcher_->StartGetRequest(CloudPrintURLFetcher::REQUEST_MAX, url, this,
302 max_retries_, std::string()); 300 max_retries_, std::string());
303 } else { 301 } else {
304 // We have already sent 20 requests continuously. And we expect that 302 // We have already sent 20 requests continuously. And we expect that
305 // it takes more than 1 second due to the overload protection settings. 303 // it takes more than 1 second due to the overload protection settings.
306 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); 304 EXPECT_TRUE(Time::Now() - start_time_ >= one_second);
307 io_message_loop_proxy()->PostTask(FROM_HERE, 305 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
308 base::MessageLoop::QuitClosure());
309 } 306 }
310 return CloudPrintURLFetcher::STOP_PROCESSING; 307 return CloudPrintURLFetcher::STOP_PROCESSING;
311 } 308 }
312 309
313 CloudPrintURLFetcher::ResponseAction 310 CloudPrintURLFetcher::ResponseAction
314 CloudPrintURLFetcherRetryBackoffTest::HandleRawData( 311 CloudPrintURLFetcherRetryBackoffTest::HandleRawData(
315 const net::URLFetcher* source, 312 const net::URLFetcher* source,
316 const GURL& url, 313 const GURL& url,
317 const std::string& data) { 314 const std::string& data) {
318 response_count_++; 315 response_count_++;
319 // First attempt + 11 retries = 12 total responses. 316 // First attempt + 11 retries = 12 total responses.
320 EXPECT_LE(response_count_, 12); 317 EXPECT_LE(response_count_, 12);
321 return CloudPrintURLFetcher::RETRY_REQUEST; 318 return CloudPrintURLFetcher::RETRY_REQUEST;
322 } 319 }
323 320
324 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() { 321 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() {
325 // It takes more than 200 ms to finish all 11 requests. 322 // It takes more than 200 ms to finish all 11 requests.
326 EXPECT_TRUE(Time::Now() - start_time_ >= TimeDelta::FromMilliseconds(200)); 323 EXPECT_TRUE(Time::Now() - start_time_ >= TimeDelta::FromMilliseconds(200));
327 io_message_loop_proxy()->PostTask(FROM_HERE, 324 io_task_runner()->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
328 base::MessageLoop::QuitClosure());
329 } 325 }
330 326
331 TEST_F(CloudPrintURLFetcherBasicTest, HandleRawResponse) { 327 TEST_F(CloudPrintURLFetcherBasicTest, HandleRawResponse) {
332 net::SpawnedTestServer test_server(net::SpawnedTestServer::TYPE_HTTP, 328 net::SpawnedTestServer test_server(net::SpawnedTestServer::TYPE_HTTP,
333 net::SpawnedTestServer::kLocalhost, 329 net::SpawnedTestServer::kLocalhost,
334 base::FilePath(kDocRoot)); 330 base::FilePath(kDocRoot));
335 ASSERT_TRUE(test_server.Start()); 331 ASSERT_TRUE(test_server.Start());
336 SetHandleRawResponse(true); 332 SetHandleRawResponse(true);
337 333
338 CreateFetcher(test_server.GetURL("echo"), 0); 334 CreateFetcher(test_server.GetURL("echo"), 0);
(...skipping 29 matching lines...) Expand all
368 base::FilePath(kDocRoot)); 364 base::FilePath(kDocRoot));
369 ASSERT_TRUE(test_server.Start()); 365 ASSERT_TRUE(test_server.Start());
370 366
371 GURL url(test_server.GetURL("defaultresponse")); 367 GURL url(test_server.GetURL("defaultresponse"));
372 CreateFetcher(url, 11); 368 CreateFetcher(url, 11);
373 369
374 base::MessageLoop::current()->Run(); 370 base::MessageLoop::current()->Run();
375 } 371 }
376 372
377 } // namespace cloud_print 373 } // namespace cloud_print
OLDNEW
« no previous file with comments | « chrome/service/cloud_print/cloud_print_proxy_backend.cc ('k') | chrome/service/cloud_print/connector_settings_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698