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/scoped_vector.h" | 9 #include "base/scoped_vector.h" |
10 #include "net/base/host_resolver_unittest.h" | |
11 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
12 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
13 #include "net/socket/client_socket.h" | 12 #include "net/socket/client_socket.h" |
14 #include "net/socket/client_socket_factory.h" | 13 #include "net/socket/client_socket_factory.h" |
15 #include "net/socket/client_socket_handle.h" | 14 #include "net/socket/client_socket_handle.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 namespace net { | 17 namespace net { |
19 | 18 |
20 namespace { | 19 namespace { |
(...skipping 765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
786 EXPECT_EQ(&req3, request_order_[1]); | 785 EXPECT_EQ(&req3, request_order_[1]); |
787 } | 786 } |
788 | 787 |
789 // When a ConnectJob is coupled to a request, even if a free socket becomes | 788 // When a ConnectJob is coupled to a request, even if a free socket becomes |
790 // available, the request will be serviced by the ConnectJob. | 789 // available, the request will be serviced by the ConnectJob. |
791 TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { | 790 TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
792 CreatePool(kDefaultMaxSocketsPerGroup); | 791 CreatePool(kDefaultMaxSocketsPerGroup); |
793 ClientSocketPoolBase::EnableLateBindingOfSockets(false); | 792 ClientSocketPoolBase::EnableLateBindingOfSockets(false); |
794 | 793 |
795 // Start job 1 (async OK) | 794 // Start job 1 (async OK) |
796 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 795 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
797 | 796 |
798 TestSocketRequest req1(pool_.get(), &request_order_); | 797 TestSocketRequest req1(pool_.get(), &request_order_); |
799 int rv = req1.handle.Init("a", ignored_request_info_, 5, &req1); | 798 int rv = req1.handle.Init("a", ignored_request_info_, 5, &req1); |
800 EXPECT_EQ(ERR_IO_PENDING, rv); | 799 EXPECT_EQ(ERR_IO_PENDING, rv); |
801 EXPECT_EQ(OK, req1.WaitForResult()); | 800 EXPECT_EQ(OK, req1.WaitForResult()); |
802 | 801 |
803 // Job 1 finished OK. Start job 2 (also async OK). Release socket 1. | 802 // Job 1 finished OK. Start job 2 (also async OK). Release socket 1. |
804 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); | 803 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
805 | 804 |
806 TestSocketRequest req2(pool_.get(), &request_order_); | 805 TestSocketRequest req2(pool_.get(), &request_order_); |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1128 EXPECT_EQ(1U, TestSocketRequest::completion_count); | 1127 EXPECT_EQ(1U, TestSocketRequest::completion_count); |
1129 EXPECT_EQ(2, client_socket_factory_.allocation_count()); | 1128 EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
1130 } | 1129 } |
1131 | 1130 |
1132 // When requests and ConnectJobs are not coupled, the request will get serviced | 1131 // When requests and ConnectJobs are not coupled, the request will get serviced |
1133 // by whatever comes first. | 1132 // by whatever comes first. |
1134 TEST_F(ClientSocketPoolBaseTest_LateBinding, ReleaseSockets) { | 1133 TEST_F(ClientSocketPoolBaseTest_LateBinding, ReleaseSockets) { |
1135 CreatePool(kDefaultMaxSocketsPerGroup); | 1134 CreatePool(kDefaultMaxSocketsPerGroup); |
1136 | 1135 |
1137 // Start job 1 (async OK) | 1136 // Start job 1 (async OK) |
1138 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 1137 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
1139 | 1138 |
1140 TestSocketRequest req1(pool_.get(), &request_order_); | 1139 TestSocketRequest req1(pool_.get(), &request_order_); |
1141 int rv = req1.handle.Init("a", ignored_request_info_, 5, &req1); | 1140 int rv = req1.handle.Init("a", ignored_request_info_, 5, &req1); |
1142 EXPECT_EQ(ERR_IO_PENDING, rv); | 1141 EXPECT_EQ(ERR_IO_PENDING, rv); |
1143 EXPECT_EQ(OK, req1.WaitForResult()); | 1142 EXPECT_EQ(OK, req1.WaitForResult()); |
1144 | 1143 |
1145 // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending | 1144 // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
1146 // without a job. | 1145 // without a job. |
1147 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); | 1146 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
1148 | 1147 |
(...skipping 22 matching lines...) Expand all Loading... |
1171 EXPECT_EQ(&req2, request_order_[1]); | 1170 EXPECT_EQ(&req2, request_order_[1]); |
1172 EXPECT_EQ(&req3, request_order_[2]); | 1171 EXPECT_EQ(&req3, request_order_[2]); |
1173 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 1172 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
1174 } | 1173 } |
1175 | 1174 |
1176 // The requests are not coupled to the jobs. So, the requests should finish in | 1175 // The requests are not coupled to the jobs. So, the requests should finish in |
1177 // their priority / insertion order. | 1176 // their priority / insertion order. |
1178 TEST_F(ClientSocketPoolBaseTest_LateBinding, PendingJobCompletionOrder) { | 1177 TEST_F(ClientSocketPoolBaseTest_LateBinding, PendingJobCompletionOrder) { |
1179 CreatePool(kDefaultMaxSocketsPerGroup); | 1178 CreatePool(kDefaultMaxSocketsPerGroup); |
1180 // First two jobs are async. | 1179 // First two jobs are async. |
1181 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); | 1180 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
1182 | 1181 |
1183 TestSocketRequest req1(pool_.get(), &request_order_); | 1182 TestSocketRequest req1(pool_.get(), &request_order_); |
1184 int rv = req1.handle.Init("a", ignored_request_info_, 5, &req1); | 1183 int rv = req1.handle.Init("a", ignored_request_info_, 5, &req1); |
1185 EXPECT_EQ(ERR_IO_PENDING, rv); | 1184 EXPECT_EQ(ERR_IO_PENDING, rv); |
1186 | 1185 |
1187 TestSocketRequest req2(pool_.get(), &request_order_); | 1186 TestSocketRequest req2(pool_.get(), &request_order_); |
1188 rv = req2.handle.Init("a", ignored_request_info_, 5, &req2); | 1187 rv = req2.handle.Init("a", ignored_request_info_, 5, &req2); |
1189 EXPECT_EQ(ERR_IO_PENDING, rv); | 1188 EXPECT_EQ(ERR_IO_PENDING, rv); |
1190 | 1189 |
1191 // The pending job is sync. | 1190 // The pending job is sync. |
1192 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | 1191 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
1193 | 1192 |
1194 TestSocketRequest req3(pool_.get(), &request_order_); | 1193 TestSocketRequest req3(pool_.get(), &request_order_); |
1195 rv = req3.handle.Init("a", ignored_request_info_, 5, &req3); | 1194 rv = req3.handle.Init("a", ignored_request_info_, 5, &req3); |
1196 EXPECT_EQ(ERR_IO_PENDING, rv); | 1195 EXPECT_EQ(ERR_IO_PENDING, rv); |
1197 | 1196 |
1198 EXPECT_EQ(ERR_CONNECTION_FAILED, req1.WaitForResult()); | 1197 EXPECT_EQ(ERR_CONNECTION_FAILED, req1.WaitForResult()); |
1199 EXPECT_EQ(OK, req2.WaitForResult()); | 1198 EXPECT_EQ(OK, req2.WaitForResult()); |
1200 EXPECT_EQ(ERR_CONNECTION_FAILED, req3.WaitForResult()); | 1199 EXPECT_EQ(ERR_CONNECTION_FAILED, req3.WaitForResult()); |
1201 | 1200 |
1202 ASSERT_EQ(3U, request_order_.size()); | 1201 ASSERT_EQ(3U, request_order_.size()); |
1203 EXPECT_EQ(&req1, request_order_[0]); | 1202 EXPECT_EQ(&req1, request_order_[0]); |
1204 EXPECT_EQ(&req2, request_order_[1]); | 1203 EXPECT_EQ(&req2, request_order_[1]); |
1205 EXPECT_EQ(&req3, request_order_[2]); | 1204 EXPECT_EQ(&req3, request_order_[2]); |
1206 } | 1205 } |
1207 | 1206 |
1208 TEST_F(ClientSocketPoolBaseTest_LateBinding, LoadState) { | 1207 TEST_F(ClientSocketPoolBaseTest_LateBinding, LoadState) { |
1209 CreatePool(kDefaultMaxSocketsPerGroup); | 1208 CreatePool(kDefaultMaxSocketsPerGroup); |
1210 connect_job_factory_->set_job_type( | 1209 connect_job_factory_->set_job_type( |
1211 TestConnectJob::kMockAdvancingLoadStateJob); | 1210 TestConnectJob::kMockAdvancingLoadStateJob); |
1212 | 1211 |
1213 TestSocketRequest req1(pool_.get(), &request_order_); | 1212 TestSocketRequest req1(pool_.get(), &request_order_); |
1214 int rv = req1.handle.Init("a", ignored_request_info_, 5, &req1); | 1213 int rv = req1.handle.Init("a", ignored_request_info_, 5, &req1); |
1215 EXPECT_EQ(ERR_IO_PENDING, rv); | 1214 EXPECT_EQ(ERR_IO_PENDING, rv); |
1216 EXPECT_EQ(LOAD_STATE_IDLE, req1.handle.GetLoadState()); | 1215 EXPECT_EQ(LOAD_STATE_IDLE, req1.handle.GetLoadState()); |
1217 | 1216 |
1218 MessageLoop::current()->RunAllPending(); | 1217 MessageLoop::current()->RunAllPending(); |
1219 | 1218 |
1220 TestSocketRequest req2(pool_.get(), &request_order_); | 1219 TestSocketRequest req2(pool_.get(), &request_order_); |
1221 rv = req2.handle.Init("a", ignored_request_info_, 5, &req2); | 1220 rv = req2.handle.Init("a", ignored_request_info_, 5, &req2); |
1222 EXPECT_EQ(ERR_IO_PENDING, rv); | 1221 EXPECT_EQ(ERR_IO_PENDING, rv); |
1223 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, req1.handle.GetLoadState()); | 1222 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, req1.handle.GetLoadState()); |
1224 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, req2.handle.GetLoadState()); | 1223 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, req2.handle.GetLoadState()); |
1225 } | 1224 } |
1226 | 1225 |
1227 } // namespace | 1226 } // namespace |
1228 | 1227 |
1229 } // namespace net | 1228 } // namespace net |
OLD | NEW |