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/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "net/base/load_timing_info.h" | 21 #include "net/base/load_timing_info.h" |
22 #include "net/base/load_timing_info_test_util.h" | 22 #include "net/base/load_timing_info_test_util.h" |
23 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
24 #include "net/base/request_priority.h" | 24 #include "net/base/request_priority.h" |
25 #include "net/base/test_completion_callback.h" | 25 #include "net/base/test_completion_callback.h" |
26 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
| 27 #include "net/log/captured_net_log_entry.h" |
27 #include "net/log/net_log.h" | 28 #include "net/log/net_log.h" |
28 #include "net/log/net_log_unittest.h" | 29 #include "net/log/net_log_unittest.h" |
| 30 #include "net/log/test_net_log.h" |
29 #include "net/socket/client_socket_factory.h" | 31 #include "net/socket/client_socket_factory.h" |
30 #include "net/socket/client_socket_handle.h" | 32 #include "net/socket/client_socket_handle.h" |
31 #include "net/socket/socket_test_util.h" | 33 #include "net/socket/socket_test_util.h" |
32 #include "net/socket/ssl_client_socket.h" | 34 #include "net/socket/ssl_client_socket.h" |
33 #include "net/socket/stream_socket.h" | 35 #include "net/socket/stream_socket.h" |
34 #include "net/udp/datagram_client_socket.h" | 36 #include "net/udp/datagram_client_socket.h" |
35 #include "testing/gmock/include/gmock/gmock.h" | 37 #include "testing/gmock/include/gmock/gmock.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
37 | 39 |
38 using ::testing::Invoke; | 40 using ::testing::Invoke; |
(...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 "a", | 767 "a", |
766 request, | 768 request, |
767 base::TimeDelta::FromMicroseconds(1), | 769 base::TimeDelta::FromMicroseconds(1), |
768 &delegate, | 770 &delegate, |
769 &client_socket_factory_, | 771 &client_socket_factory_, |
770 &log); | 772 &log); |
771 ASSERT_EQ(ERR_IO_PENDING, job->Connect()); | 773 ASSERT_EQ(ERR_IO_PENDING, job->Connect()); |
772 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 774 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
773 EXPECT_EQ(ERR_TIMED_OUT, delegate.WaitForResult()); | 775 EXPECT_EQ(ERR_TIMED_OUT, delegate.WaitForResult()); |
774 | 776 |
775 TestNetLog::CapturedEntryList entries; | 777 CapturedNetLogEntry::List entries; |
776 log.GetEntries(&entries); | 778 log.GetEntries(&entries); |
777 | 779 |
778 EXPECT_EQ(6u, entries.size()); | 780 EXPECT_EQ(6u, entries.size()); |
779 EXPECT_TRUE(LogContainsBeginEvent( | 781 EXPECT_TRUE(LogContainsBeginEvent( |
780 entries, 0, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); | 782 entries, 0, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); |
781 EXPECT_TRUE(LogContainsBeginEvent( | 783 EXPECT_TRUE(LogContainsBeginEvent( |
782 entries, 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT)); | 784 entries, 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT)); |
783 EXPECT_TRUE(LogContainsEvent( | 785 EXPECT_TRUE(LogContainsEvent( |
784 entries, 2, NetLog::TYPE_CONNECT_JOB_SET_SOCKET, | 786 entries, 2, NetLog::TYPE_CONNECT_JOB_SET_SOCKET, |
785 NetLog::PHASE_NONE)); | 787 NetLog::PHASE_NONE)); |
(...skipping 21 matching lines...) Expand all Loading... |
807 callback.callback(), | 809 callback.callback(), |
808 pool_.get(), | 810 pool_.get(), |
809 log.bound())); | 811 log.bound())); |
810 EXPECT_TRUE(handle.is_initialized()); | 812 EXPECT_TRUE(handle.is_initialized()); |
811 EXPECT_TRUE(handle.socket()); | 813 EXPECT_TRUE(handle.socket()); |
812 TestLoadTimingInfoConnectedNotReused(handle); | 814 TestLoadTimingInfoConnectedNotReused(handle); |
813 | 815 |
814 handle.Reset(); | 816 handle.Reset(); |
815 TestLoadTimingInfoNotConnected(handle); | 817 TestLoadTimingInfoNotConnected(handle); |
816 | 818 |
817 TestNetLog::CapturedEntryList entries; | 819 CapturedNetLogEntry::List entries; |
818 log.GetEntries(&entries); | 820 log.GetEntries(&entries); |
819 | 821 |
820 EXPECT_EQ(4u, entries.size()); | 822 EXPECT_EQ(4u, entries.size()); |
821 EXPECT_TRUE(LogContainsBeginEvent( | 823 EXPECT_TRUE(LogContainsBeginEvent( |
822 entries, 0, NetLog::TYPE_SOCKET_POOL)); | 824 entries, 0, NetLog::TYPE_SOCKET_POOL)); |
823 EXPECT_TRUE(LogContainsEvent( | 825 EXPECT_TRUE(LogContainsEvent( |
824 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, | 826 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
825 NetLog::PHASE_NONE)); | 827 NetLog::PHASE_NONE)); |
826 EXPECT_TRUE(LogContainsEvent( | 828 EXPECT_TRUE(LogContainsEvent( |
827 entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, | 829 entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, |
(...skipping 20 matching lines...) Expand all Loading... |
848 params_, | 850 params_, |
849 DEFAULT_PRIORITY, | 851 DEFAULT_PRIORITY, |
850 callback.callback(), | 852 callback.callback(), |
851 pool_.get(), | 853 pool_.get(), |
852 log.bound())); | 854 log.bound())); |
853 EXPECT_FALSE(handle.socket()); | 855 EXPECT_FALSE(handle.socket()); |
854 EXPECT_FALSE(handle.is_ssl_error()); | 856 EXPECT_FALSE(handle.is_ssl_error()); |
855 EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL); | 857 EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL); |
856 TestLoadTimingInfoNotConnected(handle); | 858 TestLoadTimingInfoNotConnected(handle); |
857 | 859 |
858 TestNetLog::CapturedEntryList entries; | 860 CapturedNetLogEntry::List entries; |
859 log.GetEntries(&entries); | 861 log.GetEntries(&entries); |
860 | 862 |
861 EXPECT_EQ(3u, entries.size()); | 863 EXPECT_EQ(3u, entries.size()); |
862 EXPECT_TRUE(LogContainsBeginEvent( | 864 EXPECT_TRUE(LogContainsBeginEvent( |
863 entries, 0, NetLog::TYPE_SOCKET_POOL)); | 865 entries, 0, NetLog::TYPE_SOCKET_POOL)); |
864 EXPECT_TRUE(LogContainsEvent( | 866 EXPECT_TRUE(LogContainsEvent( |
865 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, | 867 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
866 NetLog::PHASE_NONE)); | 868 NetLog::PHASE_NONE)); |
867 EXPECT_TRUE(LogContainsEndEvent( | 869 EXPECT_TRUE(LogContainsEndEvent( |
868 entries, 2, NetLog::TYPE_SOCKET_POOL)); | 870 entries, 2, NetLog::TYPE_SOCKET_POOL)); |
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1683 TestLoadTimingInfoNotConnected(handle); | 1685 TestLoadTimingInfoNotConnected(handle); |
1684 | 1686 |
1685 EXPECT_EQ(OK, callback.WaitForResult()); | 1687 EXPECT_EQ(OK, callback.WaitForResult()); |
1686 EXPECT_TRUE(handle.is_initialized()); | 1688 EXPECT_TRUE(handle.is_initialized()); |
1687 EXPECT_TRUE(handle.socket()); | 1689 EXPECT_TRUE(handle.socket()); |
1688 TestLoadTimingInfoConnectedNotReused(handle); | 1690 TestLoadTimingInfoConnectedNotReused(handle); |
1689 | 1691 |
1690 handle.Reset(); | 1692 handle.Reset(); |
1691 TestLoadTimingInfoNotConnected(handle); | 1693 TestLoadTimingInfoNotConnected(handle); |
1692 | 1694 |
1693 TestNetLog::CapturedEntryList entries; | 1695 CapturedNetLogEntry::List entries; |
1694 log.GetEntries(&entries); | 1696 log.GetEntries(&entries); |
1695 | 1697 |
1696 EXPECT_EQ(4u, entries.size()); | 1698 EXPECT_EQ(4u, entries.size()); |
1697 EXPECT_TRUE(LogContainsBeginEvent( | 1699 EXPECT_TRUE(LogContainsBeginEvent( |
1698 entries, 0, NetLog::TYPE_SOCKET_POOL)); | 1700 entries, 0, NetLog::TYPE_SOCKET_POOL)); |
1699 EXPECT_TRUE(LogContainsEvent( | 1701 EXPECT_TRUE(LogContainsEvent( |
1700 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, | 1702 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
1701 NetLog::PHASE_NONE)); | 1703 NetLog::PHASE_NONE)); |
1702 EXPECT_TRUE(LogContainsEvent( | 1704 EXPECT_TRUE(LogContainsEvent( |
1703 entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, | 1705 entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, |
(...skipping 19 matching lines...) Expand all Loading... |
1723 params_, | 1725 params_, |
1724 DEFAULT_PRIORITY, | 1726 DEFAULT_PRIORITY, |
1725 callback.callback(), | 1727 callback.callback(), |
1726 pool_.get(), | 1728 pool_.get(), |
1727 log.bound())); | 1729 log.bound())); |
1728 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); | 1730 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
1729 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 1731 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
1730 EXPECT_FALSE(handle.is_ssl_error()); | 1732 EXPECT_FALSE(handle.is_ssl_error()); |
1731 EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL); | 1733 EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL); |
1732 | 1734 |
1733 TestNetLog::CapturedEntryList entries; | 1735 CapturedNetLogEntry::List entries; |
1734 log.GetEntries(&entries); | 1736 log.GetEntries(&entries); |
1735 | 1737 |
1736 EXPECT_EQ(3u, entries.size()); | 1738 EXPECT_EQ(3u, entries.size()); |
1737 EXPECT_TRUE(LogContainsBeginEvent( | 1739 EXPECT_TRUE(LogContainsBeginEvent( |
1738 entries, 0, NetLog::TYPE_SOCKET_POOL)); | 1740 entries, 0, NetLog::TYPE_SOCKET_POOL)); |
1739 EXPECT_TRUE(LogContainsEvent( | 1741 EXPECT_TRUE(LogContainsEvent( |
1740 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, | 1742 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
1741 NetLog::PHASE_NONE)); | 1743 NetLog::PHASE_NONE)); |
1742 EXPECT_TRUE(LogContainsEndEvent( | 1744 EXPECT_TRUE(LogContainsEndEvent( |
1743 entries, 2, NetLog::TYPE_SOCKET_POOL)); | 1745 entries, 2, NetLog::TYPE_SOCKET_POOL)); |
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2237 pool_.get(), | 2239 pool_.get(), |
2238 log.bound()); | 2240 log.bound()); |
2239 ASSERT_EQ(OK, rv); | 2241 ASSERT_EQ(OK, rv); |
2240 EXPECT_TRUE(handle.is_reused()); | 2242 EXPECT_TRUE(handle.is_reused()); |
2241 TestLoadTimingInfoConnectedReused(handle); | 2243 TestLoadTimingInfoConnectedReused(handle); |
2242 | 2244 |
2243 ASSERT_TRUE(pool_->HasGroup("a")); | 2245 ASSERT_TRUE(pool_->HasGroup("a")); |
2244 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 2246 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
2245 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 2247 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
2246 | 2248 |
2247 TestNetLog::CapturedEntryList entries; | 2249 CapturedNetLogEntry::List entries; |
2248 log.GetEntries(&entries); | 2250 log.GetEntries(&entries); |
2249 EXPECT_TRUE(LogContainsEntryWithType( | 2251 EXPECT_TRUE(LogContainsEntryWithType( |
2250 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); | 2252 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
2251 } | 2253 } |
2252 | 2254 |
2253 // Make sure we cleanup old unused sockets when the cleanup timer is disabled. | 2255 // Make sure we cleanup old unused sockets when the cleanup timer is disabled. |
2254 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerNoReuse) { | 2256 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerNoReuse) { |
2255 // Disable cleanup timer. | 2257 // Disable cleanup timer. |
2256 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false); | 2258 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false); |
2257 | 2259 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2319 log.bound()); | 2321 log.bound()); |
2320 ASSERT_EQ(ERR_IO_PENDING, rv); | 2322 ASSERT_EQ(ERR_IO_PENDING, rv); |
2321 ASSERT_EQ(OK, callback3.WaitForResult()); | 2323 ASSERT_EQ(OK, callback3.WaitForResult()); |
2322 EXPECT_FALSE(handle.is_reused()); | 2324 EXPECT_FALSE(handle.is_reused()); |
2323 | 2325 |
2324 // Make sure the idle socket is closed. | 2326 // Make sure the idle socket is closed. |
2325 ASSERT_TRUE(pool_->HasGroup("a")); | 2327 ASSERT_TRUE(pool_->HasGroup("a")); |
2326 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 2328 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
2327 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 2329 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
2328 | 2330 |
2329 TestNetLog::CapturedEntryList entries; | 2331 CapturedNetLogEntry::List entries; |
2330 log.GetEntries(&entries); | 2332 log.GetEntries(&entries); |
2331 EXPECT_FALSE(LogContainsEntryWithType( | 2333 EXPECT_FALSE(LogContainsEntryWithType( |
2332 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); | 2334 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
2333 } | 2335 } |
2334 | 2336 |
2335 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { | 2337 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { |
2336 CreatePoolWithIdleTimeouts( | 2338 CreatePoolWithIdleTimeouts( |
2337 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | 2339 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
2338 base::TimeDelta(), // Time out unused sockets immediately. | 2340 base::TimeDelta(), // Time out unused sockets immediately. |
2339 base::TimeDelta::FromDays(1)); // Don't time out used sockets. | 2341 base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2391 BoundTestNetLog log; | 2393 BoundTestNetLog log; |
2392 rv = handle.Init("a", | 2394 rv = handle.Init("a", |
2393 params_, | 2395 params_, |
2394 LOWEST, | 2396 LOWEST, |
2395 callback.callback(), | 2397 callback.callback(), |
2396 pool_.get(), | 2398 pool_.get(), |
2397 log.bound()); | 2399 log.bound()); |
2398 EXPECT_EQ(OK, rv); | 2400 EXPECT_EQ(OK, rv); |
2399 EXPECT_TRUE(handle.is_reused()); | 2401 EXPECT_TRUE(handle.is_reused()); |
2400 | 2402 |
2401 TestNetLog::CapturedEntryList entries; | 2403 CapturedNetLogEntry::List entries; |
2402 log.GetEntries(&entries); | 2404 log.GetEntries(&entries); |
2403 EXPECT_TRUE(LogContainsEntryWithType( | 2405 EXPECT_TRUE(LogContainsEntryWithType( |
2404 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); | 2406 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
2405 } | 2407 } |
2406 | 2408 |
2407 // Make sure that we process all pending requests even when we're stalling | 2409 // Make sure that we process all pending requests even when we're stalling |
2408 // because of multiple releasing disconnected sockets. | 2410 // because of multiple releasing disconnected sockets. |
2409 TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { | 2411 TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
2410 CreatePoolWithIdleTimeouts( | 2412 CreatePoolWithIdleTimeouts( |
2411 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | 2413 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
(...skipping 1706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4118 request(1)->handle()->Reset(); | 4120 request(1)->handle()->Reset(); |
4119 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 4121 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
4120 | 4122 |
4121 EXPECT_EQ(OK, request(2)->WaitForResult()); | 4123 EXPECT_EQ(OK, request(2)->WaitForResult()); |
4122 EXPECT_FALSE(request(1)->have_result()); | 4124 EXPECT_FALSE(request(1)->have_result()); |
4123 } | 4125 } |
4124 | 4126 |
4125 } // namespace | 4127 } // namespace |
4126 | 4128 |
4127 } // namespace net | 4129 } // namespace net |
OLD | NEW |