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

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

Issue 1452823011: Make PacketReader/PacketWriter interfaces async-only. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years 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
« no previous file with comments | « blimp/net/blimp_connection.cc ('k') | blimp/net/blimp_message_pump.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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;
26 using testing::Return; 24 using testing::Return;
27 using testing::SaveArg; 25 using testing::SaveArg;
28 26
29 namespace blimp { 27 namespace blimp {
30 namespace { 28 namespace {
31 29
32 class BlimpConnectionTest : public testing::Test { 30 class BlimpConnectionTest : public testing::Test {
33 public: 31 public:
34 BlimpConnectionTest() { 32 BlimpConnectionTest() {
35 message1_ = CreateInputMessage();
36 message2_ = CreateControlMessage();
37 scoped_ptr<testing::StrictMock<MockPacketReader>> reader(
38 new testing::StrictMock<MockPacketReader>);
39 reader_ = reader.get();
40 scoped_ptr<testing::StrictMock<MockPacketWriter>> writer( 33 scoped_ptr<testing::StrictMock<MockPacketWriter>> writer(
41 new testing::StrictMock<MockPacketWriter>); 34 new testing::StrictMock<MockPacketWriter>);
42 writer_ = writer.get(); 35 writer_ = writer.get();
43 connection_.reset(new BlimpConnection(std::move(reader), 36 connection_.reset(new BlimpConnection(make_scoped_ptr(new MockPacketReader),
44 std::move(writer))); 37 std::move(writer)));
45 connection_->SetConnectionErrorObserver(&error_observer_); 38 connection_->SetConnectionErrorObserver(&error_observer_);
46 } 39 }
47 40
48 ~BlimpConnectionTest() override {} 41 ~BlimpConnectionTest() override {}
49 42
43 protected:
50 scoped_ptr<BlimpMessage> CreateInputMessage() { 44 scoped_ptr<BlimpMessage> CreateInputMessage() {
51 scoped_ptr<BlimpMessage> msg(new BlimpMessage); 45 scoped_ptr<BlimpMessage> msg(new BlimpMessage);
52 msg->set_type(BlimpMessage::INPUT); 46 msg->set_type(BlimpMessage::INPUT);
53 return msg; 47 return msg;
54 } 48 }
55 49
56 scoped_ptr<BlimpMessage> CreateControlMessage() { 50 scoped_ptr<BlimpMessage> CreateControlMessage() {
57 scoped_ptr<BlimpMessage> msg(new BlimpMessage); 51 scoped_ptr<BlimpMessage> msg(new BlimpMessage);
58 msg->set_type(BlimpMessage::CONTROL); 52 msg->set_type(BlimpMessage::CONTROL);
59 return msg; 53 return msg;
60 } 54 }
61 55
62 protected: 56 base::MessageLoop message_loop_;
63 base::MessageLoopForIO message_loop_;
64 scoped_ptr<BlimpMessage> message1_;
65 scoped_ptr<BlimpMessage> message2_;
66
67 testing::StrictMock<MockPacketReader>* reader_;
68 testing::StrictMock<MockPacketWriter>* writer_; 57 testing::StrictMock<MockPacketWriter>* writer_;
69 testing::StrictMock<MockConnectionErrorObserver> error_observer_; 58 testing::StrictMock<MockConnectionErrorObserver> error_observer_;
70 testing::StrictMock<MockBlimpMessageProcessor> receiver_; 59 testing::StrictMock<MockBlimpMessageProcessor> receiver_;
71 scoped_ptr<BlimpConnection> connection_; 60 scoped_ptr<BlimpConnection> connection_;
72 }; 61 };
73 62
74 // Reader completes reading one packet synchronously.
75 // Two read cases here. BlimpMessagePumpTest covers other cases.
76 TEST_F(BlimpConnectionTest, SyncPacketRead) {
77 EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _));
78 EXPECT_CALL(*reader_, ReadPacket(NotNull(), _))
79 .WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()),
80 Return(message1_->ByteSize())));
81 connection_->SetIncomingMessageProcessor(&receiver_);
82 }
83
84 // Reader completes reading one packet synchronously withe error.
85 TEST_F(BlimpConnectionTest, SyncPacketReadWithError) {
86 InSequence s;
87 EXPECT_CALL(*reader_, ReadPacket(NotNull(), _))
88 .WillOnce(Return(net::ERR_FAILED));
89 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED));
90 connection_->SetIncomingMessageProcessor(&receiver_);
91 }
92
93 // Writer completes writing two packets synchronously.
94 TEST_F(BlimpConnectionTest, SyncTwoPacketsWrite) {
95 InSequence s;
96 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _))
97 .WillOnce(Return(net::OK))
98 .RetiresOnSaturation();
99 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _))
100 .WillOnce(Return(net::OK))
101 .RetiresOnSaturation();
102
103 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
104 net::TestCompletionCallback complete_cb_1;
105 sender->ProcessMessage(CreateInputMessage(),
106 complete_cb_1.callback());
107 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
108 net::TestCompletionCallback complete_cb_2;
109 sender->ProcessMessage(CreateControlMessage(),
110 complete_cb_2.callback());
111 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult());
112 }
113
114 // Writer completes writing two packets synchronously.
115 // First write succeeds, second fails.
116 TEST_F(BlimpConnectionTest, SyncTwoPacketsWriteWithError) {
117 InSequence s;
118 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _))
119 .WillOnce(Return(net::OK))
120 .RetiresOnSaturation();
121 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _))
122 .WillOnce(Return(net::ERR_FAILED))
123 .RetiresOnSaturation();
124 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED));
125
126 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
127 net::TestCompletionCallback complete_cb_1;
128 sender->ProcessMessage(CreateInputMessage(),
129 complete_cb_1.callback());
130 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
131 net::TestCompletionCallback complete_cb_2;
132 sender->ProcessMessage(CreateControlMessage(),
133 complete_cb_2.callback());
134 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult());
135 }
136
137 // Write completes writing two packets asynchronously. 63 // Write completes writing two packets asynchronously.
138 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) { 64 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWrite) {
139 net::CompletionCallback write_packet_cb; 65 net::CompletionCallback write_packet_cb;
140 66
141 InSequence s; 67 InSequence s;
142 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _)) 68 EXPECT_CALL(*writer_,
143 .WillOnce( 69 WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
144 DoAll(SaveArg<1>(&write_packet_cb), Return(net::ERR_IO_PENDING))) 70 .WillOnce(SaveArg<1>(&write_packet_cb))
145 .RetiresOnSaturation(); 71 .RetiresOnSaturation();
146 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _)) 72 EXPECT_CALL(*writer_,
147 .WillOnce( 73 WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
148 DoAll(SaveArg<1>(&write_packet_cb), Return(net::ERR_IO_PENDING))) 74 .WillOnce(SaveArg<1>(&write_packet_cb))
149 .RetiresOnSaturation(); 75 .RetiresOnSaturation();
150 76
151 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); 77 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
152 net::TestCompletionCallback complete_cb_1; 78 net::TestCompletionCallback complete_cb_1;
153 ASSERT_TRUE(write_packet_cb.is_null()); 79 ASSERT_TRUE(write_packet_cb.is_null());
154 sender->ProcessMessage(CreateInputMessage(), 80 sender->ProcessMessage(CreateInputMessage(),
155 complete_cb_1.callback()); 81 complete_cb_1.callback());
156 ASSERT_FALSE(write_packet_cb.is_null()); 82 ASSERT_FALSE(write_packet_cb.is_null());
157 base::ResetAndReturn(&write_packet_cb).Run(net::OK); 83 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
158 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); 84 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
159 85
160 net::TestCompletionCallback complete_cb_2; 86 net::TestCompletionCallback complete_cb_2;
161 ASSERT_TRUE(write_packet_cb.is_null()); 87 ASSERT_TRUE(write_packet_cb.is_null());
162 sender->ProcessMessage(CreateControlMessage(), 88 sender->ProcessMessage(CreateControlMessage(),
163 complete_cb_2.callback()); 89 complete_cb_2.callback());
164 ASSERT_FALSE(write_packet_cb.is_null()); 90 ASSERT_FALSE(write_packet_cb.is_null());
165 base::ResetAndReturn(&write_packet_cb).Run(net::OK); 91 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
166 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult()); 92 EXPECT_EQ(net::OK, complete_cb_2.WaitForResult());
167 } 93 }
168 94
169 // Writer completes writing two packets asynchronously. 95 // Writer completes writing two packets asynchronously.
170 // First write succeeds, second fails. 96 // First write succeeds, second fails.
171 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) { 97 TEST_F(BlimpConnectionTest, AsyncTwoPacketsWriteWithError) {
172 net::CompletionCallback write_packet_cb; 98 net::CompletionCallback write_packet_cb;
173 99
174 InSequence s; 100 InSequence s;
175 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message1_), _)) 101 EXPECT_CALL(*writer_,
176 .WillOnce( 102 WritePacket(BufferEqualsProto(*CreateInputMessage()), _))
177 DoAll(SaveArg<1>(&write_packet_cb), Return(net::ERR_IO_PENDING))) 103 .WillOnce(SaveArg<1>(&write_packet_cb))
178 .RetiresOnSaturation(); 104 .RetiresOnSaturation();
179 EXPECT_CALL(*writer_, WritePacket(BufferEqualsProto(*message2_), _)) 105 EXPECT_CALL(*writer_,
180 .WillOnce( 106 WritePacket(BufferEqualsProto(*CreateControlMessage()), _))
181 DoAll(SaveArg<1>(&write_packet_cb), Return(net::ERR_IO_PENDING))) 107 .WillOnce(SaveArg<1>(&write_packet_cb))
182 .RetiresOnSaturation(); 108 .RetiresOnSaturation();
183 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED)); 109 EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED));
184 110
185 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor(); 111 BlimpMessageProcessor* sender = connection_->GetOutgoingMessageProcessor();
186 net::TestCompletionCallback complete_cb_1; 112 net::TestCompletionCallback complete_cb_1;
187 sender->ProcessMessage(CreateInputMessage(), 113 sender->ProcessMessage(CreateInputMessage(),
188 complete_cb_1.callback()); 114 complete_cb_1.callback());
189 base::ResetAndReturn(&write_packet_cb).Run(net::OK); 115 base::ResetAndReturn(&write_packet_cb).Run(net::OK);
190 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult()); 116 EXPECT_EQ(net::OK, complete_cb_1.WaitForResult());
191 117
192 net::TestCompletionCallback complete_cb_2; 118 net::TestCompletionCallback complete_cb_2;
193 sender->ProcessMessage(CreateControlMessage(), 119 sender->ProcessMessage(CreateControlMessage(),
194 complete_cb_2.callback()); 120 complete_cb_2.callback());
195 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED); 121 base::ResetAndReturn(&write_packet_cb).Run(net::ERR_FAILED);
196 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult()); 122 EXPECT_EQ(net::ERR_FAILED, complete_cb_2.WaitForResult());
197 } 123 }
198 124
199 } // namespace 125 } // namespace
200
201 } // namespace blimp 126 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/net/blimp_connection.cc ('k') | blimp/net/blimp_message_pump.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698