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

Side by Side Diff: mojo/edk/system/ipc_support_unittest.cc

Issue 1412283002: Convert mojo::system::Dispatcher to use our new refcounting stuff (instead of base's). (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: no change Created 5 years, 2 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 | « mojo/edk/system/ipc_support.cc ('k') | mojo/edk/system/message_in_transit.cc » ('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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "mojo/edk/system/ipc_support.h" 5 #include "mojo/edk/system/ipc_support.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 26 matching lines...) Expand all
37 using test::TestIOThread; 37 using test::TestIOThread;
38 38
39 namespace system { 39 namespace system {
40 namespace { 40 namespace {
41 41
42 const char kConnectionIdFlag[] = "test-connection-id"; 42 const char kConnectionIdFlag[] = "test-connection-id";
43 43
44 // Tests writing a message (containing just data) to |write_mp| and then reading 44 // Tests writing a message (containing just data) to |write_mp| and then reading
45 // it from |read_mp| (it should be the next message, i.e., there should be no 45 // it from |read_mp| (it should be the next message, i.e., there should be no
46 // other messages already enqueued in that direction). 46 // other messages already enqueued in that direction).
47 void TestWriteReadMessage(scoped_refptr<MessagePipeDispatcher> write_mp, 47 void TestWriteReadMessage(MessagePipeDispatcher* write_mp,
48 scoped_refptr<MessagePipeDispatcher> read_mp) { 48 MessagePipeDispatcher* read_mp) {
49 // Set up waiting on the read end first (to avoid racing). 49 // Set up waiting on the read end first (to avoid racing).
50 Waiter waiter; 50 Waiter waiter;
51 waiter.Init(); 51 waiter.Init();
52 ASSERT_EQ( 52 ASSERT_EQ(
53 MOJO_RESULT_OK, 53 MOJO_RESULT_OK,
54 read_mp->AddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 0, nullptr)); 54 read_mp->AddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 0, nullptr));
55 55
56 // Write a message with just 'x' through the write end. 56 // Write a message with just 'x' through the write end.
57 EXPECT_EQ(MOJO_RESULT_OK, 57 EXPECT_EQ(MOJO_RESULT_OK,
58 write_mp->WriteMessage(UserPointer<const void>("x"), 1, nullptr, 58 write_mp->WriteMessage(UserPointer<const void>("x"), 1, nullptr,
(...skipping 10 matching lines...) Expand all
69 read_mp->ReadMessage(UserPointer<void>(buffer), 69 read_mp->ReadMessage(UserPointer<void>(buffer),
70 MakeUserPointer(&buffer_size), 0, nullptr, 70 MakeUserPointer(&buffer_size), 0, nullptr,
71 MOJO_READ_MESSAGE_FLAG_NONE)); 71 MOJO_READ_MESSAGE_FLAG_NONE));
72 EXPECT_EQ(1u, buffer_size); 72 EXPECT_EQ(1u, buffer_size);
73 EXPECT_EQ('x', buffer[0]); 73 EXPECT_EQ('x', buffer[0]);
74 } 74 }
75 75
76 // Writes a message pipe dispatcher (in a message) to |write_mp| and reads it 76 // Writes a message pipe dispatcher (in a message) to |write_mp| and reads it
77 // from |read_mp| (it should be the next message, i.e., there should be no other 77 // from |read_mp| (it should be the next message, i.e., there should be no other
78 // other messages already enqueued in that direction). 78 // other messages already enqueued in that direction).
79 scoped_refptr<MessagePipeDispatcher> SendMessagePipeDispatcher( 79 RefPtr<MessagePipeDispatcher> SendMessagePipeDispatcher(
80 scoped_refptr<MessagePipeDispatcher> write_mp, 80 MessagePipeDispatcher* write_mp,
81 scoped_refptr<MessagePipeDispatcher> read_mp, 81 MessagePipeDispatcher* read_mp,
82 scoped_refptr<MessagePipeDispatcher> mp_to_send) { 82 RefPtr<MessagePipeDispatcher>&& mp_to_send) {
83 CHECK_NE(mp_to_send, write_mp); 83 CHECK_NE(mp_to_send.get(), write_mp);
84 CHECK_NE(mp_to_send, read_mp); 84 CHECK_NE(mp_to_send.get(), read_mp);
85 85
86 // Set up waiting on the read end first (to avoid racing). 86 // Set up waiting on the read end first (to avoid racing).
87 Waiter waiter; 87 Waiter waiter;
88 waiter.Init(); 88 waiter.Init();
89 CHECK_EQ( 89 CHECK_EQ(
90 read_mp->AddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 0, nullptr), 90 read_mp->AddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 0, nullptr),
91 MOJO_RESULT_OK); 91 MOJO_RESULT_OK);
92 92
93 // Write a message with just |mp_to_send| through the write end. 93 // Write a message with just |mp_to_send| through the write end.
94 DispatcherTransport transport( 94 DispatcherTransport transport(
95 test::DispatcherTryStartTransport(mp_to_send.get())); 95 test::DispatcherTryStartTransport(mp_to_send.get()));
96 CHECK(transport.is_valid()); 96 CHECK(transport.is_valid());
97 std::vector<DispatcherTransport> transports; 97 std::vector<DispatcherTransport> transports;
98 transports.push_back(transport); 98 transports.push_back(transport);
99 CHECK_EQ(write_mp->WriteMessage(NullUserPointer(), 0, &transports, 99 CHECK_EQ(write_mp->WriteMessage(NullUserPointer(), 0, &transports,
100 MOJO_WRITE_MESSAGE_FLAG_NONE), 100 MOJO_WRITE_MESSAGE_FLAG_NONE),
101 MOJO_RESULT_OK); 101 MOJO_RESULT_OK);
102 transport.End(); 102 transport.End();
103 mp_to_send = nullptr;
103 104
104 // Wait for it to arrive. 105 // Wait for it to arrive.
105 CHECK_EQ(waiter.Wait(test::ActionDeadline(), nullptr), MOJO_RESULT_OK); 106 CHECK_EQ(waiter.Wait(test::ActionDeadline(), nullptr), MOJO_RESULT_OK);
106 read_mp->RemoveAwakable(&waiter, nullptr); 107 read_mp->RemoveAwakable(&waiter, nullptr);
107 108
108 // Read the message from the read end. 109 // Read the message from the read end.
109 DispatcherVector dispatchers; 110 DispatcherVector dispatchers;
110 uint32_t num_dispatchers = 10; 111 uint32_t num_dispatchers = 10;
111 CHECK_EQ( 112 CHECK_EQ(
112 read_mp->ReadMessage(NullUserPointer(), NullUserPointer(), &dispatchers, 113 read_mp->ReadMessage(NullUserPointer(), NullUserPointer(), &dispatchers,
113 &num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE), 114 &num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE),
114 MOJO_RESULT_OK); 115 MOJO_RESULT_OK);
115 CHECK_EQ(dispatchers.size(), 1u); 116 CHECK_EQ(dispatchers.size(), 1u);
116 CHECK_EQ(num_dispatchers, 1u); 117 CHECK_EQ(num_dispatchers, 1u);
117 CHECK_EQ(dispatchers[0]->GetType(), Dispatcher::Type::MESSAGE_PIPE); 118 CHECK_EQ(dispatchers[0]->GetType(), Dispatcher::Type::MESSAGE_PIPE);
118 return scoped_refptr<MessagePipeDispatcher>( 119 return RefPtr<MessagePipeDispatcher>(
119 static_cast<MessagePipeDispatcher*>(dispatchers[0].get())); 120 static_cast<MessagePipeDispatcher*>(dispatchers[0].get()));
120 } 121 }
121 122
122 class TestMasterProcessDelegate : public embedder::MasterProcessDelegate { 123 class TestMasterProcessDelegate : public embedder::MasterProcessDelegate {
123 public: 124 public:
124 TestMasterProcessDelegate() 125 TestMasterProcessDelegate()
125 : on_slave_disconnect_event_(false, false) {} // Auto reset. 126 : on_slave_disconnect_event_(false, false) {} // Auto reset.
126 ~TestMasterProcessDelegate() override {} 127 ~TestMasterProcessDelegate() override {}
127 128
128 // Warning: There's only one slave disconnect event (which resets 129 // Warning: There's only one slave disconnect event (which resets
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 IPCSupport* master_ipc_support) 166 IPCSupport* master_ipc_support)
166 : test_io_thread_(test_io_thread), 167 : test_io_thread_(test_io_thread),
167 master_ipc_support_(master_ipc_support), 168 master_ipc_support_(master_ipc_support),
168 connection_id_(master_ipc_support_->GenerateConnectionIdentifier()), 169 connection_id_(master_ipc_support_->GenerateConnectionIdentifier()),
169 slave_id_(kInvalidProcessIdentifier), 170 slave_id_(kInvalidProcessIdentifier),
170 event_(true, false) {} 171 event_(true, false) {}
171 ~TestSlaveConnection() {} 172 ~TestSlaveConnection() {}
172 173
173 // After this is called, |ShutdownChannelToSlave()| must be called (possibly 174 // After this is called, |ShutdownChannelToSlave()| must be called (possibly
174 // after |WaitForChannelToSlave()|) before destruction. 175 // after |WaitForChannelToSlave()|) before destruction.
175 scoped_refptr<MessagePipeDispatcher> ConnectToSlave() { 176 RefPtr<MessagePipeDispatcher> ConnectToSlave() {
176 embedder::PlatformChannelPair channel_pair; 177 embedder::PlatformChannelPair channel_pair;
177 // Note: |ChannelId|s and |ProcessIdentifier|s are interchangeable. 178 // Note: |ChannelId|s and |ProcessIdentifier|s are interchangeable.
178 scoped_refptr<MessagePipeDispatcher> mp = 179 RefPtr<MessagePipeDispatcher> mp = master_ipc_support_->ConnectToSlave(
179 master_ipc_support_->ConnectToSlave( 180 connection_id_, nullptr, channel_pair.PassServerHandle(),
180 connection_id_, nullptr, channel_pair.PassServerHandle(), 181 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event_)),
181 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event_)), 182 nullptr, &slave_id_);
182 nullptr, &slave_id_);
183 EXPECT_TRUE(mp); 183 EXPECT_TRUE(mp);
184 EXPECT_NE(slave_id_, kInvalidProcessIdentifier); 184 EXPECT_NE(slave_id_, kInvalidProcessIdentifier);
185 EXPECT_NE(slave_id_, kMasterProcessIdentifier); 185 EXPECT_NE(slave_id_, kMasterProcessIdentifier);
186 slave_platform_handle_ = channel_pair.PassClientHandle(); 186 slave_platform_handle_ = channel_pair.PassClientHandle();
187 return mp; 187 return mp;
188 } 188 }
189 189
190 void WaitForChannelToSlave() { 190 void WaitForChannelToSlave() {
191 EXPECT_TRUE(event_.TimedWait(TestTimeouts::action_timeout())); 191 EXPECT_TRUE(event_.TimedWait(TestTimeouts::action_timeout()));
192 } 192 }
(...skipping 13 matching lines...) Expand all
206 return slave_platform_handle_.Pass(); 206 return slave_platform_handle_.Pass();
207 } 207 }
208 208
209 const ConnectionIdentifier& connection_id() const { return connection_id_; } 209 const ConnectionIdentifier& connection_id() const { return connection_id_; }
210 210
211 private: 211 private:
212 TestIOThread* const test_io_thread_; 212 TestIOThread* const test_io_thread_;
213 IPCSupport* const master_ipc_support_; 213 IPCSupport* const master_ipc_support_;
214 const ConnectionIdentifier connection_id_; 214 const ConnectionIdentifier connection_id_;
215 // The master's message pipe dispatcher. 215 // The master's message pipe dispatcher.
216 scoped_refptr<MessagePipeDispatcher> message_pipe_; 216 RefPtr<MessagePipeDispatcher> message_pipe_;
217 ProcessIdentifier slave_id_; 217 ProcessIdentifier slave_id_;
218 base::WaitableEvent event_; 218 base::WaitableEvent event_;
219 embedder::ScopedPlatformHandle slave_platform_handle_; 219 embedder::ScopedPlatformHandle slave_platform_handle_;
220 220
221 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveConnection); 221 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveConnection);
222 }; 222 };
223 223
224 // Encapsulates the state of a slave. (Note, however, that we share a 224 // Encapsulates the state of a slave. (Note, however, that we share a
225 // |PlatformSupport| and an I/O thread.) 225 // |PlatformSupport| and an I/O thread.)
226 class TestSlave { 226 class TestSlave {
227 public: 227 public:
228 // Note: Before destruction, |ShutdownIPCSupport()| must be called. 228 // Note: Before destruction, |ShutdownIPCSupport()| must be called.
229 TestSlave(embedder::PlatformSupport* platform_support, 229 TestSlave(embedder::PlatformSupport* platform_support,
230 TestIOThread* test_io_thread, 230 TestIOThread* test_io_thread,
231 embedder::ScopedPlatformHandle platform_handle) 231 embedder::ScopedPlatformHandle platform_handle)
232 : test_io_thread_(test_io_thread), 232 : test_io_thread_(test_io_thread),
233 slave_ipc_support_(platform_support, 233 slave_ipc_support_(platform_support,
234 embedder::ProcessType::SLAVE, 234 embedder::ProcessType::SLAVE,
235 test_io_thread->task_runner(), 235 test_io_thread->task_runner(),
236 &slave_process_delegate_, 236 &slave_process_delegate_,
237 test_io_thread->task_runner(), 237 test_io_thread->task_runner(),
238 platform_handle.Pass()), 238 platform_handle.Pass()),
239 event_(true, false) {} 239 event_(true, false) {}
240 ~TestSlave() {} 240 ~TestSlave() {}
241 241
242 // After this is called, |ShutdownChannelToMaster()| must be called (possibly 242 // After this is called, |ShutdownChannelToMaster()| must be called (possibly
243 // after |WaitForChannelToMaster()|) before destruction. 243 // after |WaitForChannelToMaster()|) before destruction.
244 scoped_refptr<MessagePipeDispatcher> ConnectToMaster( 244 RefPtr<MessagePipeDispatcher> ConnectToMaster(
245 const ConnectionIdentifier& connection_id) { 245 const ConnectionIdentifier& connection_id) {
246 ProcessIdentifier master_id = kInvalidProcessIdentifier; 246 ProcessIdentifier master_id = kInvalidProcessIdentifier;
247 scoped_refptr<MessagePipeDispatcher> mp = 247 RefPtr<MessagePipeDispatcher> mp = slave_ipc_support_.ConnectToMaster(
248 slave_ipc_support_.ConnectToMaster( 248 connection_id,
249 connection_id, 249 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event_)),
250 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event_)), 250 nullptr, &master_id);
251 nullptr, &master_id);
252 EXPECT_TRUE(mp); 251 EXPECT_TRUE(mp);
253 EXPECT_EQ(kMasterProcessIdentifier, master_id); 252 EXPECT_EQ(kMasterProcessIdentifier, master_id);
254 return mp; 253 return mp;
255 } 254 }
256 255
257 void WaitForChannelToMaster() { 256 void WaitForChannelToMaster() {
258 EXPECT_TRUE(event_.TimedWait(TestTimeouts::action_timeout())); 257 EXPECT_TRUE(event_.TimedWait(TestTimeouts::action_timeout()));
259 } 258 }
260 259
261 void ShutdownChannelToMaster() { 260 void ShutdownChannelToMaster() {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 slave_connection_.reset( 306 slave_connection_.reset(
308 new TestSlaveConnection(test_io_thread_, master_ipc_support_)); 307 new TestSlaveConnection(test_io_thread_, master_ipc_support_));
309 master_mp_ = slave_connection_->ConnectToSlave(); 308 master_mp_ = slave_connection_->ConnectToSlave();
310 309
311 slave_.reset(new TestSlave(platform_support_, test_io_thread_, 310 slave_.reset(new TestSlave(platform_support_, test_io_thread_,
312 slave_connection_->PassSlavePlatformHandle())); 311 slave_connection_->PassSlavePlatformHandle()));
313 slave_mp_ = slave_->ConnectToMaster(slave_connection_->connection_id()); 312 slave_mp_ = slave_->ConnectToMaster(slave_connection_->connection_id());
314 } 313 }
315 314
316 void TestConnection() { 315 void TestConnection() {
317 TestWriteReadMessage(master_mp_, slave_mp_); 316 TestWriteReadMessage(master_mp_.get(), slave_mp_.get());
318 TestWriteReadMessage(slave_mp_, master_mp_); 317 TestWriteReadMessage(slave_mp_.get(), master_mp_.get());
319 } 318 }
320 319
321 scoped_refptr<MessagePipeDispatcher> PassMasterMessagePipe() { 320 RefPtr<MessagePipeDispatcher> PassMasterMessagePipe() {
322 return master_mp_.Pass(); 321 return master_mp_.Pass();
323 } 322 }
324 323
325 scoped_refptr<MessagePipeDispatcher> PassSlaveMessagePipe() { 324 RefPtr<MessagePipeDispatcher> PassSlaveMessagePipe() {
326 return slave_mp_.Pass(); 325 return slave_mp_.Pass();
327 } 326 }
328 327
329 void Shutdown() { 328 void Shutdown() {
330 if (master_mp_) { 329 if (master_mp_) {
331 master_mp_->Close(); 330 master_mp_->Close();
332 master_mp_ = nullptr; 331 master_mp_ = nullptr;
333 } 332 }
334 if (slave_mp_) { 333 if (slave_mp_) {
335 slave_mp_->Close(); 334 slave_mp_->Close();
(...skipping 17 matching lines...) Expand all
353 // Note: To close the slave message pipe, use |PassSlaveMessagePipe()|. 352 // Note: To close the slave message pipe, use |PassSlaveMessagePipe()|.
354 MessagePipeDispatcher* slave_mp() { return slave_mp_.get(); } 353 MessagePipeDispatcher* slave_mp() { return slave_mp_.get(); }
355 354
356 private: 355 private:
357 embedder::SimplePlatformSupport* const platform_support_; 356 embedder::SimplePlatformSupport* const platform_support_;
358 TestIOThread* const test_io_thread_; 357 TestIOThread* const test_io_thread_;
359 TestMasterProcessDelegate* const master_process_delegate_; 358 TestMasterProcessDelegate* const master_process_delegate_;
360 IPCSupport* const master_ipc_support_; 359 IPCSupport* const master_ipc_support_;
361 360
362 std::unique_ptr<TestSlaveConnection> slave_connection_; 361 std::unique_ptr<TestSlaveConnection> slave_connection_;
363 scoped_refptr<MessagePipeDispatcher> master_mp_; 362 RefPtr<MessagePipeDispatcher> master_mp_;
364 363
365 std::unique_ptr<TestSlave> slave_; 364 std::unique_ptr<TestSlave> slave_;
366 scoped_refptr<MessagePipeDispatcher> slave_mp_; 365 RefPtr<MessagePipeDispatcher> slave_mp_;
367 366
368 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveSetup); 367 MOJO_DISALLOW_COPY_AND_ASSIGN(TestSlaveSetup);
369 }; 368 };
370 369
371 class IPCSupportTest : public testing::Test { 370 class IPCSupportTest : public testing::Test {
372 public: 371 public:
373 // Note: Run master process delegate methods on the I/O thread. 372 // Note: Run master process delegate methods on the I/O thread.
374 IPCSupportTest() 373 IPCSupportTest()
375 : test_io_thread_(TestIOThread::StartMode::AUTO), 374 : test_io_thread_(TestIOThread::StartMode::AUTO),
376 master_ipc_support_(&platform_support_, 375 master_ipc_support_(&platform_support_,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 TestIOThread test_io_thread_; 408 TestIOThread test_io_thread_;
410 409
411 // All tests require a master. 410 // All tests require a master.
412 TestMasterProcessDelegate master_process_delegate_; 411 TestMasterProcessDelegate master_process_delegate_;
413 IPCSupport master_ipc_support_; 412 IPCSupport master_ipc_support_;
414 413
415 MOJO_DISALLOW_COPY_AND_ASSIGN(IPCSupportTest); 414 MOJO_DISALLOW_COPY_AND_ASSIGN(IPCSupportTest);
416 }; 415 };
417 416
418 using MessagePipeDispatcherPair = 417 using MessagePipeDispatcherPair =
419 std::pair<scoped_refptr<MessagePipeDispatcher>, 418 std::pair<RefPtr<MessagePipeDispatcher>, RefPtr<MessagePipeDispatcher>>;
420 scoped_refptr<MessagePipeDispatcher>>;
421 MessagePipeDispatcherPair CreateMessagePipe() { 419 MessagePipeDispatcherPair CreateMessagePipe() {
422 MessagePipeDispatcherPair rv; 420 MessagePipeDispatcherPair rv;
423 rv.first = MessagePipeDispatcher::Create( 421 rv.first = MessagePipeDispatcher::Create(
424 MessagePipeDispatcher::kDefaultCreateOptions); 422 MessagePipeDispatcher::kDefaultCreateOptions);
425 rv.second = MessagePipeDispatcher::Create( 423 rv.second = MessagePipeDispatcher::Create(
426 MessagePipeDispatcher::kDefaultCreateOptions); 424 MessagePipeDispatcher::kDefaultCreateOptions);
427 auto mp = MessagePipe::CreateLocalLocal(); 425 auto mp = MessagePipe::CreateLocalLocal();
428 rv.first->Init(mp.Clone(), 0); 426 rv.first->Init(mp.Clone(), 0);
429 rv.second->Init(std::move(mp), 1); 427 rv.second->Init(std::move(mp), 1);
430 return rv; 428 return rv;
(...skipping 29 matching lines...) Expand all
460 // going away. 458 // going away.
461 TEST_F(IPCSupportTest, ConnectTwoSlaves) { 459 TEST_F(IPCSupportTest, ConnectTwoSlaves) {
462 std::unique_ptr<TestSlaveSetup> s1(SetupSlave()); 460 std::unique_ptr<TestSlaveSetup> s1(SetupSlave());
463 std::unique_ptr<TestSlaveSetup> s2(SetupSlave()); 461 std::unique_ptr<TestSlaveSetup> s2(SetupSlave());
464 s1->TestConnection(); 462 s1->TestConnection();
465 s2->TestConnection(); 463 s2->TestConnection();
466 464
467 // Make a message pipe (logically "in" the master) and send one end to each 465 // Make a message pipe (logically "in" the master) and send one end to each
468 // slave. 466 // slave.
469 MessagePipeDispatcherPair send_mp = CreateMessagePipe(); 467 MessagePipeDispatcherPair send_mp = CreateMessagePipe();
470 scoped_refptr<MessagePipeDispatcher> slave1_received_mp = 468 RefPtr<MessagePipeDispatcher> slave1_received_mp = SendMessagePipeDispatcher(
471 SendMessagePipeDispatcher(s1->master_mp(), s1->slave_mp(), send_mp.first); 469 s1->master_mp(), s1->slave_mp(), std::move(send_mp.first));
472 scoped_refptr<MessagePipeDispatcher> slave2_received_mp = 470 RefPtr<MessagePipeDispatcher> slave2_received_mp = SendMessagePipeDispatcher(
473 SendMessagePipeDispatcher(s2->master_mp(), s2->slave_mp(), 471 s2->master_mp(), s2->slave_mp(), std::move(send_mp.second));
474 send_mp.second);
475 472
476 // These should be connected. 473 // These should be connected.
477 TestWriteReadMessage(slave1_received_mp, slave2_received_mp); 474 TestWriteReadMessage(slave1_received_mp.get(), slave2_received_mp.get());
478 TestWriteReadMessage(slave2_received_mp, slave1_received_mp); 475 TestWriteReadMessage(slave2_received_mp.get(), slave1_received_mp.get());
479 476
480 s1->PassMasterMessagePipe()->Close(); 477 s1->PassMasterMessagePipe()->Close();
481 s2->PassMasterMessagePipe()->Close(); 478 s2->PassMasterMessagePipe()->Close();
482 s1->PassSlaveMessagePipe()->Close(); 479 s1->PassSlaveMessagePipe()->Close();
483 s2->PassSlaveMessagePipe()->Close(); 480 s2->PassSlaveMessagePipe()->Close();
484 481
485 // They should still be connected. 482 // They should still be connected.
486 TestWriteReadMessage(slave1_received_mp, slave2_received_mp); 483 TestWriteReadMessage(slave1_received_mp.get(), slave2_received_mp.get());
487 TestWriteReadMessage(slave2_received_mp, slave1_received_mp); 484 TestWriteReadMessage(slave2_received_mp.get(), slave1_received_mp.get());
488 485
489 slave1_received_mp->Close(); 486 slave1_received_mp->Close();
490 slave2_received_mp->Close(); 487 slave2_received_mp->Close();
491 488
492 s1->Shutdown(); 489 s1->Shutdown();
493 s2->Shutdown(); 490 s2->Shutdown();
494 491
495 ShutdownMasterIPCSupport(); 492 ShutdownMasterIPCSupport();
496 } 493 }
497 494
498 // Like |ConnectTwoSlaves|, but does it twice, to test reusing a connection. 495 // Like |ConnectTwoSlaves|, but does it twice, to test reusing a connection.
499 TEST_F(IPCSupportTest, ConnectTwoSlavesTwice) { 496 TEST_F(IPCSupportTest, ConnectTwoSlavesTwice) {
500 std::unique_ptr<TestSlaveSetup> s1(SetupSlave()); 497 std::unique_ptr<TestSlaveSetup> s1(SetupSlave());
501 std::unique_ptr<TestSlaveSetup> s2(SetupSlave()); 498 std::unique_ptr<TestSlaveSetup> s2(SetupSlave());
502 s1->TestConnection(); 499 s1->TestConnection();
503 s2->TestConnection(); 500 s2->TestConnection();
504 501
505 MessagePipeDispatcherPair send_mp1 = CreateMessagePipe(); 502 MessagePipeDispatcherPair send_mp1 = CreateMessagePipe();
506 scoped_refptr<MessagePipeDispatcher> slave1_received_mp1 = 503 RefPtr<MessagePipeDispatcher> slave1_received_mp1 = SendMessagePipeDispatcher(
507 SendMessagePipeDispatcher(s1->master_mp(), s1->slave_mp(), 504 s1->master_mp(), s1->slave_mp(), std::move(send_mp1.first));
508 send_mp1.first); 505 RefPtr<MessagePipeDispatcher> slave2_received_mp1 = SendMessagePipeDispatcher(
509 scoped_refptr<MessagePipeDispatcher> slave2_received_mp1 = 506 s2->master_mp(), s2->slave_mp(), std::move(send_mp1.second));
510 SendMessagePipeDispatcher(s2->master_mp(), s2->slave_mp(),
511 send_mp1.second);
512 507
513 MessagePipeDispatcherPair send_mp2 = CreateMessagePipe(); 508 MessagePipeDispatcherPair send_mp2 = CreateMessagePipe();
514 scoped_refptr<MessagePipeDispatcher> slave1_received_mp2 = 509 RefPtr<MessagePipeDispatcher> slave1_received_mp2 = SendMessagePipeDispatcher(
515 SendMessagePipeDispatcher(s1->master_mp(), s1->slave_mp(), 510 s1->master_mp(), s1->slave_mp(), std::move(send_mp2.first));
516 send_mp2.first); 511 RefPtr<MessagePipeDispatcher> slave2_received_mp2 = SendMessagePipeDispatcher(
517 scoped_refptr<MessagePipeDispatcher> slave2_received_mp2 = 512 s2->master_mp(), s2->slave_mp(), std::move(send_mp2.second));
518 SendMessagePipeDispatcher(s2->master_mp(), s2->slave_mp(),
519 send_mp2.second);
520 513
521 s1->PassMasterMessagePipe()->Close(); 514 s1->PassMasterMessagePipe()->Close();
522 s2->PassMasterMessagePipe()->Close(); 515 s2->PassMasterMessagePipe()->Close();
523 s1->PassSlaveMessagePipe()->Close(); 516 s1->PassSlaveMessagePipe()->Close();
524 s2->PassSlaveMessagePipe()->Close(); 517 s2->PassSlaveMessagePipe()->Close();
525 518
526 TestWriteReadMessage(slave1_received_mp1, slave2_received_mp1); 519 TestWriteReadMessage(slave1_received_mp1.get(), slave2_received_mp1.get());
527 TestWriteReadMessage(slave2_received_mp1, slave1_received_mp1); 520 TestWriteReadMessage(slave2_received_mp1.get(), slave1_received_mp1.get());
528 521
529 TestWriteReadMessage(slave1_received_mp2, slave2_received_mp2); 522 TestWriteReadMessage(slave1_received_mp2.get(), slave2_received_mp2.get());
530 TestWriteReadMessage(slave2_received_mp2, slave1_received_mp2); 523 TestWriteReadMessage(slave2_received_mp2.get(), slave1_received_mp2.get());
531 524
532 slave1_received_mp1->Close(); 525 slave1_received_mp1->Close();
533 slave2_received_mp1->Close(); 526 slave2_received_mp1->Close();
534 527
535 TestWriteReadMessage(slave1_received_mp2, slave2_received_mp2); 528 TestWriteReadMessage(slave1_received_mp2.get(), slave2_received_mp2.get());
536 TestWriteReadMessage(slave2_received_mp2, slave1_received_mp2); 529 TestWriteReadMessage(slave2_received_mp2.get(), slave1_received_mp2.get());
537 530
538 slave1_received_mp2->Close(); 531 slave1_received_mp2->Close();
539 slave2_received_mp2->Close(); 532 slave2_received_mp2->Close();
540 533
541 s1->Shutdown(); 534 s1->Shutdown();
542 s2->Shutdown(); 535 s2->Shutdown();
543 536
544 ShutdownMasterIPCSupport(); 537 ShutdownMasterIPCSupport();
545 } 538 }
546 539
547 // Creates a message pipe in the slave, which sends both ends (in separate 540 // Creates a message pipe in the slave, which sends both ends (in separate
548 // messages) to the master. 541 // messages) to the master.
549 TEST_F(IPCSupportTest, SlavePassBackToMaster) { 542 TEST_F(IPCSupportTest, SlavePassBackToMaster) {
550 std::unique_ptr<TestSlaveSetup> s(SetupSlave()); 543 std::unique_ptr<TestSlaveSetup> s(SetupSlave());
551 544
552 s->TestConnection(); 545 s->TestConnection();
553 546
554 // Make a message pipe (logically "in" the slave) and send both ends 547 // Make a message pipe (logically "in" the slave) and send both ends
555 // (separately) to the master. 548 // (separately) to the master.
556 MessagePipeDispatcherPair send_mp = CreateMessagePipe(); 549 MessagePipeDispatcherPair send_mp = CreateMessagePipe();
557 scoped_refptr<MessagePipeDispatcher> received_mp1 = 550 RefPtr<MessagePipeDispatcher> received_mp1 = SendMessagePipeDispatcher(
558 SendMessagePipeDispatcher(s->slave_mp(), s->master_mp(), send_mp.first); 551 s->slave_mp(), s->master_mp(), std::move(send_mp.first));
559 552
560 TestWriteReadMessage(received_mp1, send_mp.second); 553 TestWriteReadMessage(received_mp1.get(), send_mp.second.get());
561 TestWriteReadMessage(send_mp.second, received_mp1); 554 TestWriteReadMessage(send_mp.second.get(), received_mp1.get());
562 555
563 scoped_refptr<MessagePipeDispatcher> received_mp2 = 556 RefPtr<MessagePipeDispatcher> received_mp2 = SendMessagePipeDispatcher(
564 SendMessagePipeDispatcher(s->slave_mp(), s->master_mp(), send_mp.second); 557 s->slave_mp(), s->master_mp(), std::move(send_mp.second));
565 558
566 s->PassMasterMessagePipe()->Close(); 559 s->PassMasterMessagePipe()->Close();
567 s->PassSlaveMessagePipe()->Close(); 560 s->PassSlaveMessagePipe()->Close();
568 561
569 TestWriteReadMessage(received_mp1, received_mp2); 562 TestWriteReadMessage(received_mp1.get(), received_mp2.get());
570 TestWriteReadMessage(received_mp2, received_mp1); 563 TestWriteReadMessage(received_mp2.get(), received_mp1.get());
571 564
572 s->Shutdown(); 565 s->Shutdown();
573 566
574 // These should still be connected. 567 // These should still be connected.
575 // TODO(vtl): This is not yet implemented, thus will fail here! 568 // TODO(vtl): This is not yet implemented, thus will fail here!
576 // TestWriteReadMessage(received_mp1, received_mp2); 569 // TestWriteReadMessage(received_mp1.get(), received_mp2.get());
577 // TestWriteReadMessage(received_mp2, received_mp1); 570 // TestWriteReadMessage(received_mp2.get(), received_mp1.get());
578 571
579 received_mp1->Close(); 572 received_mp1->Close();
580 received_mp2->Close(); 573 received_mp2->Close();
581 574
582 ShutdownMasterIPCSupport(); 575 ShutdownMasterIPCSupport();
583 } 576 }
584 577
585 } // namespace 578 } // namespace
586 579
587 // Note: This test isn't in an anonymous namespace, since it needs to be 580 // Note: This test isn't in an anonymous namespace, since it needs to be
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 test_io_thread.PostTaskAndWait(FROM_HERE, 715 test_io_thread.PostTaskAndWait(FROM_HERE,
723 base::Bind(&IPCSupport::ShutdownOnIOThread, 716 base::Bind(&IPCSupport::ShutdownOnIOThread,
724 base::Unretained(&ipc_support))); 717 base::Unretained(&ipc_support)));
725 } 718 }
726 719
727 // TODO(vtl): Also test the case of the master "dying" before the slave. (The 720 // TODO(vtl): Also test the case of the master "dying" before the slave. (The
728 // slave should get OnMasterDisconnect(), which we currently don't test.) 721 // slave should get OnMasterDisconnect(), which we currently don't test.)
729 722
730 } // namespace system 723 } // namespace system
731 } // namespace mojo 724 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/ipc_support.cc ('k') | mojo/edk/system/message_in_transit.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698