Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(855)

Side by Side Diff: blimp/net/blimp_connection_unittest.cc

Issue 2439403003: Refactor BlimpConnection to TCPConnection (Closed)
Patch Set: Added missing Engine Transport Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "blimp/net/blimp_connection.h"
6
7 #include <stddef.h>
8
9 #include <string>
10 #include <utility>
11
12 #include "base/callback_helpers.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/message_loop/message_loop.h"
15 #include "blimp/common/create_blimp_message.h"
16 #include "blimp/common/proto/blimp_message.pb.h"
17 #include "blimp/net/common.h"
18 #include "blimp/net/connection_error_observer.h"
19 #include "blimp/net/message_port.h"
20 #include "blimp/net/test_common.h"
21 #include "net/base/completion_callback.h"
22 #include "net/base/io_buffer.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/test_completion_callback.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 using testing::_;
29 using testing::InSequence;
30 using testing::Return;
31 using testing::SaveArg;
32
33 namespace blimp {
34 namespace {
35
36 class BlimpConnectionTest : public testing::Test {
37 public:
38 BlimpConnectionTest() {
39 std::unique_ptr<MockPacketReader> mock_reader(new MockPacketReader);
40 std::unique_ptr<MockPacketWriter> mock_writer(new MockPacketWriter);
41 mock_reader_ = mock_reader.get();
42 mock_writer_ = mock_writer.get();
43 connection_ =
44 base::MakeUnique<BlimpConnection>(base::MakeUnique<MessagePort>(
45 std::move(mock_reader), std::move(mock_writer)));
46
47 connection_->AddConnectionErrorObserver(&error_observer1_);
48 connection_->AddConnectionErrorObserver(&error_observer2_);
49 connection_->AddConnectionErrorObserver(&error_observer3_);
50 connection_->RemoveConnectionErrorObserver(&error_observer3_);
51 }
52
53 ~BlimpConnectionTest() override {}
54
55 void DropConnection() { connection_.reset(); }
56
57 protected:
58 std::unique_ptr<BlimpMessage> CreateInputMessage() {
59 InputMessage* input;
60 return CreateBlimpMessage(&input);
61 }
62
63 std::unique_ptr<BlimpMessage> CreateControlMessage() {
64 TabControlMessage* control;
65 return CreateBlimpMessage(&control);
66 }
67
68 base::MessageLoop message_loop_;
69 MockPacketReader* mock_reader_;
70 MockPacketWriter* mock_writer_;
71 testing::StrictMock<MockConnectionErrorObserver> error_observer1_;
72 testing::StrictMock<MockConnectionErrorObserver> error_observer2_;
73
74 // This error observer is Removed() immediately after it's added;
75 // it should never be called.
76 testing::StrictMock<MockConnectionErrorObserver> error_observer3_;
77
78 testing::StrictMock<MockBlimpMessageProcessor> receiver_;
79 std::unique_ptr<BlimpConnection> connection_;
80 };
81
82 // Write completes writing two packets asynchronously.
83 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) {
84 net::CompletionCallback write_packet_cb;
85
86 InSequence s;
87 EXPECT_CALL(*mock_writer_,
88 WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
89 .WillOnce(SaveArg<1>(&write_packet_cb))
90 .RetiresOnSaturation();
91 EXPECT_CALL(*mock_writer_,
92 WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
93 .WillOnce(SaveArg<1>(&write_packet_cb))
94 .RetiresOnSaturation();
95 EXPECT_CALL(error_observer1_, OnConnectionError(_)).Times(0);
96 EXPECT_CALL(error_observer2_, OnConnectionError(_)).Times(0);
97 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
98
99 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
100 net::TestCompletionCallback complete_cb_1;
101 ASSERT_TRUE(write_packet_cb.is_null());
102 sender->ProcessMessage(CreateInputMessage(),
103 complete_cb_1.callback());
104 ASSERT_FALSE(write_packet_cb.is_null());
105 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
106 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
107
108 net::TestCompletionCallback complete_cb_2;
109 ASSERT_TRUE(write_packet_cb.is_null());
110 sender->ProcessMessage(CreateControlMessage(),
111 complete_cb_2.callback());
112 ASSERT_FALSE(write_packet_cb.is_null());
113 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
114 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult());
115 }
116
117 // Writer completes writing two packets asynchronously.
118 // First write succeeds, second fails.
119 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) {
120 net::CompletionCallback write_packet_cb;
121
122 InSequence s;
123 EXPECT_CALL(*mock_writer_,
124 WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
125 .WillOnce(SaveArg<1>(&write_packet_cb))
126 .RetiresOnSaturation();
127 EXPECT_CALL(*mock_writer_,
128 WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
129 .WillOnce(SaveArg<1>(&write_packet_cb))
130 .RetiresOnSaturation();
131 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED));
132 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED));
133 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
134
135 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
136 net::TestCompletionCallback complete_cb_1;
137 sender->ProcessMessage(CreateInputMessage(),
138 complete_cb_1.callback());
139 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
140 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
141
142 net::TestCompletionCallback complete_cb_2;
143 sender->ProcessMessage(CreateControlMessage(),
144 complete_cb_2.callback());
145 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED);
146 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult());
147 }
148
149 TEST_F(BlimpConnectionTest, DeleteHappyObserversAreOK) {
150 net::CompletionCallback write_packet_cb;
151
152 InSequence s;
153 EXPECT_CALL(*mock_writer_,
154 WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
155 .WillOnce(SaveArg<1>(&write_packet_cb))
156 .RetiresOnSaturation();
157 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED))
158 .WillOnce(testing::InvokeWithoutArgs(
159 this, &BlimpConnectionTest::DropConnection));
160
161 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
162 net::TestCompletionCallback complete_cb_1;
163 sender->ProcessMessage(CreateInputMessage(), complete_cb_1.callback());
164 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED);
165 EXPECT_EQ(net::ERR_FAILED, complete_cb_1.WaitForResult());
166 }
167
168 // Verifies that a ReadPacket error causes ErrorObservers to be notified.
169 TEST_F(BlimpConnectionTest, ReadPacketErrorInvokesErrorObservers) {
170 scoped_refptr<net::GrowableIOBuffer> read_packet_buffer;
171 net::CompletionCallback read_packet_cb;
172
173 EXPECT_CALL(*mock_reader_, ReadPacket(_, _))
174 .WillOnce(
175 DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb)))
176 .RetiresOnSaturation();
177
178 EXPECT_CALL(error_observer1_, OnConnectionError(net::ERR_FAILED));
179 EXPECT_CALL(error_observer2_, OnConnectionError(net::ERR_FAILED));
180 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
181
182 EXPECT_CALL(receiver_, MockableProcessMessage(_, _)).Times(0);
183
184 // Trigger the first ReadPacket() call by setting the MessageProcessor.
185 connection_->SetIncomingMessageProcessor(&receiver_);
186 EXPECT_TRUE(read_packet_buffer);
187 EXPECT_FALSE(read_packet_cb.is_null());
188
189 // Signal an error back from the ReadPacket operation.
190 base::ResetAndReturn(&read_packet_cb).Run(net::ERR_FAILED);
191 }
192
193 // Verifies that EndConnection messages received from the peer are
194 // routed through to registered ConnectionErrorObservers as errors.
195 TEST_F(BlimpConnectionTest, EndConnectionInvokesErrorObservers) {
196 scoped_refptr<net::GrowableIOBuffer> read_packet_buffer;
197 net::CompletionCallback read_packet_cb;
198
199 EXPECT_CALL(*mock_reader_, ReadPacket(_, _))
200 .WillOnce(
201 DoAll(SaveArg<0>(&read_packet_buffer), SaveArg<1>(&read_packet_cb)))
202 .WillOnce(Return())
203 .RetiresOnSaturation();
204
205 EXPECT_CALL(error_observer1_,
206 OnConnectionError(EndConnectionMessage::PROTOCOL_MISMATCH));
207 EXPECT_CALL(error_observer2_,
208 OnConnectionError(EndConnectionMessage::PROTOCOL_MISMATCH));
209 EXPECT_CALL(error_observer3_, OnConnectionError(_)).Times(0);
210
211 EXPECT_CALL(receiver_, MockableProcessMessage(_, _)).Times(0);
212
213 // Trigger the first ReadPacket() call by setting the MessageProcessor.
214 connection_->SetIncomingMessageProcessor(&receiver_);
215 EXPECT_TRUE(read_packet_buffer);
216 EXPECT_FALSE(read_packet_cb.is_null());
217
218 // Create an EndConnection message to return from ReadPacket.
219 std::unique_ptr<BlimpMessage> message =
220 CreateEndConnectionMessage(EndConnectionMessage::PROTOCOL_MISMATCH);
221
222 // Put the EndConnection message in the buffer and invoke the read callback.
223 read_packet_buffer->SetCapacity(message->ByteSize());
224 ASSERT_TRUE(message->SerializeToArray(read_packet_buffer->data(),
225 message->GetCachedSize()));
226 base::ResetAndReturn(&read_packet_cb).Run(message->ByteSize());
227 }
228
229 } // namespace
230 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698