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

Side by Side Diff: remoting/protocol/message_reader_unittest.cc

Issue 1655433002: Remove done notifications from incoming message handlers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months 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 | « remoting/protocol/message_reader.cc ('k') | remoting/protocol/protobuf_message_parser.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 (c) 2012 The Chromium Authors. All rights reserved. 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 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 <string> 5 #include <string>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 12 matching lines...) Expand all
23 using testing::DoAll; 23 using testing::DoAll;
24 using testing::Mock; 24 using testing::Mock;
25 using testing::SaveArg; 25 using testing::SaveArg;
26 26
27 namespace remoting { 27 namespace remoting {
28 namespace protocol { 28 namespace protocol {
29 29
30 namespace { 30 namespace {
31 const char kTestMessage1[] = "Message1"; 31 const char kTestMessage1[] = "Message1";
32 const char kTestMessage2[] = "Message2"; 32 const char kTestMessage2[] = "Message2";
33
34 ACTION(CallDoneTask) {
35 arg0.Run();
36 }
37 } // namespace 33 } // namespace
38 34
39 class MockMessageReceivedCallback { 35 class MockMessageReceivedCallback {
40 public: 36 public:
41 MOCK_METHOD1(OnMessage, void(const base::Closure&)); 37 MOCK_METHOD0(OnMessage, void());
42 }; 38 };
43 39
44 class MessageReaderTest : public testing::Test { 40 class MessageReaderTest : public testing::Test {
45 public: 41 public:
46 MessageReaderTest()
47 : in_callback_(false) {
48 }
49
50 // Following two methods are used by the ReadFromCallback test. 42 // Following two methods are used by the ReadFromCallback test.
51 void AddSecondMessage(const base::Closure& task) { 43 void AddSecondMessage() { AddMessage(kTestMessage2); }
52 AddMessage(kTestMessage2);
53 in_callback_ = true;
54 task.Run();
55 in_callback_ = false;
56 }
57
58 void OnSecondMessage(const base::Closure& task) {
59 EXPECT_FALSE(in_callback_);
60 task.Run();
61 }
62 44
63 // Used by the DeleteFromCallback() test. 45 // Used by the DeleteFromCallback() test.
64 void DeleteReader(const base::Closure& task) { 46 void DeleteReader() { reader_.reset(); }
65 reader_.reset();
66 task.Run();
67 }
68 47
69 protected: 48 protected:
70 void SetUp() override { 49 void SetUp() override {
71 reader_.reset(new MessageReader()); 50 reader_.reset(new MessageReader());
72 } 51 }
73 52
74 void TearDown() override { STLDeleteElements(&messages_); } 53 void TearDown() override { STLDeleteElements(&messages_); }
75 54
76 void InitReader() { 55 void InitReader() {
77 reader_->SetMessageReceivedCallback( 56 reader_->SetMessageReceivedCallback(
(...skipping 13 matching lines...) Expand all
91 std::string result(buffer->total_bytes(), ' '); 70 std::string result(buffer->total_bytes(), ' ');
92 buffer->CopyTo(const_cast<char*>(result.data()), result.size()); 71 buffer->CopyTo(const_cast<char*>(result.data()), result.size());
93 return result == expected; 72 return result == expected;
94 } 73 }
95 74
96 void OnReadError(int error) { 75 void OnReadError(int error) {
97 read_error_ = error; 76 read_error_ = error;
98 reader_.reset(); 77 reader_.reset();
99 } 78 }
100 79
101 void OnMessage(scoped_ptr<CompoundBuffer> buffer, 80 void OnMessage(scoped_ptr<CompoundBuffer> buffer) {
102 const base::Closure& done_callback) {
103 messages_.push_back(buffer.release()); 81 messages_.push_back(buffer.release());
104 callback_.OnMessage(done_callback); 82 callback_.OnMessage();
105 } 83 }
106 84
107 base::MessageLoop message_loop_; 85 base::MessageLoop message_loop_;
108 scoped_ptr<MessageReader> reader_; 86 scoped_ptr<MessageReader> reader_;
109 FakeStreamSocket socket_; 87 FakeStreamSocket socket_;
110 MockMessageReceivedCallback callback_; 88 MockMessageReceivedCallback callback_;
111 int read_error_ = 0; 89 int read_error_ = 0;
112 std::vector<CompoundBuffer*> messages_; 90 std::vector<CompoundBuffer*> messages_;
113 bool in_callback_;
114 }; 91 };
115 92
116 // Receive one message and process it with delay 93 // Receive one message.
117 TEST_F(MessageReaderTest, OneMessage_Delay) { 94 TEST_F(MessageReaderTest, OneMessage) {
118 base::Closure done_task;
119
120 AddMessage(kTestMessage1); 95 AddMessage(kTestMessage1);
121 96
122 EXPECT_CALL(callback_, OnMessage(_)) 97 EXPECT_CALL(callback_, OnMessage()).Times(1);
123 .Times(1)
124 .WillOnce(SaveArg<0>(&done_task));
125 98
126 InitReader(); 99 InitReader();
127 base::RunLoop().RunUntilIdle(); 100 base::RunLoop().RunUntilIdle();
128
129 Mock::VerifyAndClearExpectations(&callback_);
130 Mock::VerifyAndClearExpectations(&socket_);
131
132 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
133
134 // Verify that the reader starts reading again only after we've
135 // finished processing the previous message.
136 EXPECT_FALSE(socket_.read_pending());
137
138 done_task.Run();
139
140 EXPECT_TRUE(socket_.read_pending());
141 }
142
143 // Receive one message and process it instantly.
144 TEST_F(MessageReaderTest, OneMessage_Instant) {
145 AddMessage(kTestMessage1);
146
147 EXPECT_CALL(callback_, OnMessage(_))
148 .Times(1)
149 .WillOnce(CallDoneTask());
150
151 InitReader();
152 base::RunLoop().RunUntilIdle();
153 101
154 EXPECT_TRUE(socket_.read_pending()); 102 EXPECT_TRUE(socket_.read_pending());
155 EXPECT_EQ(1U, messages_.size()); 103 EXPECT_EQ(1U, messages_.size());
156 } 104 }
157 105
158 // Receive two messages in one packet. 106 // Receive two messages in one packet.
159 TEST_F(MessageReaderTest, TwoMessages_Together) { 107 TEST_F(MessageReaderTest, TwoMessages_Together) {
160 base::Closure done_task1;
161 base::Closure done_task2;
162
163 AddMessage(kTestMessage1); 108 AddMessage(kTestMessage1);
164 AddMessage(kTestMessage2); 109 AddMessage(kTestMessage2);
165 110
166 EXPECT_CALL(callback_, OnMessage(_)) 111 EXPECT_CALL(callback_, OnMessage()).Times(2);
167 .Times(2)
168 .WillOnce(SaveArg<0>(&done_task1))
169 .WillOnce(SaveArg<0>(&done_task2));
170 112
171 InitReader(); 113 InitReader();
172 base::RunLoop().RunUntilIdle(); 114 base::RunLoop().RunUntilIdle();
173 115
174 Mock::VerifyAndClearExpectations(&callback_); 116 Mock::VerifyAndClearExpectations(&callback_);
175 Mock::VerifyAndClearExpectations(&socket_); 117 Mock::VerifyAndClearExpectations(&socket_);
176 118
177 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1)); 119 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
178 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2)); 120 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
179 121
180 // Verify that the reader starts reading again only after we've
181 // finished processing the previous message.
182 EXPECT_FALSE(socket_.read_pending());
183
184 done_task1.Run();
185 base::RunLoop().RunUntilIdle();
186
187 EXPECT_FALSE(socket_.read_pending());
188
189 done_task2.Run();
190 base::RunLoop().RunUntilIdle();
191
192 EXPECT_TRUE(socket_.read_pending());
193 }
194
195 // Receive two messages in one packet, and process the first one
196 // instantly.
197 TEST_F(MessageReaderTest, TwoMessages_Instant) {
198 base::Closure done_task2;
199
200 AddMessage(kTestMessage1);
201 AddMessage(kTestMessage2);
202
203 EXPECT_CALL(callback_, OnMessage(_))
204 .Times(2)
205 .WillOnce(CallDoneTask())
206 .WillOnce(SaveArg<0>(&done_task2));
207
208 InitReader();
209 base::RunLoop().RunUntilIdle();
210
211 Mock::VerifyAndClearExpectations(&callback_);
212 Mock::VerifyAndClearExpectations(&socket_);
213
214 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
215
216 // Verify that the reader starts reading again only after we've
217 // finished processing the second message.
218 EXPECT_FALSE(socket_.read_pending());
219
220 done_task2.Run();
221
222 EXPECT_TRUE(socket_.read_pending());
223 }
224
225 // Receive two messages in one packet, and process both of them
226 // instantly.
227 TEST_F(MessageReaderTest, TwoMessages_Instant2) {
228 AddMessage(kTestMessage1);
229 AddMessage(kTestMessage2);
230
231 EXPECT_CALL(callback_, OnMessage(_))
232 .Times(2)
233 .WillOnce(CallDoneTask())
234 .WillOnce(CallDoneTask());
235
236 InitReader();
237 base::RunLoop().RunUntilIdle();
238
239 EXPECT_TRUE(socket_.read_pending()); 122 EXPECT_TRUE(socket_.read_pending());
240 } 123 }
241 124
242 // Receive two messages in separate packets. 125 // Receive two messages in separate packets.
243 TEST_F(MessageReaderTest, TwoMessages_Separately) { 126 TEST_F(MessageReaderTest, TwoMessages_Separately) {
244 base::Closure done_task;
245
246 AddMessage(kTestMessage1); 127 AddMessage(kTestMessage1);
247 128
248 EXPECT_CALL(callback_, OnMessage(_)) 129 EXPECT_CALL(callback_, OnMessage())
249 .Times(1) 130 .Times(1);
250 .WillOnce(SaveArg<0>(&done_task));
251 131
252 InitReader(); 132 InitReader();
253 base::RunLoop().RunUntilIdle(); 133 base::RunLoop().RunUntilIdle();
254 134
255 Mock::VerifyAndClearExpectations(&callback_); 135 Mock::VerifyAndClearExpectations(&callback_);
256 Mock::VerifyAndClearExpectations(&socket_); 136 Mock::VerifyAndClearExpectations(&socket_);
257 137
258 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1)); 138 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
259 139
260 // Verify that the reader starts reading again only after we've
261 // finished processing the previous message.
262 EXPECT_FALSE(socket_.read_pending());
263
264 done_task.Run();
265 base::RunLoop().RunUntilIdle();
266
267 EXPECT_TRUE(socket_.read_pending()); 140 EXPECT_TRUE(socket_.read_pending());
268 141
269 // Write another message and verify that we receive it. 142 // Write another message and verify that we receive it.
270 EXPECT_CALL(callback_, OnMessage(_)) 143 EXPECT_CALL(callback_, OnMessage())
271 .Times(1) 144 .Times(1);
272 .WillOnce(SaveArg<0>(&done_task));
273 AddMessage(kTestMessage2); 145 AddMessage(kTestMessage2);
274 base::RunLoop().RunUntilIdle(); 146 base::RunLoop().RunUntilIdle();
275 147
276 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2)); 148 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
277 149
278 // Verify that the reader starts reading again only after we've
279 // finished processing the previous message.
280 EXPECT_FALSE(socket_.read_pending());
281
282 done_task.Run();
283
284 EXPECT_TRUE(socket_.read_pending()); 150 EXPECT_TRUE(socket_.read_pending());
285 } 151 }
286 152
287 // Read() returns error. 153 // Read() returns error.
288 TEST_F(MessageReaderTest, ReadError) { 154 TEST_F(MessageReaderTest, ReadError) {
289 socket_.AppendReadError(net::ERR_FAILED); 155 socket_.AppendReadError(net::ERR_FAILED);
290 156
291 EXPECT_CALL(callback_, OnMessage(_)).Times(0); 157 EXPECT_CALL(callback_, OnMessage()).Times(0);
292 158
293 InitReader(); 159 InitReader();
294 160
295 EXPECT_EQ(net::ERR_FAILED, read_error_); 161 EXPECT_EQ(net::ERR_FAILED, read_error_);
296 EXPECT_FALSE(reader_); 162 EXPECT_FALSE(reader_);
297 } 163 }
298 164
299 // Verify that we the OnMessage callback is not reentered. 165 // Verify that we the OnMessage callback is not reentered.
300 TEST_F(MessageReaderTest, ReadFromCallback) { 166 TEST_F(MessageReaderTest, ReadFromCallback) {
301 AddMessage(kTestMessage1); 167 AddMessage(kTestMessage1);
302 168
303 EXPECT_CALL(callback_, OnMessage(_)) 169 EXPECT_CALL(callback_, OnMessage())
304 .Times(2) 170 .Times(2)
305 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage)) 171 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage));
306 .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage));
307 172
308 InitReader(); 173 InitReader();
309 base::RunLoop().RunUntilIdle(); 174 base::RunLoop().RunUntilIdle();
310 175
311 EXPECT_TRUE(socket_.read_pending()); 176 EXPECT_TRUE(socket_.read_pending());
312 } 177 }
313 178
314 // Verify that we stop getting callbacks after deleting MessageReader. 179 // Verify that we stop getting callbacks after deleting MessageReader.
315 TEST_F(MessageReaderTest, DeleteFromCallback) { 180 TEST_F(MessageReaderTest, DeleteFromCallback) {
316 base::Closure done_task1;
317 base::Closure done_task2;
318
319 AddMessage(kTestMessage1); 181 AddMessage(kTestMessage1);
320 AddMessage(kTestMessage2); 182 AddMessage(kTestMessage2);
321 183
322 // OnMessage() should never be called for the second message. 184 // OnMessage() should never be called for the second message.
323 EXPECT_CALL(callback_, OnMessage(_)) 185 EXPECT_CALL(callback_, OnMessage())
324 .Times(1) 186 .Times(1)
325 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader)); 187 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader));
326 188
327 InitReader(); 189 InitReader();
328 base::RunLoop().RunUntilIdle(); 190 base::RunLoop().RunUntilIdle();
329 } 191 }
330 192
331 } // namespace protocol 193 } // namespace protocol
332 } // namespace remoting 194 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/message_reader.cc ('k') | remoting/protocol/protobuf_message_parser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698