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

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

Issue 10870021: Make MessageReader class not ref-counted. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 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') | no next file » | 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.h" 10 #include "base/message_loop.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/thread.h"
14 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
15 #include "net/socket/socket.h" 14 #include "net/socket/socket.h"
16 #include "remoting/protocol/fake_session.h" 15 #include "remoting/protocol/fake_session.h"
17 #include "remoting/protocol/message_reader.h" 16 #include "remoting/protocol/message_reader.h"
18 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
20 #include "third_party/libjingle/source/talk/base/byteorder.h" 19 #include "third_party/libjingle/source/talk/base/byteorder.h"
21 20
22 using testing::_; 21 using testing::_;
23 using testing::DoAll; 22 using testing::DoAll;
(...skipping 13 matching lines...) Expand all
37 } // namespace 36 } // namespace
38 37
39 class MockMessageReceivedCallback { 38 class MockMessageReceivedCallback {
40 public: 39 public:
41 MOCK_METHOD1(OnMessage, void(const base::Closure&)); 40 MOCK_METHOD1(OnMessage, void(const base::Closure&));
42 }; 41 };
43 42
44 class MessageReaderTest : public testing::Test { 43 class MessageReaderTest : public testing::Test {
45 public: 44 public:
46 MessageReaderTest() 45 MessageReaderTest()
47 : other_thread_("SecondTestThread"), 46 : run_task_finished_(false, false) {
48 run_task_finished_(false, false) {
49 }
50
51 void RunDoneTaskOnOtherThread(const base::Closure& done_task) {
52 other_thread_.message_loop()->PostTask(
53 FROM_HERE,
54 base::Bind(&MessageReaderTest::RunClosure,
55 base::Unretained(this), done_task));
56 } 47 }
57 48
58 protected: 49 protected:
59 virtual void SetUp() OVERRIDE { 50 virtual void SetUp() OVERRIDE {
60 reader_ = new MessageReader(); 51 reader_.reset(new MessageReader());
61 } 52 }
62 53
63 virtual void TearDown() OVERRIDE { 54 virtual void TearDown() OVERRIDE {
64 STLDeleteElements(&messages_); 55 STLDeleteElements(&messages_);
65 } 56 }
66 57
67 void InitReader() { 58 void InitReader() {
68 reader_->Init(&socket_, base::Bind( 59 reader_->Init(&socket_, base::Bind(
69 &MessageReaderTest::OnMessage, base::Unretained(this))); 60 &MessageReaderTest::OnMessage, base::Unretained(this)));
70 } 61 }
(...skipping 16 matching lines...) Expand all
87 run_task_finished_.Signal(); 78 run_task_finished_.Signal();
88 } 79 }
89 80
90 void OnMessage(scoped_ptr<CompoundBuffer> buffer, 81 void OnMessage(scoped_ptr<CompoundBuffer> buffer,
91 const base::Closure& done_callback) { 82 const base::Closure& done_callback) {
92 messages_.push_back(buffer.release()); 83 messages_.push_back(buffer.release());
93 callback_.OnMessage(done_callback); 84 callback_.OnMessage(done_callback);
94 } 85 }
95 86
96 MessageLoop message_loop_; 87 MessageLoop message_loop_;
97 base::Thread other_thread_;
98 base::WaitableEvent run_task_finished_; 88 base::WaitableEvent run_task_finished_;
99 scoped_refptr<MessageReader> reader_; 89 scoped_ptr<MessageReader> reader_;
100 FakeSocket socket_; 90 FakeSocket socket_;
101 MockMessageReceivedCallback callback_; 91 MockMessageReceivedCallback callback_;
102 std::vector<CompoundBuffer*> messages_; 92 std::vector<CompoundBuffer*> messages_;
103 }; 93 };
104 94
105 // Receive one message and process it with delay 95 // Receive one message and process it with delay
106 TEST_F(MessageReaderTest, OneMessage_Delay) { 96 TEST_F(MessageReaderTest, OneMessage_Delay) {
107 base::Closure done_task; 97 base::Closure done_task;
108 98
109 AddMessage(kTestMessage1); 99 AddMessage(kTestMessage1);
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 246
257 // Verify that the reader starts reading again only after we've 247 // Verify that the reader starts reading again only after we've
258 // finished processing the previous message. 248 // finished processing the previous message.
259 EXPECT_FALSE(socket_.read_pending()); 249 EXPECT_FALSE(socket_.read_pending());
260 250
261 done_task.Run(); 251 done_task.Run();
262 252
263 EXPECT_TRUE(socket_.read_pending()); 253 EXPECT_TRUE(socket_.read_pending());
264 } 254 }
265 255
266 // Verify that socket operations occur on same thread, even when the OnMessage()
267 // callback triggers |done_task| to run on a different thread.
268 TEST_F(MessageReaderTest, UseSocketOnCorrectThread) {
269 AddMessage(kTestMessage1);
270 other_thread_.Start();
271
272 EXPECT_CALL(callback_, OnMessage(_))
273 .WillOnce(Invoke(this, &MessageReaderTest::RunDoneTaskOnOtherThread));
274
275 InitReader();
276
277 run_task_finished_.Wait();
278 message_loop_.RunAllPending();
279
280 Mock::VerifyAndClearExpectations(&callback_);
281
282 // Write another message and verify that we receive it.
283 base::Closure done_task;
284 EXPECT_CALL(callback_, OnMessage(_))
285 .WillOnce(SaveArg<0>(&done_task));
286 AddMessage(kTestMessage2);
287 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
288
289 done_task.Run();
290 }
291
292 // Read() returns error. 256 // Read() returns error.
293 TEST_F(MessageReaderTest, ReadError) { 257 TEST_F(MessageReaderTest, ReadError) {
294 socket_.set_next_read_error(net::ERR_FAILED); 258 socket_.set_next_read_error(net::ERR_FAILED);
295 259
296 // Add a message. It should never be read after the error above. 260 // Add a message. It should never be read after the error above.
297 AddMessage(kTestMessage1); 261 AddMessage(kTestMessage1);
298 262
299 EXPECT_CALL(callback_, OnMessage(_)) 263 EXPECT_CALL(callback_, OnMessage(_))
300 .Times(0); 264 .Times(0);
301 265
302 InitReader(); 266 InitReader();
303 } 267 }
304 268
305 } // namespace protocol 269 } // namespace protocol
306 } // namespace remoting 270 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/message_reader.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698