| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "net/socket/tcp_client_socket.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "net/base/address_list.h" | 8 #include "net/base/address_list.h" |
| 9 #include "net/base/host_resolver.h" | 9 #include "net/base/host_resolver.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 connected_sock_->PauseReads(); | 68 connected_sock_->PauseReads(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void ResumeServerReads() { | 71 void ResumeServerReads() { |
| 72 connected_sock_->ResumeReads(); | 72 connected_sock_->ResumeReads(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 int DrainClientSocket(IOBuffer* buf, | 75 int DrainClientSocket(IOBuffer* buf, |
| 76 uint32 buf_len, | 76 uint32 buf_len, |
| 77 uint32 bytes_to_read, | 77 uint32 bytes_to_read, |
| 78 TestOldCompletionCallback* callback); | 78 TestCompletionCallback* callback); |
| 79 | 79 |
| 80 void SendClientRequest(); | 80 void SendClientRequest(); |
| 81 | 81 |
| 82 void set_close_server_socket_on_next_send(bool close) { | 82 void set_close_server_socket_on_next_send(bool close) { |
| 83 close_server_socket_on_next_send_ = close; | 83 close_server_socket_on_next_send_ = close; |
| 84 } | 84 } |
| 85 | 85 |
| 86 protected: | 86 protected: |
| 87 int listen_port_; | 87 int listen_port_; |
| 88 CapturingNetLog net_log_; | 88 CapturingNetLog net_log_; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 rv = callback.WaitForResult(); | 129 rv = callback.WaitForResult(); |
| 130 CHECK_EQ(rv, OK); | 130 CHECK_EQ(rv, OK); |
| 131 sock_.reset( | 131 sock_.reset( |
| 132 socket_factory_->CreateTransportClientSocket(addr, | 132 socket_factory_->CreateTransportClientSocket(addr, |
| 133 &net_log_, | 133 &net_log_, |
| 134 NetLog::Source())); | 134 NetLog::Source())); |
| 135 } | 135 } |
| 136 | 136 |
| 137 int TransportClientSocketTest::DrainClientSocket( | 137 int TransportClientSocketTest::DrainClientSocket( |
| 138 IOBuffer* buf, uint32 buf_len, | 138 IOBuffer* buf, uint32 buf_len, |
| 139 uint32 bytes_to_read, TestOldCompletionCallback* callback) { | 139 uint32 bytes_to_read, TestCompletionCallback* callback) { |
| 140 int rv = OK; | 140 int rv = OK; |
| 141 uint32 bytes_read = 0; | 141 uint32 bytes_read = 0; |
| 142 | 142 |
| 143 while (bytes_read < bytes_to_read) { | 143 while (bytes_read < bytes_to_read) { |
| 144 rv = sock_->Read(buf, buf_len, callback); | 144 rv = sock_->Read(buf, buf_len, callback->callback()); |
| 145 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 145 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 146 | 146 |
| 147 if (rv == ERR_IO_PENDING) | 147 if (rv == ERR_IO_PENDING) |
| 148 rv = callback->WaitForResult(); | 148 rv = callback->WaitForResult(); |
| 149 | 149 |
| 150 EXPECT_GE(rv, 0); | 150 EXPECT_GE(rv, 0); |
| 151 bytes_read += rv; | 151 bytes_read += rv; |
| 152 } | 152 } |
| 153 | 153 |
| 154 return static_cast<int>(bytes_read); | 154 return static_cast<int>(bytes_read); |
| 155 } | 155 } |
| 156 | 156 |
| 157 void TransportClientSocketTest::SendClientRequest() { | 157 void TransportClientSocketTest::SendClientRequest() { |
| 158 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 158 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 159 scoped_refptr<IOBuffer> request_buffer( | 159 scoped_refptr<IOBuffer> request_buffer( |
| 160 new IOBuffer(arraysize(request_text) - 1)); | 160 new IOBuffer(arraysize(request_text) - 1)); |
| 161 TestOldCompletionCallback callback; | 161 TestCompletionCallback callback; |
| 162 int rv; | 162 int rv; |
| 163 | 163 |
| 164 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 164 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 165 rv = sock_->Write(request_buffer, arraysize(request_text) - 1, &callback); | 165 rv = sock_->Write(request_buffer, arraysize(request_text) - 1, |
| 166 callback.callback()); |
| 166 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 167 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 167 | 168 |
| 168 if (rv == ERR_IO_PENDING) | 169 if (rv == ERR_IO_PENDING) |
| 169 rv = callback.WaitForResult(); | 170 rv = callback.WaitForResult(); |
| 170 EXPECT_EQ(rv, static_cast<int>(arraysize(request_text) - 1)); | 171 EXPECT_EQ(rv, static_cast<int>(arraysize(request_text) - 1)); |
| 171 } | 172 } |
| 172 | 173 |
| 173 // TODO(leighton): Add SCTP to this list when it is ready. | 174 // TODO(leighton): Add SCTP to this list when it is ready. |
| 174 INSTANTIATE_TEST_CASE_P(StreamSocket, | 175 INSTANTIATE_TEST_CASE_P(StreamSocket, |
| 175 TransportClientSocketTest, | 176 TransportClientSocketTest, |
| 176 ::testing::Values(TCP)); | 177 ::testing::Values(TCP)); |
| 177 | 178 |
| 178 TEST_P(TransportClientSocketTest, Connect) { | 179 TEST_P(TransportClientSocketTest, Connect) { |
| 179 TestOldCompletionCallback callback; | 180 TestCompletionCallback callback; |
| 180 EXPECT_FALSE(sock_->IsConnected()); | 181 EXPECT_FALSE(sock_->IsConnected()); |
| 181 | 182 |
| 182 int rv = sock_->Connect(&callback); | 183 int rv = sock_->Connect(callback.callback()); |
| 183 | 184 |
| 184 net::CapturingNetLog::EntryList net_log_entries; | 185 net::CapturingNetLog::EntryList net_log_entries; |
| 185 net_log_.GetEntries(&net_log_entries); | 186 net_log_.GetEntries(&net_log_entries); |
| 186 EXPECT_TRUE(net::LogContainsBeginEvent( | 187 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 187 net_log_entries, 0, net::NetLog::TYPE_SOCKET_ALIVE)); | 188 net_log_entries, 0, net::NetLog::TYPE_SOCKET_ALIVE)); |
| 188 EXPECT_TRUE(net::LogContainsBeginEvent( | 189 EXPECT_TRUE(net::LogContainsBeginEvent( |
| 189 net_log_entries, 1, net::NetLog::TYPE_TCP_CONNECT)); | 190 net_log_entries, 1, net::NetLog::TYPE_TCP_CONNECT)); |
| 190 if (rv != OK) { | 191 if (rv != OK) { |
| 191 ASSERT_EQ(rv, ERR_IO_PENDING); | 192 ASSERT_EQ(rv, ERR_IO_PENDING); |
| 192 rv = callback.WaitForResult(); | 193 rv = callback.WaitForResult(); |
| 193 EXPECT_EQ(rv, OK); | 194 EXPECT_EQ(rv, OK); |
| 194 } | 195 } |
| 195 | 196 |
| 196 EXPECT_TRUE(sock_->IsConnected()); | 197 EXPECT_TRUE(sock_->IsConnected()); |
| 197 net_log_.GetEntries(&net_log_entries); | 198 net_log_.GetEntries(&net_log_entries); |
| 198 EXPECT_TRUE(net::LogContainsEndEvent( | 199 EXPECT_TRUE(net::LogContainsEndEvent( |
| 199 net_log_entries, -1, net::NetLog::TYPE_TCP_CONNECT)); | 200 net_log_entries, -1, net::NetLog::TYPE_TCP_CONNECT)); |
| 200 | 201 |
| 201 sock_->Disconnect(); | 202 sock_->Disconnect(); |
| 202 EXPECT_FALSE(sock_->IsConnected()); | 203 EXPECT_FALSE(sock_->IsConnected()); |
| 203 } | 204 } |
| 204 | 205 |
| 205 TEST_P(TransportClientSocketTest, IsConnected) { | 206 TEST_P(TransportClientSocketTest, IsConnected) { |
| 206 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 207 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
| 207 TestOldCompletionCallback callback; | 208 TestCompletionCallback callback; |
| 208 uint32 bytes_read; | 209 uint32 bytes_read; |
| 209 | 210 |
| 210 EXPECT_FALSE(sock_->IsConnected()); | 211 EXPECT_FALSE(sock_->IsConnected()); |
| 211 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | 212 EXPECT_FALSE(sock_->IsConnectedAndIdle()); |
| 212 int rv = sock_->Connect(&callback); | 213 int rv = sock_->Connect(callback.callback()); |
| 213 if (rv != OK) { | 214 if (rv != OK) { |
| 214 ASSERT_EQ(rv, ERR_IO_PENDING); | 215 ASSERT_EQ(rv, ERR_IO_PENDING); |
| 215 rv = callback.WaitForResult(); | 216 rv = callback.WaitForResult(); |
| 216 EXPECT_EQ(rv, OK); | 217 EXPECT_EQ(rv, OK); |
| 217 } | 218 } |
| 218 EXPECT_TRUE(sock_->IsConnected()); | 219 EXPECT_TRUE(sock_->IsConnected()); |
| 219 EXPECT_TRUE(sock_->IsConnectedAndIdle()); | 220 EXPECT_TRUE(sock_->IsConnectedAndIdle()); |
| 220 | 221 |
| 221 // Send the request and wait for the server to respond. | 222 // Send the request and wait for the server to respond. |
| 222 SendClientRequest(); | 223 SendClientRequest(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 &callback); | 255 &callback); |
| 255 ASSERT_EQ(bytes_read, arraysize(kServerReply) - 2); | 256 ASSERT_EQ(bytes_read, arraysize(kServerReply) - 2); |
| 256 | 257 |
| 257 // Once the data is drained, the socket should now be seen as | 258 // Once the data is drained, the socket should now be seen as |
| 258 // closed. | 259 // closed. |
| 259 EXPECT_FALSE(sock_->IsConnected()); | 260 EXPECT_FALSE(sock_->IsConnected()); |
| 260 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | 261 EXPECT_FALSE(sock_->IsConnectedAndIdle()); |
| 261 } | 262 } |
| 262 | 263 |
| 263 TEST_P(TransportClientSocketTest, Read) { | 264 TEST_P(TransportClientSocketTest, Read) { |
| 264 TestOldCompletionCallback callback; | 265 TestCompletionCallback callback; |
| 265 int rv = sock_->Connect(&callback); | 266 int rv = sock_->Connect(callback.callback()); |
| 266 if (rv != OK) { | 267 if (rv != OK) { |
| 267 ASSERT_EQ(rv, ERR_IO_PENDING); | 268 ASSERT_EQ(rv, ERR_IO_PENDING); |
| 268 | 269 |
| 269 rv = callback.WaitForResult(); | 270 rv = callback.WaitForResult(); |
| 270 EXPECT_EQ(rv, OK); | 271 EXPECT_EQ(rv, OK); |
| 271 } | 272 } |
| 272 SendClientRequest(); | 273 SendClientRequest(); |
| 273 | 274 |
| 274 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 275 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
| 275 uint32 bytes_read = DrainClientSocket(buf, 4096, arraysize(kServerReply) - 1, | 276 uint32 bytes_read = DrainClientSocket(buf, 4096, arraysize(kServerReply) - 1, |
| 276 &callback); | 277 &callback); |
| 277 ASSERT_EQ(bytes_read, arraysize(kServerReply) - 1); | 278 ASSERT_EQ(bytes_read, arraysize(kServerReply) - 1); |
| 278 | 279 |
| 279 // All data has been read now. Read once more to force an ERR_IO_PENDING, and | 280 // All data has been read now. Read once more to force an ERR_IO_PENDING, and |
| 280 // then close the server socket, and note the close. | 281 // then close the server socket, and note the close. |
| 281 | 282 |
| 282 rv = sock_->Read(buf, 4096, &callback); | 283 rv = sock_->Read(buf, 4096, callback.callback()); |
| 283 ASSERT_EQ(ERR_IO_PENDING, rv); | 284 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 284 EXPECT_EQ(static_cast<int64>(std::string(kServerReply).size()), | 285 EXPECT_EQ(static_cast<int64>(std::string(kServerReply).size()), |
| 285 sock_->NumBytesRead()); | 286 sock_->NumBytesRead()); |
| 286 CloseServerSocket(); | 287 CloseServerSocket(); |
| 287 EXPECT_EQ(0, callback.WaitForResult()); | 288 EXPECT_EQ(0, callback.WaitForResult()); |
| 288 } | 289 } |
| 289 | 290 |
| 290 TEST_P(TransportClientSocketTest, Read_SmallChunks) { | 291 TEST_P(TransportClientSocketTest, Read_SmallChunks) { |
| 291 TestOldCompletionCallback callback; | 292 TestCompletionCallback callback; |
| 292 int rv = sock_->Connect(&callback); | 293 int rv = sock_->Connect(callback.callback()); |
| 293 if (rv != OK) { | 294 if (rv != OK) { |
| 294 ASSERT_EQ(rv, ERR_IO_PENDING); | 295 ASSERT_EQ(rv, ERR_IO_PENDING); |
| 295 | 296 |
| 296 rv = callback.WaitForResult(); | 297 rv = callback.WaitForResult(); |
| 297 EXPECT_EQ(rv, OK); | 298 EXPECT_EQ(rv, OK); |
| 298 } | 299 } |
| 299 SendClientRequest(); | 300 SendClientRequest(); |
| 300 | 301 |
| 301 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); | 302 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); |
| 302 uint32 bytes_read = 0; | 303 uint32 bytes_read = 0; |
| 303 while (bytes_read < arraysize(kServerReply) - 1) { | 304 while (bytes_read < arraysize(kServerReply) - 1) { |
| 304 rv = sock_->Read(buf, 1, &callback); | 305 rv = sock_->Read(buf, 1, callback.callback()); |
| 305 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 306 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 306 | 307 |
| 307 if (rv == ERR_IO_PENDING) | 308 if (rv == ERR_IO_PENDING) |
| 308 rv = callback.WaitForResult(); | 309 rv = callback.WaitForResult(); |
| 309 | 310 |
| 310 ASSERT_EQ(1, rv); | 311 ASSERT_EQ(1, rv); |
| 311 bytes_read += rv; | 312 bytes_read += rv; |
| 312 } | 313 } |
| 313 | 314 |
| 314 // All data has been read now. Read once more to force an ERR_IO_PENDING, and | 315 // All data has been read now. Read once more to force an ERR_IO_PENDING, and |
| 315 // then close the server socket, and note the close. | 316 // then close the server socket, and note the close. |
| 316 | 317 |
| 317 rv = sock_->Read(buf, 1, &callback); | 318 rv = sock_->Read(buf, 1, callback.callback()); |
| 318 EXPECT_EQ(static_cast<int64>(std::string(kServerReply).size()), | 319 EXPECT_EQ(static_cast<int64>(std::string(kServerReply).size()), |
| 319 sock_->NumBytesRead()); | 320 sock_->NumBytesRead()); |
| 320 ASSERT_EQ(ERR_IO_PENDING, rv); | 321 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 321 CloseServerSocket(); | 322 CloseServerSocket(); |
| 322 EXPECT_EQ(0, callback.WaitForResult()); | 323 EXPECT_EQ(0, callback.WaitForResult()); |
| 323 } | 324 } |
| 324 | 325 |
| 325 TEST_P(TransportClientSocketTest, Read_Interrupted) { | 326 TEST_P(TransportClientSocketTest, Read_Interrupted) { |
| 326 TestOldCompletionCallback callback; | 327 TestCompletionCallback callback; |
| 327 int rv = sock_->Connect(&callback); | 328 int rv = sock_->Connect(callback.callback()); |
| 328 if (rv != OK) { | 329 if (rv != OK) { |
| 329 ASSERT_EQ(ERR_IO_PENDING, rv); | 330 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 330 | 331 |
| 331 rv = callback.WaitForResult(); | 332 rv = callback.WaitForResult(); |
| 332 EXPECT_EQ(rv, OK); | 333 EXPECT_EQ(rv, OK); |
| 333 } | 334 } |
| 334 SendClientRequest(); | 335 SendClientRequest(); |
| 335 | 336 |
| 336 // Do a partial read and then exit. This test should not crash! | 337 // Do a partial read and then exit. This test should not crash! |
| 337 scoped_refptr<IOBuffer> buf(new IOBuffer(16)); | 338 scoped_refptr<IOBuffer> buf(new IOBuffer(16)); |
| 338 rv = sock_->Read(buf, 16, &callback); | 339 rv = sock_->Read(buf, 16, callback.callback()); |
| 339 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 340 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 340 EXPECT_EQ(0, sock_->NumBytesRead()); | 341 EXPECT_EQ(0, sock_->NumBytesRead()); |
| 341 | 342 |
| 342 if (rv == ERR_IO_PENDING) { | 343 if (rv == ERR_IO_PENDING) { |
| 343 rv = callback.WaitForResult(); | 344 rv = callback.WaitForResult(); |
| 344 EXPECT_EQ(16, sock_->NumBytesRead()); | 345 EXPECT_EQ(16, sock_->NumBytesRead()); |
| 345 } | 346 } |
| 346 | 347 |
| 347 EXPECT_NE(0, rv); | 348 EXPECT_NE(0, rv); |
| 348 } | 349 } |
| 349 | 350 |
| 350 TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_ReadFirst) { | 351 TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_ReadFirst) { |
| 351 TestOldCompletionCallback callback; | 352 TestCompletionCallback callback; |
| 352 int rv = sock_->Connect(&callback); | 353 int rv = sock_->Connect(callback.callback()); |
| 353 if (rv != OK) { | 354 if (rv != OK) { |
| 354 ASSERT_EQ(rv, ERR_IO_PENDING); | 355 ASSERT_EQ(rv, ERR_IO_PENDING); |
| 355 | 356 |
| 356 rv = callback.WaitForResult(); | 357 rv = callback.WaitForResult(); |
| 357 EXPECT_EQ(rv, OK); | 358 EXPECT_EQ(rv, OK); |
| 358 } | 359 } |
| 359 | 360 |
| 360 // Read first. There's no data, so it should return ERR_IO_PENDING. | 361 // Read first. There's no data, so it should return ERR_IO_PENDING. |
| 361 const int kBufLen = 4096; | 362 const int kBufLen = 4096; |
| 362 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); | 363 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); |
| 363 rv = sock_->Read(buf, kBufLen, &callback); | 364 rv = sock_->Read(buf, kBufLen, callback.callback()); |
| 364 EXPECT_EQ(ERR_IO_PENDING, rv); | 365 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 365 | 366 |
| 366 PauseServerReads(); | 367 PauseServerReads(); |
| 367 const int kWriteBufLen = 64 * 1024; | 368 const int kWriteBufLen = 64 * 1024; |
| 368 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); | 369 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); |
| 369 char* request_data = request_buffer->data(); | 370 char* request_data = request_buffer->data(); |
| 370 memset(request_data, 'A', kWriteBufLen); | 371 memset(request_data, 'A', kWriteBufLen); |
| 371 TestOldCompletionCallback write_callback; | 372 TestCompletionCallback write_callback; |
| 372 | 373 |
| 373 while (true) { | 374 while (true) { |
| 374 rv = sock_->Write(request_buffer, kWriteBufLen, &write_callback); | 375 rv = sock_->Write(request_buffer, kWriteBufLen, write_callback.callback()); |
| 375 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 376 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 376 | 377 |
| 377 if (rv == ERR_IO_PENDING) { | 378 if (rv == ERR_IO_PENDING) { |
| 378 ResumeServerReads(); | 379 ResumeServerReads(); |
| 379 rv = write_callback.WaitForResult(); | 380 rv = write_callback.WaitForResult(); |
| 380 break; | 381 break; |
| 381 } | 382 } |
| 382 } | 383 } |
| 383 | 384 |
| 384 // At this point, both read and write have returned ERR_IO_PENDING, and the | 385 // At this point, both read and write have returned ERR_IO_PENDING, and the |
| 385 // write callback has executed. We wait for the read callback to run now to | 386 // write callback has executed. We wait for the read callback to run now to |
| 386 // make sure that the socket can handle full duplex communications. | 387 // make sure that the socket can handle full duplex communications. |
| 387 | 388 |
| 388 rv = callback.WaitForResult(); | 389 rv = callback.WaitForResult(); |
| 389 EXPECT_GE(rv, 0); | 390 EXPECT_GE(rv, 0); |
| 390 } | 391 } |
| 391 | 392 |
| 392 TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_WriteFirst) { | 393 TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_WriteFirst) { |
| 393 TestOldCompletionCallback callback; | 394 TestCompletionCallback callback; |
| 394 int rv = sock_->Connect(&callback); | 395 int rv = sock_->Connect(callback.callback()); |
| 395 if (rv != OK) { | 396 if (rv != OK) { |
| 396 ASSERT_EQ(ERR_IO_PENDING, rv); | 397 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 397 | 398 |
| 398 rv = callback.WaitForResult(); | 399 rv = callback.WaitForResult(); |
| 399 EXPECT_EQ(OK, rv); | 400 EXPECT_EQ(OK, rv); |
| 400 } | 401 } |
| 401 | 402 |
| 402 PauseServerReads(); | 403 PauseServerReads(); |
| 403 const int kWriteBufLen = 64 * 1024; | 404 const int kWriteBufLen = 64 * 1024; |
| 404 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); | 405 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); |
| 405 char* request_data = request_buffer->data(); | 406 char* request_data = request_buffer->data(); |
| 406 memset(request_data, 'A', kWriteBufLen); | 407 memset(request_data, 'A', kWriteBufLen); |
| 407 TestOldCompletionCallback write_callback; | 408 TestCompletionCallback write_callback; |
| 408 | 409 |
| 409 while (true) { | 410 while (true) { |
| 410 rv = sock_->Write(request_buffer, kWriteBufLen, &write_callback); | 411 rv = sock_->Write(request_buffer, kWriteBufLen, write_callback.callback()); |
| 411 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 412 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 412 | 413 |
| 413 if (rv == ERR_IO_PENDING) | 414 if (rv == ERR_IO_PENDING) |
| 414 break; | 415 break; |
| 415 } | 416 } |
| 416 | 417 |
| 417 // Now we have the Write() blocked on ERR_IO_PENDING. It's time to force the | 418 // Now we have the Write() blocked on ERR_IO_PENDING. It's time to force the |
| 418 // Read() to block on ERR_IO_PENDING too. | 419 // Read() to block on ERR_IO_PENDING too. |
| 419 | 420 |
| 420 const int kBufLen = 4096; | 421 const int kBufLen = 4096; |
| 421 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); | 422 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); |
| 422 while (true) { | 423 while (true) { |
| 423 rv = sock_->Read(buf, kBufLen, &callback); | 424 rv = sock_->Read(buf, kBufLen, callback.callback()); |
| 424 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 425 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 425 if (rv == ERR_IO_PENDING) | 426 if (rv == ERR_IO_PENDING) |
| 426 break; | 427 break; |
| 427 } | 428 } |
| 428 | 429 |
| 429 // At this point, both read and write have returned ERR_IO_PENDING. Now we | 430 // At this point, both read and write have returned ERR_IO_PENDING. Now we |
| 430 // run the write and read callbacks to make sure they can handle full duplex | 431 // run the write and read callbacks to make sure they can handle full duplex |
| 431 // communications. | 432 // communications. |
| 432 | 433 |
| 433 ResumeServerReads(); | 434 ResumeServerReads(); |
| 434 rv = write_callback.WaitForResult(); | 435 rv = write_callback.WaitForResult(); |
| 435 EXPECT_GE(rv, 0); | 436 EXPECT_GE(rv, 0); |
| 436 | 437 |
| 437 // It's possible the read is blocked because it's already read all the data. | 438 // It's possible the read is blocked because it's already read all the data. |
| 438 // Close the server socket, so there will at least be a 0-byte read. | 439 // Close the server socket, so there will at least be a 0-byte read. |
| 439 CloseServerSocket(); | 440 CloseServerSocket(); |
| 440 | 441 |
| 441 rv = callback.WaitForResult(); | 442 rv = callback.WaitForResult(); |
| 442 EXPECT_GE(rv, 0); | 443 EXPECT_GE(rv, 0); |
| 443 } | 444 } |
| 444 | 445 |
| 445 } // namespace | 446 } // namespace |
| 446 | 447 |
| 447 } // namespace net | 448 } // namespace net |
| OLD | NEW |