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

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

Issue 6271004: Changed MessageReader so that it doesn't read from the socket if there are (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ref-counted MessageReader Created 9 years, 11 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
« no previous file with comments | « remoting/protocol/message_reader.cc ('k') | remoting/protocol/protobuf_video_reader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 <string>
6
7 #include "base/message_loop.h"
8 #include "net/socket/socket.h"
9 #include "remoting/protocol/fake_session.h"
10 #include "remoting/protocol/message_reader.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "third_party/libjingle/source/talk/base/byteorder.h"
14
15 using testing::_;
16 using testing::DoAll;
17 using testing::Mock;
18 using testing::SaveArg;
19
20 namespace remoting {
21 namespace protocol {
22
23 namespace {
24 const char kTestMessage1[] = "Message1";
25 const char kTestMessage2[] = "Message2";
26
27 ACTION(CallDoneTask) {
28 arg1->Run();
29 delete arg1;
30 }
31 }
32
33 class MockMessageReceivedCallback {
34 public:
35 MOCK_METHOD2(OnMessage, void(CompoundBuffer*, Task*));
36 };
37
38 class MessageReaderTest : public testing::Test {
39 protected:
40 virtual void SetUp() {
41 reader_ = new MessageReader();
42 }
43
44 void InitReader() {
45 reader_->Init(&socket_, NewCallback(
46 &callback_, &MockMessageReceivedCallback::OnMessage));
47 }
48
49 void AddMessage(const std::string& message) {
50 std::string data = std::string(4, ' ') + message;
51 talk_base::SetBE32(const_cast<char*>(data.data()), message.size());
52
53 socket_.AppendInputData(data.data(), data.size());
54 }
55
56 bool CompareResult(CompoundBuffer* buffer, const std::string& expected) {
57 std::string result(buffer->total_bytes(), ' ');
58 buffer->CopyTo(const_cast<char*>(result.data()), result.size());
59 return result == expected;
60 }
61
62 // MessageLoop must be first here, so that is is destroyed the last.
63 MessageLoop message_loop_;
64
65 scoped_refptr<MessageReader> reader_;
66 FakeSocket socket_;
67 MockMessageReceivedCallback callback_;
68 };
69
70 // Receive one message and process it with delay
71 TEST_F(MessageReaderTest, OneMessage_Delay) {
72 CompoundBuffer* buffer;
73 Task* done_task;
74
75 AddMessage(kTestMessage1);
76
77 EXPECT_CALL(callback_, OnMessage(_, _))
78 .Times(1)
79 .WillOnce(DoAll(SaveArg<0>(&buffer),
80 SaveArg<1>(&done_task)));
81
82 InitReader();
83
84 Mock::VerifyAndClearExpectations(&callback_);
85 Mock::VerifyAndClearExpectations(&socket_);
86
87 EXPECT_TRUE(CompareResult(buffer, kTestMessage1));
88
89 // Verify that the reader starts reading again only after we've
90 // finished processing the previous message.
91 EXPECT_FALSE(socket_.read_pending());
92
93 done_task->Run();
94
95 EXPECT_TRUE(socket_.read_pending());
96 }
97
98 // Receive one message and process it instantly.
99 TEST_F(MessageReaderTest, OneMessage_Instant) {
100 AddMessage(kTestMessage1);
101
102 EXPECT_CALL(callback_, OnMessage(_, _))
103 .Times(1)
104 .WillOnce(CallDoneTask());
105
106 InitReader();
107
108 EXPECT_TRUE(socket_.read_pending());
109 }
110
111 // Receive two messages in one packet.
112 TEST_F(MessageReaderTest, TwoMessages_Together) {
113 CompoundBuffer* buffer1;
114 Task* done_task1;
115 CompoundBuffer* buffer2;
116 Task* done_task2;
117
118 AddMessage(kTestMessage1);
119 AddMessage(kTestMessage2);
120
121 EXPECT_CALL(callback_, OnMessage(_, _))
122 .Times(2)
123 .WillOnce(DoAll(SaveArg<0>(&buffer1),
124 SaveArg<1>(&done_task1)))
125 .WillOnce(DoAll(SaveArg<0>(&buffer2),
126 SaveArg<1>(&done_task2)));
127
128 InitReader();
129
130 Mock::VerifyAndClearExpectations(&callback_);
131 Mock::VerifyAndClearExpectations(&socket_);
132
133 EXPECT_TRUE(CompareResult(buffer1, kTestMessage1));
134 EXPECT_TRUE(CompareResult(buffer2, kTestMessage2));
135
136 // Verify that the reader starts reading again only after we've
137 // finished processing the previous message.
138 EXPECT_FALSE(socket_.read_pending());
139
140 done_task1->Run();
141
142 EXPECT_FALSE(socket_.read_pending());
143
144 done_task2->Run();
145
146 EXPECT_TRUE(socket_.read_pending());
147 }
148
149 // Receive two messages in one packet, and process the first one
150 // instantly.
151 TEST_F(MessageReaderTest, TwoMessages_Instant) {
152 CompoundBuffer* buffer2;
153 Task* done_task2;
154
155 AddMessage(kTestMessage1);
156 AddMessage(kTestMessage2);
157
158 EXPECT_CALL(callback_, OnMessage(_, _))
159 .Times(2)
160 .WillOnce(CallDoneTask())
161 .WillOnce(DoAll(SaveArg<0>(&buffer2),
162 SaveArg<1>(&done_task2)));
163
164 InitReader();
165
166 Mock::VerifyAndClearExpectations(&callback_);
167 Mock::VerifyAndClearExpectations(&socket_);
168
169 EXPECT_TRUE(CompareResult(buffer2, kTestMessage2));
170
171 // Verify that the reader starts reading again only after we've
172 // finished processing the second message.
173 EXPECT_FALSE(socket_.read_pending());
174
175 done_task2->Run();
176
177 EXPECT_TRUE(socket_.read_pending());
178 }
179
180 // Receive two messages in one packet, and process both of them
181 // instantly.
182 TEST_F(MessageReaderTest, TwoMessages_Instant2) {
183 AddMessage(kTestMessage1);
184 AddMessage(kTestMessage2);
185
186 EXPECT_CALL(callback_, OnMessage(_, _))
187 .Times(2)
188 .WillOnce(CallDoneTask())
189 .WillOnce(CallDoneTask());
190
191 InitReader();
192
193 EXPECT_TRUE(socket_.read_pending());
194 }
195
196 // Receive two messages in separate packets.
197 TEST_F(MessageReaderTest, TwoMessages_Separately) {
198 CompoundBuffer* buffer;
199 Task* done_task;
200
201 AddMessage(kTestMessage1);
202
203 EXPECT_CALL(callback_, OnMessage(_, _))
204 .Times(1)
205 .WillOnce(DoAll(SaveArg<0>(&buffer),
206 SaveArg<1>(&done_task)));
207
208 InitReader();
209
210 Mock::VerifyAndClearExpectations(&callback_);
211 Mock::VerifyAndClearExpectations(&socket_);
212
213 EXPECT_TRUE(CompareResult(buffer, kTestMessage1));
214
215 // Verify that the reader starts reading again only after we've
216 // finished processing the previous message.
217 EXPECT_FALSE(socket_.read_pending());
218
219 done_task->Run();
220
221 EXPECT_TRUE(socket_.read_pending());
222
223 // Write another message and verify that we receive it.
224 EXPECT_CALL(callback_, OnMessage(_, _))
225 .Times(1)
226 .WillOnce(DoAll(SaveArg<0>(&buffer),
227 SaveArg<1>(&done_task)));
228 AddMessage(kTestMessage2);
229
230 EXPECT_TRUE(CompareResult(buffer, kTestMessage2));
231
232 // Verify that the reader starts reading again only after we've
233 // finished processing the previous message.
234 EXPECT_FALSE(socket_.read_pending());
235
236 done_task->Run();
237
238 EXPECT_TRUE(socket_.read_pending());
239 }
240
241 } // namespace protocol
242 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/message_reader.cc ('k') | remoting/protocol/protobuf_video_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698