Fix threading issue in CookieMonster task ordering.
[chromium-blink-merge.git] / net / quic / quic_reliable_client_stream_test.cc
blobaaebda27fe0d8a0cf9cc60d799f7646dcd0095cd
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
3 // found in the LICENSE file.
5 #include "net/quic/quic_reliable_client_stream.h"
7 #include "net/base/net_errors.h"
8 #include "net/base/test_completion_callback.h"
9 #include "net/quic/quic_client_session.h"
10 #include "net/quic/quic_utils.h"
11 #include "net/quic/test_tools/quic_test_utils.h"
12 #include "testing/gmock/include/gmock/gmock.h"
14 using testing::Return;
15 using testing::StrEq;
16 using testing::_;
18 namespace net {
19 namespace test {
20 namespace {
22 class MockDelegate : public QuicReliableClientStream::Delegate {
23 public:
24 MockDelegate() {}
26 MOCK_METHOD0(OnSendData, int());
27 MOCK_METHOD2(OnSendDataComplete, int(int, bool*));
28 MOCK_METHOD2(OnDataReceived, int(const char*, int));
29 MOCK_METHOD1(OnClose, void(QuicErrorCode));
30 MOCK_METHOD1(OnError, void(int));
31 MOCK_METHOD0(HasSendHeadersComplete, bool());
33 private:
34 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
37 class QuicReliableClientStreamTest : public ::testing::Test {
38 public:
39 QuicReliableClientStreamTest()
40 : session_(new MockConnection(1, IPEndPoint(), false), false),
41 stream_(1, &session_, BoundNetLog()) {
42 stream_.SetDelegate(&delegate_);
45 testing::StrictMock<MockDelegate> delegate_;
46 MockSession session_;
47 QuicReliableClientStream stream_;
48 QuicCryptoClientConfig crypto_config_;
51 TEST_F(QuicReliableClientStreamTest, TerminateFromPeer) {
52 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
54 stream_.TerminateFromPeer(true);
57 TEST_F(QuicReliableClientStreamTest, ProcessData) {
58 const char data[] = "hello world!";
59 EXPECT_CALL(delegate_, OnDataReceived(StrEq(data), arraysize(data)));
60 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
62 EXPECT_EQ(arraysize(data), stream_.ProcessData(data, arraysize(data)));
65 TEST_F(QuicReliableClientStreamTest, ProcessDataWithError) {
66 const char data[] = "hello world!";
67 EXPECT_CALL(delegate_,
68 OnDataReceived(StrEq(data),
69 arraysize(data))).WillOnce(Return(ERR_UNEXPECTED));
70 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
73 EXPECT_EQ(0u, stream_.ProcessData(data, arraysize(data)));
76 TEST_F(QuicReliableClientStreamTest, OnError) {
77 EXPECT_CALL(delegate_, OnError(ERR_INTERNET_DISCONNECTED));
79 stream_.OnError(ERR_INTERNET_DISCONNECTED);
80 EXPECT_FALSE(stream_.GetDelegate());
83 TEST_F(QuicReliableClientStreamTest, WriteStreamData) {
84 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
86 const char kData1[] = "hello world";
87 const size_t kDataLen = arraysize(kData1);
89 // All data written.
90 EXPECT_CALL(session_, WritevData(stream_.id(), _, _, _, _)).WillOnce(
91 Return(QuicConsumedData(kDataLen, true)));
92 TestCompletionCallback callback;
93 EXPECT_EQ(OK, stream_.WriteStreamData(base::StringPiece(kData1, kDataLen),
94 true, callback.callback()));
97 TEST_F(QuicReliableClientStreamTest, WriteStreamDataAsync) {
98 EXPECT_CALL(delegate_, HasSendHeadersComplete());
99 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
101 const char kData1[] = "hello world";
102 const size_t kDataLen = arraysize(kData1);
104 // No data written.
105 EXPECT_CALL(session_, WritevData(stream_.id(), _, _, _, _)).WillOnce(
106 Return(QuicConsumedData(0, false)));
107 TestCompletionCallback callback;
108 EXPECT_EQ(ERR_IO_PENDING,
109 stream_.WriteStreamData(base::StringPiece(kData1, kDataLen),
110 true, callback.callback()));
111 ASSERT_FALSE(callback.have_result());
113 // All data written.
114 EXPECT_CALL(session_, WritevData(stream_.id(), _, _, _, _)).WillOnce(
115 Return(QuicConsumedData(kDataLen, true)));
116 stream_.OnCanWrite();
117 ASSERT_TRUE(callback.have_result());
118 EXPECT_EQ(OK, callback.WaitForResult());
121 } // namespace
122 } // namespace test
123 } // namespace net