| 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "net/base/client_socket.h" | 6 #include "net/base/client_socket.h" |
| 7 #include "net/base/client_socket_factory.h" | |
| 8 #include "net/base/client_socket_handle.h" | 7 #include "net/base/client_socket_handle.h" |
| 9 #include "net/base/client_socket_pool.h" | 8 #include "net/base/client_socket_pool.h" |
| 10 #include "net/base/host_resolver_unittest.h" | |
| 11 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 12 #include "net/base/test_completion_callback.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 11 |
| 15 namespace net { | |
| 16 | |
| 17 namespace { | 12 namespace { |
| 18 | 13 |
| 19 const int kMaxSocketsPerGroup = 6; | 14 const int kMaxSocketsPerGroup = 6; |
| 20 | 15 |
| 21 // Note that the first and the last are the same, the first should be handled | 16 // Note that the first and the last are the same, the first should be handled |
| 22 // before the last, since it was inserted first. | 17 // before the last, since it was inserted first. |
| 23 const int kPriorities[10] = { 1, 7, 9, 5, 6, 2, 8, 3, 4, 1 }; | 18 const int kPriorities[10] = { 1, 7, 9, 5, 6, 2, 8, 3, 4, 1 }; |
| 24 | 19 |
| 25 // This is the number of extra requests beyond the first few that use up all | 20 // This is the number of extra requests beyond the first few that use up all |
| 26 // available sockets in the socket group. | 21 // available sockets in the socket group. |
| 27 const int kNumPendingRequests = arraysize(kPriorities); | 22 const int kNumPendingRequests = arraysize(kPriorities); |
| 28 | 23 |
| 29 const int kNumRequests = kMaxSocketsPerGroup + kNumPendingRequests; | 24 class MockClientSocket : public net::ClientSocket { |
| 30 | |
| 31 class MockClientSocket : public ClientSocket { | |
| 32 public: | 25 public: |
| 33 MockClientSocket() : connected_(false) {} | 26 MockClientSocket() : connected_(false) { |
| 27 allocation_count++; |
| 28 } |
| 34 | 29 |
| 35 // ClientSocket methods: | 30 // ClientSocket methods: |
| 36 virtual int Connect(CompletionCallback* callback) { | 31 virtual int Connect(net::CompletionCallback* callback) { |
| 37 connected_ = true; | 32 connected_ = true; |
| 38 return OK; | 33 return net::OK; |
| 39 } | 34 } |
| 40 virtual void Disconnect() { | 35 virtual void Disconnect() { |
| 41 connected_ = false; | 36 connected_ = false; |
| 42 } | 37 } |
| 43 virtual bool IsConnected() const { | 38 virtual bool IsConnected() const { |
| 44 return connected_; | 39 return connected_; |
| 45 } | 40 } |
| 46 virtual bool IsConnectedAndIdle() const { | 41 virtual bool IsConnectedAndIdle() const { |
| 47 return connected_; | 42 return connected_; |
| 48 } | 43 } |
| 49 | 44 |
| 50 // Socket methods: | 45 // Socket methods: |
| 51 virtual int Read(IOBuffer* buf, int buf_len, | 46 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 52 CompletionCallback* callback) { | 47 net::CompletionCallback* callback) { |
| 53 return ERR_FAILED; | 48 return net::ERR_FAILED; |
| 54 } | 49 } |
| 55 virtual int Write(IOBuffer* buf, int buf_len, | 50 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 56 CompletionCallback* callback) { | 51 net::CompletionCallback* callback) { |
| 57 return ERR_FAILED; | 52 return net::ERR_FAILED; |
| 58 } | 53 } |
| 59 | 54 |
| 55 static int allocation_count; |
| 56 |
| 60 private: | 57 private: |
| 61 bool connected_; | 58 bool connected_; |
| 62 }; | 59 }; |
| 63 | 60 |
| 64 class MockFailingClientSocket : public ClientSocket { | 61 int MockClientSocket::allocation_count = 0; |
| 65 public: | |
| 66 MockFailingClientSocket() {} | |
| 67 | |
| 68 // ClientSocket methods: | |
| 69 virtual int Connect(CompletionCallback* callback) { | |
| 70 return ERR_CONNECTION_FAILED; | |
| 71 } | |
| 72 | |
| 73 virtual void Disconnect() {} | |
| 74 | |
| 75 virtual bool IsConnected() const { | |
| 76 return false; | |
| 77 } | |
| 78 virtual bool IsConnectedAndIdle() const { | |
| 79 return false; | |
| 80 } | |
| 81 | |
| 82 // Socket methods: | |
| 83 virtual int Read(IOBuffer* buf, int buf_len, | |
| 84 CompletionCallback* callback) { | |
| 85 return ERR_FAILED; | |
| 86 } | |
| 87 | |
| 88 virtual int Write(IOBuffer* buf, int buf_len, | |
| 89 CompletionCallback* callback) { | |
| 90 return ERR_FAILED; | |
| 91 } | |
| 92 }; | |
| 93 | |
| 94 class MockClientSocketFactory : public ClientSocketFactory { | |
| 95 public: | |
| 96 enum ClientSocketType { | |
| 97 MOCK_CLIENT_SOCKET, | |
| 98 MOCK_FAILING_CLIENT_SOCKET, | |
| 99 }; | |
| 100 | |
| 101 MockClientSocketFactory() | |
| 102 : allocation_count_(0), client_socket_type_(MOCK_CLIENT_SOCKET) {} | |
| 103 | |
| 104 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses) { | |
| 105 allocation_count_++; | |
| 106 switch (client_socket_type_) { | |
| 107 case MOCK_CLIENT_SOCKET: | |
| 108 return new MockClientSocket(); | |
| 109 case MOCK_FAILING_CLIENT_SOCKET: | |
| 110 return new MockFailingClientSocket(); | |
| 111 default: | |
| 112 NOTREACHED(); | |
| 113 return new MockClientSocket(); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 virtual SSLClientSocket* CreateSSLClientSocket( | |
| 118 ClientSocket* transport_socket, | |
| 119 const std::string& hostname, | |
| 120 const SSLConfig& ssl_config) { | |
| 121 NOTIMPLEMENTED(); | |
| 122 return NULL; | |
| 123 } | |
| 124 | |
| 125 int allocation_count() const { return allocation_count_; } | |
| 126 | |
| 127 void set_client_socket_type(ClientSocketType type) { | |
| 128 client_socket_type_ = type; | |
| 129 } | |
| 130 | |
| 131 private: | |
| 132 int allocation_count_; | |
| 133 ClientSocketType client_socket_type_; | |
| 134 }; | |
| 135 | 62 |
| 136 class TestSocketRequest : public CallbackRunner< Tuple1<int> > { | 63 class TestSocketRequest : public CallbackRunner< Tuple1<int> > { |
| 137 public: | 64 public: |
| 138 TestSocketRequest( | 65 TestSocketRequest( |
| 139 ClientSocketPool* pool, | 66 net::ClientSocketPool* pool, |
| 140 std::vector<TestSocketRequest*>* request_order) | 67 std::vector<TestSocketRequest*>* request_order) |
| 141 : handle(pool), request_order_(request_order) {} | 68 : handle(pool), request_order_(request_order) {} |
| 142 | 69 |
| 143 ClientSocketHandle handle; | 70 net::ClientSocketHandle handle; |
| 144 | 71 |
| 145 int WaitForResult() { | 72 void EnsureSocket() { |
| 146 return callback_.WaitForResult(); | 73 DCHECK(handle.is_initialized()); |
| 74 request_order_->push_back(this); |
| 75 if (!handle.socket()) { |
| 76 handle.set_socket(new MockClientSocket()); |
| 77 handle.socket()->Connect(NULL); |
| 78 } |
| 147 } | 79 } |
| 148 | 80 |
| 149 virtual void RunWithParams(const Tuple1<int>& params) { | 81 virtual void RunWithParams(const Tuple1<int>& params) { |
| 150 callback_.RunWithParams(params); | 82 DCHECK(params.a == net::OK); |
| 151 completion_count++; | 83 completion_count++; |
| 152 request_order_->push_back(this); | 84 EnsureSocket(); |
| 153 } | 85 } |
| 154 | 86 |
| 155 static int completion_count; | 87 static int completion_count; |
| 156 | 88 |
| 157 private: | 89 private: |
| 158 std::vector<TestSocketRequest*>* request_order_; | 90 std::vector<TestSocketRequest*>* request_order_; |
| 159 TestCompletionCallback callback_; | |
| 160 }; | 91 }; |
| 161 | 92 |
| 162 int TestSocketRequest::completion_count = 0; | 93 int TestSocketRequest::completion_count = 0; |
| 163 | 94 |
| 164 class ClientSocketPoolTest : public testing::Test { | 95 class ClientSocketPoolTest : public testing::Test { |
| 165 protected: | 96 protected: |
| 166 ClientSocketPoolTest() | 97 ClientSocketPoolTest() |
| 167 : pool_(new ClientSocketPool(kMaxSocketsPerGroup, | 98 : pool_(new net::ClientSocketPool(kMaxSocketsPerGroup)) {} |
| 168 &client_socket_factory_)) {} | |
| 169 | 99 |
| 170 virtual void SetUp() { | 100 virtual void SetUp() { |
| 101 MockClientSocket::allocation_count = 0; |
| 171 TestSocketRequest::completion_count = 0; | 102 TestSocketRequest::completion_count = 0; |
| 172 } | 103 } |
| 173 | 104 |
| 174 MockClientSocketFactory client_socket_factory_; | 105 scoped_refptr<net::ClientSocketPool> pool_; |
| 175 scoped_refptr<ClientSocketPool> pool_; | |
| 176 std::vector<TestSocketRequest*> request_order_; | 106 std::vector<TestSocketRequest*> request_order_; |
| 177 }; | 107 }; |
| 178 | 108 |
| 179 TEST_F(ClientSocketPoolTest, Basic) { | 109 TEST_F(ClientSocketPoolTest, Basic) { |
| 180 TestCompletionCallback callback; | 110 TestSocketRequest r(pool_.get(), &request_order_); |
| 181 ClientSocketHandle handle(pool_.get()); | 111 int rv; |
| 182 int rv = handle.Init("a", "www.google.com", 80, 0, &callback); | |
| 183 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 184 EXPECT_FALSE(handle.is_initialized()); | |
| 185 EXPECT_FALSE(handle.socket()); | |
| 186 | 112 |
| 187 EXPECT_EQ(OK, callback.WaitForResult()); | 113 rv = r.handle.Init("a", 0, &r); |
| 188 EXPECT_TRUE(handle.is_initialized()); | 114 EXPECT_EQ(net::OK, rv); |
| 189 EXPECT_TRUE(handle.socket()); | 115 EXPECT_TRUE(r.handle.is_initialized()); |
| 190 | 116 |
| 191 handle.Reset(); | 117 r.handle.Reset(); |
| 192 | 118 |
| 193 // The handle's Reset method may have posted a task. | 119 // The handle's Reset method may have posted a task. |
| 194 MessageLoop::current()->RunAllPending(); | 120 MessageLoop::current()->RunAllPending(); |
| 195 } | 121 } |
| 196 | 122 |
| 197 TEST_F(ClientSocketPoolTest, InitHostResolutionFailure) { | 123 TEST_F(ClientSocketPoolTest, WithIdleConnection) { |
| 198 RuleBasedHostMapper* host_mapper = new RuleBasedHostMapper; | 124 TestSocketRequest r(pool_.get(), &request_order_); |
| 199 host_mapper->AddSimulatedFailure("unresolvable.host.name"); | 125 int rv; |
| 200 ScopedHostMapper scoped_host_mapper(host_mapper); | |
| 201 TestSocketRequest req(pool_.get(), &request_order_); | |
| 202 EXPECT_EQ(ERR_IO_PENDING, | |
| 203 req.handle.Init("a", "unresolvable.host.name", 80, 5, &req)); | |
| 204 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req.WaitForResult()); | |
| 205 } | |
| 206 | 126 |
| 207 TEST_F(ClientSocketPoolTest, InitConnectionFailure) { | 127 rv = r.handle.Init("a", 0, &r); |
| 208 client_socket_factory_.set_client_socket_type( | 128 EXPECT_EQ(net::OK, rv); |
| 209 MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); | 129 EXPECT_TRUE(r.handle.is_initialized()); |
| 210 TestSocketRequest req(pool_.get(), &request_order_); | 130 |
| 211 EXPECT_EQ(ERR_IO_PENDING, | 131 // Create a socket. |
| 212 req.handle.Init("a", "unresolvable.host.name", 80, 5, &req)); | 132 r.EnsureSocket(); |
| 213 EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult()); | 133 |
| 134 // Release the socket. It should find its way into the idle list. We're |
| 135 // testing that this does not trigger a crash. |
| 136 r.handle.Reset(); |
| 137 |
| 138 // The handle's Reset method may have posted a task. |
| 139 MessageLoop::current()->RunAllPending(); |
| 214 } | 140 } |
| 215 | 141 |
| 216 TEST_F(ClientSocketPoolTest, PendingRequests) { | 142 TEST_F(ClientSocketPoolTest, PendingRequests) { |
| 217 int rv; | 143 int rv; |
| 218 | 144 |
| 219 scoped_ptr<TestSocketRequest> reqs[kNumRequests]; | 145 scoped_ptr<TestSocketRequest> reqs[kMaxSocketsPerGroup + kNumPendingRequests]; |
| 220 | 146 |
| 221 for (size_t i = 0; i < arraysize(reqs); ++i) | 147 for (size_t i = 0; i < arraysize(reqs); ++i) |
| 222 reqs[i].reset(new TestSocketRequest(pool_.get(), &request_order_)); | 148 reqs[i].reset(new TestSocketRequest(pool_.get(), &request_order_)); |
| 223 | 149 |
| 224 // Create connections or queue up requests. | 150 // Create connections or queue up requests. |
| 225 for (int i = 0; i < kMaxSocketsPerGroup; ++i) { | 151 for (int i = 0; i < kMaxSocketsPerGroup; ++i) { |
| 226 EXPECT_EQ( | 152 rv = reqs[i]->handle.Init("a", 5, reqs[i].get()); |
| 227 ERR_IO_PENDING, | 153 EXPECT_EQ(net::OK, rv); |
| 228 reqs[i]->handle.Init("a", "www.google.com", 80, 5, reqs[i].get())); | 154 reqs[i]->EnsureSocket(); |
| 229 EXPECT_EQ(OK, reqs[i]->WaitForResult()); | |
| 230 } | 155 } |
| 231 | |
| 232 for (int i = 0; i < kNumPendingRequests; ++i) { | 156 for (int i = 0; i < kNumPendingRequests; ++i) { |
| 233 rv = reqs[kMaxSocketsPerGroup + i]->handle.Init( | 157 rv = reqs[kMaxSocketsPerGroup + i]->handle.Init( |
| 234 "a", "www.google.com", 80, kPriorities[i], | 158 "a", kPriorities[i], reqs[kMaxSocketsPerGroup + i].get()); |
| 235 reqs[kMaxSocketsPerGroup + i].get()); | 159 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 236 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 237 } | 160 } |
| 238 | 161 |
| 239 // Release any connections until we have no connections. | 162 // Release any connections until we have no connections. |
| 240 bool released_one; | 163 bool released_one; |
| 241 do { | 164 do { |
| 242 released_one = false; | 165 released_one = false; |
| 243 for (size_t i = 0; i < arraysize(reqs); ++i) { | 166 for (size_t i = 0; i < arraysize(reqs); ++i) { |
| 244 if (reqs[i]->handle.is_initialized()) { | 167 if (reqs[i]->handle.is_initialized()) { |
| 245 reqs[i]->handle.Reset(); | 168 reqs[i]->handle.Reset(); |
| 246 MessageLoop::current()->RunAllPending(); | 169 MessageLoop::current()->RunAllPending(); |
| 247 released_one = true; | 170 released_one = true; |
| 248 } | 171 } |
| 249 } | 172 } |
| 250 } while (released_one); | 173 } while (released_one); |
| 251 | 174 |
| 252 EXPECT_EQ(kMaxSocketsPerGroup, client_socket_factory_.allocation_count()); | 175 EXPECT_EQ(kMaxSocketsPerGroup, MockClientSocket::allocation_count); |
| 253 EXPECT_EQ(kNumRequests, TestSocketRequest::completion_count); | 176 EXPECT_EQ(kNumPendingRequests, TestSocketRequest::completion_count); |
| 254 | 177 |
| 255 for (int i = 0; i < kMaxSocketsPerGroup; ++i) { | 178 for (int i = 0; i < kMaxSocketsPerGroup; ++i) { |
| 256 EXPECT_EQ(request_order_[i], reqs[i].get()) << | 179 EXPECT_EQ(request_order_[i], reqs[i].get()) << |
| 257 "Request " << i << " was not in order."; | 180 "Request " << i << " was not in order."; |
| 258 } | 181 } |
| 259 | 182 |
| 260 for (int i = 0; i < kNumPendingRequests - 1; ++i) { | 183 for (int i = 0; i < kNumPendingRequests - 1; ++i) { |
| 261 int index_in_queue = (kNumPendingRequests - 1) - kPriorities[i]; | 184 int index_in_queue = (kNumPendingRequests - 1) - kPriorities[i]; |
| 262 EXPECT_EQ(request_order_[kMaxSocketsPerGroup + index_in_queue], | 185 EXPECT_EQ(request_order_[kMaxSocketsPerGroup + index_in_queue], |
| 263 reqs[kMaxSocketsPerGroup + i].get()) << | 186 reqs[kMaxSocketsPerGroup + i].get()) << |
| 264 "Request " << kMaxSocketsPerGroup + i << " was not in order."; | 187 "Request " << kMaxSocketsPerGroup + i << " was not in order."; |
| 265 } | 188 } |
| 266 | 189 |
| 267 EXPECT_EQ(request_order_[arraysize(reqs) - 1], | 190 EXPECT_EQ(request_order_[arraysize(reqs) - 1], |
| 268 reqs[arraysize(reqs) - 1].get()) << | 191 reqs[arraysize(reqs) - 1].get()) << |
| 269 "The last request with priority 1 should not have been inserted " | 192 "The last request with priority 1 should not have been inserted " |
| 270 "earlier into the queue."; | 193 "earlier into the queue."; |
| 271 } | 194 } |
| 272 | 195 |
| 273 TEST_F(ClientSocketPoolTest, PendingRequests_NoKeepAlive) { | 196 TEST_F(ClientSocketPoolTest, PendingRequests_NoKeepAlive) { |
| 274 scoped_ptr<TestSocketRequest> reqs[kNumRequests]; | 197 int rv; |
| 198 |
| 199 scoped_ptr<TestSocketRequest> reqs[kMaxSocketsPerGroup + kNumPendingRequests]; |
| 275 for (size_t i = 0; i < arraysize(reqs); ++i) | 200 for (size_t i = 0; i < arraysize(reqs); ++i) |
| 276 reqs[i].reset(new TestSocketRequest(pool_.get(), &request_order_)); | 201 reqs[i].reset(new TestSocketRequest(pool_.get(), &request_order_)); |
| 277 | 202 |
| 278 // Create connections or queue up requests. | 203 // Create connections or queue up requests. |
| 279 for (int i = 0; i < kMaxSocketsPerGroup; ++i) { | 204 for (size_t i = 0; i < arraysize(reqs); ++i) { |
| 280 EXPECT_EQ( | 205 rv = reqs[i]->handle.Init("a", 0, reqs[i].get()); |
| 281 ERR_IO_PENDING, | 206 if (rv != net::ERR_IO_PENDING) { |
| 282 reqs[i]->handle.Init("a", "www.google.com", 80, 0, reqs[i].get())); | 207 EXPECT_EQ(net::OK, rv); |
| 283 EXPECT_EQ(OK, reqs[i]->WaitForResult()); | 208 reqs[i]->EnsureSocket(); |
| 284 } | 209 } |
| 285 | |
| 286 for (int i = 0; i < kNumPendingRequests; ++i) { | |
| 287 EXPECT_EQ(ERR_IO_PENDING, reqs[kMaxSocketsPerGroup + i]->handle.Init( | |
| 288 "a", "www.google.com", 80, 0, reqs[kMaxSocketsPerGroup + i].get())); | |
| 289 } | 210 } |
| 290 | 211 |
| 291 // Release any connections until we have no connections. | 212 // Release any connections until we have no connections. |
| 292 | 213 bool released_one; |
| 293 while (TestSocketRequest::completion_count < kNumRequests) { | 214 do { |
| 294 int num_released = 0; | 215 released_one = false; |
| 295 for (size_t i = 0; i < arraysize(reqs); ++i) { | 216 for (size_t i = 0; i < arraysize(reqs); ++i) { |
| 296 if (reqs[i]->handle.is_initialized()) { | 217 if (reqs[i]->handle.is_initialized()) { |
| 297 reqs[i]->handle.socket()->Disconnect(); | 218 reqs[i]->handle.socket()->Disconnect(); |
| 298 reqs[i]->handle.Reset(); | 219 reqs[i]->handle.Reset(); |
| 299 num_released++; | 220 MessageLoop::current()->RunAllPending(); |
| 221 released_one = true; |
| 300 } | 222 } |
| 301 } | 223 } |
| 302 int curr_num_completed = TestSocketRequest::completion_count; | 224 } while (released_one); |
| 303 for (int i = 0; | |
| 304 (i < num_released) && (i + curr_num_completed < kNumRequests); ++i) { | |
| 305 EXPECT_EQ(OK, reqs[i + curr_num_completed]->WaitForResult()); | |
| 306 } | |
| 307 } | |
| 308 | 225 |
| 309 EXPECT_EQ(kNumRequests, client_socket_factory_.allocation_count()); | 226 EXPECT_EQ(kMaxSocketsPerGroup + kNumPendingRequests, |
| 310 EXPECT_EQ(kNumRequests, TestSocketRequest::completion_count); | 227 MockClientSocket::allocation_count); |
| 311 } | 228 EXPECT_EQ(kNumPendingRequests, TestSocketRequest::completion_count); |
| 312 | |
| 313 // This test will start up a RequestSocket() and then immediately Cancel() it. | |
| 314 // The pending host resolution will eventually complete, and destroy the | |
| 315 // ClientSocketPool which will crash if the group was not cleared properly. | |
| 316 TEST_F(ClientSocketPoolTest, CancelRequestClearGroup) { | |
| 317 TestSocketRequest req(pool_.get(), &request_order_); | |
| 318 EXPECT_EQ(ERR_IO_PENDING, | |
| 319 req.handle.Init("a", "www.google.com", 80, 5, &req)); | |
| 320 req.handle.Reset(); | |
| 321 // There is a race condition here. If the worker pool doesn't post the task | |
| 322 // before we get here, then this might not run ConnectingSocket::IOComplete | |
| 323 // and therefore leak the canceled ConnectingSocket. | |
| 324 MessageLoop::current()->RunAllPending(); | |
| 325 } | 229 } |
| 326 | 230 |
| 327 TEST_F(ClientSocketPoolTest, CancelRequest) { | 231 TEST_F(ClientSocketPoolTest, CancelRequest) { |
| 328 scoped_ptr<TestSocketRequest> reqs[kNumRequests]; | 232 int rv; |
| 233 |
| 234 scoped_ptr<TestSocketRequest> reqs[kMaxSocketsPerGroup + kNumPendingRequests]; |
| 329 | 235 |
| 330 for (size_t i = 0; i < arraysize(reqs); ++i) | 236 for (size_t i = 0; i < arraysize(reqs); ++i) |
| 331 reqs[i].reset(new TestSocketRequest(pool_.get(), &request_order_)); | 237 reqs[i].reset(new TestSocketRequest(pool_.get(), &request_order_)); |
| 332 | 238 |
| 333 // Create connections or queue up requests. | 239 // Create connections or queue up requests. |
| 334 for (int i = 0; i < kMaxSocketsPerGroup; ++i) { | 240 for (int i = 0; i < kMaxSocketsPerGroup; ++i) { |
| 335 EXPECT_EQ( | 241 rv = reqs[i]->handle.Init("a", 5, reqs[i].get()); |
| 336 ERR_IO_PENDING, | 242 EXPECT_EQ(net::OK, rv); |
| 337 reqs[i]->handle.Init("a", "www.google.com", 80, 5, reqs[i].get())); | 243 reqs[i]->EnsureSocket(); |
| 338 EXPECT_EQ(OK, reqs[i]->WaitForResult()); | |
| 339 } | 244 } |
| 340 | |
| 341 for (int i = 0; i < kNumPendingRequests; ++i) { | 245 for (int i = 0; i < kNumPendingRequests; ++i) { |
| 342 EXPECT_EQ(ERR_IO_PENDING, reqs[kMaxSocketsPerGroup + i]->handle.Init( | 246 rv = reqs[kMaxSocketsPerGroup + i]->handle.Init( |
| 343 "a", "www.google.com", 80, kPriorities[i], | 247 "a", kPriorities[i], reqs[kMaxSocketsPerGroup + i].get()); |
| 344 reqs[kMaxSocketsPerGroup + i].get())); | 248 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 345 } | 249 } |
| 346 | 250 |
| 347 // Cancel a request. | 251 // Cancel a request. |
| 348 size_t index_to_cancel = kMaxSocketsPerGroup + 2; | 252 size_t index_to_cancel = kMaxSocketsPerGroup + 2; |
| 349 EXPECT_TRUE(!reqs[index_to_cancel]->handle.is_initialized()); | 253 EXPECT_TRUE(!reqs[index_to_cancel]->handle.is_initialized()); |
| 350 reqs[index_to_cancel]->handle.Reset(); | 254 reqs[index_to_cancel]->handle.Reset(); |
| 351 | 255 |
| 352 // Release any connections until we have no connections. | 256 // Release any connections until we have no connections. |
| 353 bool released_one; | 257 bool released_one; |
| 354 do { | 258 do { |
| 355 released_one = false; | 259 released_one = false; |
| 356 for (size_t i = 0; i < arraysize(reqs); ++i) { | 260 for (size_t i = 0; i < arraysize(reqs); ++i) { |
| 357 if (reqs[i]->handle.is_initialized()) { | 261 if (reqs[i]->handle.is_initialized()) { |
| 358 reqs[i]->handle.Reset(); | 262 reqs[i]->handle.Reset(); |
| 359 MessageLoop::current()->RunAllPending(); | 263 MessageLoop::current()->RunAllPending(); |
| 360 released_one = true; | 264 released_one = true; |
| 361 } | 265 } |
| 362 } | 266 } |
| 363 } while (released_one); | 267 } while (released_one); |
| 364 | 268 |
| 365 EXPECT_EQ(kMaxSocketsPerGroup, client_socket_factory_.allocation_count()); | 269 EXPECT_EQ(kMaxSocketsPerGroup, MockClientSocket::allocation_count); |
| 366 EXPECT_EQ(kNumRequests - 1, TestSocketRequest::completion_count); | 270 EXPECT_EQ(kNumPendingRequests - 1, TestSocketRequest::completion_count); |
| 367 for (int i = 0; i < kMaxSocketsPerGroup; ++i) { | 271 for (int i = 0; i < kMaxSocketsPerGroup; ++i) { |
| 368 EXPECT_EQ(request_order_[i], reqs[i].get()) << | 272 EXPECT_EQ(request_order_[i], reqs[i].get()) << |
| 369 "Request " << i << " was not in order."; | 273 "Request " << i << " was not in order."; |
| 370 } | 274 } |
| 371 | 275 |
| 372 for (int i = 0; i < kNumPendingRequests - 1; ++i) { | 276 for (int i = 0; i < kNumPendingRequests - 1; ++i) { |
| 373 if (i == 2) continue; | 277 if (i == 2) continue; |
| 374 int index_in_queue = (kNumPendingRequests - 1) - kPriorities[i]; | 278 int index_in_queue = (kNumPendingRequests - 1) - kPriorities[i]; |
| 375 if (kPriorities[i] < kPriorities[index_to_cancel - kMaxSocketsPerGroup]) | 279 if (kPriorities[i] < kPriorities[index_to_cancel - kMaxSocketsPerGroup]) |
| 376 index_in_queue--; | 280 index_in_queue--; |
| 377 EXPECT_EQ(request_order_[kMaxSocketsPerGroup + index_in_queue], | 281 EXPECT_EQ(request_order_[kMaxSocketsPerGroup + index_in_queue], |
| 378 reqs[kMaxSocketsPerGroup + i].get()) << | 282 reqs[kMaxSocketsPerGroup + i].get()) << |
| 379 "Request " << kMaxSocketsPerGroup + i << " was not in order."; | 283 "Request " << kMaxSocketsPerGroup + i << " was not in order."; |
| 380 } | 284 } |
| 381 | 285 |
| 382 EXPECT_EQ(request_order_[arraysize(reqs) - 2], | 286 EXPECT_EQ(request_order_[arraysize(reqs) - 2], |
| 383 reqs[arraysize(reqs) - 1].get()) << | 287 reqs[arraysize(reqs) - 1].get()) << |
| 384 "The last request with priority 1 should not have been inserted " | 288 "The last request with priority 1 should not have been inserted " |
| 385 "earlier into the queue."; | 289 "earlier into the queue."; |
| 386 } | 290 } |
| 387 | 291 |
| 388 } // namespace | 292 } // namespace |
| 389 | |
| 390 } // namespace net | |
| OLD | NEW |