| 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/transport_client_socket_pool.h" | 5 #include "net/socket/transport_client_socket_pool.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.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/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 13 #include "net/base/ip_address.h" | 14 #include "net/base/ip_address.h" |
| 14 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/load_timing_info.h" | 16 #include "net/base/load_timing_info.h" |
| 16 #include "net/base/load_timing_info_test_util.h" | 17 #include "net/base/load_timing_info_test_util.h" |
| 17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 18 #include "net/base/test_completion_callback.h" | 19 #include "net/base/test_completion_callback.h" |
| 19 #include "net/dns/mock_host_resolver.h" | 20 #include "net/dns/mock_host_resolver.h" |
| 20 #include "net/log/test_net_log.h" | 21 #include "net/log/test_net_log.h" |
| 21 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 ASSERT_EQ(OK, result); | 493 ASSERT_EQ(OK, result); |
| 493 | 494 |
| 494 if (!within_callback_) { | 495 if (!within_callback_) { |
| 495 // Don't allow reuse of the socket. Disconnect it and then release it and | 496 // Don't allow reuse of the socket. Disconnect it and then release it and |
| 496 // run through the MessageLoop once to get it completely released. | 497 // run through the MessageLoop once to get it completely released. |
| 497 handle_->socket()->Disconnect(); | 498 handle_->socket()->Disconnect(); |
| 498 handle_->Reset(); | 499 handle_->Reset(); |
| 499 { | 500 { |
| 500 base::MessageLoop::ScopedNestableTaskAllower allow( | 501 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 501 base::MessageLoop::current()); | 502 base::MessageLoop::current()); |
| 502 base::MessageLoop::current()->RunUntilIdle(); | 503 base::RunLoop().RunUntilIdle(); |
| 503 } | 504 } |
| 504 within_callback_ = true; | 505 within_callback_ = true; |
| 505 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 506 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
| 506 HostPortPair("www.google.com", 80), false, OnHostResolutionCallback(), | 507 HostPortPair("www.google.com", 80), false, OnHostResolutionCallback(), |
| 507 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | 508 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 508 int rv = handle_->Init("a", dest, LOWEST, | 509 int rv = handle_->Init("a", dest, LOWEST, |
| 509 ClientSocketPool::RespectLimits::ENABLED, | 510 ClientSocketPool::RespectLimits::ENABLED, |
| 510 callback(), pool_, BoundNetLog()); | 511 callback(), pool_, BoundNetLog()); |
| 511 EXPECT_EQ(OK, rv); | 512 EXPECT_EQ(OK, rv); |
| 512 } | 513 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 EXPECT_FALSE(handle.is_initialized()); | 598 EXPECT_FALSE(handle.is_initialized()); |
| 598 EXPECT_FALSE(handle.socket()); | 599 EXPECT_FALSE(handle.socket()); |
| 599 | 600 |
| 600 EXPECT_EQ(OK, callback.WaitForResult()); | 601 EXPECT_EQ(OK, callback.WaitForResult()); |
| 601 EXPECT_TRUE(handle.is_initialized()); | 602 EXPECT_TRUE(handle.is_initialized()); |
| 602 EXPECT_TRUE(handle.socket()); | 603 EXPECT_TRUE(handle.socket()); |
| 603 TestLoadTimingInfoConnectedNotReused(handle); | 604 TestLoadTimingInfoConnectedNotReused(handle); |
| 604 | 605 |
| 605 handle.Reset(); | 606 handle.Reset(); |
| 606 // Need to run all pending to release the socket back to the pool. | 607 // Need to run all pending to release the socket back to the pool. |
| 607 base::MessageLoop::current()->RunUntilIdle(); | 608 base::RunLoop().RunUntilIdle(); |
| 608 | 609 |
| 609 // Now we should have 1 idle socket. | 610 // Now we should have 1 idle socket. |
| 610 EXPECT_EQ(1, pool_.IdleSocketCount()); | 611 EXPECT_EQ(1, pool_.IdleSocketCount()); |
| 611 | 612 |
| 612 rv = handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 613 rv = handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 613 callback.callback(), &pool_, BoundNetLog()); | 614 callback.callback(), &pool_, BoundNetLog()); |
| 614 EXPECT_EQ(OK, rv); | 615 EXPECT_EQ(OK, rv); |
| 615 EXPECT_EQ(0, pool_.IdleSocketCount()); | 616 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 616 TestLoadTimingInfoConnectedReused(handle); | 617 TestLoadTimingInfoConnectedReused(handle); |
| 617 } | 618 } |
| 618 | 619 |
| 619 TEST_F(TransportClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) { | 620 TEST_F(TransportClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) { |
| 620 TestCompletionCallback callback; | 621 TestCompletionCallback callback; |
| 621 ClientSocketHandle handle; | 622 ClientSocketHandle handle; |
| 622 int rv = | 623 int rv = |
| 623 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 624 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 624 callback.callback(), &pool_, BoundNetLog()); | 625 callback.callback(), &pool_, BoundNetLog()); |
| 625 EXPECT_EQ(ERR_IO_PENDING, rv); | 626 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 626 EXPECT_FALSE(handle.is_initialized()); | 627 EXPECT_FALSE(handle.is_initialized()); |
| 627 EXPECT_FALSE(handle.socket()); | 628 EXPECT_FALSE(handle.socket()); |
| 628 | 629 |
| 629 EXPECT_EQ(OK, callback.WaitForResult()); | 630 EXPECT_EQ(OK, callback.WaitForResult()); |
| 630 EXPECT_TRUE(handle.is_initialized()); | 631 EXPECT_TRUE(handle.is_initialized()); |
| 631 EXPECT_TRUE(handle.socket()); | 632 EXPECT_TRUE(handle.socket()); |
| 632 | 633 |
| 633 handle.Reset(); | 634 handle.Reset(); |
| 634 | 635 |
| 635 // Need to run all pending to release the socket back to the pool. | 636 // Need to run all pending to release the socket back to the pool. |
| 636 base::MessageLoop::current()->RunUntilIdle(); | 637 base::RunLoop().RunUntilIdle(); |
| 637 | 638 |
| 638 // Now we should have 1 idle socket. | 639 // Now we should have 1 idle socket. |
| 639 EXPECT_EQ(1, pool_.IdleSocketCount()); | 640 EXPECT_EQ(1, pool_.IdleSocketCount()); |
| 640 | 641 |
| 641 // After an IP address change, we should have 0 idle sockets. | 642 // After an IP address change, we should have 0 idle sockets. |
| 642 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | 643 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 643 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. | 644 base::RunLoop().RunUntilIdle(); // Notification happens async. |
| 644 | 645 |
| 645 EXPECT_EQ(0, pool_.IdleSocketCount()); | 646 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 646 } | 647 } |
| 647 | 648 |
| 648 TEST_F(TransportClientSocketPoolTest, BackupSocketConnect) { | 649 TEST_F(TransportClientSocketPoolTest, BackupSocketConnect) { |
| 649 // Case 1 tests the first socket stalling, and the backup connecting. | 650 // Case 1 tests the first socket stalling, and the backup connecting. |
| 650 MockTransportClientSocketFactory::ClientSocketType case1_types[] = { | 651 MockTransportClientSocketFactory::ClientSocketType case1_types[] = { |
| 651 // The first socket will not connect. | 652 // The first socket will not connect. |
| 652 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, | 653 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
| 653 // The second socket will connect more quickly. | 654 // The second socket will connect more quickly. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 677 TestCompletionCallback callback; | 678 TestCompletionCallback callback; |
| 678 ClientSocketHandle handle; | 679 ClientSocketHandle handle; |
| 679 int rv = | 680 int rv = |
| 680 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 681 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 681 callback.callback(), &pool_, BoundNetLog()); | 682 callback.callback(), &pool_, BoundNetLog()); |
| 682 EXPECT_EQ(ERR_IO_PENDING, rv); | 683 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 683 EXPECT_FALSE(handle.is_initialized()); | 684 EXPECT_FALSE(handle.is_initialized()); |
| 684 EXPECT_FALSE(handle.socket()); | 685 EXPECT_FALSE(handle.socket()); |
| 685 | 686 |
| 686 // Create the first socket, set the timer. | 687 // Create the first socket, set the timer. |
| 687 base::MessageLoop::current()->RunUntilIdle(); | 688 base::RunLoop().RunUntilIdle(); |
| 688 | 689 |
| 689 // Wait for the backup socket timer to fire. | 690 // Wait for the backup socket timer to fire. |
| 690 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | 691 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 691 ClientSocketPool::kMaxConnectRetryIntervalMs + 50)); | 692 ClientSocketPool::kMaxConnectRetryIntervalMs + 50)); |
| 692 | 693 |
| 693 // Let the appropriate socket connect. | 694 // Let the appropriate socket connect. |
| 694 base::MessageLoop::current()->RunUntilIdle(); | 695 base::RunLoop().RunUntilIdle(); |
| 695 | 696 |
| 696 EXPECT_EQ(OK, callback.WaitForResult()); | 697 EXPECT_EQ(OK, callback.WaitForResult()); |
| 697 EXPECT_TRUE(handle.is_initialized()); | 698 EXPECT_TRUE(handle.is_initialized()); |
| 698 EXPECT_TRUE(handle.socket()); | 699 EXPECT_TRUE(handle.socket()); |
| 699 | 700 |
| 700 // One socket is stalled, the other is active. | 701 // One socket is stalled, the other is active. |
| 701 EXPECT_EQ(0, pool_.IdleSocketCount()); | 702 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 702 handle.Reset(); | 703 handle.Reset(); |
| 703 | 704 |
| 704 // Close all pending connect jobs and existing sockets. | 705 // Close all pending connect jobs and existing sockets. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 720 TestCompletionCallback callback; | 721 TestCompletionCallback callback; |
| 721 ClientSocketHandle handle; | 722 ClientSocketHandle handle; |
| 722 int rv = | 723 int rv = |
| 723 handle.Init("c", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 724 handle.Init("c", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 724 callback.callback(), &pool_, BoundNetLog()); | 725 callback.callback(), &pool_, BoundNetLog()); |
| 725 EXPECT_EQ(ERR_IO_PENDING, rv); | 726 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 726 EXPECT_FALSE(handle.is_initialized()); | 727 EXPECT_FALSE(handle.is_initialized()); |
| 727 EXPECT_FALSE(handle.socket()); | 728 EXPECT_FALSE(handle.socket()); |
| 728 | 729 |
| 729 // Create the first socket, set the timer. | 730 // Create the first socket, set the timer. |
| 730 base::MessageLoop::current()->RunUntilIdle(); | 731 base::RunLoop().RunUntilIdle(); |
| 731 | 732 |
| 732 if (index == CANCEL_AFTER_WAIT) { | 733 if (index == CANCEL_AFTER_WAIT) { |
| 733 // Wait for the backup socket timer to fire. | 734 // Wait for the backup socket timer to fire. |
| 734 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | 735 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 735 ClientSocketPool::kMaxConnectRetryIntervalMs)); | 736 ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 736 } | 737 } |
| 737 | 738 |
| 738 // Let the appropriate socket connect. | 739 // Let the appropriate socket connect. |
| 739 base::MessageLoop::current()->RunUntilIdle(); | 740 base::RunLoop().RunUntilIdle(); |
| 740 | 741 |
| 741 handle.Reset(); | 742 handle.Reset(); |
| 742 | 743 |
| 743 EXPECT_FALSE(callback.have_result()); | 744 EXPECT_FALSE(callback.have_result()); |
| 744 EXPECT_FALSE(handle.is_initialized()); | 745 EXPECT_FALSE(handle.is_initialized()); |
| 745 EXPECT_FALSE(handle.socket()); | 746 EXPECT_FALSE(handle.socket()); |
| 746 | 747 |
| 747 // One socket is stalled, the other is active. | 748 // One socket is stalled, the other is active. |
| 748 EXPECT_EQ(0, pool_.IdleSocketCount()); | 749 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 749 } | 750 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 767 TestCompletionCallback callback; | 768 TestCompletionCallback callback; |
| 768 ClientSocketHandle handle; | 769 ClientSocketHandle handle; |
| 769 int rv = | 770 int rv = |
| 770 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 771 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 771 callback.callback(), &pool_, BoundNetLog()); | 772 callback.callback(), &pool_, BoundNetLog()); |
| 772 EXPECT_EQ(ERR_IO_PENDING, rv); | 773 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 773 EXPECT_FALSE(handle.is_initialized()); | 774 EXPECT_FALSE(handle.is_initialized()); |
| 774 EXPECT_FALSE(handle.socket()); | 775 EXPECT_FALSE(handle.socket()); |
| 775 | 776 |
| 776 // Create the first socket, set the timer. | 777 // Create the first socket, set the timer. |
| 777 base::MessageLoop::current()->RunUntilIdle(); | 778 base::RunLoop().RunUntilIdle(); |
| 778 | 779 |
| 779 // Wait for the backup socket timer to fire. | 780 // Wait for the backup socket timer to fire. |
| 780 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | 781 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 781 ClientSocketPool::kMaxConnectRetryIntervalMs)); | 782 ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 782 | 783 |
| 783 // Let the second connect be synchronous. Otherwise, the emulated | 784 // Let the second connect be synchronous. Otherwise, the emulated |
| 784 // host resolution takes an extra trip through the message loop. | 785 // host resolution takes an extra trip through the message loop. |
| 785 host_resolver_->set_synchronous_mode(true); | 786 host_resolver_->set_synchronous_mode(true); |
| 786 | 787 |
| 787 // Let the appropriate socket connect. | 788 // Let the appropriate socket connect. |
| 788 base::MessageLoop::current()->RunUntilIdle(); | 789 base::RunLoop().RunUntilIdle(); |
| 789 | 790 |
| 790 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 791 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 791 EXPECT_FALSE(handle.is_initialized()); | 792 EXPECT_FALSE(handle.is_initialized()); |
| 792 EXPECT_FALSE(handle.socket()); | 793 EXPECT_FALSE(handle.socket()); |
| 793 ASSERT_EQ(1u, handle.connection_attempts().size()); | 794 ASSERT_EQ(1u, handle.connection_attempts().size()); |
| 794 EXPECT_EQ(ERR_CONNECTION_FAILED, handle.connection_attempts()[0].result); | 795 EXPECT_EQ(ERR_CONNECTION_FAILED, handle.connection_attempts()[0].result); |
| 795 EXPECT_EQ(0, pool_.IdleSocketCount()); | 796 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 796 handle.Reset(); | 797 handle.Reset(); |
| 797 | 798 |
| 798 // Reset for the next case. | 799 // Reset for the next case. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 818 TestCompletionCallback callback; | 819 TestCompletionCallback callback; |
| 819 ClientSocketHandle handle; | 820 ClientSocketHandle handle; |
| 820 int rv = | 821 int rv = |
| 821 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 822 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 822 callback.callback(), &pool_, BoundNetLog()); | 823 callback.callback(), &pool_, BoundNetLog()); |
| 823 EXPECT_EQ(ERR_IO_PENDING, rv); | 824 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 824 EXPECT_FALSE(handle.is_initialized()); | 825 EXPECT_FALSE(handle.is_initialized()); |
| 825 EXPECT_FALSE(handle.socket()); | 826 EXPECT_FALSE(handle.socket()); |
| 826 | 827 |
| 827 // Create the first socket, set the timer. | 828 // Create the first socket, set the timer. |
| 828 base::MessageLoop::current()->RunUntilIdle(); | 829 base::RunLoop().RunUntilIdle(); |
| 829 | 830 |
| 830 // Wait for the backup socket timer to fire. | 831 // Wait for the backup socket timer to fire. |
| 831 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | 832 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 832 ClientSocketPool::kMaxConnectRetryIntervalMs)); | 833 ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 833 | 834 |
| 834 // Let the second connect be synchronous. Otherwise, the emulated | 835 // Let the second connect be synchronous. Otherwise, the emulated |
| 835 // host resolution takes an extra trip through the message loop. | 836 // host resolution takes an extra trip through the message loop. |
| 836 host_resolver_->set_synchronous_mode(true); | 837 host_resolver_->set_synchronous_mode(true); |
| 837 | 838 |
| 838 // Let the appropriate socket connect. | 839 // Let the appropriate socket connect. |
| 839 base::MessageLoop::current()->RunUntilIdle(); | 840 base::RunLoop().RunUntilIdle(); |
| 840 | 841 |
| 841 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 842 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 842 EXPECT_FALSE(handle.is_initialized()); | 843 EXPECT_FALSE(handle.is_initialized()); |
| 843 EXPECT_FALSE(handle.socket()); | 844 EXPECT_FALSE(handle.socket()); |
| 844 ASSERT_EQ(1u, handle.connection_attempts().size()); | 845 ASSERT_EQ(1u, handle.connection_attempts().size()); |
| 845 EXPECT_EQ(ERR_CONNECTION_FAILED, handle.connection_attempts()[0].result); | 846 EXPECT_EQ(ERR_CONNECTION_FAILED, handle.connection_attempts()[0].result); |
| 846 handle.Reset(); | 847 handle.Reset(); |
| 847 | 848 |
| 848 // Reset for the next case. | 849 // Reset for the next case. |
| 849 host_resolver_->set_synchronous_mode(false); | 850 host_resolver_->set_synchronous_mode(false); |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1126 handle.socket()->GetPeerAddress(&endpoint); | 1127 handle.socket()->GetPeerAddress(&endpoint); |
| 1127 // Verify that the socket used is connected to the IPv6 address. | 1128 // Verify that the socket used is connected to the IPv6 address. |
| 1128 EXPECT_TRUE(endpoint.address().IsIPv6()); | 1129 EXPECT_TRUE(endpoint.address().IsIPv6()); |
| 1129 // Verify that TCP FastOpen was not turned on for the socket. | 1130 // Verify that TCP FastOpen was not turned on for the socket. |
| 1130 EXPECT_FALSE(socket_data.IsUsingTCPFastOpen()); | 1131 EXPECT_FALSE(socket_data.IsUsingTCPFastOpen()); |
| 1131 } | 1132 } |
| 1132 | 1133 |
| 1133 } // namespace | 1134 } // namespace |
| 1134 | 1135 |
| 1135 } // namespace net | 1136 } // namespace net |
| OLD | NEW |