Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: net/socket/tcp_client_socket_pool_unittest.cc

Issue 146037: Fix crash in ClientSocketPoolBase. (Closed)
Patch Set: Fix formatting. Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/socket/tcp_client_socket_pool.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/tcp_client_socket_pool.h" 5 #include "net/socket/tcp_client_socket_pool.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 "net/base/host_resolver_unittest.h" 9 #include "net/base/host_resolver_unittest.h"
10 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 } 88 }
89 89
90 virtual int Write(IOBuffer* buf, int buf_len, 90 virtual int Write(IOBuffer* buf, int buf_len,
91 CompletionCallback* callback) { 91 CompletionCallback* callback) {
92 return ERR_FAILED; 92 return ERR_FAILED;
93 } 93 }
94 }; 94 };
95 95
96 class MockPendingClientSocket : public ClientSocket { 96 class MockPendingClientSocket : public ClientSocket {
97 public: 97 public:
98 MockPendingClientSocket() 98 MockPendingClientSocket(bool should_connect)
99 : method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} 99 : method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
100 should_connect_(should_connect),
101 is_connected_(false) {}
100 102
101 // ClientSocket methods: 103 // ClientSocket methods:
102 virtual int Connect(CompletionCallback* callback) { 104 virtual int Connect(CompletionCallback* callback) {
103 MessageLoop::current()->PostTask( 105 MessageLoop::current()->PostTask(
104 FROM_HERE, 106 FROM_HERE,
105 method_factory_.NewRunnableMethod( 107 method_factory_.NewRunnableMethod(
106 &MockPendingClientSocket::DoCallback, callback)); 108 &MockPendingClientSocket::DoCallback, callback));
107 return ERR_IO_PENDING; 109 return ERR_IO_PENDING;
108 } 110 }
109 111
110 virtual void Disconnect() {} 112 virtual void Disconnect() {}
111 113
112 virtual bool IsConnected() const { 114 virtual bool IsConnected() const {
113 return false; 115 return is_connected_;
114 } 116 }
115 virtual bool IsConnectedAndIdle() const { 117 virtual bool IsConnectedAndIdle() const {
116 return false; 118 return is_connected_;
117 } 119 }
118 120
119 // Socket methods: 121 // Socket methods:
120 virtual int Read(IOBuffer* buf, int buf_len, 122 virtual int Read(IOBuffer* buf, int buf_len,
121 CompletionCallback* callback) { 123 CompletionCallback* callback) {
122 return ERR_FAILED; 124 return ERR_FAILED;
123 } 125 }
124 126
125 virtual int Write(IOBuffer* buf, int buf_len, 127 virtual int Write(IOBuffer* buf, int buf_len,
126 CompletionCallback* callback) { 128 CompletionCallback* callback) {
127 return ERR_FAILED; 129 return ERR_FAILED;
128 } 130 }
129 131
130 private: 132 private:
131 void DoCallback(CompletionCallback* callback) { 133 void DoCallback(CompletionCallback* callback) {
132 callback->Run(OK); 134 if (should_connect_) {
135 is_connected_ = true;
136 callback->Run(OK);
137 } else {
138 is_connected_ = false;
139 callback->Run(ERR_CONNECTION_FAILED);
140 }
133 } 141 }
134 142
135 ScopedRunnableMethodFactory<MockPendingClientSocket> method_factory_; 143 ScopedRunnableMethodFactory<MockPendingClientSocket> method_factory_;
144 bool should_connect_;
145 bool is_connected_;
136 }; 146 };
137 147
138 class MockClientSocketFactory : public ClientSocketFactory { 148 class MockClientSocketFactory : public ClientSocketFactory {
139 public: 149 public:
140 enum ClientSocketType { 150 enum ClientSocketType {
141 MOCK_CLIENT_SOCKET, 151 MOCK_CLIENT_SOCKET,
142 MOCK_FAILING_CLIENT_SOCKET, 152 MOCK_FAILING_CLIENT_SOCKET,
143 MOCK_PENDING_CLIENT_SOCKET, 153 MOCK_PENDING_CLIENT_SOCKET,
154 MOCK_PENDING_FAILING_CLIENT_SOCKET,
144 }; 155 };
145 156
146 MockClientSocketFactory() 157 MockClientSocketFactory()
147 : allocation_count_(0), client_socket_type_(MOCK_CLIENT_SOCKET) {} 158 : allocation_count_(0), client_socket_type_(MOCK_CLIENT_SOCKET) {}
148 159
149 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses) { 160 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses) {
150 allocation_count_++; 161 allocation_count_++;
151 switch (client_socket_type_) { 162 switch (client_socket_type_) {
152 case MOCK_CLIENT_SOCKET: 163 case MOCK_CLIENT_SOCKET:
153 return new MockClientSocket(); 164 return new MockClientSocket();
154 case MOCK_FAILING_CLIENT_SOCKET: 165 case MOCK_FAILING_CLIENT_SOCKET:
155 return new MockFailingClientSocket(); 166 return new MockFailingClientSocket();
156 case MOCK_PENDING_CLIENT_SOCKET: 167 case MOCK_PENDING_CLIENT_SOCKET:
157 return new MockPendingClientSocket(); 168 return new MockPendingClientSocket(true);
169 case MOCK_PENDING_FAILING_CLIENT_SOCKET:
170 return new MockPendingClientSocket(false);
158 default: 171 default:
159 NOTREACHED(); 172 NOTREACHED();
160 return new MockClientSocket(); 173 return new MockClientSocket();
161 } 174 }
162 } 175 }
163 176
164 virtual SSLClientSocket* CreateSSLClientSocket( 177 virtual SSLClientSocket* CreateSSLClientSocket(
165 ClientSocket* transport_socket, 178 ClientSocket* transport_socket,
166 const std::string& hostname, 179 const std::string& hostname,
167 const SSLConfig& ssl_config) { 180 const SSLConfig& ssl_config) {
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 // Let's wait for the rest to complete now. 589 // Let's wait for the rest to complete now.
577 590
578 for (size_t i = kMaxSocketsPerGroup; i < arraysize(reqs); ++i) { 591 for (size_t i = kMaxSocketsPerGroup; i < arraysize(reqs); ++i) {
579 EXPECT_EQ(OK, reqs[i]->WaitForResult()); 592 EXPECT_EQ(OK, reqs[i]->WaitForResult());
580 reqs[i]->handle.Reset(); 593 reqs[i]->handle.Reset();
581 } 594 }
582 595
583 EXPECT_EQ(kNumPendingRequests, TestSocketRequest::completion_count); 596 EXPECT_EQ(kNumPendingRequests, TestSocketRequest::completion_count);
584 } 597 }
585 598
599 // Make sure that pending requests get serviced after active requests fail.
600 TEST_F(TCPClientSocketPoolTest, FailingActiveRequestWithPendingRequests) {
601 client_socket_factory_.set_client_socket_type(
602 MockClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET);
603
604 scoped_ptr<TestSocketRequest> reqs[kMaxSocketsPerGroup * 2 + 1];
605
606 // Queue up all the requests
607
608 HostResolver::RequestInfo info("www.google.com", 80);
609 for (size_t i = 0; i < arraysize(reqs); ++i) {
610 reqs[i].reset(new TestSocketRequest(pool_.get(), &request_order_));
611 int rv = reqs[i]->handle.Init("a", info, 5, reqs[i].get());
612 EXPECT_EQ(ERR_IO_PENDING, rv);
613 }
614
615 for (size_t i = 0; i < arraysize(reqs); ++i)
616 EXPECT_EQ(ERR_CONNECTION_FAILED, reqs[i]->WaitForResult());
617 }
618
586 } // namespace 619 } // namespace
587 620
588 } // namespace net 621 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/tcp_client_socket_pool.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698