OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/raw_channel.h" | 5 #include "mojo/edk/system/raw_channel.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
17 #include "base/location.h" | 17 #include "base/location.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
22 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
23 #include "base/synchronization/waitable_event.h" | 23 #include "base/synchronization/waitable_event.h" |
24 #include "base/test/test_io_thread.h" | 24 #include "base/test/test_io_thread.h" |
25 #include "base/threading/simple_thread.h" | 25 #include "base/threading/simple_thread.h" |
26 #include "build/build_config.h" // TODO(vtl): Remove this. | 26 #include "build/build_config.h" |
| 27 #include "mojo/edk/embedder/embedder.h" |
| 28 #include "mojo/edk/embedder/embedder_internal.h" |
27 #include "mojo/edk/embedder/platform_channel_pair.h" | 29 #include "mojo/edk/embedder/platform_channel_pair.h" |
28 #include "mojo/edk/embedder/platform_handle.h" | 30 #include "mojo/edk/embedder/platform_handle.h" |
29 #include "mojo/edk/embedder/scoped_platform_handle.h" | 31 #include "mojo/edk/embedder/scoped_platform_handle.h" |
30 #include "mojo/edk/system/message_in_transit.h" | 32 #include "mojo/edk/system/message_in_transit.h" |
31 #include "mojo/edk/system/test_utils.h" | 33 #include "mojo/edk/system/test_utils.h" |
32 #include "mojo/edk/system/transport_data.h" | 34 #include "mojo/edk/system/transport_data.h" |
33 #include "mojo/edk/test/test_utils.h" | 35 #include "mojo/edk/test/test_utils.h" |
34 #include "mojo/public/cpp/system/macros.h" | 36 #include "mojo/public/cpp/system/macros.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
36 | 38 |
(...skipping 29 matching lines...) Expand all Loading... |
66 scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); | 68 scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); |
67 | 69 |
68 size_t write_size = 0; | 70 size_t write_size = 0; |
69 test::BlockingWrite(handle, message->main_buffer(), | 71 test::BlockingWrite(handle, message->main_buffer(), |
70 message->main_buffer_size(), &write_size); | 72 message->main_buffer_size(), &write_size); |
71 return write_size == message->main_buffer_size(); | 73 return write_size == message->main_buffer_size(); |
72 } | 74 } |
73 | 75 |
74 // ----------------------------------------------------------------------------- | 76 // ----------------------------------------------------------------------------- |
75 | 77 |
76 class RawChannelTest : public test::MojoSystemTest { | 78 class RawChannelTest : public testing::Test { |
77 public: | 79 public: |
78 RawChannelTest() {} | 80 RawChannelTest() {} |
79 ~RawChannelTest() override {} | 81 ~RawChannelTest() override {} |
80 | 82 |
81 void SetUp() override { | 83 void SetUp() override { |
82 PlatformChannelPair channel_pair; | 84 PlatformChannelPair channel_pair; |
83 handles[0] = channel_pair.PassServerHandle(); | 85 handles[0] = channel_pair.PassServerHandle(); |
84 handles[1] = channel_pair.PassClientHandle();\ | 86 handles[1] = channel_pair.PassClientHandle();\ |
85 } | 87 } |
86 | 88 |
87 void TearDown() override { | 89 void TearDown() override { |
88 handles[0].reset(); | 90 handles[0].reset(); |
89 handles[1].reset(); | 91 handles[1].reset(); |
90 } | 92 } |
91 | 93 |
| 94 void FlushIOThread() { |
| 95 base::WaitableEvent event(false, false); |
| 96 internal::g_io_thread_task_runner->PostTask( |
| 97 FROM_HERE, |
| 98 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); |
| 99 event.Wait(); |
| 100 } |
| 101 |
92 protected: | 102 protected: |
93 ScopedPlatformHandle handles[2]; | 103 ScopedPlatformHandle handles[2]; |
94 | 104 |
95 private: | 105 private: |
96 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelTest); | 106 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelTest); |
97 }; | 107 }; |
98 | 108 |
99 // RawChannelTest.WriteMessage ------------------------------------------------- | 109 // RawChannelTest.WriteMessage ------------------------------------------------- |
100 | 110 |
101 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { | 111 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 std::vector<unsigned char> bytes_; | 192 std::vector<unsigned char> bytes_; |
183 | 193 |
184 MOJO_DISALLOW_COPY_AND_ASSIGN(TestMessageReaderAndChecker); | 194 MOJO_DISALLOW_COPY_AND_ASSIGN(TestMessageReaderAndChecker); |
185 }; | 195 }; |
186 | 196 |
187 // Tests writing (and verifies reading using our own custom reader). | 197 // Tests writing (and verifies reading using our own custom reader). |
188 TEST_F(RawChannelTest, WriteMessage) { | 198 TEST_F(RawChannelTest, WriteMessage) { |
189 WriteOnlyRawChannelDelegate delegate; | 199 WriteOnlyRawChannelDelegate delegate; |
190 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 200 RawChannel* rc = RawChannel::Create(handles[0].Pass()); |
191 TestMessageReaderAndChecker checker(handles[1].get()); | 201 TestMessageReaderAndChecker checker(handles[1].get()); |
192 test_io_thread()->PostTaskAndWait( | 202 internal::g_io_thread_task_runner->PostTask( |
193 FROM_HERE, | 203 FROM_HERE, |
194 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 204 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
195 | 205 |
196 // Write and read, for a variety of sizes. | 206 // Write and read, for a variety of sizes. |
197 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { | 207 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { |
198 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); | 208 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); |
199 EXPECT_TRUE(checker.ReadAndCheckNextMessage(size)) << size; | 209 EXPECT_TRUE(checker.ReadAndCheckNextMessage(size)) << size; |
200 } | 210 } |
201 | 211 |
202 // Write/queue and read afterwards, for a variety of sizes. | 212 // Write/queue and read afterwards, for a variety of sizes. |
203 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) | 213 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) |
204 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); | 214 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); |
205 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) | 215 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) |
206 EXPECT_TRUE(checker.ReadAndCheckNextMessage(size)) << size; | 216 EXPECT_TRUE(checker.ReadAndCheckNextMessage(size)) << size; |
207 | 217 |
208 test_io_thread()->PostTaskAndWait( | 218 internal::g_io_thread_task_runner->PostTask( |
209 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); | 219 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); |
210 } | 220 } |
211 | 221 |
212 // RawChannelTest.OnReadMessage ------------------------------------------------ | 222 // RawChannelTest.OnReadMessage ------------------------------------------------ |
213 | 223 |
214 class ReadCheckerRawChannelDelegate : public RawChannel::Delegate { | 224 class ReadCheckerRawChannelDelegate : public RawChannel::Delegate { |
215 public: | 225 public: |
216 ReadCheckerRawChannelDelegate() : done_event_(false, false), position_(0) {} | 226 ReadCheckerRawChannelDelegate() : done_event_(false, false), position_(0) {} |
217 ~ReadCheckerRawChannelDelegate() override {} | 227 ~ReadCheckerRawChannelDelegate() override {} |
218 | 228 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 std::vector<uint32_t> expected_sizes_; | 277 std::vector<uint32_t> expected_sizes_; |
268 size_t position_; | 278 size_t position_; |
269 | 279 |
270 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate); | 280 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate); |
271 }; | 281 }; |
272 | 282 |
273 // Tests reading (writing using our own custom writer). | 283 // Tests reading (writing using our own custom writer). |
274 TEST_F(RawChannelTest, OnReadMessage) { | 284 TEST_F(RawChannelTest, OnReadMessage) { |
275 ReadCheckerRawChannelDelegate delegate; | 285 ReadCheckerRawChannelDelegate delegate; |
276 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 286 RawChannel* rc = RawChannel::Create(handles[0].Pass()); |
277 test_io_thread()->PostTaskAndWait( | 287 internal::g_io_thread_task_runner->PostTask( |
278 FROM_HERE, | 288 FROM_HERE, |
279 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 289 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
280 | 290 |
281 // Write and read, for a variety of sizes. | 291 // Write and read, for a variety of sizes. |
282 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { | 292 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { |
283 delegate.SetExpectedSizes(std::vector<uint32_t>(1, size)); | 293 delegate.SetExpectedSizes(std::vector<uint32_t>(1, size)); |
284 | 294 |
285 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), size)); | 295 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), size)); |
286 | 296 |
287 delegate.Wait(); | 297 delegate.Wait(); |
288 } | 298 } |
289 | 299 |
290 // Set up reader and write as fast as we can. | 300 // Set up reader and write as fast as we can. |
291 // Write/queue and read afterwards, for a variety of sizes. | 301 // Write/queue and read afterwards, for a variety of sizes. |
292 std::vector<uint32_t> expected_sizes; | 302 std::vector<uint32_t> expected_sizes; |
293 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) | 303 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) |
294 expected_sizes.push_back(size); | 304 expected_sizes.push_back(size); |
295 delegate.SetExpectedSizes(expected_sizes); | 305 delegate.SetExpectedSizes(expected_sizes); |
296 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) | 306 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) |
297 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), size)); | 307 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), size)); |
298 delegate.Wait(); | 308 delegate.Wait(); |
299 | 309 |
300 test_io_thread()->PostTaskAndWait( | 310 internal::g_io_thread_task_runner->PostTask( |
301 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); | 311 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); |
302 } | 312 } |
303 | 313 |
304 // RawChannelTest.WriteMessageAndOnReadMessage --------------------------------- | 314 // RawChannelTest.WriteMessageAndOnReadMessage --------------------------------- |
305 | 315 |
306 class RawChannelWriterThread : public base::SimpleThread { | 316 class RawChannelWriterThread : public base::SimpleThread { |
307 public: | 317 public: |
308 RawChannelWriterThread(RawChannel* raw_channel, size_t write_count) | 318 RawChannelWriterThread(RawChannel* raw_channel, size_t write_count) |
309 : base::SimpleThread("raw_channel_writer_thread"), | 319 : base::SimpleThread("raw_channel_writer_thread"), |
310 raw_channel_(raw_channel), | 320 raw_channel_(raw_channel), |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 374 |
365 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate); | 375 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate); |
366 }; | 376 }; |
367 | 377 |
368 TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) { | 378 TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) { |
369 static const size_t kNumWriterThreads = 10; | 379 static const size_t kNumWriterThreads = 10; |
370 static const size_t kNumWriteMessagesPerThread = 400; | 380 static const size_t kNumWriteMessagesPerThread = 400; |
371 | 381 |
372 WriteOnlyRawChannelDelegate writer_delegate; | 382 WriteOnlyRawChannelDelegate writer_delegate; |
373 RawChannel* writer_rc = RawChannel::Create(handles[0].Pass()); | 383 RawChannel* writer_rc = RawChannel::Create(handles[0].Pass()); |
374 test_io_thread()->PostTaskAndWait( | 384 internal::g_io_thread_task_runner->PostTask( |
375 FROM_HERE, | 385 FROM_HERE, |
376 base::Bind(&InitOnIOThread, writer_rc, | 386 base::Bind(&InitOnIOThread, writer_rc, |
377 base::Unretained(&writer_delegate))); | 387 base::Unretained(&writer_delegate))); |
378 | 388 |
379 ReadCountdownRawChannelDelegate reader_delegate(kNumWriterThreads * | 389 ReadCountdownRawChannelDelegate reader_delegate(kNumWriterThreads * |
380 kNumWriteMessagesPerThread); | 390 kNumWriteMessagesPerThread); |
381 RawChannel* reader_rc = RawChannel::Create(handles[1].Pass()); | 391 RawChannel* reader_rc = RawChannel::Create(handles[1].Pass()); |
382 test_io_thread()->PostTaskAndWait( | 392 internal::g_io_thread_task_runner->PostTask( |
383 FROM_HERE, | 393 FROM_HERE, |
384 base::Bind(&InitOnIOThread, reader_rc, | 394 base::Bind(&InitOnIOThread, reader_rc, |
385 base::Unretained(&reader_delegate))); | 395 base::Unretained(&reader_delegate))); |
386 | 396 |
387 { | 397 { |
388 ScopedVector<RawChannelWriterThread> writer_threads; | 398 ScopedVector<RawChannelWriterThread> writer_threads; |
389 for (size_t i = 0; i < kNumWriterThreads; i++) { | 399 for (size_t i = 0; i < kNumWriterThreads; i++) { |
390 writer_threads.push_back(new RawChannelWriterThread( | 400 writer_threads.push_back(new RawChannelWriterThread( |
391 writer_rc, kNumWriteMessagesPerThread)); | 401 writer_rc, kNumWriteMessagesPerThread)); |
392 } | 402 } |
393 for (size_t i = 0; i < writer_threads.size(); i++) | 403 for (size_t i = 0; i < writer_threads.size(); i++) |
394 writer_threads[i]->Start(); | 404 writer_threads[i]->Start(); |
395 } // Joins all the writer threads. | 405 } // Joins all the writer threads. |
396 | 406 |
397 // Sleep a bit, to let any extraneous reads be processed. (There shouldn't be | 407 // Sleep a bit, to let any extraneous reads be processed. (There shouldn't be |
398 // any, but we want to know about them.) | 408 // any, but we want to know about them.) |
399 test::Sleep(test::DeadlineFromMilliseconds(100)); | 409 test::Sleep(test::DeadlineFromMilliseconds(100)); |
400 | 410 |
401 // Wait for reading to finish. | 411 // Wait for reading to finish. |
402 reader_delegate.Wait(); | 412 reader_delegate.Wait(); |
403 | 413 |
404 test_io_thread()->PostTaskAndWait( | 414 internal::g_io_thread_task_runner->PostTask( |
405 FROM_HERE, | 415 FROM_HERE, |
406 base::Bind(&RawChannel::Shutdown, base::Unretained(reader_rc))); | 416 base::Bind(&RawChannel::Shutdown, base::Unretained(reader_rc))); |
407 | 417 |
408 test_io_thread()->PostTaskAndWait( | 418 internal::g_io_thread_task_runner->PostTask( |
409 FROM_HERE, | 419 FROM_HERE, |
410 base::Bind(&RawChannel::Shutdown, base::Unretained(writer_rc))); | 420 base::Bind(&RawChannel::Shutdown, base::Unretained(writer_rc))); |
411 } | 421 } |
412 | 422 |
413 // RawChannelTest.OnError ------------------------------------------------------ | 423 // RawChannelTest.OnError ------------------------------------------------------ |
414 | 424 |
415 class ErrorRecordingRawChannelDelegate | 425 class ErrorRecordingRawChannelDelegate |
416 : public ReadCountdownRawChannelDelegate { | 426 : public ReadCountdownRawChannelDelegate { |
417 public: | 427 public: |
418 ErrorRecordingRawChannelDelegate(size_t expected_read_count, | 428 ErrorRecordingRawChannelDelegate(size_t expected_read_count, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 bool expecting_read_error_; | 473 bool expecting_read_error_; |
464 bool expecting_write_error_; | 474 bool expecting_write_error_; |
465 | 475 |
466 MOJO_DISALLOW_COPY_AND_ASSIGN(ErrorRecordingRawChannelDelegate); | 476 MOJO_DISALLOW_COPY_AND_ASSIGN(ErrorRecordingRawChannelDelegate); |
467 }; | 477 }; |
468 | 478 |
469 // Tests (fatal) errors. | 479 // Tests (fatal) errors. |
470 TEST_F(RawChannelTest, OnError) { | 480 TEST_F(RawChannelTest, OnError) { |
471 ErrorRecordingRawChannelDelegate delegate(0, true, true); | 481 ErrorRecordingRawChannelDelegate delegate(0, true, true); |
472 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 482 RawChannel* rc = RawChannel::Create(handles[0].Pass()); |
473 test_io_thread()->PostTaskAndWait( | 483 internal::g_io_thread_task_runner->PostTask( |
474 FROM_HERE, | 484 FROM_HERE, |
475 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 485 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
| 486 FlushIOThread(); |
476 | 487 |
477 // Close the handle of the other end, which should make writing fail. | 488 // Close the handle of the other end, which should make writing fail. |
478 handles[1].reset(); | 489 handles[1].reset(); |
479 | 490 |
480 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 491 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
481 | 492 |
482 // We should get a write error. | 493 // We should get a write error. |
483 delegate.WaitForWriteError(); | 494 delegate.WaitForWriteError(); |
484 | 495 |
485 // We should also get a read error. | 496 // We should also get a read error. |
486 delegate.WaitForReadError(); | 497 delegate.WaitForReadError(); |
487 | 498 |
488 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(2))); | 499 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(2))); |
489 | 500 |
490 // Sleep a bit, to make sure we don't get another |OnError()| | 501 // Sleep a bit, to make sure we don't get another |OnError()| |
491 // notification. (If we actually get another one, |OnError()| crashes.) | 502 // notification. (If we actually get another one, |OnError()| crashes.) |
492 test::Sleep(test::DeadlineFromMilliseconds(20)); | 503 test::Sleep(test::DeadlineFromMilliseconds(20)); |
493 | 504 |
494 test_io_thread()->PostTaskAndWait( | 505 internal::g_io_thread_task_runner->PostTask( |
495 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); | 506 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); |
496 } | 507 } |
497 | 508 |
498 // RawChannelTest.ReadUnaffectedByWriteError ----------------------------------- | 509 // RawChannelTest.ReadUnaffectedByWriteError ----------------------------------- |
499 | 510 |
500 TEST_F(RawChannelTest, ReadUnaffectedByWriteError) { | 511 TEST_F(RawChannelTest, ReadUnaffectedByWriteError) { |
501 const size_t kMessageCount = 5; | 512 const size_t kMessageCount = 5; |
502 | 513 |
503 // Write a few messages into the other end. | 514 // Write a few messages into the other end. |
504 uint32_t message_size = 1; | 515 uint32_t message_size = 1; |
505 for (size_t i = 0; i < kMessageCount; | 516 for (size_t i = 0; i < kMessageCount; |
506 i++, message_size += message_size / 2 + 1) | 517 i++, message_size += message_size / 2 + 1) |
507 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), message_size)); | 518 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), message_size)); |
508 | 519 |
509 // Close the other end, which should make writing fail. | 520 // Close the other end, which should make writing fail. |
510 handles[1].reset(); | 521 handles[1].reset(); |
511 | 522 |
512 // Only start up reading here. The system buffer should still contain the | 523 // Only start up reading here. The system buffer should still contain the |
513 // messages that were written. | 524 // messages that were written. |
514 ErrorRecordingRawChannelDelegate delegate(kMessageCount, true, true); | 525 ErrorRecordingRawChannelDelegate delegate(kMessageCount, true, true); |
515 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 526 RawChannel* rc = RawChannel::Create(handles[0].Pass()); |
516 test_io_thread()->PostTaskAndWait( | 527 internal::g_io_thread_task_runner->PostTask( |
517 FROM_HERE, | 528 FROM_HERE, |
518 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 529 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
| 530 FlushIOThread(); |
519 | 531 |
520 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 532 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
521 | 533 |
522 // We should definitely get a write error. | 534 // We should definitely get a write error. |
523 delegate.WaitForWriteError(); | 535 delegate.WaitForWriteError(); |
524 | 536 |
525 // Wait for reading to finish. A writing failure shouldn't affect reading. | 537 // Wait for reading to finish. A writing failure shouldn't affect reading. |
526 delegate.Wait(); | 538 delegate.Wait(); |
527 | 539 |
528 // And then we should get a read error. | 540 // And then we should get a read error. |
529 delegate.WaitForReadError(); | 541 delegate.WaitForReadError(); |
530 | 542 |
531 test_io_thread()->PostTaskAndWait( | 543 internal::g_io_thread_task_runner->PostTask( |
532 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); | 544 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); |
533 } | 545 } |
534 | 546 |
535 // RawChannelTest.ReadWritePlatformHandles ------------------------------------- | 547 // RawChannelTest.ReadWritePlatformHandles ------------------------------------- |
536 | 548 |
537 class ReadPlatformHandlesCheckerRawChannelDelegate | 549 class ReadPlatformHandlesCheckerRawChannelDelegate |
538 : public RawChannel::Delegate { | 550 : public RawChannel::Delegate { |
539 public: | 551 public: |
540 ReadPlatformHandlesCheckerRawChannelDelegate() : done_event_(false, false) {} | 552 ReadPlatformHandlesCheckerRawChannelDelegate() : done_event_(false, false) {} |
541 ~ReadPlatformHandlesCheckerRawChannelDelegate() override {} | 553 ~ReadPlatformHandlesCheckerRawChannelDelegate() override {} |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 | 602 |
591 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadPlatformHandlesCheckerRawChannelDelegate); | 603 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadPlatformHandlesCheckerRawChannelDelegate); |
592 }; | 604 }; |
593 | 605 |
594 TEST_F(RawChannelTest, ReadWritePlatformHandles) { | 606 TEST_F(RawChannelTest, ReadWritePlatformHandles) { |
595 base::ScopedTempDir temp_dir; | 607 base::ScopedTempDir temp_dir; |
596 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 608 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
597 | 609 |
598 WriteOnlyRawChannelDelegate write_delegate; | 610 WriteOnlyRawChannelDelegate write_delegate; |
599 RawChannel* rc_write = RawChannel::Create(handles[0].Pass()); | 611 RawChannel* rc_write = RawChannel::Create(handles[0].Pass()); |
600 test_io_thread()->PostTaskAndWait( | 612 internal::g_io_thread_task_runner->PostTask( |
601 FROM_HERE, | 613 FROM_HERE, |
602 base::Bind(&InitOnIOThread, rc_write, base::Unretained(&write_delegate))); | 614 base::Bind(&InitOnIOThread, rc_write, base::Unretained(&write_delegate))); |
603 | 615 |
604 ReadPlatformHandlesCheckerRawChannelDelegate read_delegate; | 616 ReadPlatformHandlesCheckerRawChannelDelegate read_delegate; |
605 RawChannel* rc_read = RawChannel::Create(handles[1].Pass()); | 617 RawChannel* rc_read = RawChannel::Create(handles[1].Pass()); |
606 test_io_thread()->PostTaskAndWait( | 618 internal::g_io_thread_task_runner->PostTask( |
607 FROM_HERE, | 619 FROM_HERE, |
608 base::Bind(&InitOnIOThread, rc_read, base::Unretained(&read_delegate))); | 620 base::Bind(&InitOnIOThread, rc_read, base::Unretained(&read_delegate))); |
609 | 621 |
610 base::FilePath unused; | 622 base::FilePath unused; |
611 base::ScopedFILE fp1( | 623 base::ScopedFILE fp1( |
612 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 624 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
613 EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); | 625 EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); |
614 base::ScopedFILE fp2( | 626 base::ScopedFILE fp2( |
615 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 627 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
616 EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get())); | 628 EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get())); |
617 | 629 |
618 { | 630 { |
619 const char kHello[] = "hello"; | 631 const char kHello[] = "hello"; |
620 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector()); | 632 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector()); |
621 platform_handles->push_back( | 633 platform_handles->push_back( |
622 test::PlatformHandleFromFILE(fp1.Pass()).release()); | 634 test::PlatformHandleFromFILE(fp1.Pass()).release()); |
623 platform_handles->push_back( | 635 platform_handles->push_back( |
624 test::PlatformHandleFromFILE(fp2.Pass()).release()); | 636 test::PlatformHandleFromFILE(fp2.Pass()).release()); |
625 | 637 |
626 scoped_ptr<MessageInTransit> message( | 638 scoped_ptr<MessageInTransit> message( |
627 new MessageInTransit(MessageInTransit::Type::MESSAGE, | 639 new MessageInTransit(MessageInTransit::Type::MESSAGE, |
628 sizeof(kHello), kHello)); | 640 sizeof(kHello), kHello)); |
629 message->SetTransportData(make_scoped_ptr(new TransportData( | 641 message->SetTransportData(make_scoped_ptr(new TransportData( |
630 platform_handles.Pass(), rc_write->GetSerializedPlatformHandleSize()))); | 642 platform_handles.Pass(), rc_write->GetSerializedPlatformHandleSize()))); |
631 EXPECT_TRUE(rc_write->WriteMessage(message.Pass())); | 643 EXPECT_TRUE(rc_write->WriteMessage(message.Pass())); |
632 } | 644 } |
633 | 645 |
634 read_delegate.Wait(); | 646 read_delegate.Wait(); |
635 | 647 |
636 test_io_thread()->PostTaskAndWait( | 648 internal::g_io_thread_task_runner->PostTask( |
637 FROM_HERE, | 649 FROM_HERE, |
638 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read))); | 650 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read))); |
639 test_io_thread()->PostTaskAndWait( | 651 internal::g_io_thread_task_runner->PostTask( |
640 FROM_HERE, | 652 FROM_HERE, |
641 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write))); | 653 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write))); |
642 } | 654 } |
643 | 655 |
644 } // namespace | 656 } // namespace |
645 } // namespace edk | 657 } // namespace edk |
646 } // namespace mojo | 658 } // namespace mojo |
OLD | NEW |