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

Side by Side Diff: content/child/shared_memory_data_consumer_handle_unittest.cc

Issue 1186053004: Cancel loading when body stream reader is detached. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@error-ipc-data-consumer
Patch Set: Created 5 years, 6 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
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 "content/child/shared_memory_data_consumer_handle.h" 5 #include "content/child/shared_memory_data_consumer_handle.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 int encoded_length() const override { return static_cast<int>(data_.size()); } 78 int encoded_length() const override { return static_cast<int>(data_.size()); }
79 79
80 private: 80 private:
81 const std::string name_; 81 const std::string name_;
82 const std::vector<char> data_; 82 const std::vector<char> data_;
83 scoped_refptr<Logger> logger_; 83 scoped_refptr<Logger> logger_;
84 84
85 DISALLOW_COPY_AND_ASSIGN(LoggingFixedReceivedData); 85 DISALLOW_COPY_AND_ASSIGN(LoggingFixedReceivedData);
86 }; 86 };
87 87
88 class DestructionTrackingFunction
89 : public base::RefCountedThreadSafe<DestructionTrackingFunction> {
90 public:
91 MOCK_METHOD0(Destruct, void(void));
92 MOCK_METHOD0(Call, void(void));
93
94 protected:
95 friend class base::RefCountedThreadSafe<DestructionTrackingFunction>;
96 virtual ~DestructionTrackingFunction() { Destruct(); }
97 };
98
88 class MockClient : public WebDataConsumerHandle::Client { 99 class MockClient : public WebDataConsumerHandle::Client {
89 public: 100 public:
90 MOCK_METHOD0(didGetReadable, void()); 101 MOCK_METHOD0(didGetReadable, void());
91 }; 102 };
92 103
93 std::string ToString(const void* p, size_t size) { 104 std::string ToString(const void* p, size_t size) {
94 const char* q = static_cast<const char*>(p); 105 const char* q = static_cast<const char*>(p);
95 return std::string(q, q + size); 106 return std::string(q, q + size);
96 } 107 }
97 108
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 EXPECT_EQ(nullptr, buffer); 586 EXPECT_EQ(nullptr, buffer);
576 587
577 writer_->Close(); 588 writer_->Close();
578 589
579 result = reader->beginRead(&buffer, kNone, &size); 590 result = reader->beginRead(&buffer, kNone, &size);
580 EXPECT_EQ(kDone, result); 591 EXPECT_EQ(kDone, result);
581 EXPECT_EQ(0u, size); 592 EXPECT_EQ(0u, size);
582 EXPECT_EQ(nullptr, buffer); 593 EXPECT_EQ(nullptr, buffer);
583 } 594 }
584 595
596 TEST_P(SharedMemoryDataConsumerHandleTest, CallOnClearWhenDestructed1) {
597 // Call |on_clear| when the handle is gone and if there is no reader.
598 Checkpoint checkpoint;
599 scoped_refptr<DestructionTrackingFunction> on_clear(
600 new StrictMock<DestructionTrackingFunction>);
601
602 InSequence s;
603 EXPECT_CALL(checkpoint, Call(0));
604 EXPECT_CALL(checkpoint, Call(1));
605 EXPECT_CALL(*on_clear, Call());
606 EXPECT_CALL(*on_clear, Destruct());
607 EXPECT_CALL(checkpoint, Call(2));
608
609 checkpoint.Call(0);
610 handle_.reset(new SharedMemoryDataConsumerHandle(
611 kApplyBackpressure,
612 base::Bind(&DestructionTrackingFunction::Call, on_clear), &writer_));
613 handle_.reset();
614 on_clear = nullptr;
615 checkpoint.Call(1);
616 RunPostedTasks();
617 checkpoint.Call(2);
618 }
619
620 TEST_P(SharedMemoryDataConsumerHandleTest, CallOnClearWhenDestructed2) {
621 // Call |on_clear| when the reader is gone if the handle is alredy gone.
622 Checkpoint checkpoint;
623 scoped_refptr<DestructionTrackingFunction> on_clear(
624 new StrictMock<DestructionTrackingFunction>);
625
626 InSequence s;
627 EXPECT_CALL(checkpoint, Call(0));
628 EXPECT_CALL(checkpoint, Call(1));
629 EXPECT_CALL(checkpoint, Call(2));
630 EXPECT_CALL(checkpoint, Call(3));
631 EXPECT_CALL(*on_clear, Call());
632 EXPECT_CALL(*on_clear, Destruct());
633 EXPECT_CALL(checkpoint, Call(4));
634
635 checkpoint.Call(0);
636 handle_.reset(new SharedMemoryDataConsumerHandle(
637 kApplyBackpressure,
638 base::Bind(&DestructionTrackingFunction::Call, on_clear), &writer_));
639 auto reader = handle_->ObtainReader(nullptr);
640 handle_.reset();
641 on_clear = nullptr;
642 checkpoint.Call(1);
643 RunPostedTasks();
644 checkpoint.Call(2);
645 reader.reset();
646 checkpoint.Call(3);
647 RunPostedTasks();
648 checkpoint.Call(4);
649 }
650
651 TEST_P(SharedMemoryDataConsumerHandleTest, DoNotCallOnClearWhenDone) {
652 Checkpoint checkpoint;
653 scoped_refptr<DestructionTrackingFunction> on_clear(
654 new StrictMock<DestructionTrackingFunction>);
655
656 InSequence s;
657 EXPECT_CALL(checkpoint, Call(0));
658 EXPECT_CALL(checkpoint, Call(1));
659 EXPECT_CALL(*on_clear, Destruct());
660 EXPECT_CALL(checkpoint, Call(2));
661 EXPECT_CALL(checkpoint, Call(3));
662 EXPECT_CALL(checkpoint, Call(4));
663
664 checkpoint.Call(0);
665 handle_.reset(new SharedMemoryDataConsumerHandle(
666 kApplyBackpressure,
667 base::Bind(&DestructionTrackingFunction::Call, on_clear), &writer_));
668 on_clear = nullptr;
669 checkpoint.Call(1);
670 writer_->Close();
671 checkpoint.Call(2);
672 handle_.reset();
673 checkpoint.Call(3);
674 RunPostedTasks();
675 checkpoint.Call(4);
676 }
677
678 TEST_P(SharedMemoryDataConsumerHandleTest, DoNotCallOnClearWhenErrored) {
679 Checkpoint checkpoint;
680 scoped_refptr<DestructionTrackingFunction> on_clear(
681 new StrictMock<DestructionTrackingFunction>);
682
683 InSequence s;
684 EXPECT_CALL(checkpoint, Call(0));
685 EXPECT_CALL(checkpoint, Call(1));
686 EXPECT_CALL(*on_clear, Destruct());
687 EXPECT_CALL(checkpoint, Call(2));
688 EXPECT_CALL(checkpoint, Call(3));
689 EXPECT_CALL(checkpoint, Call(4));
690
691 checkpoint.Call(0);
692 handle_.reset(new SharedMemoryDataConsumerHandle(
693 kApplyBackpressure,
694 base::Bind(&DestructionTrackingFunction::Call, on_clear), &writer_));
695 on_clear = nullptr;
696 checkpoint.Call(1);
697 writer_->Fail();
698 checkpoint.Call(2);
699 handle_.reset();
700 checkpoint.Call(3);
701 RunPostedTasks();
702 checkpoint.Call(4);
703 }
704
585 TEST_P(SharedMemoryDataConsumerHandleTest, TwoPhaseReadWithMultipleData) { 705 TEST_P(SharedMemoryDataConsumerHandleTest, TwoPhaseReadWithMultipleData) {
586 writer_->AddData(NewFixedData("Once ")); 706 writer_->AddData(NewFixedData("Once "));
587 writer_->AddData(NewFixedData("upon ")); 707 writer_->AddData(NewFixedData("upon "));
588 708
589 Result result; 709 Result result;
590 const void* buffer = &result; 710 const void* buffer = &result;
591 size_t size = 99; 711 size_t size = 99;
592 712
593 auto reader = handle_->ObtainReader(nullptr); 713 auto reader = handle_->ObtainReader(nullptr);
594 result = reader->beginRead(&buffer, kNone, &size); 714 result = reader->beginRead(&buffer, kNone, &size);
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
896 logger->log()); 1016 logger->log());
897 } 1017 }
898 1018
899 INSTANTIATE_TEST_CASE_P(SharedMemoryDataConsumerHandleTest, 1019 INSTANTIATE_TEST_CASE_P(SharedMemoryDataConsumerHandleTest,
900 SharedMemoryDataConsumerHandleTest, 1020 SharedMemoryDataConsumerHandleTest,
901 ::testing::Values(kApplyBackpressure, 1021 ::testing::Values(kApplyBackpressure,
902 kDoNotApplyBackpressure)); 1022 kDoNotApplyBackpressure));
903 } // namespace 1023 } // namespace
904 1024
905 } // namespace content 1025 } // namespace content
OLDNEW
« no previous file with comments | « content/child/shared_memory_data_consumer_handle.cc ('k') | content/child/web_url_loader_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698