| 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 "net/proxy/multi_threaded_proxy_resolver.h" | 5 #include "net/proxy/multi_threaded_proxy_resolver.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
| 19 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/base/test_completion_callback.h" | 21 #include "net/base/test_completion_callback.h" |
| 22 #include "net/log/net_log.h" | 22 #include "net/log/net_log.h" |
| 23 #include "net/log/net_log_event_type.h" |
| 23 #include "net/log/test_net_log.h" | 24 #include "net/log/test_net_log.h" |
| 24 #include "net/log/test_net_log_entry.h" | 25 #include "net/log/test_net_log_entry.h" |
| 25 #include "net/log/test_net_log_util.h" | 26 #include "net/log/test_net_log_util.h" |
| 26 #include "net/proxy/mock_proxy_resolver.h" | 27 #include "net/proxy/mock_proxy_resolver.h" |
| 27 #include "net/proxy/proxy_info.h" | 28 #include "net/proxy/proxy_info.h" |
| 28 #include "net/proxy/proxy_resolver_factory.h" | 29 #include "net/proxy/proxy_resolver_factory.h" |
| 29 #include "net/test/gtest_util.h" | 30 #include "net/test/gtest_util.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 56 const BoundNetLog& net_log) override { | 57 const BoundNetLog& net_log) override { |
| 57 if (!resolve_latency_.is_zero()) | 58 if (!resolve_latency_.is_zero()) |
| 58 base::PlatformThread::Sleep(resolve_latency_); | 59 base::PlatformThread::Sleep(resolve_latency_); |
| 59 | 60 |
| 60 CheckIsOnWorkerThread(); | 61 CheckIsOnWorkerThread(); |
| 61 | 62 |
| 62 EXPECT_TRUE(callback.is_null()); | 63 EXPECT_TRUE(callback.is_null()); |
| 63 EXPECT_TRUE(request == NULL); | 64 EXPECT_TRUE(request == NULL); |
| 64 | 65 |
| 65 // Write something into |net_log| (doesn't really have any meaning.) | 66 // Write something into |net_log| (doesn't really have any meaning.) |
| 66 net_log.BeginEvent(NetLog::TYPE_PAC_JAVASCRIPT_ALERT); | 67 net_log.BeginEvent(NetLogEventType::PAC_JAVASCRIPT_ALERT); |
| 67 | 68 |
| 68 results->UseNamedProxy(query_url.host()); | 69 results->UseNamedProxy(query_url.host()); |
| 69 | 70 |
| 70 // Return a success code which represents the request's order. | 71 // Return a success code which represents the request's order. |
| 71 return request_count_++; | 72 return request_count_++; |
| 72 } | 73 } |
| 73 | 74 |
| 74 void CancelRequest(RequestHandle request) override { NOTREACHED(); } | 75 void CancelRequest(RequestHandle request) override { NOTREACHED(); } |
| 75 | 76 |
| 76 LoadState GetLoadState(RequestHandle request) const override { | 77 LoadState GetLoadState(RequestHandle request) const override { |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 EXPECT_EQ("PROXY request0:80", results0.ToPacString()); | 258 EXPECT_EQ("PROXY request0:80", results0.ToPacString()); |
| 258 | 259 |
| 259 // The mock proxy resolver should have written 1 log entry. And | 260 // The mock proxy resolver should have written 1 log entry. And |
| 260 // on completion, this should have been copied into |log0|. | 261 // on completion, this should have been copied into |log0|. |
| 261 // We also have 1 log entry that was emitted by the | 262 // We also have 1 log entry that was emitted by the |
| 262 // MultiThreadedProxyResolver. | 263 // MultiThreadedProxyResolver. |
| 263 TestNetLogEntry::List entries0; | 264 TestNetLogEntry::List entries0; |
| 264 log0.GetEntries(&entries0); | 265 log0.GetEntries(&entries0); |
| 265 | 266 |
| 266 ASSERT_EQ(2u, entries0.size()); | 267 ASSERT_EQ(2u, entries0.size()); |
| 267 EXPECT_EQ(NetLog::TYPE_SUBMITTED_TO_RESOLVER_THREAD, entries0[0].type); | 268 EXPECT_EQ(NetLogEventType::SUBMITTED_TO_RESOLVER_THREAD, entries0[0].type); |
| 268 | 269 |
| 269 // Start 3 more requests (request1 to request3). | 270 // Start 3 more requests (request1 to request3). |
| 270 | 271 |
| 271 TestCompletionCallback callback1; | 272 TestCompletionCallback callback1; |
| 272 ProxyInfo results1; | 273 ProxyInfo results1; |
| 273 rv = resolver().GetProxyForURL(GURL("http://request1"), &results1, | 274 rv = resolver().GetProxyForURL(GURL("http://request1"), &results1, |
| 274 callback1.callback(), NULL, BoundNetLog()); | 275 callback1.callback(), NULL, BoundNetLog()); |
| 275 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 276 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 276 | 277 |
| 277 TestCompletionCallback callback2; | 278 TestCompletionCallback callback2; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 // Check that request 0 completed as expected. | 348 // Check that request 0 completed as expected. |
| 348 // The NetLog has 1 entry that came from the MultiThreadedProxyResolver, and | 349 // The NetLog has 1 entry that came from the MultiThreadedProxyResolver, and |
| 349 // 1 entry from the mock proxy resolver. | 350 // 1 entry from the mock proxy resolver. |
| 350 EXPECT_EQ(0, callback0.WaitForResult()); | 351 EXPECT_EQ(0, callback0.WaitForResult()); |
| 351 EXPECT_EQ("PROXY request0:80", results0.ToPacString()); | 352 EXPECT_EQ("PROXY request0:80", results0.ToPacString()); |
| 352 | 353 |
| 353 TestNetLogEntry::List entries0; | 354 TestNetLogEntry::List entries0; |
| 354 log0.GetEntries(&entries0); | 355 log0.GetEntries(&entries0); |
| 355 | 356 |
| 356 ASSERT_EQ(2u, entries0.size()); | 357 ASSERT_EQ(2u, entries0.size()); |
| 357 EXPECT_EQ(NetLog::TYPE_SUBMITTED_TO_RESOLVER_THREAD, | 358 EXPECT_EQ(NetLogEventType::SUBMITTED_TO_RESOLVER_THREAD, |
| 358 entries0[0].type); | 359 entries0[0].type); |
| 359 | 360 |
| 360 // Check that request 1 completed as expected. | 361 // Check that request 1 completed as expected. |
| 361 EXPECT_EQ(1, callback1.WaitForResult()); | 362 EXPECT_EQ(1, callback1.WaitForResult()); |
| 362 EXPECT_EQ("PROXY request1:80", results1.ToPacString()); | 363 EXPECT_EQ("PROXY request1:80", results1.ToPacString()); |
| 363 | 364 |
| 364 TestNetLogEntry::List entries1; | 365 TestNetLogEntry::List entries1; |
| 365 log1.GetEntries(&entries1); | 366 log1.GetEntries(&entries1); |
| 366 | 367 |
| 367 ASSERT_EQ(4u, entries1.size()); | 368 ASSERT_EQ(4u, entries1.size()); |
| 368 EXPECT_TRUE(LogContainsBeginEvent( | 369 EXPECT_TRUE(LogContainsBeginEvent( |
| 369 entries1, 0, | 370 entries1, 0, |
| 370 NetLog::TYPE_WAITING_FOR_PROXY_RESOLVER_THREAD)); | 371 NetLogEventType::WAITING_FOR_PROXY_RESOLVER_THREAD)); |
| 371 EXPECT_TRUE(LogContainsEndEvent( | 372 EXPECT_TRUE(LogContainsEndEvent( |
| 372 entries1, 1, | 373 entries1, 1, |
| 373 NetLog::TYPE_WAITING_FOR_PROXY_RESOLVER_THREAD)); | 374 NetLogEventType::WAITING_FOR_PROXY_RESOLVER_THREAD)); |
| 374 | 375 |
| 375 // Check that request 2 completed as expected. | 376 // Check that request 2 completed as expected. |
| 376 EXPECT_EQ(2, callback2.WaitForResult()); | 377 EXPECT_EQ(2, callback2.WaitForResult()); |
| 377 EXPECT_EQ("PROXY request2:80", results2.ToPacString()); | 378 EXPECT_EQ("PROXY request2:80", results2.ToPacString()); |
| 378 | 379 |
| 379 TestNetLogEntry::List entries2; | 380 TestNetLogEntry::List entries2; |
| 380 log2.GetEntries(&entries2); | 381 log2.GetEntries(&entries2); |
| 381 | 382 |
| 382 ASSERT_EQ(4u, entries2.size()); | 383 ASSERT_EQ(4u, entries2.size()); |
| 383 EXPECT_TRUE(LogContainsBeginEvent( | 384 EXPECT_TRUE(LogContainsBeginEvent( |
| 384 entries2, 0, | 385 entries2, 0, |
| 385 NetLog::TYPE_WAITING_FOR_PROXY_RESOLVER_THREAD)); | 386 NetLogEventType::WAITING_FOR_PROXY_RESOLVER_THREAD)); |
| 386 EXPECT_TRUE(LogContainsEndEvent( | 387 EXPECT_TRUE(LogContainsEndEvent( |
| 387 entries2, 1, | 388 entries2, 1, |
| 388 NetLog::TYPE_WAITING_FOR_PROXY_RESOLVER_THREAD)); | 389 NetLogEventType::WAITING_FOR_PROXY_RESOLVER_THREAD)); |
| 389 } | 390 } |
| 390 | 391 |
| 391 // Cancel a request which is in progress, and then cancel a request which | 392 // Cancel a request which is in progress, and then cancel a request which |
| 392 // is pending. | 393 // is pending. |
| 393 TEST_F(MultiThreadedProxyResolverTest, SingleThread_CancelRequest) { | 394 TEST_F(MultiThreadedProxyResolverTest, SingleThread_CancelRequest) { |
| 394 const size_t kNumThreads = 1u; | 395 const size_t kNumThreads = 1u; |
| 395 ASSERT_NO_FATAL_FAILURE(Init(kNumThreads)); | 396 ASSERT_NO_FATAL_FAILURE(Init(kNumThreads)); |
| 396 | 397 |
| 397 int rv; | 398 int rv; |
| 398 | 399 |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 779 } | 780 } |
| 780 // The factory destructor will block until the worker thread stops, but it may | 781 // The factory destructor will block until the worker thread stops, but it may |
| 781 // post tasks to the origin message loop which are still pending. Run them | 782 // post tasks to the origin message loop which are still pending. Run them |
| 782 // now to ensure it works as expected. | 783 // now to ensure it works as expected. |
| 783 base::RunLoop().RunUntilIdle(); | 784 base::RunLoop().RunUntilIdle(); |
| 784 } | 785 } |
| 785 | 786 |
| 786 } // namespace | 787 } // namespace |
| 787 | 788 |
| 788 } // namespace net | 789 } // namespace net |
| OLD | NEW |