Instrumenting internals of AccountIdFetcher::Start to locate the source of jankiness.
[chromium-blink-merge.git] / remoting / protocol / message_reader_unittest.cc
blob5c43788a299391adb3f337e6b96ee238ed28f98f
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
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/stl_util.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "net/base/net_errors.h"
15 #include "net/socket/socket.h"
16 #include "remoting/protocol/fake_stream_socket.h"
17 #include "remoting/protocol/message_reader.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/webrtc/base/byteorder.h"
22 using testing::_;
23 using testing::DoAll;
24 using testing::Mock;
25 using testing::SaveArg;
27 namespace remoting {
28 namespace protocol {
30 namespace {
31 const char kTestMessage1[] = "Message1";
32 const char kTestMessage2[] = "Message2";
34 ACTION(CallDoneTask) {
35 arg0.Run();
37 } // namespace
39 class MockMessageReceivedCallback {
40 public:
41 MOCK_METHOD1(OnMessage, void(const base::Closure&));
44 class MessageReaderTest : public testing::Test {
45 public:
46 MessageReaderTest()
47 : in_callback_(false) {
50 // Following two methods are used by the ReadFromCallback test.
51 void AddSecondMessage(const base::Closure& task) {
52 AddMessage(kTestMessage2);
53 in_callback_ = true;
54 task.Run();
55 in_callback_ = false;
58 void OnSecondMessage(const base::Closure& task) {
59 EXPECT_FALSE(in_callback_);
60 task.Run();
63 // Used by the DeleteFromCallback() test.
64 void DeleteReader(const base::Closure& task) {
65 reader_.reset();
66 task.Run();
69 protected:
70 void SetUp() override { reader_.reset(new MessageReader()); }
72 void TearDown() override { STLDeleteElements(&messages_); }
74 void InitReader() {
75 reader_->Init(&socket_, base::Bind(
76 &MessageReaderTest::OnMessage, base::Unretained(this)));
79 void AddMessage(const std::string& message) {
80 std::string data = std::string(4, ' ') + message;
81 rtc::SetBE32(const_cast<char*>(data.data()), message.size());
83 socket_.AppendInputData(data);
86 bool CompareResult(CompoundBuffer* buffer, const std::string& expected) {
87 std::string result(buffer->total_bytes(), ' ');
88 buffer->CopyTo(const_cast<char*>(result.data()), result.size());
89 return result == expected;
92 void OnMessage(scoped_ptr<CompoundBuffer> buffer,
93 const base::Closure& done_callback) {
94 messages_.push_back(buffer.release());
95 callback_.OnMessage(done_callback);
98 base::MessageLoop message_loop_;
99 scoped_ptr<MessageReader> reader_;
100 FakeStreamSocket socket_;
101 MockMessageReceivedCallback callback_;
102 std::vector<CompoundBuffer*> messages_;
103 bool in_callback_;
106 // Receive one message and process it with delay
107 TEST_F(MessageReaderTest, OneMessage_Delay) {
108 base::Closure done_task;
110 AddMessage(kTestMessage1);
112 EXPECT_CALL(callback_, OnMessage(_))
113 .Times(1)
114 .WillOnce(SaveArg<0>(&done_task));
116 InitReader();
117 base::RunLoop().RunUntilIdle();
119 Mock::VerifyAndClearExpectations(&callback_);
120 Mock::VerifyAndClearExpectations(&socket_);
122 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
124 // Verify that the reader starts reading again only after we've
125 // finished processing the previous message.
126 EXPECT_FALSE(socket_.read_pending());
128 done_task.Run();
130 EXPECT_TRUE(socket_.read_pending());
133 // Receive one message and process it instantly.
134 TEST_F(MessageReaderTest, OneMessage_Instant) {
135 AddMessage(kTestMessage1);
137 EXPECT_CALL(callback_, OnMessage(_))
138 .Times(1)
139 .WillOnce(CallDoneTask());
141 InitReader();
142 base::RunLoop().RunUntilIdle();
144 EXPECT_TRUE(socket_.read_pending());
145 EXPECT_EQ(1U, messages_.size());
148 // Receive two messages in one packet.
149 TEST_F(MessageReaderTest, TwoMessages_Together) {
150 base::Closure done_task1;
151 base::Closure done_task2;
153 AddMessage(kTestMessage1);
154 AddMessage(kTestMessage2);
156 EXPECT_CALL(callback_, OnMessage(_))
157 .Times(2)
158 .WillOnce(SaveArg<0>(&done_task1))
159 .WillOnce(SaveArg<0>(&done_task2));
161 InitReader();
162 base::RunLoop().RunUntilIdle();
164 Mock::VerifyAndClearExpectations(&callback_);
165 Mock::VerifyAndClearExpectations(&socket_);
167 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
168 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
170 // Verify that the reader starts reading again only after we've
171 // finished processing the previous message.
172 EXPECT_FALSE(socket_.read_pending());
174 done_task1.Run();
175 base::RunLoop().RunUntilIdle();
177 EXPECT_FALSE(socket_.read_pending());
179 done_task2.Run();
180 base::RunLoop().RunUntilIdle();
182 EXPECT_TRUE(socket_.read_pending());
185 // Receive two messages in one packet, and process the first one
186 // instantly.
187 TEST_F(MessageReaderTest, TwoMessages_Instant) {
188 base::Closure done_task2;
190 AddMessage(kTestMessage1);
191 AddMessage(kTestMessage2);
193 EXPECT_CALL(callback_, OnMessage(_))
194 .Times(2)
195 .WillOnce(CallDoneTask())
196 .WillOnce(SaveArg<0>(&done_task2));
198 InitReader();
199 base::RunLoop().RunUntilIdle();
201 Mock::VerifyAndClearExpectations(&callback_);
202 Mock::VerifyAndClearExpectations(&socket_);
204 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
206 // Verify that the reader starts reading again only after we've
207 // finished processing the second message.
208 EXPECT_FALSE(socket_.read_pending());
210 done_task2.Run();
212 EXPECT_TRUE(socket_.read_pending());
215 // Receive two messages in one packet, and process both of them
216 // instantly.
217 TEST_F(MessageReaderTest, TwoMessages_Instant2) {
218 AddMessage(kTestMessage1);
219 AddMessage(kTestMessage2);
221 EXPECT_CALL(callback_, OnMessage(_))
222 .Times(2)
223 .WillOnce(CallDoneTask())
224 .WillOnce(CallDoneTask());
226 InitReader();
227 base::RunLoop().RunUntilIdle();
229 EXPECT_TRUE(socket_.read_pending());
232 // Receive two messages in separate packets.
233 TEST_F(MessageReaderTest, TwoMessages_Separately) {
234 base::Closure done_task;
236 AddMessage(kTestMessage1);
238 EXPECT_CALL(callback_, OnMessage(_))
239 .Times(1)
240 .WillOnce(SaveArg<0>(&done_task));
242 InitReader();
243 base::RunLoop().RunUntilIdle();
245 Mock::VerifyAndClearExpectations(&callback_);
246 Mock::VerifyAndClearExpectations(&socket_);
248 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
250 // Verify that the reader starts reading again only after we've
251 // finished processing the previous message.
252 EXPECT_FALSE(socket_.read_pending());
254 done_task.Run();
255 base::RunLoop().RunUntilIdle();
257 EXPECT_TRUE(socket_.read_pending());
259 // Write another message and verify that we receive it.
260 EXPECT_CALL(callback_, OnMessage(_))
261 .Times(1)
262 .WillOnce(SaveArg<0>(&done_task));
263 AddMessage(kTestMessage2);
264 base::RunLoop().RunUntilIdle();
266 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
268 // Verify that the reader starts reading again only after we've
269 // finished processing the previous message.
270 EXPECT_FALSE(socket_.read_pending());
272 done_task.Run();
274 EXPECT_TRUE(socket_.read_pending());
277 // Read() returns error.
278 TEST_F(MessageReaderTest, ReadError) {
279 socket_.set_next_read_error(net::ERR_FAILED);
281 // Add a message. It should never be read after the error above.
282 AddMessage(kTestMessage1);
284 EXPECT_CALL(callback_, OnMessage(_))
285 .Times(0);
287 InitReader();
290 // Verify that we the OnMessage callback is not reentered.
291 TEST_F(MessageReaderTest, ReadFromCallback) {
292 AddMessage(kTestMessage1);
294 EXPECT_CALL(callback_, OnMessage(_))
295 .Times(2)
296 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage))
297 .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage));
299 InitReader();
300 base::RunLoop().RunUntilIdle();
302 EXPECT_TRUE(socket_.read_pending());
305 // Verify that we stop getting callbacks after deleting MessageReader.
306 TEST_F(MessageReaderTest, DeleteFromCallback) {
307 base::Closure done_task1;
308 base::Closure done_task2;
310 AddMessage(kTestMessage1);
311 AddMessage(kTestMessage2);
313 // OnMessage() should never be called for the second message.
314 EXPECT_CALL(callback_, OnMessage(_))
315 .Times(1)
316 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader));
318 InitReader();
319 base::RunLoop().RunUntilIdle();
322 } // namespace protocol
323 } // namespace remoting