| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "blimp/common/proto/blimp_message.pb.h" | 10 #include "blimp/common/proto/blimp_message.pb.h" |
| 11 #include "blimp/net/blimp_connection.h" | 11 #include "blimp/net/blimp_connection.h" |
| 12 #include "blimp/net/common.h" | 12 #include "blimp/net/common.h" |
| 13 #include "blimp/net/connection_error_observer.h" | 13 #include "blimp/net/connection_error_observer.h" |
| 14 #include "blimp/net/test_common.h" | 14 #include "blimp/net/test_common.h" |
| 15 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 using testing::_; | 22 using testing::_; |
| 23 using testing::DoAll; | |
| 24 using testing::InSequence; | 23 using testing::InSequence; |
| 25 using testing::NotNull; | 24 using testing::NotNull; |
| 26 using testing::Return; | 25 using testing::Return; |
| 27 using testing::SaveArg; | 26 using testing::SaveArg; |
| 28 | 27 |
| 29 namespace blimp { | 28 namespace blimp { |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| 32 class BlimpConnectionTest : public testing::Test { | 31 class BlimpConnectionTest : public testing::Test { |
| 33 public: | 32 public: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 63 scoped_ptr<BlimpMessage> message1_; | 62 scoped_ptr<BlimpMessage> message1_; |
| 64 scoped_ptr<BlimpMessage> message2_; | 63 scoped_ptr<BlimpMessage> message2_; |
| 65 | 64 |
| 66 testing::StrictMock<MockPacketReader>* reader_; | 65 testing::StrictMock<MockPacketReader>* reader_; |
| 67 testing::StrictMock<MockPacketWriter>* writer_; | 66 testing::StrictMock<MockPacketWriter>* writer_; |
| 68 testing::StrictMock<MockConnectionErrorObserver> error_observer_; | 67 testing::StrictMock<MockConnectionErrorObserver> error_observer_; |
| 69 testing::StrictMock<MockBlimpMessageProcessor> receiver_; | 68 testing::StrictMock<MockBlimpMessageProcessor> receiver_; |
| 70 scoped_ptr<BlimpConnection> connection_; | 69 scoped_ptr<BlimpConnection> connection_; |
| 71 }; | 70 }; |
| 72 | 71 |
| 73 // Reader completes reading one packet synchronously. | |
| 74 // Two read cases here. BlimpMessagePumpTest covers other cases. | |
| 75 TEST_F(BlimpConnectionTest, SyncPacketRead) { | |
| 76 EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _)); | |
| 77 EXPECT_CALL(*reader_, ReadPacket(NotNull(), _)) | |
| 78 .WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()), | |
| 79 Return(message1_->ByteSize()))); | |
| 80 connection_->SetIncomingMessageProcessor(&receiver_); | |
| 81 } | |
| 82 | |
| 83 // Reader completes reading one packet synchronously withe error. | |
| 84 TEST_F(BlimpConnectionTest, SyncPacketReadWithError) { | |
| 85 InSequence s; | |
| 86 EXPECT_CALL(*reader_, ReadPacket(NotNull(), _)) | |
| 87 .WillOnce(Return(net::ERR_FAILED)); | |
| 88 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED)); | |
| 89 connection_->SetIncomingMessageProcessor(&receiver_); | |
| 90 } | |
| 91 | |
| 92 // Writer completes writing two packets synchronously. | |
| 93 TEST_F(BlimpConnectionTest, SyncTwoPacketsWrite) { | |
| 94 InSequence s; | |
| 95 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _)) | |
| 96 .WillOnce(Return(net::OK)) | |
| 97 .RetiresOnSaturation(); | |
| 98 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _)) | |
| 99 .WillOnce(Return(net::OK)) | |
| 100 .RetiresOnSaturation(); | |
| 101 | |
| 102 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); | |
| 103 net::TestCompletionCallback complete_cb_1; | |
| 104 sender->ProcessMessage(CreateInputMessage().Pass(), complete_cb_1.callback()); | |
| 105 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); | |
| 106 net::TestCompletionCallback complete_cb_2; | |
| 107 sender->ProcessMessage(CreateControlMessage().Pass(), | |
| 108 complete_cb_2.callback()); | |
| 109 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult()); | |
| 110 } | |
| 111 | |
| 112 // Writer completes writing two packets synchronously. | |
| 113 // First write succeeds, second fails. | |
| 114 TEST_F(BlimpConnectionTest, SyncTwoPacketsWriteWithError) { | |
| 115 InSequence s; | |
| 116 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _)) | |
| 117 .WillOnce(Return(net::OK)) | |
| 118 .RetiresOnSaturation(); | |
| 119 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _)) | |
| 120 .WillOnce(Return(net::ERR_FAILED)) | |
| 121 .RetiresOnSaturation(); | |
| 122 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED)); | |
| 123 | |
| 124 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); | |
| 125 net::TestCompletionCallback complete_cb_1; | |
| 126 sender->ProcessMessage(CreateInputMessage().Pass(), complete_cb_1.callback()); | |
| 127 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); | |
| 128 net::TestCompletionCallback complete_cb_2; | |
| 129 sender->ProcessMessage(CreateControlMessage().Pass(), | |
| 130 complete_cb_2.callback()); | |
| 131 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult()); | |
| 132 } | |
| 133 | |
| 134 // Write completes writing two packets asynchronously. | 72 // Write completes writing two packets asynchronously. |
| 135 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) { | 73 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) { |
| 136 net::CompletionCallback write_packet_cb; | 74 net::CompletionCallback write_packet_cb; |
| 137 | 75 |
| 138 InSequence s; | 76 InSequence s; |
| 139 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _)) | 77 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _)) |
| 140 .WillOnce( | 78 .WillOnce(SaveArg<1>(&write_packet_cb)) |
| 141 DoAll(SaveArg<1>(&write_packet_cb), Return(net::ERR_IO_PENDING))) | |
| 142 .RetiresOnSaturation(); | 79 .RetiresOnSaturation(); |
| 143 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _)) | 80 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _)) |
| 144 .WillOnce( | 81 .WillOnce(SaveArg<1>(&write_packet_cb)) |
| 145 DoAll(SaveArg<1>(&write_packet_cb), Return(net::ERR_IO_PENDING))) | |
| 146 .RetiresOnSaturation(); | 82 .RetiresOnSaturation(); |
| 147 | 83 |
| 148 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); | 84 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); |
| 149 net::TestCompletionCallback complete_cb_1; | 85 net::TestCompletionCallback complete_cb_1; |
| 150 ASSERT_TRUE(write_packet_cb.is_null()); | 86 ASSERT_TRUE(write_packet_cb.is_null()); |
| 151 sender->ProcessMessage(CreateInputMessage().Pass(), complete_cb_1.callback()); | 87 sender->ProcessMessage(CreateInputMessage().Pass(), complete_cb_1.callback()); |
| 152 ASSERT_FALSE(write_packet_cb.is_null()); | 88 ASSERT_FALSE(write_packet_cb.is_null()); |
| 153 base::ResetAndReturn(&write_packet_cb).Run(net::OK); | 89 base::ResetAndReturn(&write_packet_cb).Run(net::OK); |
| 154 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); | 90 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); |
| 155 | 91 |
| 156 net::TestCompletionCallback complete_cb_2; | 92 net::TestCompletionCallback complete_cb_2; |
| 157 ASSERT_TRUE(write_packet_cb.is_null()); | 93 ASSERT_TRUE(write_packet_cb.is_null()); |
| 158 sender->ProcessMessage(CreateControlMessage().Pass(), | 94 sender->ProcessMessage(CreateControlMessage().Pass(), |
| 159 complete_cb_2.callback()); | 95 complete_cb_2.callback()); |
| 160 ASSERT_FALSE(write_packet_cb.is_null()); | 96 ASSERT_FALSE(write_packet_cb.is_null()); |
| 161 base::ResetAndReturn(&write_packet_cb).Run(net::OK); | 97 base::ResetAndReturn(&write_packet_cb).Run(net::OK); |
| 162 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult()); | 98 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult()); |
| 163 } | 99 } |
| 164 | 100 |
| 165 // Writer completes writing two packets asynchronously. | 101 // Writer completes writing two packets asynchronously. |
| 166 // First write succeeds, second fails. | 102 // First write succeeds, second fails. |
| 167 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) { | 103 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) { |
| 168 net::CompletionCallback write_packet_cb; | 104 net::CompletionCallback write_packet_cb; |
| 169 | 105 |
| 170 InSequence s; | 106 InSequence s; |
| 171 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _)) | 107 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _)) |
| 172 .WillOnce( | 108 .WillOnce(SaveArg<1>(&write_packet_cb)) |
| 173 DoAll(SaveArg<1>(&write_packet_cb), Return(net::ERR_IO_PENDING))) | |
| 174 .RetiresOnSaturation(); | 109 .RetiresOnSaturation(); |
| 175 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _)) | 110 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _)) |
| 176 .WillOnce( | 111 .WillOnce(SaveArg<1>(&write_packet_cb)) |
| 177 DoAll(SaveArg<1>(&write_packet_cb), Return(net::ERR_IO_PENDING))) | |
| 178 .RetiresOnSaturation(); | 112 .RetiresOnSaturation(); |
| 179 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED)); | 113 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED)); |
| 180 | 114 |
| 181 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); | 115 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); |
| 182 net::TestCompletionCallback complete_cb_1; | 116 net::TestCompletionCallback complete_cb_1; |
| 183 sender->ProcessMessage(CreateInputMessage().Pass(), complete_cb_1.callback()); | 117 sender->ProcessMessage(CreateInputMessage().Pass(), complete_cb_1.callback()); |
| 184 base::ResetAndReturn(&write_packet_cb).Run(net::OK); | 118 base::ResetAndReturn(&write_packet_cb).Run(net::OK); |
| 185 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); | 119 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); |
| 186 | 120 |
| 187 net::TestCompletionCallback complete_cb_2; | 121 net::TestCompletionCallback complete_cb_2; |
| 188 sender->ProcessMessage(CreateControlMessage().Pass(), | 122 sender->ProcessMessage(CreateControlMessage().Pass(), |
| 189 complete_cb_2.callback()); | 123 complete_cb_2.callback()); |
| 190 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED); | 124 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED); |
| 191 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult()); | 125 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult()); |
| 192 } | 126 } |
| 193 | 127 |
| 194 } // namespace | 128 } // namespace |
| 195 | 129 |
| 196 } // namespace blimp | 130 } // namespace blimp |
| OLD | NEW |