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

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

Issue 9827006: Refactor VideoStub interface to accept ownership of video packets. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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.h" 10 #include "base/message_loop.h"
11 #include "base/stl_util.h"
11 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
13 #include "net/socket/socket.h" 14 #include "net/socket/socket.h"
14 #include "remoting/protocol/fake_session.h" 15 #include "remoting/protocol/fake_session.h"
15 #include "remoting/protocol/message_reader.h" 16 #include "remoting/protocol/message_reader.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
18 #include "third_party/libjingle/source/talk/base/byteorder.h" 19 #include "third_party/libjingle/source/talk/base/byteorder.h"
19 20
20 using testing::_; 21 using testing::_;
21 using testing::DoAll; 22 using testing::DoAll;
22 using testing::Mock; 23 using testing::Mock;
23 using testing::SaveArg; 24 using testing::SaveArg;
24 25
25 namespace remoting { 26 namespace remoting {
26 namespace protocol { 27 namespace protocol {
27 28
28 namespace { 29 namespace {
29 const char kTestMessage1[] = "Message1"; 30 const char kTestMessage1[] = "Message1";
30 const char kTestMessage2[] = "Message2"; 31 const char kTestMessage2[] = "Message2";
31 32
32 ACTION(CallDoneTask) { 33 ACTION(CallDoneTask) {
33 arg1.Run(); 34 arg0.Run();
34 } 35 }
35 } 36 } // namespace
36 37
37 class MockMessageReceivedCallback { 38 class MockMessageReceivedCallback {
38 public: 39 public:
39 MOCK_METHOD2(OnMessage, void(CompoundBuffer*, const base::Closure&)); 40 MOCK_METHOD1(OnMessage, void(const base::Closure&));
40 }; 41 };
41 42
42 class MessageReaderTest : public testing::Test { 43 class MessageReaderTest : public testing::Test {
43 public: 44 public:
44 MessageReaderTest() 45 MessageReaderTest()
45 : other_thread_("SecondTestThread"), 46 : other_thread_("SecondTestThread"),
46 run_task_finished_(false, false) { 47 run_task_finished_(false, false) {
47 } 48 }
48 49
49 void RunDoneTaskOnOtherThread(CompoundBuffer* buffer, 50 void RunDoneTaskOnOtherThread(const base::Closure& done_task) {
50 const base::Closure& done_task) {
51 other_thread_.message_loop()->PostTask( 51 other_thread_.message_loop()->PostTask(
52 FROM_HERE, 52 FROM_HERE,
53 base::Bind(&MessageReaderTest::RunAndDeleteTask, 53 base::Bind(&MessageReaderTest::RunClosure,
54 base::Unretained(this), done_task)); 54 base::Unretained(this), done_task));
55 } 55 }
56 56
57 protected: 57 protected:
58 virtual void SetUp() { 58 virtual void SetUp() OVERRIDE {
59 reader_ = new MessageReader(); 59 reader_ = new MessageReader();
60 } 60 }
61 61
62 virtual void TearDown() OVERRIDE {
63 STLDeleteElements(&messages_);
64 }
65
62 void InitReader() { 66 void InitReader() {
63 reader_->Init(&socket_, base::Bind( 67 reader_->Init(&socket_, base::Bind(
64 &MockMessageReceivedCallback::OnMessage, base::Unretained(&callback_))); 68 &MessageReaderTest::OnMessage, base::Unretained(this)));
65 } 69 }
66 70
67 void AddMessage(const std::string& message) { 71 void AddMessage(const std::string& message) {
68 std::string data = std::string(4, ' ') + message; 72 std::string data = std::string(4, ' ') + message;
69 talk_base::SetBE32(const_cast<char*>(data.data()), message.size()); 73 talk_base::SetBE32(const_cast<char*>(data.data()), message.size());
70 74
71 socket_.AppendInputData(std::vector<char>(data.begin(), data.end())); 75 socket_.AppendInputData(std::vector<char>(data.begin(), data.end()));
72 } 76 }
73 77
74 bool CompareResult(CompoundBuffer* buffer, const std::string& expected) { 78 bool CompareResult(CompoundBuffer* buffer, const std::string& expected) {
75 std::string result(buffer->total_bytes(), ' '); 79 std::string result(buffer->total_bytes(), ' ');
76 buffer->CopyTo(const_cast<char*>(result.data()), result.size()); 80 buffer->CopyTo(const_cast<char*>(result.data()), result.size());
77 return result == expected; 81 return result == expected;
78 } 82 }
79 83
80 void RunAndDeleteTask(const base::Closure& task) { 84 void RunClosure(const base::Closure& task) {
81 task.Run(); 85 task.Run();
82 run_task_finished_.Signal(); 86 run_task_finished_.Signal();
83 } 87 }
84 88
89 void OnMessage(scoped_ptr<CompoundBuffer> buffer,
90 const base::Closure& done_callback) {
91 messages_.push_back(buffer.release());
92 callback_.OnMessage(done_callback);
93 }
94
85 MessageLoop message_loop_; 95 MessageLoop message_loop_;
86 base::Thread other_thread_; 96 base::Thread other_thread_;
87 base::WaitableEvent run_task_finished_; 97 base::WaitableEvent run_task_finished_;
88 scoped_refptr<MessageReader> reader_; 98 scoped_refptr<MessageReader> reader_;
89 FakeSocket socket_; 99 FakeSocket socket_;
90 MockMessageReceivedCallback callback_; 100 MockMessageReceivedCallback callback_;
101 std::vector<CompoundBuffer*> messages_;
Alpha Left Google 2012/03/26 20:29:18 use vector<scoped_ptr<CompoundBuffer> > also.
Sergey Ulanov 2012/03/26 20:42:13 Same problem here. vector<scoped_ptr<CompoundBuffe
91 }; 102 };
92 103
93 // Receive one message and process it with delay 104 // Receive one message and process it with delay
94 TEST_F(MessageReaderTest, OneMessage_Delay) { 105 TEST_F(MessageReaderTest, OneMessage_Delay) {
95 CompoundBuffer* buffer;
96 base::Closure done_task; 106 base::Closure done_task;
97 107
98 AddMessage(kTestMessage1); 108 AddMessage(kTestMessage1);
99 109
100 EXPECT_CALL(callback_, OnMessage(_, _)) 110 EXPECT_CALL(callback_, OnMessage(_))
101 .Times(1) 111 .Times(1)
102 .WillOnce(DoAll(SaveArg<0>(&buffer), 112 .WillOnce(SaveArg<0>(&done_task));
103 SaveArg<1>(&done_task)));
104 113
105 InitReader(); 114 InitReader();
106 115
107 Mock::VerifyAndClearExpectations(&callback_); 116 Mock::VerifyAndClearExpectations(&callback_);
108 Mock::VerifyAndClearExpectations(&socket_); 117 Mock::VerifyAndClearExpectations(&socket_);
109 118
110 EXPECT_TRUE(CompareResult(buffer, kTestMessage1)); 119 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
111 120
112 // Verify that the reader starts reading again only after we've 121 // Verify that the reader starts reading again only after we've
113 // finished processing the previous message. 122 // finished processing the previous message.
114 EXPECT_FALSE(socket_.read_pending()); 123 EXPECT_FALSE(socket_.read_pending());
115 124
116 RunAndDeleteTask(done_task); 125 done_task.Run();
117 126
118 EXPECT_TRUE(socket_.read_pending()); 127 EXPECT_TRUE(socket_.read_pending());
119 } 128 }
120 129
121 // Receive one message and process it instantly. 130 // Receive one message and process it instantly.
122 TEST_F(MessageReaderTest, OneMessage_Instant) { 131 TEST_F(MessageReaderTest, OneMessage_Instant) {
123 AddMessage(kTestMessage1); 132 AddMessage(kTestMessage1);
124 133
125 EXPECT_CALL(callback_, OnMessage(_, _)) 134 EXPECT_CALL(callback_, OnMessage(_))
126 .Times(1) 135 .Times(1)
127 .WillOnce(CallDoneTask()); 136 .WillOnce(CallDoneTask());
128 137
129 InitReader(); 138 InitReader();
130 139
131 EXPECT_TRUE(socket_.read_pending()); 140 EXPECT_TRUE(socket_.read_pending());
141 EXPECT_EQ(1U, messages_.size());
132 } 142 }
133 143
134 // Receive two messages in one packet. 144 // Receive two messages in one packet.
135 TEST_F(MessageReaderTest, TwoMessages_Together) { 145 TEST_F(MessageReaderTest, TwoMessages_Together) {
136 CompoundBuffer* buffer1;
137 base::Closure done_task1; 146 base::Closure done_task1;
138 CompoundBuffer* buffer2;
139 base::Closure done_task2; 147 base::Closure done_task2;
140 148
141 AddMessage(kTestMessage1); 149 AddMessage(kTestMessage1);
142 AddMessage(kTestMessage2); 150 AddMessage(kTestMessage2);
143 151
144 EXPECT_CALL(callback_, OnMessage(_, _)) 152 EXPECT_CALL(callback_, OnMessage(_))
145 .Times(2) 153 .Times(2)
146 .WillOnce(DoAll(SaveArg<0>(&buffer1), 154 .WillOnce(SaveArg<0>(&done_task1))
147 SaveArg<1>(&done_task1))) 155 .WillOnce(SaveArg<0>(&done_task2));
148 .WillOnce(DoAll(SaveArg<0>(&buffer2),
149 SaveArg<1>(&done_task2)));
150 156
151 InitReader(); 157 InitReader();
152 158
153 Mock::VerifyAndClearExpectations(&callback_); 159 Mock::VerifyAndClearExpectations(&callback_);
154 Mock::VerifyAndClearExpectations(&socket_); 160 Mock::VerifyAndClearExpectations(&socket_);
155 161
156 EXPECT_TRUE(CompareResult(buffer1, kTestMessage1)); 162 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
157 EXPECT_TRUE(CompareResult(buffer2, kTestMessage2)); 163 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
158 164
159 // Verify that the reader starts reading again only after we've 165 // Verify that the reader starts reading again only after we've
160 // finished processing the previous message. 166 // finished processing the previous message.
161 EXPECT_FALSE(socket_.read_pending()); 167 EXPECT_FALSE(socket_.read_pending());
162 168
163 RunAndDeleteTask(done_task1); 169 done_task1.Run();
164 170
165 EXPECT_FALSE(socket_.read_pending()); 171 EXPECT_FALSE(socket_.read_pending());
166 172
167 RunAndDeleteTask(done_task2); 173 done_task2.Run();
168 174
169 EXPECT_TRUE(socket_.read_pending()); 175 EXPECT_TRUE(socket_.read_pending());
170 } 176 }
171 177
172 // Receive two messages in one packet, and process the first one 178 // Receive two messages in one packet, and process the first one
173 // instantly. 179 // instantly.
174 TEST_F(MessageReaderTest, TwoMessages_Instant) { 180 TEST_F(MessageReaderTest, TwoMessages_Instant) {
175 CompoundBuffer* buffer2;
176 base::Closure done_task2; 181 base::Closure done_task2;
177 182
178 AddMessage(kTestMessage1); 183 AddMessage(kTestMessage1);
179 AddMessage(kTestMessage2); 184 AddMessage(kTestMessage2);
180 185
181 EXPECT_CALL(callback_, OnMessage(_, _)) 186 EXPECT_CALL(callback_, OnMessage(_))
182 .Times(2) 187 .Times(2)
183 .WillOnce(CallDoneTask()) 188 .WillOnce(CallDoneTask())
184 .WillOnce(DoAll(SaveArg<0>(&buffer2), 189 .WillOnce(SaveArg<0>(&done_task2));
185 SaveArg<1>(&done_task2)));
186 190
187 InitReader(); 191 InitReader();
188 192
189 Mock::VerifyAndClearExpectations(&callback_); 193 Mock::VerifyAndClearExpectations(&callback_);
190 Mock::VerifyAndClearExpectations(&socket_); 194 Mock::VerifyAndClearExpectations(&socket_);
191 195
192 EXPECT_TRUE(CompareResult(buffer2, kTestMessage2)); 196 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
193 197
194 // Verify that the reader starts reading again only after we've 198 // Verify that the reader starts reading again only after we've
195 // finished processing the second message. 199 // finished processing the second message.
196 EXPECT_FALSE(socket_.read_pending()); 200 EXPECT_FALSE(socket_.read_pending());
197 201
198 RunAndDeleteTask(done_task2); 202 done_task2.Run();
199 203
200 EXPECT_TRUE(socket_.read_pending()); 204 EXPECT_TRUE(socket_.read_pending());
201 } 205 }
202 206
203 // Receive two messages in one packet, and process both of them 207 // Receive two messages in one packet, and process both of them
204 // instantly. 208 // instantly.
205 TEST_F(MessageReaderTest, TwoMessages_Instant2) { 209 TEST_F(MessageReaderTest, TwoMessages_Instant2) {
206 AddMessage(kTestMessage1); 210 AddMessage(kTestMessage1);
207 AddMessage(kTestMessage2); 211 AddMessage(kTestMessage2);
208 212
209 EXPECT_CALL(callback_, OnMessage(_, _)) 213 EXPECT_CALL(callback_, OnMessage(_))
210 .Times(2) 214 .Times(2)
211 .WillOnce(CallDoneTask()) 215 .WillOnce(CallDoneTask())
212 .WillOnce(CallDoneTask()); 216 .WillOnce(CallDoneTask());
213 217
214 InitReader(); 218 InitReader();
215 219
216 EXPECT_TRUE(socket_.read_pending()); 220 EXPECT_TRUE(socket_.read_pending());
217 } 221 }
218 222
219 // Receive two messages in separate packets. 223 // Receive two messages in separate packets.
220 TEST_F(MessageReaderTest, TwoMessages_Separately) { 224 TEST_F(MessageReaderTest, TwoMessages_Separately) {
221 CompoundBuffer* buffer;
222 base::Closure done_task; 225 base::Closure done_task;
223 226
224 AddMessage(kTestMessage1); 227 AddMessage(kTestMessage1);
225 228
226 EXPECT_CALL(callback_, OnMessage(_, _)) 229 EXPECT_CALL(callback_, OnMessage(_))
227 .Times(1) 230 .Times(1)
228 .WillOnce(DoAll(SaveArg<0>(&buffer), 231 .WillOnce(SaveArg<0>(&done_task));
229 SaveArg<1>(&done_task)));
230 232
231 InitReader(); 233 InitReader();
232 234
233 Mock::VerifyAndClearExpectations(&callback_); 235 Mock::VerifyAndClearExpectations(&callback_);
234 Mock::VerifyAndClearExpectations(&socket_); 236 Mock::VerifyAndClearExpectations(&socket_);
235 237
236 EXPECT_TRUE(CompareResult(buffer, kTestMessage1)); 238 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
237 239
238 // Verify that the reader starts reading again only after we've 240 // Verify that the reader starts reading again only after we've
239 // finished processing the previous message. 241 // finished processing the previous message.
240 EXPECT_FALSE(socket_.read_pending()); 242 EXPECT_FALSE(socket_.read_pending());
241 243
242 RunAndDeleteTask(done_task); 244 done_task.Run();
243 245
244 EXPECT_TRUE(socket_.read_pending()); 246 EXPECT_TRUE(socket_.read_pending());
245 247
246 // Write another message and verify that we receive it. 248 // Write another message and verify that we receive it.
247 EXPECT_CALL(callback_, OnMessage(_, _)) 249 EXPECT_CALL(callback_, OnMessage(_))
248 .Times(1) 250 .Times(1)
249 .WillOnce(DoAll(SaveArg<0>(&buffer), 251 .WillOnce(SaveArg<0>(&done_task));
250 SaveArg<1>(&done_task)));
251 AddMessage(kTestMessage2); 252 AddMessage(kTestMessage2);
252 253
253 EXPECT_TRUE(CompareResult(buffer, kTestMessage2)); 254 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
254 255
255 // Verify that the reader starts reading again only after we've 256 // Verify that the reader starts reading again only after we've
256 // finished processing the previous message. 257 // finished processing the previous message.
257 EXPECT_FALSE(socket_.read_pending()); 258 EXPECT_FALSE(socket_.read_pending());
258 259
259 RunAndDeleteTask(done_task); 260 done_task.Run();
260 261
261 EXPECT_TRUE(socket_.read_pending()); 262 EXPECT_TRUE(socket_.read_pending());
262 } 263 }
263 264
264 // Verify that socket operations occur on same thread, even when the OnMessage() 265 // Verify that socket operations occur on same thread, even when the OnMessage()
265 // callback triggers |done_task| to run on a different thread. 266 // callback triggers |done_task| to run on a different thread.
266 TEST_F(MessageReaderTest, UseSocketOnCorrectThread) { 267 TEST_F(MessageReaderTest, UseSocketOnCorrectThread) {
267
268 AddMessage(kTestMessage1); 268 AddMessage(kTestMessage1);
269 other_thread_.Start(); 269 other_thread_.Start();
270 270
271 EXPECT_CALL(callback_, OnMessage(_, _)) 271 EXPECT_CALL(callback_, OnMessage(_))
272 .Times(1) 272 .Times(1)
273 .WillOnce(Invoke(this, &MessageReaderTest::RunDoneTaskOnOtherThread)); 273 .WillOnce(Invoke(this, &MessageReaderTest::RunDoneTaskOnOtherThread));
274 274
275 InitReader(); 275 InitReader();
276 276
277 run_task_finished_.Wait(); 277 run_task_finished_.Wait();
278 message_loop_.RunAllPending(); 278 message_loop_.RunAllPending();
279 279
280 // Write another message and verify that we receive it. 280 // Write another message and verify that we receive it.
281 CompoundBuffer* buffer;
282 base::Closure done_task; 281 base::Closure done_task;
283 EXPECT_CALL(callback_, OnMessage(_, _)) 282 EXPECT_CALL(callback_, OnMessage(_))
284 .Times(1) 283 .Times(1)
285 .WillOnce(DoAll(SaveArg<0>(&buffer), 284 .WillOnce(SaveArg<0>(&done_task));
286 SaveArg<1>(&done_task)));
287 AddMessage(kTestMessage2); 285 AddMessage(kTestMessage2);
288 EXPECT_TRUE(CompareResult(buffer, kTestMessage2)); 286 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
289 287
290 RunAndDeleteTask(done_task); 288 done_task.Run();
291 } 289 }
292 290
293 } // namespace protocol 291 } // namespace protocol
294 } // namespace remoting 292 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698