OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/platform_thread.h" | 9 #include "base/platform_thread.h" |
10 #include "base/scoped_vector.h" | 10 #include "base/scoped_vector.h" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 AddressList ignored; | 136 AddressList ignored; |
137 client_socket_factory_->CreateTCPClientSocket(ignored); | 137 client_socket_factory_->CreateTCPClientSocket(ignored); |
138 set_socket(new MockClientSocket()); | 138 set_socket(new MockClientSocket()); |
139 switch (job_type_) { | 139 switch (job_type_) { |
140 case kMockJob: | 140 case kMockJob: |
141 return DoConnect(true /* successful */, false /* sync */); | 141 return DoConnect(true /* successful */, false /* sync */); |
142 case kMockFailingJob: | 142 case kMockFailingJob: |
143 return DoConnect(false /* error */, false /* sync */); | 143 return DoConnect(false /* error */, false /* sync */); |
144 case kMockPendingJob: | 144 case kMockPendingJob: |
145 set_load_state(LOAD_STATE_CONNECTING); | 145 set_load_state(LOAD_STATE_CONNECTING); |
146 MessageLoop::current()->PostDelayedTask( | 146 MessageLoop::current()->PostTask( |
147 FROM_HERE, | 147 FROM_HERE, |
148 method_factory_.NewRunnableMethod( | 148 method_factory_.NewRunnableMethod( |
149 &TestConnectJob::DoConnect, | 149 &TestConnectJob::DoConnect, |
150 true /* successful */, | 150 true /* successful */, |
151 true /* async */), | 151 true /* async */)); |
152 2); | |
153 return ERR_IO_PENDING; | 152 return ERR_IO_PENDING; |
154 case kMockPendingFailingJob: | 153 case kMockPendingFailingJob: |
155 set_load_state(LOAD_STATE_CONNECTING); | 154 set_load_state(LOAD_STATE_CONNECTING); |
156 MessageLoop::current()->PostDelayedTask( | 155 MessageLoop::current()->PostTask( |
157 FROM_HERE, | 156 FROM_HERE, |
158 method_factory_.NewRunnableMethod( | 157 method_factory_.NewRunnableMethod( |
159 &TestConnectJob::DoConnect, | 158 &TestConnectJob::DoConnect, |
160 false /* error */, | 159 false /* error */, |
161 true /* async */), | 160 true /* async */)); |
162 2); | |
163 return ERR_IO_PENDING; | 161 return ERR_IO_PENDING; |
164 case kMockWaitingJob: | 162 case kMockWaitingJob: |
165 client_socket_factory_->WaitForSignal(this); | 163 client_socket_factory_->WaitForSignal(this); |
166 waiting_success_ = true; | 164 waiting_success_ = true; |
167 return ERR_IO_PENDING; | 165 return ERR_IO_PENDING; |
168 case kMockAdvancingLoadStateJob: | 166 case kMockAdvancingLoadStateJob: |
169 MessageLoop::current()->PostDelayedTask( | 167 MessageLoop::current()->PostTask( |
170 FROM_HERE, | 168 FROM_HERE, |
171 method_factory_.NewRunnableMethod( | 169 method_factory_.NewRunnableMethod( |
172 &TestConnectJob::AdvanceLoadState, load_state_), | 170 &TestConnectJob::AdvanceLoadState, load_state_)); |
173 2); | |
174 return ERR_IO_PENDING; | 171 return ERR_IO_PENDING; |
175 default: | 172 default: |
176 NOTREACHED(); | 173 NOTREACHED(); |
177 set_socket(NULL); | 174 set_socket(NULL); |
178 return ERR_FAILED; | 175 return ERR_FAILED; |
179 } | 176 } |
180 } | 177 } |
181 | 178 |
182 void set_load_state(LoadState load_state) { load_state_ = load_state; } | 179 void set_load_state(LoadState load_state) { load_state_ = load_state; } |
183 | 180 |
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { | 710 TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
714 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | 711 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
715 | 712 |
716 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 713 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
717 EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); | 714 EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
718 EXPECT_EQ(OK, StartRequest("c", kDefaultPriority)); | 715 EXPECT_EQ(OK, StartRequest("c", kDefaultPriority)); |
719 | 716 |
720 // Create one asynchronous request. | 717 // Create one asynchronous request. |
721 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 718 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
722 EXPECT_EQ(ERR_IO_PENDING, StartRequest("d", kDefaultPriority)); | 719 EXPECT_EQ(ERR_IO_PENDING, StartRequest("d", kDefaultPriority)); |
723 PlatformThread::Sleep(10); | |
724 MessageLoop::current()->RunAllPending(); | |
725 | 720 |
726 // The next synchronous request should wait for its turn. | 721 // The next synchronous request should wait for its turn. |
727 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | 722 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
728 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", kDefaultPriority)); | 723 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", kDefaultPriority)); |
729 | 724 |
730 ReleaseAllConnections(KEEP_ALIVE); | 725 ReleaseAllConnections(KEEP_ALIVE); |
731 | 726 |
732 EXPECT_EQ(static_cast<int>(requests_.size()), | 727 EXPECT_EQ(static_cast<int>(requests_.size()), |
733 client_socket_factory_.allocation_count()); | 728 client_socket_factory_.allocation_count()); |
734 | 729 |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 next_job_type_(next_job_type) {} | 946 next_job_type_(next_job_type) {} |
952 | 947 |
953 virtual void RunWithParams(const Tuple1<int>& params) { | 948 virtual void RunWithParams(const Tuple1<int>& params) { |
954 callback_.RunWithParams(params); | 949 callback_.RunWithParams(params); |
955 ASSERT_EQ(OK, params.a); | 950 ASSERT_EQ(OK, params.a); |
956 | 951 |
957 if (!within_callback_) { | 952 if (!within_callback_) { |
958 test_connect_job_factory_->set_job_type(next_job_type_); | 953 test_connect_job_factory_->set_job_type(next_job_type_); |
959 handle_->Reset(); | 954 handle_->Reset(); |
960 within_callback_ = true; | 955 within_callback_ = true; |
961 TestCompletionCallback next_job_callback; | |
962 int rv = InitHandle( | 956 int rv = InitHandle( |
963 handle_, "a", kDefaultPriority, &next_job_callback, pool_.get(), | 957 handle_, "a", kDefaultPriority, this, pool_.get(), NULL); |
964 NULL); | |
965 switch (next_job_type_) { | 958 switch (next_job_type_) { |
966 case TestConnectJob::kMockJob: | 959 case TestConnectJob::kMockJob: |
967 EXPECT_EQ(OK, rv); | 960 EXPECT_EQ(OK, rv); |
968 break; | 961 break; |
969 case TestConnectJob::kMockPendingJob: | 962 case TestConnectJob::kMockPendingJob: |
970 EXPECT_EQ(ERR_IO_PENDING, rv); | 963 EXPECT_EQ(ERR_IO_PENDING, rv); |
971 | |
972 // For pending jobs, wait for new socket to be created. This makes | |
973 // sure there are no more pending operations nor any unclosed sockets | |
974 // when the test finishes. | |
975 // We need to give it a little bit of time to run, so that all the | |
976 // operations that happen on timers (e.g. cleanup of idle | |
977 // connections) can execute. | |
978 MessageLoop::current()->SetNestableTasksAllowed(true); | |
979 PlatformThread::Sleep(10); | |
980 EXPECT_EQ(OK, next_job_callback.WaitForResult()); | |
981 break; | 964 break; |
982 default: | 965 default: |
983 FAIL() << "Unexpected job type: " << next_job_type_; | 966 FAIL() << "Unexpected job type: " << next_job_type_; |
984 break; | 967 break; |
985 } | 968 } |
986 } | 969 } |
987 } | 970 } |
988 | 971 |
989 int WaitForResult() { | 972 int WaitForResult() { |
990 return callback_.WaitForResult(); | 973 return callback_.WaitForResult(); |
(...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1841 | 1824 |
1842 pool_->CleanupTimedOutIdleSockets(); | 1825 pool_->CleanupTimedOutIdleSockets(); |
1843 rv = InitHandle(req.handle(), "a", 0, &req, pool_.get(), NULL); | 1826 rv = InitHandle(req.handle(), "a", 0, &req, pool_.get(), NULL); |
1844 EXPECT_EQ(OK, rv); | 1827 EXPECT_EQ(OK, rv); |
1845 EXPECT_TRUE(req.handle()->is_reused()); | 1828 EXPECT_TRUE(req.handle()->is_reused()); |
1846 } | 1829 } |
1847 | 1830 |
1848 } // namespace | 1831 } // namespace |
1849 | 1832 |
1850 } // namespace net | 1833 } // namespace net |
OLD | NEW |