Chromium Code Reviews| Index: net/quic/chromium/quic_chromium_client_stream_test.cc |
| diff --git a/net/quic/chromium/quic_chromium_client_stream_test.cc b/net/quic/chromium/quic_chromium_client_stream_test.cc |
| index 2d0a864795918ea2d41803cb0729bcc9b0ea9a6f..f04c1dd70177296ead4fb64022a7a5bbd33705e0 100644 |
| --- a/net/quic/chromium/quic_chromium_client_stream_test.cc |
| +++ b/net/quic/chromium/quic_chromium_client_stream_test.cc |
| @@ -51,8 +51,6 @@ class MockDelegate : public QuicChromiumClientStream::Delegate { |
| } |
| MOCK_METHOD2(OnTrailingHeadersAvailableMock, |
| void(const SpdyHeaderBlock& headers, size_t frame_len)); |
| - MOCK_METHOD2(OnDataReceived, int(const char*, int)); |
| - MOCK_METHOD0(OnDataAvailable, void()); |
| MOCK_METHOD0(OnClose, void()); |
| MOCK_METHOD1(OnError, void(int)); |
| MOCK_METHOD0(HasSendHeadersComplete, bool()); |
| @@ -64,14 +62,6 @@ class MockDelegate : public QuicChromiumClientStream::Delegate { |
| DISALLOW_COPY_AND_ASSIGN(MockDelegate); |
| }; |
| -class TestQuicClientSessionBaseStream : public QuicSpdyStream { |
| - public: |
| - TestQuicClientSessionBaseStream(QuicStreamId id, QuicSpdySession* session) |
| - : QuicSpdyStream(id, session) {} |
| - |
| - void OnDataAvailable() override {} |
| -}; |
| - |
| class MockQuicClientSessionBase : public QuicClientSessionBase { |
| public: |
| explicit MockQuicClientSessionBase(QuicConnection* connection, |
| @@ -142,7 +132,8 @@ class MockQuicClientSessionBase : public QuicClientSessionBase { |
| MOCK_METHOD1(OnHeadersHeadOfLineBlocking, void(QuicTime::Delta delta)); |
| std::unique_ptr<QuicStream> CreateStream(QuicStreamId id) { |
| - return QuicMakeUnique<TestQuicClientSessionBaseStream>(id, this); |
| + return QuicMakeUnique<QuicChromiumClientStream>(id, this, |
| + NetLogWithSource()); |
| } |
| using QuicSession::ActivateStream; |
| @@ -271,6 +262,10 @@ class QuicChromiumClientStreamTest |
| return QuicSpdySessionPeer::GetNthServerInitiatedStreamId(session_, n); |
| } |
| + void ResetStreamCallback(QuicChromiumClientStream* stream, int /*rv*/) { |
| + stream->Reset(QUIC_STREAM_CANCELLED); |
| + } |
| + |
| QuicCryptoClientConfig crypto_config_; |
| std::unique_ptr<QuicChromiumClientStream::Handle> handle_; |
| testing::StrictMock<MockDelegate> delegate_; |
| @@ -396,25 +391,43 @@ TEST_P(QuicChromiumClientStreamTest, OnFinRead) { |
| stream_->OnStreamFrame(frame2); |
| } |
| -TEST_P(QuicChromiumClientStreamTest, OnDataAvailableBeforeHeaders) { |
| - EXPECT_CALL(delegate_, OnClose()); |
| +TEST_P(QuicChromiumClientStreamTest, OnDataAvailable) { |
| + InitializeHeaders(); |
| + ProcessHeadersFull(headers_); |
| - EXPECT_CALL(delegate_, OnDataAvailable()).Times(0); |
| - stream_->OnDataAvailable(); |
| + const char data[] = "hello world!"; |
| + int data_len = strlen(data); |
| + stream_->OnStreamFrame(QuicStreamFrame(kTestStreamId, /*fin=*/false, |
| + /*offset=*/0, data)); |
| + |
| + // Read the body and verify that it arrives correctly. |
| + TestCompletionCallback callback; |
| + scoped_refptr<IOBuffer> buffer(new IOBuffer(2 * data_len)); |
| + EXPECT_EQ(data_len, |
| + handle_->ReadBody(buffer.get(), 2 * data_len, callback.callback())); |
| + EXPECT_EQ(QuicStringPiece(data), QuicStringPiece(buffer->data(), data_len)); |
| + |
| + EXPECT_CALL(delegate_, OnClose()); |
| } |
| -TEST_P(QuicChromiumClientStreamTest, OnDataAvailable) { |
| +TEST_P(QuicChromiumClientStreamTest, OnDataAvailableEarly) { |
|
xunjieli
2017/05/12 15:22:22
Why is this testing OnDataAvailable Early?
The OnS
Ryan Hamilton
2017/05/12 17:26:24
Good point. Done.
|
| InitializeHeaders(); |
| ProcessHeadersFull(headers_); |
| const char data[] = "hello world!"; |
| + int data_len = strlen(data); |
| + |
| + // Start to read the body. |
| + TestCompletionCallback callback; |
| + scoped_refptr<IOBuffer> buffer(new IOBuffer(2 * data_len)); |
| + EXPECT_EQ(ERR_IO_PENDING, |
| + handle_->ReadBody(buffer.get(), 2 * data_len, callback.callback())); |
| + |
| stream_->OnStreamFrame(QuicStreamFrame(kTestStreamId, /*fin=*/false, |
| /*offset=*/0, data)); |
| - EXPECT_CALL(delegate_, OnDataAvailable()) |
| - .WillOnce(testing::Invoke(CreateFunctor( |
| - &QuicChromiumClientStreamTest::ReadData, base::Unretained(this), |
| - QuicStringPiece(data, arraysize(data) - 1)))); |
| + EXPECT_EQ(data_len, callback.WaitForResult()); |
| + EXPECT_EQ(QuicStringPiece(data), QuicStringPiece(buffer->data(), data_len)); |
| base::RunLoop().RunUntilIdle(); |
| EXPECT_CALL(delegate_, OnClose()); |
| @@ -442,12 +455,21 @@ TEST_P(QuicChromiumClientStreamTest, OnDataAvailableWithError) { |
| EXPECT_CALL(session_, SendRstStream(kTestStreamId, QUIC_STREAM_CANCELLED, 0)); |
| const char data[] = "hello world!"; |
| + int data_len = strlen(data); |
| + |
| + // Start to read the body. |
| + TestCompletionCallback callback; |
| + scoped_refptr<IOBuffer> buffer(new IOBuffer(2 * data_len)); |
| + EXPECT_EQ(ERR_IO_PENDING, |
| + handle_->ReadBody( |
| + buffer.get(), 2 * data_len, |
| + base::Bind(&QuicChromiumClientStreamTest::ResetStreamCallback, |
| + base::Unretained(this), stream_))); |
| + |
| + // Receive the data and close the stream during the callback. |
| stream_->OnStreamFrame(QuicStreamFrame(kTestStreamId, /*fin=*/false, |
| /*offset=*/0, data)); |
| - EXPECT_CALL(delegate_, OnDataAvailable()) |
| - .WillOnce(testing::Invoke(CreateFunctor( |
| - &QuicChromiumClientStream::Reset, |
| - base::Unretained(stream_), QUIC_STREAM_CANCELLED))); |
| + |
| base::RunLoop().RunUntilIdle(); |
| EXPECT_CALL(delegate_, OnClose()); |
| @@ -465,13 +487,16 @@ TEST_P(QuicChromiumClientStreamTest, OnTrailers) { |
| ProcessHeadersFull(headers_); |
| const char data[] = "hello world!"; |
| + int data_len = strlen(data); |
| stream_->OnStreamFrame(QuicStreamFrame(kTestStreamId, /*fin=*/false, |
| /*offset=*/0, data)); |
| - EXPECT_CALL(delegate_, OnDataAvailable()) |
| - .WillOnce(testing::Invoke(CreateFunctor( |
| - &QuicChromiumClientStreamTest::ReadData, base::Unretained(this), |
| - QuicStringPiece(data, arraysize(data) - 1)))); |
| + // Read the body and verify that it arrives correctly. |
| + TestCompletionCallback callback; |
| + scoped_refptr<IOBuffer> buffer(new IOBuffer(2 * data_len)); |
| + EXPECT_EQ(data_len, |
| + handle_->ReadBody(buffer.get(), 2 * data_len, callback.callback())); |
| + EXPECT_EQ(QuicStringPiece(data), QuicStringPiece(buffer->data(), data_len)); |
| SpdyHeaderBlock trailers; |
| trailers["bar"] = "foo"; |
| @@ -485,16 +510,9 @@ TEST_P(QuicChromiumClientStreamTest, OnTrailers) { |
| run_loop.Run(); |
| - // OnDataAvailable callback should follow trailers notification. |
| - base::RunLoop run_loop3; |
| - EXPECT_CALL(delegate_, OnDataAvailable()) |
| - .Times(1) |
| - .WillOnce(testing::DoAll( |
| - testing::Invoke(CreateFunctor(&QuicChromiumClientStreamTest::ReadData, |
| - base::Unretained(this), |
| - QuicStringPiece())), |
| - testing::InvokeWithoutArgs([&run_loop3]() { run_loop3.Quit(); }))); |
| - run_loop3.Run(); |
| + // Read the body and verify that it arrives correctly. |
| + EXPECT_EQ(0, |
| + handle_->ReadBody(buffer.get(), 2 * data_len, callback.callback())); |
| // Make sure kFinalOffsetHeaderKey is gone from the delivered actual trailers. |
| trailers.erase(kFinalOffsetHeaderKey); |
| @@ -510,24 +528,21 @@ TEST_P(QuicChromiumClientStreamTest, MarkTrailersConsumedWhenNotifyDelegate) { |
| ProcessHeadersFull(headers_); |
| const char data[] = "hello world!"; |
| + int data_len = strlen(data); |
| stream_->OnStreamFrame(QuicStreamFrame(kTestStreamId, /*fin=*/false, |
| /*offset=*/0, data)); |
| - base::RunLoop run_loop; |
| - EXPECT_CALL(delegate_, OnDataAvailable()) |
| - .Times(1) |
| - .WillOnce(testing::DoAll( |
| - testing::Invoke(CreateFunctor( |
| - &QuicChromiumClientStreamTest::ReadData, base::Unretained(this), |
| - QuicStringPiece(data, arraysize(data) - 1))), |
| - testing::Invoke([&run_loop]() { run_loop.Quit(); }))); |
| - |
| - // Wait for the read to complete. |
| - run_loop.Run(); |
| + // Read the body and verify that it arrives correctly. |
| + TestCompletionCallback callback; |
| + scoped_refptr<IOBuffer> buffer(new IOBuffer(2 * data_len)); |
| + EXPECT_EQ(data_len, |
| + handle_->ReadBody(buffer.get(), 2 * data_len, callback.callback())); |
| + EXPECT_EQ(QuicStringPiece(data), QuicStringPiece(buffer->data(), data_len)); |
| // Read again, and it will be pending. |
| - scoped_refptr<IOBuffer> buffer(new IOBuffer(1)); |
| - EXPECT_THAT(stream_->Read(buffer.get(), 1), IsError(ERR_IO_PENDING)); |
| + EXPECT_THAT( |
| + handle_->ReadBody(buffer.get(), 2 * data_len, callback.callback()), |
| + IsError(ERR_IO_PENDING)); |
| SpdyHeaderBlock trailers; |
| trailers["bar"] = "foo"; |
| @@ -543,16 +558,8 @@ TEST_P(QuicChromiumClientStreamTest, MarkTrailersConsumedWhenNotifyDelegate) { |
| run_loop2.Run(); |
| - // OnDataAvailable callback should follow trailers notification. |
| - base::RunLoop run_loop3; |
| - EXPECT_CALL(delegate_, OnDataAvailable()) |
| - .Times(1) |
| - .WillOnce(testing::DoAll( |
| - testing::Invoke(CreateFunctor(&QuicChromiumClientStreamTest::ReadData, |
| - base::Unretained(this), |
| - QuicStringPiece())), |
| - testing::InvokeWithoutArgs([&run_loop3]() { run_loop3.Quit(); }))); |
| - run_loop3.Run(); |
| + // Read the body and verify that it arrives correctly. |
| + EXPECT_EQ(0, callback.WaitForResult()); |
| // Make sure the stream is properly closed since trailers and data are all |
| // consumed. |
| @@ -573,20 +580,16 @@ TEST_P(QuicChromiumClientStreamTest, ReadAfterTrailersReceivedButNotDelivered) { |
| ProcessHeadersFull(headers_); |
| const char data[] = "hello world!"; |
| + int data_len = strlen(data); |
| stream_->OnStreamFrame(QuicStreamFrame(kTestStreamId, /*fin=*/false, |
| /*offset=*/0, data)); |
| - base::RunLoop run_loop; |
| - EXPECT_CALL(delegate_, OnDataAvailable()) |
| - .Times(1) |
| - .WillOnce(testing::DoAll( |
| - testing::Invoke(CreateFunctor( |
| - &QuicChromiumClientStreamTest::ReadData, base::Unretained(this), |
| - QuicStringPiece(data, arraysize(data) - 1))), |
| - testing::Invoke([&run_loop]() { run_loop.Quit(); }))); |
| - |
| - // Wait for the read to complete. |
| - run_loop.Run(); |
| + // Read the body and verify that it arrives correctly. |
| + TestCompletionCallback callback; |
| + scoped_refptr<IOBuffer> buffer(new IOBuffer(2 * data_len)); |
| + EXPECT_EQ(data_len, |
| + handle_->ReadBody(buffer.get(), 2 * data_len, callback.callback())); |
| + EXPECT_EQ(QuicStringPiece(data), QuicStringPiece(buffer->data(), data_len)); |
| // Deliver trailers. Delegate notification is posted asynchronously. |
| SpdyHeaderBlock trailers; |
| @@ -595,9 +598,11 @@ TEST_P(QuicChromiumClientStreamTest, ReadAfterTrailersReceivedButNotDelivered) { |
| QuicHeaderList t = ProcessTrailers(trailers); |
| + EXPECT_FALSE(stream_->IsDoneReading()); |
| // Read again, it return ERR_IO_PENDING. |
| - scoped_refptr<IOBuffer> buffer(new IOBuffer(1)); |
| - EXPECT_THAT(stream_->Read(buffer.get(), 1), ERR_IO_PENDING); |
| + EXPECT_THAT( |
| + handle_->ReadBody(buffer.get(), 2 * data_len, callback.callback()), |
| + IsError(ERR_IO_PENDING)); |
| // Trailers are not delivered |
| EXPECT_FALSE(stream_->IsDoneReading()); |
| @@ -610,15 +615,9 @@ TEST_P(QuicChromiumClientStreamTest, ReadAfterTrailersReceivedButNotDelivered) { |
| run_loop2.Run(); |
| - base::RunLoop run_loop3; |
| + // Read the body and verify that it arrives correctly. |
| // OnDataAvailable() should follow right after and Read() will return 0. |
| - EXPECT_CALL(delegate_, OnDataAvailable()) |
| - .WillOnce(testing::DoAll( |
| - testing::Invoke(CreateFunctor(&QuicChromiumClientStreamTest::ReadData, |
| - base::Unretained(this), |
| - QuicStringPiece())), |
| - testing::Invoke([&run_loop3]() { run_loop3.Quit(); }))); |
| - run_loop3.Run(); |
| + EXPECT_EQ(0, callback.WaitForResult()); |
| // Make sure the stream is properly closed since trailers and data are all |
| // consumed. |