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> |
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 471 RawChannel* rc = RawChannel::Create(handles[0].Pass()); |
472 test_io_thread()->PostTaskAndWait( | 472 test_io_thread()->PostTaskAndWait( |
473 FROM_HERE, | 473 FROM_HERE, |
474 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 474 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
475 | 475 |
476 // Close the handle of the other end, which should make writing fail. | 476 // Close the handle of the other end, which should make writing fail. |
477 handles[1].reset(); | 477 handles[1].reset(); |
478 | 478 |
479 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 479 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
480 | 480 |
481 // We should get a write error. | 481 // We should get a shutdown error. |
482 delegate.WaitForWriteError(); | |
483 | |
484 // We should also get a read error. | |
485 delegate.WaitForReadError(); | 482 delegate.WaitForReadError(); |
486 | 483 |
487 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(2))); | |
488 | |
489 // Sleep a bit, to make sure we don't get another |OnError()| | 484 // Sleep a bit, to make sure we don't get another |OnError()| |
490 // notification. (If we actually get another one, |OnError()| crashes.) | 485 // notification. (If we actually get another one, |OnError()| crashes.) |
491 test::Sleep(test::DeadlineFromMilliseconds(20)); | 486 test::Sleep(test::DeadlineFromMilliseconds(20)); |
492 | 487 |
493 test_io_thread()->PostTaskAndWait( | 488 test_io_thread()->PostTaskAndWait( |
494 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); | 489 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); |
495 } | 490 } |
496 | 491 |
497 // RawChannelTest.ReadUnaffectedByWriteError ----------------------------------- | 492 // RawChannelTest.ReadUnaffectedByWriteError ----------------------------------- |
498 | 493 |
499 TEST_F(RawChannelTest, ReadUnaffectedByWriteError) { | 494 TEST_F(RawChannelTest, ReadUnaffectedByWriteError) { |
500 const size_t kMessageCount = 5; | 495 const size_t kMessageCount = 5; |
501 | 496 |
502 // Write a few messages into the other end. | 497 // Write a few messages into the other end. |
| 498 RawChannel* other = RawChannel::Create(handles[1].Pass()); |
| 499 WriteOnlyRawChannelDelegate write_delegate; |
| 500 test_io_thread()->PostTaskAndWait( |
| 501 FROM_HERE, |
| 502 base::Bind(&InitOnIOThread, other, base::Unretained(&write_delegate))); |
503 uint32_t message_size = 1; | 503 uint32_t message_size = 1; |
504 for (size_t i = 0; i < kMessageCount; | 504 for (size_t i = 0; i < kMessageCount; |
505 i++, message_size += message_size / 2 + 1) | 505 i++, message_size += message_size / 2 + 1) |
506 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), message_size)); | 506 EXPECT_TRUE(other->WriteMessage(MakeTestMessage(message_size))); |
507 | 507 |
508 // Close the other end, which should make writing fail. | 508 // Close the other end, which should make writing fail. |
509 handles[1].reset(); | 509 test_io_thread()->PostTaskAndWait( |
| 510 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(other))); |
510 | 511 |
511 // Only start up reading here. The system buffer should still contain the | 512 // Only start up reading here. The RawChannel ensures that it won't close the |
512 // messages that were written. | 513 // pipe until the other end read everything. |
513 ErrorRecordingRawChannelDelegate delegate(kMessageCount, true, true); | 514 ErrorRecordingRawChannelDelegate delegate(kMessageCount, true, true); |
514 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 515 RawChannel* rc = RawChannel::Create(handles[0].Pass()); |
515 test_io_thread()->PostTaskAndWait( | 516 test_io_thread()->PostTaskAndWait( |
516 FROM_HERE, | 517 FROM_HERE, |
517 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 518 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
518 | 519 |
519 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | |
520 | |
521 // We should definitely get a write error. | |
522 delegate.WaitForWriteError(); | |
523 | |
524 // Wait for reading to finish. A writing failure shouldn't affect reading. | 520 // Wait for reading to finish. A writing failure shouldn't affect reading. |
525 delegate.Wait(); | 521 delegate.Wait(); |
526 | 522 |
527 // And then we should get a read error. | 523 // And then we should get a read error. |
528 delegate.WaitForReadError(); | 524 delegate.WaitForReadError(); |
529 | 525 |
530 test_io_thread()->PostTaskAndWait( | 526 test_io_thread()->PostTaskAndWait( |
531 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); | 527 FROM_HERE, base::Bind(&RawChannel::Shutdown, base::Unretained(rc))); |
532 } | 528 } |
533 | 529 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 FROM_HERE, | 648 FROM_HERE, |
653 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read))); | 649 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read))); |
654 test_io_thread()->PostTaskAndWait( | 650 test_io_thread()->PostTaskAndWait( |
655 FROM_HERE, | 651 FROM_HERE, |
656 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write))); | 652 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write))); |
657 } | 653 } |
658 | 654 |
659 } // namespace | 655 } // namespace |
660 } // namespace edk | 656 } // namespace edk |
661 } // namespace mojo | 657 } // namespace mojo |
OLD | NEW |