| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "jingle/glue/pseudotcp_adapter.h" | 5 #include "jingle/glue/pseudotcp_adapter.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 read_buffer_ = buf; | 129 read_buffer_ = buf; |
| 130 read_buffer_size_ = buf_len; | 130 read_buffer_size_ = buf_len; |
| 131 return net::ERR_IO_PENDING; | 131 return net::ERR_IO_PENDING; |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 virtual int Write(net::IOBuffer* buf, int buf_len, | 135 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 136 const net::CompletionCallback& callback) OVERRIDE { | 136 const net::CompletionCallback& callback) OVERRIDE { |
| 137 DCHECK(buf); | 137 DCHECK(buf); |
| 138 if (peer_socket_) { | 138 if (peer_socket_) { |
| 139 MessageLoop::current()->PostDelayedTask( | 139 base::MessageLoop::current()->PostDelayedTask( |
| 140 FROM_HERE, | 140 FROM_HERE, |
| 141 base::Bind(&FakeSocket::AppendInputPacket, | 141 base::Bind(&FakeSocket::AppendInputPacket, |
| 142 base::Unretained(peer_socket_), | 142 base::Unretained(peer_socket_), |
| 143 std::vector<char>(buf->data(), buf->data() + buf_len)), | 143 std::vector<char>(buf->data(), buf->data() + buf_len)), |
| 144 base::TimeDelta::FromMilliseconds(latency_ms_)); | 144 base::TimeDelta::FromMilliseconds(latency_ms_)); |
| 145 } | 145 } |
| 146 | 146 |
| 147 return buf_len; | 147 return buf_len; |
| 148 } | 148 } |
| 149 | 149 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 163 | 163 |
| 164 std::deque<std::vector<char> > incoming_packets_; | 164 std::deque<std::vector<char> > incoming_packets_; |
| 165 | 165 |
| 166 FakeSocket* peer_socket_; | 166 FakeSocket* peer_socket_; |
| 167 RateLimiter* rate_limiter_; | 167 RateLimiter* rate_limiter_; |
| 168 int latency_ms_; | 168 int latency_ms_; |
| 169 }; | 169 }; |
| 170 | 170 |
| 171 class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { | 171 class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { |
| 172 public: | 172 public: |
| 173 TCPChannelTester(MessageLoop* message_loop, | 173 TCPChannelTester(base::MessageLoop* message_loop, |
| 174 net::Socket* client_socket, | 174 net::Socket* client_socket, |
| 175 net::Socket* host_socket) | 175 net::Socket* host_socket) |
| 176 : message_loop_(message_loop), | 176 : message_loop_(message_loop), |
| 177 host_socket_(host_socket), | 177 host_socket_(host_socket), |
| 178 client_socket_(client_socket), | 178 client_socket_(client_socket), |
| 179 done_(false), | 179 done_(false), |
| 180 write_errors_(0), | 180 write_errors_(0), |
| 181 read_errors_(0) { | 181 read_errors_(0) {} |
| 182 } | |
| 183 | 182 |
| 184 void Start() { | 183 void Start() { |
| 185 message_loop_->PostTask( | 184 message_loop_->PostTask( |
| 186 FROM_HERE, base::Bind(&TCPChannelTester::DoStart, this)); | 185 FROM_HERE, base::Bind(&TCPChannelTester::DoStart, this)); |
| 187 } | 186 } |
| 188 | 187 |
| 189 void CheckResults() { | 188 void CheckResults() { |
| 190 EXPECT_EQ(0, write_errors_); | 189 EXPECT_EQ(0, write_errors_); |
| 191 EXPECT_EQ(0, read_errors_); | 190 EXPECT_EQ(0, read_errors_); |
| 192 | 191 |
| 193 ASSERT_EQ(kTestDataSize + kMessageSize, input_buffer_->capacity()); | 192 ASSERT_EQ(kTestDataSize + kMessageSize, input_buffer_->capacity()); |
| 194 | 193 |
| 195 output_buffer_->SetOffset(0); | 194 output_buffer_->SetOffset(0); |
| 196 ASSERT_EQ(kTestDataSize, output_buffer_->size()); | 195 ASSERT_EQ(kTestDataSize, output_buffer_->size()); |
| 197 | 196 |
| 198 EXPECT_EQ(0, memcmp(output_buffer_->data(), | 197 EXPECT_EQ(0, memcmp(output_buffer_->data(), |
| 199 input_buffer_->StartOfBuffer(), kTestDataSize)); | 198 input_buffer_->StartOfBuffer(), kTestDataSize)); |
| 200 } | 199 } |
| 201 | 200 |
| 202 protected: | 201 protected: |
| 203 virtual ~TCPChannelTester() {} | 202 virtual ~TCPChannelTester() {} |
| 204 | 203 |
| 205 void Done() { | 204 void Done() { |
| 206 done_ = true; | 205 done_ = true; |
| 207 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 206 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
| 208 } | 207 } |
| 209 | 208 |
| 210 void DoStart() { | 209 void DoStart() { |
| 211 InitBuffers(); | 210 InitBuffers(); |
| 212 DoRead(); | 211 DoRead(); |
| 213 DoWrite(); | 212 DoWrite(); |
| 214 } | 213 } |
| 215 | 214 |
| 216 void InitBuffers() { | 215 void InitBuffers() { |
| 217 output_buffer_ = new net::DrainableIOBuffer( | 216 output_buffer_ = new net::DrainableIOBuffer( |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 // Allocate memory for the next read. | 280 // Allocate memory for the next read. |
| 282 input_buffer_->SetCapacity(input_buffer_->capacity() + result); | 281 input_buffer_->SetCapacity(input_buffer_->capacity() + result); |
| 283 if (input_buffer_->capacity() == kTestDataSize + kMessageSize) | 282 if (input_buffer_->capacity() == kTestDataSize + kMessageSize) |
| 284 Done(); | 283 Done(); |
| 285 } | 284 } |
| 286 } | 285 } |
| 287 | 286 |
| 288 private: | 287 private: |
| 289 friend class base::RefCountedThreadSafe<TCPChannelTester>; | 288 friend class base::RefCountedThreadSafe<TCPChannelTester>; |
| 290 | 289 |
| 291 MessageLoop* message_loop_; | 290 base::MessageLoop* message_loop_; |
| 292 net::Socket* host_socket_; | 291 net::Socket* host_socket_; |
| 293 net::Socket* client_socket_; | 292 net::Socket* client_socket_; |
| 294 bool done_; | 293 bool done_; |
| 295 | 294 |
| 296 scoped_refptr<net::DrainableIOBuffer> output_buffer_; | 295 scoped_refptr<net::DrainableIOBuffer> output_buffer_; |
| 297 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | 296 scoped_refptr<net::GrowableIOBuffer> input_buffer_; |
| 298 | 297 |
| 299 int write_errors_; | 298 int write_errors_; |
| 300 int read_errors_; | 299 int read_errors_; |
| 301 }; | 300 }; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 313 | 312 |
| 314 host_pseudotcp_.reset(new PseudoTcpAdapter(host_socket_)); | 313 host_pseudotcp_.reset(new PseudoTcpAdapter(host_socket_)); |
| 315 client_pseudotcp_.reset(new PseudoTcpAdapter(client_socket_)); | 314 client_pseudotcp_.reset(new PseudoTcpAdapter(client_socket_)); |
| 316 } | 315 } |
| 317 | 316 |
| 318 FakeSocket* host_socket_; | 317 FakeSocket* host_socket_; |
| 319 FakeSocket* client_socket_; | 318 FakeSocket* client_socket_; |
| 320 | 319 |
| 321 scoped_ptr<PseudoTcpAdapter> host_pseudotcp_; | 320 scoped_ptr<PseudoTcpAdapter> host_pseudotcp_; |
| 322 scoped_ptr<PseudoTcpAdapter> client_pseudotcp_; | 321 scoped_ptr<PseudoTcpAdapter> client_pseudotcp_; |
| 323 MessageLoop message_loop_; | 322 base::MessageLoop message_loop_; |
| 324 }; | 323 }; |
| 325 | 324 |
| 326 TEST_F(PseudoTcpAdapterTest, DataTransfer) { | 325 TEST_F(PseudoTcpAdapterTest, DataTransfer) { |
| 327 net::TestCompletionCallback host_connect_cb; | 326 net::TestCompletionCallback host_connect_cb; |
| 328 net::TestCompletionCallback client_connect_cb; | 327 net::TestCompletionCallback client_connect_cb; |
| 329 | 328 |
| 330 int rv1 = host_pseudotcp_->Connect(host_connect_cb.callback()); | 329 int rv1 = host_pseudotcp_->Connect(host_connect_cb.callback()); |
| 331 int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); | 330 int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); |
| 332 | 331 |
| 333 if (rv1 == net::ERR_IO_PENDING) | 332 if (rv1 == net::ERR_IO_PENDING) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 new TCPChannelTester(&message_loop_, host_pseudotcp_.get(), | 375 new TCPChannelTester(&message_loop_, host_pseudotcp_.get(), |
| 377 client_pseudotcp_.get()); | 376 client_pseudotcp_.get()); |
| 378 | 377 |
| 379 tester->Start(); | 378 tester->Start(); |
| 380 message_loop_.Run(); | 379 message_loop_.Run(); |
| 381 tester->CheckResults(); | 380 tester->CheckResults(); |
| 382 } | 381 } |
| 383 | 382 |
| 384 class DeleteOnConnected { | 383 class DeleteOnConnected { |
| 385 public: | 384 public: |
| 386 DeleteOnConnected(MessageLoop* message_loop, | 385 DeleteOnConnected(base::MessageLoop* message_loop, |
| 387 scoped_ptr<PseudoTcpAdapter>* adapter) | 386 scoped_ptr<PseudoTcpAdapter>* adapter) |
| 388 : message_loop_(message_loop), adapter_(adapter) {} | 387 : message_loop_(message_loop), adapter_(adapter) {} |
| 389 void OnConnected(int error) { | 388 void OnConnected(int error) { |
| 390 adapter_->reset(); | 389 adapter_->reset(); |
| 391 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 390 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
| 392 } | 391 } |
| 393 MessageLoop* message_loop_; | 392 base::MessageLoop* message_loop_; |
| 394 scoped_ptr<PseudoTcpAdapter>* adapter_; | 393 scoped_ptr<PseudoTcpAdapter>* adapter_; |
| 395 }; | 394 }; |
| 396 | 395 |
| 397 TEST_F(PseudoTcpAdapterTest, DeleteOnConnected) { | 396 TEST_F(PseudoTcpAdapterTest, DeleteOnConnected) { |
| 398 // This test verifies that deleting the adapter mid-callback doesn't lead | 397 // This test verifies that deleting the adapter mid-callback doesn't lead |
| 399 // to deleted structures being touched as the stack unrolls, so the failure | 398 // to deleted structures being touched as the stack unrolls, so the failure |
| 400 // mode is a crash rather than a normal test failure. | 399 // mode is a crash rather than a normal test failure. |
| 401 net::TestCompletionCallback client_connect_cb; | 400 net::TestCompletionCallback client_connect_cb; |
| 402 DeleteOnConnected host_delete(&message_loop_, &host_pseudotcp_); | 401 DeleteOnConnected host_delete(&message_loop_, &host_pseudotcp_); |
| 403 | 402 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 client_pseudotcp_.get()); | 436 client_pseudotcp_.get()); |
| 438 | 437 |
| 439 tester->Start(); | 438 tester->Start(); |
| 440 message_loop_.Run(); | 439 message_loop_.Run(); |
| 441 tester->CheckResults(); | 440 tester->CheckResults(); |
| 442 } | 441 } |
| 443 | 442 |
| 444 } // namespace | 443 } // namespace |
| 445 | 444 |
| 446 } // namespace jingle_glue | 445 } // namespace jingle_glue |
| OLD | NEW |