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" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); | 54 EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
55 | 55 |
56 EXPECT_EQ(true, load_timing_info.socket_reused); | 56 EXPECT_EQ(true, load_timing_info.socket_reused); |
57 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | 57 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
58 | 58 |
59 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); | 59 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
60 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); | 60 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
61 } | 61 } |
62 | 62 |
63 // Make sure |handle| sets load times correctly when it has been assigned a | 63 // Make sure |handle| sets load times correctly when it has been assigned a |
64 // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner | 64 // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
65 // of a connection where |is_reused| is false may consider the connection | 65 // of a connection where |is_reused| is false may consider the connection |
66 // reused. | 66 // reused. |
67 void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { | 67 void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
68 EXPECT_FALSE(handle.is_reused()); | 68 EXPECT_FALSE(handle.is_reused()); |
69 | 69 |
70 LoadTimingInfo load_timing_info; | 70 LoadTimingInfo load_timing_info; |
71 EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); | 71 EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
72 | 72 |
73 EXPECT_FALSE(load_timing_info.socket_reused); | 73 EXPECT_FALSE(load_timing_info.socket_reused); |
74 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | 74 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 | 420 |
421 class TestConnectJobFactory | 421 class TestConnectJobFactory |
422 : public TestClientSocketPoolBase::ConnectJobFactory { | 422 : public TestClientSocketPoolBase::ConnectJobFactory { |
423 public: | 423 public: |
424 TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, | 424 TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
425 NetLog* net_log) | 425 NetLog* net_log) |
426 : job_type_(TestConnectJob::kMockJob), | 426 : job_type_(TestConnectJob::kMockJob), |
427 job_types_(NULL), | 427 job_types_(NULL), |
428 client_socket_factory_(client_socket_factory), | 428 client_socket_factory_(client_socket_factory), |
429 net_log_(net_log) { | 429 net_log_(net_log) { |
430 } | 430 } |
431 | 431 |
432 virtual ~TestConnectJobFactory() {} | 432 virtual ~TestConnectJobFactory() {} |
433 | 433 |
434 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } | 434 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
435 | 435 |
436 void set_job_types(std::list<TestConnectJob::JobType>* job_types) { | 436 void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
437 job_types_ = job_types; | 437 job_types_ = job_types; |
438 CHECK(!job_types_->empty()); | 438 CHECK(!job_types_->empty()); |
439 } | 439 } |
440 | 440 |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_, | 693 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_, |
694 &net_log_); | 694 &net_log_); |
695 pool_.reset(new TestClientSocketPool(max_sockets, | 695 pool_.reset(new TestClientSocketPool(max_sockets, |
696 max_sockets_per_group, | 696 max_sockets_per_group, |
697 &histograms_, | 697 &histograms_, |
698 unused_idle_socket_timeout, | 698 unused_idle_socket_timeout, |
699 used_idle_socket_timeout, | 699 used_idle_socket_timeout, |
700 connect_job_factory_)); | 700 connect_job_factory_)); |
701 } | 701 } |
702 | 702 |
703 int StartRequest(const std::string& group_name, | 703 int StartRequestWithParams( |
704 net::RequestPriority priority) { | 704 const std::string& group_name, |
| 705 RequestPriority priority, |
| 706 const scoped_refptr<TestSocketParams>& params) { |
705 return test_base_.StartRequestUsingPool< | 707 return test_base_.StartRequestUsingPool< |
706 TestClientSocketPool, TestSocketParams>( | 708 TestClientSocketPool, TestSocketParams>( |
707 pool_.get(), group_name, priority, params_); | 709 pool_.get(), group_name, priority, params); |
| 710 } |
| 711 |
| 712 int StartRequest(const std::string& group_name, RequestPriority priority) { |
| 713 return StartRequestWithParams(group_name, priority, params_); |
708 } | 714 } |
709 | 715 |
710 int GetOrderOfRequest(size_t index) const { | 716 int GetOrderOfRequest(size_t index) const { |
711 return test_base_.GetOrderOfRequest(index); | 717 return test_base_.GetOrderOfRequest(index); |
712 } | 718 } |
713 | 719 |
714 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { | 720 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
715 return test_base_.ReleaseOneConnection(keep_alive); | 721 return test_base_.ReleaseOneConnection(keep_alive); |
716 } | 722 } |
717 | 723 |
(...skipping 3122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3840 TestCompletionCallback callback; | 3846 TestCompletionCallback callback; |
3841 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", | 3847 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
3842 params_, | 3848 params_, |
3843 kDefaultPriority, | 3849 kDefaultPriority, |
3844 callback.callback(), | 3850 callback.callback(), |
3845 pool_.get(), | 3851 pool_.get(), |
3846 BoundNetLog())); | 3852 BoundNetLog())); |
3847 EXPECT_EQ(OK, callback.WaitForResult()); | 3853 EXPECT_EQ(OK, callback.WaitForResult()); |
3848 } | 3854 } |
3849 | 3855 |
| 3856 // Test that when a socket pool and group are at their limits, a request |
| 3857 // with |ignore_limits| triggers creation of a new socket, and gets the socket |
| 3858 // instead of a request with the same priority that was issued earlier, but |
| 3859 // that does not have |ignore_limits| set. |
| 3860 TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
| 3861 scoped_refptr<TestSocketParams> params_ignore_limits(new TestSocketParams()); |
| 3862 params_ignore_limits->set_ignore_limits(true); |
| 3863 CreatePool(1, 1); |
| 3864 |
| 3865 // Issue a request to reach the socket pool limit. |
| 3866 EXPECT_EQ(OK, StartRequestWithParams("a", kDefaultPriority, params_)); |
| 3867 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3868 |
| 3869 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3870 |
| 3871 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", kDefaultPriority, |
| 3872 params_)); |
| 3873 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3874 |
| 3875 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", kDefaultPriority, |
| 3876 params_ignore_limits)); |
| 3877 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3878 |
| 3879 EXPECT_EQ(OK, request(2)->WaitForResult()); |
| 3880 EXPECT_FALSE(request(1)->have_result()); |
| 3881 } |
| 3882 |
| 3883 // Test that when a socket pool and group are at their limits, a request with |
| 3884 // |ignore_limits| set triggers creation of a new socket, and gets the socket |
| 3885 // instead of a request with a higher priority that was issued earlier, but |
| 3886 // that does not have |ignore_limits| set. |
| 3887 TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsLowPriority) { |
| 3888 scoped_refptr<TestSocketParams> params_ignore_limits(new TestSocketParams()); |
| 3889 params_ignore_limits->set_ignore_limits(true); |
| 3890 CreatePool(1, 1); |
| 3891 |
| 3892 // Issue a request to reach the socket pool limit. |
| 3893 EXPECT_EQ(OK, StartRequestWithParams("a", HIGHEST, params_)); |
| 3894 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3895 |
| 3896 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3897 |
| 3898 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", HIGHEST, params_)); |
| 3899 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3900 |
| 3901 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", LOW, |
| 3902 params_ignore_limits)); |
| 3903 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3904 |
| 3905 EXPECT_EQ(OK, request(2)->WaitForResult()); |
| 3906 EXPECT_FALSE(request(1)->have_result()); |
| 3907 } |
| 3908 |
| 3909 // Test that when a socket pool and group are at their limits, a request with |
| 3910 // |ignore_limits| set triggers creation of a new socket, and gets the socket |
| 3911 // instead of a request with a higher priority that was issued later and |
| 3912 // does not have |ignore_limits| set. |
| 3913 TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsLowPriority2) { |
| 3914 scoped_refptr<TestSocketParams> params_ignore_limits(new TestSocketParams()); |
| 3915 params_ignore_limits->set_ignore_limits(true); |
| 3916 CreatePool(1, 1); |
| 3917 |
| 3918 // Issue a request to reach the socket pool limit. |
| 3919 EXPECT_EQ(OK, StartRequestWithParams("a", HIGHEST, params_)); |
| 3920 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3921 |
| 3922 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3923 |
| 3924 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", LOW, |
| 3925 params_ignore_limits)); |
| 3926 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3927 |
| 3928 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", HIGHEST, params_)); |
| 3929 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3930 |
| 3931 EXPECT_EQ(OK, request(1)->WaitForResult()); |
| 3932 EXPECT_FALSE(request(2)->have_result()); |
| 3933 } |
| 3934 |
| 3935 // Test that when a socket pool and group are at their limits, a ConnectJob |
| 3936 // issued for a request with |ignore_limits| set is not cancelled when a request |
| 3937 // without |ignore_limits| issued to the same group is cancelled. |
| 3938 TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
| 3939 scoped_refptr<TestSocketParams> params_ignore_limits(new TestSocketParams()); |
| 3940 params_ignore_limits->set_ignore_limits(true); |
| 3941 CreatePool(1, 1); |
| 3942 |
| 3943 // Issue a request to reach the socket pool limit. |
| 3944 EXPECT_EQ(OK, StartRequestWithParams("a", HIGHEST, params_)); |
| 3945 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3946 |
| 3947 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3948 |
| 3949 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", HIGHEST, params_)); |
| 3950 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3951 |
| 3952 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", HIGHEST, |
| 3953 params_ignore_limits)); |
| 3954 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3955 |
| 3956 // Cancel the pending request without ignore_limits set. The ConnectJob |
| 3957 // should not be cancelled. |
| 3958 request(1)->handle()->Reset(); |
| 3959 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3960 |
| 3961 EXPECT_EQ(OK, request(2)->WaitForResult()); |
| 3962 EXPECT_FALSE(request(1)->have_result()); |
| 3963 } |
| 3964 |
| 3965 // More involved test of ignore limits. Issues a bunch of requests and later |
| 3966 // checks the order in which they receive sockets. |
| 3967 TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsOrder) { |
| 3968 scoped_refptr<TestSocketParams> params_ignore_limits(new TestSocketParams()); |
| 3969 params_ignore_limits->set_ignore_limits(true); |
| 3970 CreatePool(1, 1); |
| 3971 |
| 3972 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3973 |
| 3974 // Requests 0 and 1 do not have ignore_limits set, so they finish last. Since |
| 3975 // the maximum number of sockets per pool is 1, the second requests does not |
| 3976 // trigger a ConnectJob. |
| 3977 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", HIGHEST, params_)); |
| 3978 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", HIGHEST, params_)); |
| 3979 |
| 3980 // Requests 2 and 3 have ignore_limits set, but have a low priority, so they |
| 3981 // finish just before the first two. |
| 3982 EXPECT_EQ(ERR_IO_PENDING, |
| 3983 StartRequestWithParams("a", LOW, params_ignore_limits)); |
| 3984 EXPECT_EQ(ERR_IO_PENDING, |
| 3985 StartRequestWithParams("a", LOW, params_ignore_limits)); |
| 3986 |
| 3987 // Request 4 finishes first, since it is high priority and ignores limits. |
| 3988 EXPECT_EQ(ERR_IO_PENDING, |
| 3989 StartRequestWithParams("a", HIGHEST, params_ignore_limits)); |
| 3990 |
| 3991 // Request 5 and 6 are cancelled right after starting. This should result in |
| 3992 // creating two ConnectJobs. Since only one request (Request 1) did not |
| 3993 // result in creating a ConnectJob, only one of the ConnectJobs should be |
| 3994 // cancelled when the requests are. |
| 3995 EXPECT_EQ(ERR_IO_PENDING, |
| 3996 StartRequestWithParams("a", HIGHEST, params_ignore_limits)); |
| 3997 EXPECT_EQ(ERR_IO_PENDING, |
| 3998 StartRequestWithParams("a", HIGHEST, params_ignore_limits)); |
| 3999 EXPECT_EQ(6, pool_->NumConnectJobsInGroup("a")); |
| 4000 request(5)->handle()->Reset(); |
| 4001 EXPECT_EQ(6, pool_->NumConnectJobsInGroup("a")); |
| 4002 request(6)->handle()->Reset(); |
| 4003 ASSERT_EQ(5, pool_->NumConnectJobsInGroup("a")); |
| 4004 |
| 4005 // Wait for the last request to get a socket. |
| 4006 EXPECT_EQ(OK, request(1)->WaitForResult()); |
| 4007 |
| 4008 // Check order in which requests received sockets. |
| 4009 // These are 1-based indices, while request(x) uses 0-based indices. |
| 4010 EXPECT_EQ(1, GetOrderOfRequest(5)); |
| 4011 EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 4012 EXPECT_EQ(3, GetOrderOfRequest(4)); |
| 4013 EXPECT_EQ(4, GetOrderOfRequest(1)); |
| 4014 EXPECT_EQ(5, GetOrderOfRequest(2)); |
| 4015 } |
3850 | 4016 |
3851 } // namespace | 4017 } // namespace |
3852 | 4018 |
3853 } // namespace net | 4019 } // namespace net |
OLD | NEW |