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

Side by Side Diff: mojo/system/raw_channel_unittest.cc

Issue 257623003: Mojo: Add RawChannel tests for calling Shutdown() from OnReadMessage()/OnFatalError(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add comment Created 6 years, 7 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 | « mojo/system/raw_channel_posix.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 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/system/raw_channel.h" 5 #include "mojo/system/raw_channel.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 } 246 }
247 247
248 if (should_signal) 248 if (should_signal)
249 done_event_.Signal(); 249 done_event_.Signal();
250 } 250 }
251 virtual void OnFatalError(FatalError fatal_error) OVERRIDE { 251 virtual void OnFatalError(FatalError fatal_error) OVERRIDE {
252 // We'll get a read error when the connection is closed. 252 // We'll get a read error when the connection is closed.
253 CHECK_EQ(fatal_error, FATAL_ERROR_FAILED_READ); 253 CHECK_EQ(fatal_error, FATAL_ERROR_FAILED_READ);
254 } 254 }
255 255
256 // Wait for all the messages (of sizes |expected_sizes_|) to be seen. 256 // Waits for all the messages (of sizes |expected_sizes_|) to be seen.
257 void Wait() { 257 void Wait() {
258 done_event_.Wait(); 258 done_event_.Wait();
259 } 259 }
260 260
261 void SetExpectedSizes(const std::vector<uint32_t>& expected_sizes) { 261 void SetExpectedSizes(const std::vector<uint32_t>& expected_sizes) {
262 base::AutoLock locker(lock_); 262 base::AutoLock locker(lock_);
263 CHECK_EQ(position_, expected_sizes_.size()); 263 CHECK_EQ(position_, expected_sizes_.size());
264 expected_sizes_ = expected_sizes; 264 expected_sizes_ = expected_sizes;
265 position_ = 0; 265 position_ = 0;
266 } 266 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 message_view.num_bytes())); 355 message_view.num_bytes()));
356 356
357 if (count_ >= expected_count_) 357 if (count_ >= expected_count_)
358 done_event_.Signal(); 358 done_event_.Signal();
359 } 359 }
360 virtual void OnFatalError(FatalError fatal_error) OVERRIDE { 360 virtual void OnFatalError(FatalError fatal_error) OVERRIDE {
361 // We'll get a read error when the connection is closed. 361 // We'll get a read error when the connection is closed.
362 CHECK_EQ(fatal_error, FATAL_ERROR_FAILED_READ); 362 CHECK_EQ(fatal_error, FATAL_ERROR_FAILED_READ);
363 } 363 }
364 364
365 // Wait for all the messages to have been seen. 365 // Waits for all the messages to have been seen.
366 void Wait() { 366 void Wait() {
367 done_event_.Wait(); 367 done_event_.Wait();
368 } 368 }
369 369
370 private: 370 private:
371 base::WaitableEvent done_event_; 371 base::WaitableEvent done_event_;
372 size_t expected_count_; 372 size_t expected_count_;
373 size_t count_; 373 size_t count_;
374 374
375 DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate); 375 DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 io_thread()->PostTaskAndWait(FROM_HERE, 490 io_thread()->PostTaskAndWait(FROM_HERE,
491 base::Bind(&RawChannel::Shutdown, 491 base::Bind(&RawChannel::Shutdown,
492 base::Unretained(rc.get()))); 492 base::Unretained(rc.get())));
493 } 493 }
494 494
495 // RawChannelTest.ReadUnaffectedByWriteFatalError ------------------------------ 495 // RawChannelTest.ReadUnaffectedByWriteFatalError ------------------------------
496 496
497 TEST_F(RawChannelTest, ReadUnaffectedByWriteFatalError) { 497 TEST_F(RawChannelTest, ReadUnaffectedByWriteFatalError) {
498 const size_t kMessageCount = 5; 498 const size_t kMessageCount = 5;
499 499
500 // Write into the other end a few messages. 500 // Write a few messages into the other end.
501 uint32_t message_size = 1; 501 uint32_t message_size = 1;
502 for (size_t count = 0; count < kMessageCount; 502 for (size_t i = 0; i < kMessageCount;
503 ++count, message_size += message_size / 2 + 1) { 503 i++, message_size += message_size / 2 + 1)
504 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), message_size)); 504 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), message_size));
505 }
506 505
507 // Close the other end, which should make writing fail. 506 // Close the other end, which should make writing fail.
508 handles[1].reset(); 507 handles[1].reset();
509 508
510 // Only start up reading here. The system buffer should still contain the 509 // Only start up reading here. The system buffer should still contain the
511 // messages that were written. 510 // messages that were written.
512 FatalErrorRecordingRawChannelDelegate delegate(kMessageCount, true, true); 511 FatalErrorRecordingRawChannelDelegate delegate(kMessageCount, true, true);
513 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass())); 512 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
514 io_thread()->PostTaskAndWait(FROM_HERE, 513 io_thread()->PostTaskAndWait(FROM_HERE,
515 base::Bind(&InitOnIOThread, rc.get(), 514 base::Bind(&InitOnIOThread, rc.get(),
(...skipping 25 matching lines...) Expand all
541 io_thread()->PostTaskAndWait(FROM_HERE, 540 io_thread()->PostTaskAndWait(FROM_HERE,
542 base::Bind(&InitOnIOThread, rc.get(), 541 base::Bind(&InitOnIOThread, rc.get(),
543 base::Unretained(&delegate))); 542 base::Unretained(&delegate)));
544 io_thread()->PostTaskAndWait(FROM_HERE, 543 io_thread()->PostTaskAndWait(FROM_HERE,
545 base::Bind(&RawChannel::Shutdown, 544 base::Bind(&RawChannel::Shutdown,
546 base::Unretained(rc.get()))); 545 base::Unretained(rc.get())));
547 546
548 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); 547 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1)));
549 } 548 }
550 549
550 // RawChannelTest.ShutdownOnReadMessage ----------------------------------------
551
552 class ShutdownOnReadMessageRawChannelDelegate : public RawChannel::Delegate {
553 public:
554 explicit ShutdownOnReadMessageRawChannelDelegate(RawChannel* raw_channel)
555 : raw_channel_(raw_channel),
556 done_event_(false, false),
557 did_shutdown_(false) {}
558 virtual ~ShutdownOnReadMessageRawChannelDelegate() {}
559
560 // |RawChannel::Delegate| implementation (called on the I/O thread):
561 virtual void OnReadMessage(
562 const MessageInTransit::View& message_view) OVERRIDE {
563 EXPECT_FALSE(did_shutdown_);
564 EXPECT_TRUE(CheckMessageData(message_view.bytes(),
565 message_view.num_bytes()));
566 raw_channel_->Shutdown();
567 did_shutdown_ = true;
568 done_event_.Signal();
569 }
570 virtual void OnFatalError(FatalError /*fatal_error*/) OVERRIDE {
571 CHECK(false); // Should not get called.
572 }
573
574 // Waits for shutdown.
575 void Wait() {
576 done_event_.Wait();
577 EXPECT_TRUE(did_shutdown_);
578 }
579
580 private:
581 RawChannel* const raw_channel_;
582 base::WaitableEvent done_event_;
583 bool did_shutdown_;
584
585 DISALLOW_COPY_AND_ASSIGN(ShutdownOnReadMessageRawChannelDelegate);
586 };
587
588 // TODO(vtl): crbug.com/366768
589 #if defined(OS_WIN)
590 #define MAYBE_ShutdownOnReadMessage DISABLED_ShutdownOnReadMessage
591 #else
592 #define MAYBE_ShutdownOnReadMessage ShutdownOnReadMessage
593 #endif
594 TEST_F(RawChannelTest, MAYBE_ShutdownOnReadMessage) {
595 // Write a few messages into the other end.
596 for (size_t count = 0; count < 5; count++)
597 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), 10));
598
599 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
600 ShutdownOnReadMessageRawChannelDelegate delegate(rc.get());
601 io_thread()->PostTaskAndWait(FROM_HERE,
602 base::Bind(&InitOnIOThread, rc.get(),
603 base::Unretained(&delegate)));
604
605 // Wait for the delegate, which will shut the |RawChannel| down.
606 delegate.Wait();
607 }
608
609 // RawChannelTest.ShutdownOnFatalError{Read, Write} ----------------------------
610
611 class ShutdownOnFatalErrorRawChannelDelegate : public RawChannel::Delegate {
612 public:
613 ShutdownOnFatalErrorRawChannelDelegate(RawChannel* raw_channel,
614 FatalError shutdown_on_error_type)
615 : raw_channel_(raw_channel),
616 shutdown_on_error_type_(shutdown_on_error_type),
617 done_event_(false, false),
618 did_shutdown_(false) {}
619 virtual ~ShutdownOnFatalErrorRawChannelDelegate() {}
620
621 // |RawChannel::Delegate| implementation (called on the I/O thread):
622 virtual void OnReadMessage(
623 const MessageInTransit::View& /*message_view*/) OVERRIDE {
624 CHECK(false); // Should not get called.
625 }
626 virtual void OnFatalError(FatalError fatal_error) OVERRIDE {
627 EXPECT_FALSE(did_shutdown_);
628 if (fatal_error != shutdown_on_error_type_)
629 return;
630 raw_channel_->Shutdown();
631 did_shutdown_ = true;
632 done_event_.Signal();
633 }
634
635 // Waits for shutdown.
636 void Wait() {
637 done_event_.Wait();
638 EXPECT_TRUE(did_shutdown_);
639 }
640
641 private:
642 RawChannel* const raw_channel_;
643 const FatalError shutdown_on_error_type_;
644 base::WaitableEvent done_event_;
645 bool did_shutdown_;
646
647 DISALLOW_COPY_AND_ASSIGN(ShutdownOnFatalErrorRawChannelDelegate);
648 };
649
650 // TODO(vtl): crbug.com/366768
651 #if defined(OS_WIN)
652 #define MAYBE_ShutdownOnFatalErrorRead DISABLED_ShutdownOnFatalErrorRead
653 #else
654 #define MAYBE_ShutdownOnFatalErrorRead ShutdownOnFatalErrorRead
655 #endif
656 TEST_F(RawChannelTest, MAYBE_ShutdownOnFatalErrorRead) {
657 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
658 ShutdownOnFatalErrorRawChannelDelegate delegate(
659 rc.get(), RawChannel::Delegate::FATAL_ERROR_FAILED_READ);
660 io_thread()->PostTaskAndWait(FROM_HERE,
661 base::Bind(&InitOnIOThread, rc.get(),
662 base::Unretained(&delegate)));
663
664 // Close the handle of the other end, which should stuff fail.
665 handles[1].reset();
666
667 // Wait for the delegate, which will shut the |RawChannel| down.
668 delegate.Wait();
669 }
670
671 // TODO(vtl): crbug.com/366768
672 #if defined(OS_WIN)
673 #define MAYBE_ShutdownOnFatalErrorWrite DISABLED_ShutdownOnFatalErrorWrite
674 #else
675 #define MAYBE_ShutdownOnFatalErrorWrite ShutdownOnFatalErrorWrite
676 #endif
677 TEST_F(RawChannelTest, MAYBE_ShutdownOnFatalErrorWrite) {
678 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass()));
679 ShutdownOnFatalErrorRawChannelDelegate delegate(
680 rc.get(), RawChannel::Delegate::FATAL_ERROR_FAILED_WRITE);
681 io_thread()->PostTaskAndWait(FROM_HERE,
682 base::Bind(&InitOnIOThread, rc.get(),
683 base::Unretained(&delegate)));
684
685 // Close the handle of the other end, which should stuff fail.
686 handles[1].reset();
687
688 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1)));
689
690 // Wait for the delegate, which will shut the |RawChannel| down.
691 delegate.Wait();
692 }
693
551 } // namespace 694 } // namespace
552 } // namespace system 695 } // namespace system
553 } // namespace mojo 696 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/raw_channel_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698