| 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 "net/quic/quic_session.h" | 5 #include "net/quic/quic_session.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 12 #include "net/quic/crypto/crypto_protocol.h" | 12 #include "net/quic/crypto/crypto_protocol.h" |
| 13 #include "net/quic/quic_crypto_stream.h" | 13 #include "net/quic/quic_crypto_stream.h" |
| 14 #include "net/quic/quic_flags.h" | 14 #include "net/quic/quic_flags.h" |
| 15 #include "net/quic/quic_protocol.h" | 15 #include "net/quic/quic_protocol.h" |
| 16 #include "net/quic/quic_utils.h" | 16 #include "net/quic/quic_utils.h" |
| 17 #include "net/quic/reliable_quic_stream.h" | 17 #include "net/quic/reliable_quic_stream.h" |
| 18 #include "net/quic/test_tools/quic_connection_peer.h" | 18 #include "net/quic/test_tools/quic_connection_peer.h" |
| 19 #include "net/quic/test_tools/quic_data_stream_peer.h" | 19 #include "net/quic/test_tools/quic_data_stream_peer.h" |
| 20 #include "net/quic/test_tools/quic_flow_controller_peer.h" | 20 #include "net/quic/test_tools/quic_flow_controller_peer.h" |
| 21 #include "net/quic/test_tools/quic_session_peer.h" | 21 #include "net/quic/test_tools/quic_session_peer.h" |
| 22 #include "net/quic/test_tools/quic_test_utils.h" | 22 #include "net/quic/test_tools/quic_test_utils.h" |
| 23 #include "net/quic/test_tools/reliable_quic_stream_peer.h" | 23 #include "net/quic/test_tools/reliable_quic_stream_peer.h" |
| 24 #include "net/spdy/spdy_framer.h" | 24 #include "net/spdy/spdy_framer.h" |
| 25 #include "net/test/gtest_util.h" | 25 #include "net/test/gtest_util.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "testing/gmock_mutant.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 29 |
| 29 using base::hash_map; | 30 using base::hash_map; |
| 30 using std::set; | 31 using std::set; |
| 31 using std::vector; | 32 using std::vector; |
| 33 using testing::CreateFunctor; |
| 32 using testing::InSequence; | 34 using testing::InSequence; |
| 33 using testing::InvokeWithoutArgs; | 35 using testing::Invoke; |
| 34 using testing::Return; | 36 using testing::Return; |
| 35 using testing::StrictMock; | 37 using testing::StrictMock; |
| 36 using testing::_; | 38 using testing::_; |
| 37 | 39 |
| 38 namespace net { | 40 namespace net { |
| 39 namespace test { | 41 namespace test { |
| 40 namespace { | 42 namespace { |
| 41 | 43 |
| 42 const QuicPriority kHighestPriority = 0; | 44 const QuicPriority kHighestPriority = 0; |
| 43 const QuicPriority kSomeMiddlePriority = 3; | 45 const QuicPriority kSomeMiddlePriority = 3; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 } else { | 157 } else { |
| 156 return QuicSession::WritevData(id, data, offset, fin, | 158 return QuicSession::WritevData(id, data, offset, fin, |
| 157 ack_notifier_delegate); | 159 ack_notifier_delegate); |
| 158 } | 160 } |
| 159 } | 161 } |
| 160 | 162 |
| 161 void set_writev_consumes_all_data(bool val) { | 163 void set_writev_consumes_all_data(bool val) { |
| 162 writev_consumes_all_data_ = val; | 164 writev_consumes_all_data_ = val; |
| 163 } | 165 } |
| 164 | 166 |
| 165 QuicConsumedData SendStreamData() { | 167 QuicConsumedData SendStreamData(QuicStreamId id) { |
| 166 return WritevData(5, IOVector(), 0, true, NULL); | 168 return WritevData(id, IOVector(), 0, true, NULL); |
| 167 } | 169 } |
| 168 | 170 |
| 169 private: | 171 private: |
| 170 StrictMock<TestCryptoStream> crypto_stream_; | 172 StrictMock<TestCryptoStream> crypto_stream_; |
| 171 | 173 |
| 172 bool writev_consumes_all_data_; | 174 bool writev_consumes_all_data_; |
| 173 }; | 175 }; |
| 174 | 176 |
| 175 class QuicSessionTest : public ::testing::TestWithParam<QuicVersion> { | 177 class QuicSessionTest : public ::testing::TestWithParam<QuicVersion> { |
| 176 protected: | 178 protected: |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 TestStream* stream2 = session_.CreateOutgoingDataStream(); | 349 TestStream* stream2 = session_.CreateOutgoingDataStream(); |
| 348 TestStream* stream4 = session_.CreateOutgoingDataStream(); | 350 TestStream* stream4 = session_.CreateOutgoingDataStream(); |
| 349 TestStream* stream6 = session_.CreateOutgoingDataStream(); | 351 TestStream* stream6 = session_.CreateOutgoingDataStream(); |
| 350 | 352 |
| 351 session_.MarkWriteBlocked(stream2->id(), kSomeMiddlePriority); | 353 session_.MarkWriteBlocked(stream2->id(), kSomeMiddlePriority); |
| 352 session_.MarkWriteBlocked(stream6->id(), kSomeMiddlePriority); | 354 session_.MarkWriteBlocked(stream6->id(), kSomeMiddlePriority); |
| 353 session_.MarkWriteBlocked(stream4->id(), kSomeMiddlePriority); | 355 session_.MarkWriteBlocked(stream4->id(), kSomeMiddlePriority); |
| 354 | 356 |
| 355 InSequence s; | 357 InSequence s; |
| 356 StreamBlocker stream2_blocker(&session_, stream2->id()); | 358 StreamBlocker stream2_blocker(&session_, stream2->id()); |
| 357 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce( | 359 // Reregister, to test the loop limit. |
| 358 // Reregister, to test the loop limit. | 360 EXPECT_CALL(*stream2, OnCanWrite()) |
| 359 InvokeWithoutArgs(&stream2_blocker, &StreamBlocker::MarkWriteBlocked)); | 361 .WillOnce(Invoke(&stream2_blocker, &StreamBlocker::MarkWriteBlocked)); |
| 360 EXPECT_CALL(*stream6, OnCanWrite()); | 362 EXPECT_CALL(*stream6, OnCanWrite()); |
| 361 EXPECT_CALL(*stream4, OnCanWrite()); | 363 EXPECT_CALL(*stream4, OnCanWrite()); |
| 362 session_.OnCanWrite(); | 364 session_.OnCanWrite(); |
| 363 EXPECT_TRUE(session_.WillingAndAbleToWrite()); | 365 EXPECT_TRUE(session_.WillingAndAbleToWrite()); |
| 364 } | 366 } |
| 365 | 367 |
| 366 TEST_P(QuicSessionTest, OnCanWriteBundlesStreams) { | 368 TEST_P(QuicSessionTest, OnCanWriteBundlesStreams) { |
| 367 // Drive congestion control manually. | 369 // Drive congestion control manually. |
| 368 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>; | 370 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>; |
| 369 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm); | 371 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm); |
| 370 | 372 |
| 371 TestStream* stream2 = session_.CreateOutgoingDataStream(); | 373 TestStream* stream2 = session_.CreateOutgoingDataStream(); |
| 372 TestStream* stream4 = session_.CreateOutgoingDataStream(); | 374 TestStream* stream4 = session_.CreateOutgoingDataStream(); |
| 373 TestStream* stream6 = session_.CreateOutgoingDataStream(); | 375 TestStream* stream6 = session_.CreateOutgoingDataStream(); |
| 374 | 376 |
| 375 session_.MarkWriteBlocked(stream2->id(), kSomeMiddlePriority); | 377 session_.MarkWriteBlocked(stream2->id(), kSomeMiddlePriority); |
| 376 session_.MarkWriteBlocked(stream6->id(), kSomeMiddlePriority); | 378 session_.MarkWriteBlocked(stream6->id(), kSomeMiddlePriority); |
| 377 session_.MarkWriteBlocked(stream4->id(), kSomeMiddlePriority); | 379 session_.MarkWriteBlocked(stream4->id(), kSomeMiddlePriority); |
| 378 | 380 |
| 379 | |
| 380 EXPECT_CALL(*send_algorithm, TimeUntilSend(_, _, _)).WillRepeatedly( | 381 EXPECT_CALL(*send_algorithm, TimeUntilSend(_, _, _)).WillRepeatedly( |
| 381 Return(QuicTime::Delta::Zero())); | 382 Return(QuicTime::Delta::Zero())); |
| 382 EXPECT_CALL(*send_algorithm, GetCongestionWindow()).WillOnce( | 383 EXPECT_CALL(*send_algorithm, GetCongestionWindow()) |
| 383 Return(kMaxPacketSize * 10)); | 384 .WillOnce(Return(kMaxPacketSize * 10)); |
| 384 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(IgnoreResult( | 385 EXPECT_CALL(*stream2, OnCanWrite()) |
| 385 InvokeWithoutArgs(&session_, &TestSession::SendStreamData))); | 386 .WillOnce(IgnoreResult(Invoke(CreateFunctor( |
| 386 EXPECT_CALL(*stream6, OnCanWrite()).WillOnce(IgnoreResult( | 387 &session_, &TestSession::SendStreamData, stream2->id())))); |
| 387 InvokeWithoutArgs(&session_, &TestSession::SendStreamData))); | 388 EXPECT_CALL(*stream4, OnCanWrite()) |
| 388 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce(IgnoreResult( | 389 .WillOnce(IgnoreResult(Invoke(CreateFunctor( |
| 389 InvokeWithoutArgs(&session_, &TestSession::SendStreamData))); | 390 &session_, &TestSession::SendStreamData, stream4->id())))); |
| 391 EXPECT_CALL(*stream6, OnCanWrite()) |
| 392 .WillOnce(IgnoreResult(Invoke(CreateFunctor( |
| 393 &session_, &TestSession::SendStreamData, stream6->id())))); |
| 394 |
| 395 // Expect that we only send one packet, the writes from different streams |
| 396 // should be bundled together. |
| 390 MockPacketWriter* writer = | 397 MockPacketWriter* writer = |
| 391 static_cast<MockPacketWriter*>( | 398 static_cast<MockPacketWriter*>( |
| 392 QuicConnectionPeer::GetWriter(session_.connection())); | 399 QuicConnectionPeer::GetWriter(session_.connection())); |
| 393 EXPECT_CALL(*writer, WritePacket(_, _, _, _)).WillOnce( | 400 EXPECT_CALL(*writer, WritePacket(_, _, _, _)).WillOnce( |
| 394 Return(WriteResult(WRITE_STATUS_OK, 0))); | 401 Return(WriteResult(WRITE_STATUS_OK, 0))); |
| 395 EXPECT_CALL(*send_algorithm, OnPacketSent(_, _, _, _, _)); | 402 EXPECT_CALL(*send_algorithm, OnPacketSent(_, _, _, _, _)).Times(1); |
| 396 session_.OnCanWrite(); | 403 session_.OnCanWrite(); |
| 397 EXPECT_FALSE(session_.WillingAndAbleToWrite()); | 404 EXPECT_FALSE(session_.WillingAndAbleToWrite()); |
| 398 } | 405 } |
| 399 | 406 |
| 400 TEST_P(QuicSessionTest, OnCanWriteCongestionControlBlocks) { | 407 TEST_P(QuicSessionTest, OnCanWriteCongestionControlBlocks) { |
| 401 InSequence s; | 408 InSequence s; |
| 402 | 409 |
| 403 // Drive congestion control manually. | 410 // Drive congestion control manually. |
| 404 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>; | 411 MockSendAlgorithm* send_algorithm = new StrictMock<MockSendAlgorithm>; |
| 405 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm); | 412 QuicConnectionPeer::SetSendAlgorithm(session_.connection(), send_algorithm); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 // Force most streams to re-register, which is common scenario when we block | 475 // Force most streams to re-register, which is common scenario when we block |
| 469 // the Crypto stream, and only the crypto stream can "really" write. | 476 // the Crypto stream, and only the crypto stream can "really" write. |
| 470 | 477 |
| 471 // Due to prioritization, we *should* be asked to write the crypto stream | 478 // Due to prioritization, we *should* be asked to write the crypto stream |
| 472 // first. | 479 // first. |
| 473 // Don't re-register the crypto stream (which signals complete writing). | 480 // Don't re-register the crypto stream (which signals complete writing). |
| 474 TestCryptoStream* crypto_stream = session_.GetCryptoStream(); | 481 TestCryptoStream* crypto_stream = session_.GetCryptoStream(); |
| 475 EXPECT_CALL(*crypto_stream, OnCanWrite()); | 482 EXPECT_CALL(*crypto_stream, OnCanWrite()); |
| 476 | 483 |
| 477 // Re-register all other streams, to show they weren't able to proceed. | 484 // Re-register all other streams, to show they weren't able to proceed. |
| 478 EXPECT_CALL(*stream2, OnCanWrite()).WillOnce( | 485 EXPECT_CALL(*stream2, OnCanWrite()) |
| 479 InvokeWithoutArgs(&stream2_blocker, &StreamBlocker::MarkWriteBlocked)); | 486 .WillOnce(Invoke(&stream2_blocker, &StreamBlocker::MarkWriteBlocked)); |
| 480 | 487 EXPECT_CALL(*stream3, OnCanWrite()) |
| 481 EXPECT_CALL(*stream3, OnCanWrite()).WillOnce( | 488 .WillOnce(Invoke(&stream3_blocker, &StreamBlocker::MarkWriteBlocked)); |
| 482 InvokeWithoutArgs(&stream3_blocker, &StreamBlocker::MarkWriteBlocked)); | 489 EXPECT_CALL(*stream4, OnCanWrite()) |
| 483 | 490 .WillOnce(Invoke(&stream4_blocker, &StreamBlocker::MarkWriteBlocked)); |
| 484 EXPECT_CALL(*stream4, OnCanWrite()).WillOnce( | |
| 485 InvokeWithoutArgs(&stream4_blocker, &StreamBlocker::MarkWriteBlocked)); | |
| 486 | 491 |
| 487 session_.OnCanWrite(); | 492 session_.OnCanWrite(); |
| 488 EXPECT_TRUE(session_.WillingAndAbleToWrite()); | 493 EXPECT_TRUE(session_.WillingAndAbleToWrite()); |
| 489 EXPECT_FALSE(session_.HasPendingHandshake()); // Crypto stream wrote. | 494 EXPECT_FALSE(session_.HasPendingHandshake()); // Crypto stream wrote. |
| 490 } | 495 } |
| 491 | 496 |
| 492 TEST_P(QuicSessionTest, OnCanWriteWithClosedStream) { | 497 TEST_P(QuicSessionTest, OnCanWriteWithClosedStream) { |
| 493 TestStream* stream2 = session_.CreateOutgoingDataStream(); | 498 TestStream* stream2 = session_.CreateOutgoingDataStream(); |
| 494 TestStream* stream4 = session_.CreateOutgoingDataStream(); | 499 TestStream* stream4 = session_.CreateOutgoingDataStream(); |
| 495 TestStream* stream6 = session_.CreateOutgoingDataStream(); | 500 TestStream* stream6 = session_.CreateOutgoingDataStream(); |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 const uint32 kSmallerFlowControlWindow = kDefaultFlowControlSendWindow - 1; | 672 const uint32 kSmallerFlowControlWindow = kDefaultFlowControlSendWindow - 1; |
| 668 TestSession session(connection, kSmallerFlowControlWindow); | 673 TestSession session(connection, kSmallerFlowControlWindow); |
| 669 | 674 |
| 670 EXPECT_EQ(kDefaultFlowControlSendWindow, | 675 EXPECT_EQ(kDefaultFlowControlSendWindow, |
| 671 session.max_flow_control_receive_window_bytes()); | 676 session.max_flow_control_receive_window_bytes()); |
| 672 } | 677 } |
| 673 | 678 |
| 674 } // namespace | 679 } // namespace |
| 675 } // namespace test | 680 } // namespace test |
| 676 } // namespace net | 681 } // namespace net |
| OLD | NEW |