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

Side by Side Diff: content/browser/loader/mojo_async_resource_handler_unittest.cc

Issue 2738973002: Allow MojoAsyncResourceHandler::OnWillRead to complete asyncronously (Closed)
Patch Set: Cleanup Created 3 years, 9 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/browser/loader/mojo_async_resource_handler.h" 5 #include "content/browser/loader/mojo_async_resource_handler.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 uint32_t read_size = sizeof(buffer); 596 uint32_t read_size = sizeof(buffer);
597 MojoResult result = 597 MojoResult result =
598 mojo::ReadDataRaw(url_loader_client_.response_body(), buffer, 598 mojo::ReadDataRaw(url_loader_client_.response_body(), buffer,
599 &read_size, MOJO_READ_DATA_FLAG_NONE); 599 &read_size, MOJO_READ_DATA_FLAG_NONE);
600 if (result == MOJO_RESULT_SHOULD_WAIT) 600 if (result == MOJO_RESULT_SHOULD_WAIT)
601 continue; 601 continue;
602 ASSERT_EQ(MOJO_RESULT_OK, result); 602 ASSERT_EQ(MOJO_RESULT_OK, result);
603 contents.append(buffer, read_size); 603 contents.append(buffer, read_size);
604 } 604 }
605 EXPECT_EQ(data, contents); 605 EXPECT_EQ(data, contents);
606 EXPECT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, 606 EXPECT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->status());
607 mock_loader_->status());
608 } 607 }
609 608
610 TEST_F(MojoAsyncResourceHandlerTest, 609 TEST_F(MojoAsyncResourceHandlerTest,
611 IOBufferFromOnWillReadShouldRemainValidEvenIfHandlerIsGone) { 610 IOBufferFromOnWillReadShouldRemainValidEvenIfHandlerIsGone) {
612 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 611 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
613 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 612 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
614 // The io_buffer size that the mime sniffer requires implicitly. 613 // The io_buffer size that the mime sniffer requires implicitly.
615 ASSERT_GE(mock_loader_->io_buffer_size(), 614 ASSERT_GE(mock_loader_->io_buffer_size(),
616 kSizeMimeSnifferRequiresForFirstOnWillRead); 615 kSizeMimeSnifferRequiresForFirstOnWillRead);
617 616
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 break; 733 break;
735 ASSERT_TRUE(result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_OK); 734 ASSERT_TRUE(result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_OK);
736 } 735 }
737 } 736 }
738 737
739 TEST_F(MojoAsyncResourceHandlerTest, OutOfBandCancelDuringBodyTransmission) { 738 TEST_F(MojoAsyncResourceHandlerTest, OutOfBandCancelDuringBodyTransmission) {
740 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 739 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
741 740
742 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 741 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
743 std::string data(mock_loader_->io_buffer_size(), 'a'); 742 std::string data(mock_loader_->io_buffer_size(), 'a');
743 ASSERT_EQ(MockResourceLoader::Status::IDLE,
744 mock_loader_->OnReadCompleted(data));
744 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, 745 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING,
745 mock_loader_->OnReadCompleted(data)); 746 mock_loader_->OnWillRead());
746 url_loader_client_.RunUntilResponseBodyArrived(); 747 url_loader_client_.RunUntilResponseBodyArrived();
747 ASSERT_TRUE(url_loader_client_.response_body().is_valid()); 748 ASSERT_TRUE(url_loader_client_.response_body().is_valid());
748 749
749 net::URLRequestStatus status(net::URLRequestStatus::FAILED, net::ERR_FAILED); 750 net::URLRequestStatus status(net::URLRequestStatus::FAILED, net::ERR_FAILED);
750 ASSERT_EQ( 751 ASSERT_EQ(
751 MockResourceLoader::Status::IDLE, 752 MockResourceLoader::Status::IDLE,
752 mock_loader_->OnResponseCompletedFromExternalOutOfBandCancel(status)); 753 mock_loader_->OnResponseCompletedFromExternalOutOfBandCancel(status));
753 754
754 url_loader_client_.RunUntilComplete(); 755 url_loader_client_.RunUntilComplete();
755 EXPECT_TRUE(url_loader_client_.has_received_completion()); 756 EXPECT_TRUE(url_loader_client_.has_received_completion());
(...skipping 19 matching lines...) Expand all
775 } 776 }
776 777
777 TEST_F(MojoAsyncResourceHandlerTest, BeginWriteFailsOnWillRead) { 778 TEST_F(MojoAsyncResourceHandlerTest, BeginWriteFailsOnWillRead) {
778 handler_->set_begin_write_expectation(MOJO_RESULT_UNKNOWN); 779 handler_->set_begin_write_expectation(MOJO_RESULT_UNKNOWN);
779 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 780 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
780 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->OnWillRead()); 781 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->OnWillRead());
781 } 782 }
782 783
783 TEST_F(MojoAsyncResourceHandlerTest, BeginWriteReturnsShouldWaitOnWillRead) { 784 TEST_F(MojoAsyncResourceHandlerTest, BeginWriteReturnsShouldWaitOnWillRead) {
784 handler_->set_begin_write_expectation(MOJO_RESULT_SHOULD_WAIT); 785 handler_->set_begin_write_expectation(MOJO_RESULT_SHOULD_WAIT);
786
785 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 787 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
786 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 788 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
789
790 // Bytes are read one-at-a-time, and each OnWillRead() call completes
791 // asynchronously. Note that this loop runs 4 times (once for the terminal
792 // '\0').
793 const char kReadData[] = "ABC";
794 for (const char read_char : kReadData) {
795 handler_->set_begin_write_expectation(MOJO_RESULT_SHOULD_WAIT);
796 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING,
797 mock_loader_->OnWillRead());
798 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing());
799
800 handler_->ResetBeginWriteExpectation();
801 handler_->OnWritableForTesting();
802 EXPECT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->status());
803
804 ASSERT_EQ(MockResourceLoader::Status::IDLE,
805 mock_loader_->OnReadCompleted(std::string(1, read_char)));
806 url_loader_client_.RunUntilResponseBodyArrived();
807
808 char buffer[16];
809 uint32_t read_size = sizeof(buffer);
810 MojoResult result =
811 mojo::ReadDataRaw(url_loader_client_.response_body(), buffer,
812 &read_size, MOJO_READ_DATA_FLAG_NONE);
813 if (result == MOJO_RESULT_SHOULD_WAIT) {
Randy Smith (Not in Mondays) 2017/03/17 20:06:06 Under what circumstances would this happen? And i
mmenke 2017/03/17 22:29:39 You're absolutely right that this code is complete
814 base::RunLoop().RunUntilIdle();
815 continue;
816 }
817 ASSERT_EQ(1u, read_size);
818 EXPECT_EQ(read_char, buffer[0]);
819 }
820
821 // Should only count as one in-flight request.
822 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing());
823
824 ASSERT_EQ(MockResourceLoader::Status::IDLE,
825 mock_loader_->OnResponseCompleted(
826 net::URLRequestStatus::FromError(net::OK)));
827
828 url_loader_client_.RunUntilComplete();
829 EXPECT_TRUE(url_loader_client_.has_received_completion());
830
831 handler_.reset();
832 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
833 }
834
835 // Same as above, but after th efirst OnWriteable() call, BeginWrite() indicates
Randy Smith (Not in Mondays) 2017/03/17 20:06:06 nit: "th efirst"
mmenke 2017/03/17 22:29:39 Done.
836 // should wait again. Unclear if this can happen in practice, but seems best to
837 // support it.
838 TEST_F(MojoAsyncResourceHandlerTest,
839 BeginWriteReturnsShouldWaitTwiceOnWillRead) {
840 handler_->set_begin_write_expectation(MOJO_RESULT_SHOULD_WAIT);
841
842 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
843 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
844
845 // Bytes are read one-at-a-time, and each OnWillRead() call completes
846 // asynchronously. Note that this loop runs 4 times (once for the terminal
847 // '\0').
848 const char kReadData[] = "ABC";
849 for (const char read_char : kReadData) {
850 handler_->set_begin_write_expectation(MOJO_RESULT_SHOULD_WAIT);
851 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING,
852 mock_loader_->OnWillRead());
853 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing());
854
855 handler_->OnWritableForTesting();
856 EXPECT_EQ(MockResourceLoader::Status::CALLBACK_PENDING,
857 mock_loader_->status());
858
859 handler_->ResetBeginWriteExpectation();
860 handler_->OnWritableForTesting();
861 EXPECT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->status());
862
863 ASSERT_EQ(MockResourceLoader::Status::IDLE,
864 mock_loader_->OnReadCompleted(std::string(1, read_char)));
865 url_loader_client_.RunUntilResponseBodyArrived();
866
867 char buffer[16];
868 uint32_t read_size = sizeof(buffer);
869 MojoResult result =
870 mojo::ReadDataRaw(url_loader_client_.response_body(), buffer,
871 &read_size, MOJO_READ_DATA_FLAG_NONE);
872 if (result == MOJO_RESULT_SHOULD_WAIT) {
873 base::RunLoop().RunUntilIdle();
874 continue;
875 }
876 ASSERT_EQ(1u, read_size);
877 EXPECT_EQ(read_char, buffer[0]);
878 }
879
880 // Should only count as one in-flight request.
881 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing());
882
883 ASSERT_EQ(MockResourceLoader::Status::IDLE,
884 mock_loader_->OnResponseCompleted(
885 net::URLRequestStatus::FromError(net::OK)));
886
887 url_loader_client_.RunUntilComplete();
888 EXPECT_TRUE(url_loader_client_.has_received_completion());
889
890 handler_.reset();
891 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
787 } 892 }
788 893
789 TEST_F(MojoAsyncResourceHandlerTest, 894 TEST_F(MojoAsyncResourceHandlerTest,
790 BeginWriteReturnsShouldWaitOnWillReadAndThenReturnsOK) {
791 handler_->set_begin_write_expectation(MOJO_RESULT_SHOULD_WAIT);
792 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
793 size_t written = 0;
794 while (true) {
795 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
796 MockResourceLoader::Status result = mock_loader_->OnReadCompleted(
797 std::string(mock_loader_->io_buffer_size(), 'X'));
798 written += mock_loader_->io_buffer_size();
799 if (result == MockResourceLoader::Status::CALLBACK_PENDING)
800 break;
801 ASSERT_EQ(MockResourceLoader::Status::IDLE, result);
802 }
803
804 url_loader_client_.RunUntilResponseBodyArrived();
805 ASSERT_TRUE(url_loader_client_.response_body().is_valid());
806 handler_->ResetBeginWriteExpectation();
807 handler_->OnWritableForTesting();
808
809 std::string actual;
810 while (actual.size() < written) {
811 char buf[16];
812 uint32_t read_size = sizeof(buf);
813 MojoResult result =
814 mojo::ReadDataRaw(url_loader_client_.response_body(), buf, &read_size,
815 MOJO_READ_DATA_FLAG_NONE);
816 ASSERT_TRUE(result == MOJO_RESULT_OK || result == MOJO_RESULT_SHOULD_WAIT);
817 if (result == MOJO_RESULT_OK)
818 actual.append(buf, read_size);
819 base::RunLoop().RunUntilIdle();
820 }
821
822 EXPECT_EQ(std::string(written, 'X'), actual);
823 EXPECT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->status());
824 }
825
826 TEST_F(MojoAsyncResourceHandlerTest,
827 EndWriteFailsOnWillReadWithInsufficientInitialCapacity) { 895 EndWriteFailsOnWillReadWithInsufficientInitialCapacity) {
828 MojoAsyncResourceHandler::SetAllocationSizeForTesting(2); 896 MojoAsyncResourceHandler::SetAllocationSizeForTesting(2);
829 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 897 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
830 handler_->set_end_write_expectation(MOJO_RESULT_UNKNOWN); 898 handler_->set_end_write_expectation(MOJO_RESULT_UNKNOWN);
831 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->OnWillRead()); 899 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->OnWillRead());
832 } 900 }
833 901
834 TEST_F(MojoAsyncResourceHandlerTest, EndWriteFailsOnReadCompleted) { 902 TEST_F(MojoAsyncResourceHandlerTest, EndWriteFailsOnReadCompleted) {
835 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 903 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
836 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 904 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 MOJO_READ_DATA_FLAG_NONE); 948 MOJO_READ_DATA_FLAG_NONE);
881 if (result == MOJO_RESULT_SHOULD_WAIT) 949 if (result == MOJO_RESULT_SHOULD_WAIT)
882 break; 950 break;
883 ASSERT_EQ(MOJO_RESULT_OK, result); 951 ASSERT_EQ(MOJO_RESULT_OK, result);
884 } 952 }
885 953
886 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT); 954 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT);
887 mock_loader_->WaitUntilIdleOrCanceled(); 955 mock_loader_->WaitUntilIdleOrCanceled();
888 EXPECT_FALSE(url_loader_client_.has_received_completion()); 956 EXPECT_FALSE(url_loader_client_.has_received_completion());
889 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status()); 957 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status());
890 EXPECT_EQ(net::ERR_FAILED, mock_loader_->error_code()); 958 EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES, mock_loader_->error_code());
891 } 959 }
892 960
893 TEST_F(MojoAsyncResourceHandlerUploadTest, UploadProgressHandling) { 961 TEST_F(MojoAsyncResourceHandlerUploadTest, UploadProgressHandling) {
894 ASSERT_TRUE(CallOnWillStart()); 962 ASSERT_TRUE(CallOnWillStart());
895 963
896 // Expect no report for no progress. 964 // Expect no report for no progress.
897 set_upload_progress(net::UploadProgress(0, 1000)); 965 set_upload_progress(net::UploadProgress(0, 1000));
898 handler_->PollUploadProgress(); 966 handler_->PollUploadProgress();
899 EXPECT_FALSE(url_loader_client_.has_received_upload_progress()); 967 EXPECT_FALSE(url_loader_client_.has_received_upload_progress());
900 EXPECT_EQ(0, url_loader_client_.current_upload_position()); 968 EXPECT_EQ(0, url_loader_client_.current_upload_position());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 EXPECT_EQ(net::LOWEST, request_->priority()); 1021 EXPECT_EQ(net::LOWEST, request_->priority());
954 1022
955 handler_->SetPriority(net::RequestPriority::HIGHEST, kIntraPriority); 1023 handler_->SetPriority(net::RequestPriority::HIGHEST, kIntraPriority);
956 1024
957 EXPECT_EQ(net::HIGHEST, request_->priority()); 1025 EXPECT_EQ(net::HIGHEST, request_->priority());
958 } 1026 }
959 1027
960 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1028 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
961 OnWillReadWithLongContents) { 1029 OnWillReadWithLongContents) {
962 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 1030 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
963 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 1031
964 std::string expected; 1032 std::string expected;
965 for (int i = 0; i < 3 * mock_loader_->io_buffer_size() + 2; ++i) 1033 for (int i = 0; i < 3 * mock_loader_->io_buffer_size() + 2; ++i)
966 expected += ('A' + i % 26); 1034 expected += ('A' + i % 26);
967 1035
968 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnReadCompleted(0));
mmenke 2017/03/10 22:03:45 I assume that this was to skip past the initial no
969
970 size_t written = 0; 1036 size_t written = 0;
971 std::string actual; 1037 std::string actual;
972 while (actual.size() < expected.size()) { 1038 while (actual.size() < expected.size()) {
973 while (written < expected.size() && 1039 while (written < expected.size() &&
974 mock_loader_->status() == MockResourceLoader::Status::IDLE) { 1040 mock_loader_->status() == MockResourceLoader::Status::IDLE) {
975 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 1041 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
976 const size_t to_be_written = 1042 size_t to_be_written =
977 std::min(static_cast<size_t>(mock_loader_->io_buffer_size()), 1043 std::min(static_cast<size_t>(mock_loader_->io_buffer_size()),
978 expected.size() - written); 1044 expected.size() - written);
979 1045
980 // Request should be resumed or paused. 1046 // Request should be resumed or paused.
981 ASSERT_NE(MockResourceLoader::Status::CANCELED, 1047 ASSERT_NE(MockResourceLoader::Status::CANCELED,
982 mock_loader_->OnReadCompleted( 1048 mock_loader_->OnReadCompleted(
983 expected.substr(written, to_be_written))); 1049 expected.substr(written, to_be_written)));
984 1050
985 written += to_be_written; 1051 written += to_be_written;
986 } 1052 }
(...skipping 17 matching lines...) Expand all
1004 base::RunLoop().RunUntilIdle(); 1070 base::RunLoop().RunUntilIdle();
1005 } 1071 }
1006 EXPECT_EQ(expected, actual); 1072 EXPECT_EQ(expected, actual);
1007 } 1073 }
1008 1074
1009 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1075 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1010 BeginWriteFailsOnReadCompleted) { 1076 BeginWriteFailsOnReadCompleted) {
1011 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 1077 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
1012 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 1078 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
1013 1079
1080 // Whether the next OnReadCompleted call or OnWillRead returns the error
1081 // depends on whether or not an intermediary buffer is being used by the
1082 // MojoAsyncResourceHandler.
1014 handler_->set_begin_write_expectation(MOJO_RESULT_UNKNOWN); 1083 handler_->set_begin_write_expectation(MOJO_RESULT_UNKNOWN);
1015 ASSERT_EQ(MockResourceLoader::Status::CANCELED, 1084 MockResourceLoader::Status result = mock_loader_->OnReadCompleted(
1016 mock_loader_->OnReadCompleted( 1085 std::string(mock_loader_->io_buffer_size(), 'A'));
1017 std::string(mock_loader_->io_buffer_size(), 'A'))); 1086 if (result == MockResourceLoader::Status::CANCELED)
1087 return;
1088 ASSERT_EQ(MockResourceLoader::Status::IDLE, result);
1089 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->OnWillRead());
1018 } 1090 }
1019 1091
1020 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1092 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1021 BeginWriteReturnsShouldWaitOnReadCompleted) { 1093 BeginWriteReturnsShouldWaitOnReadCompleted) {
1022 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 1094 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
1023 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 1095 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
1024 1096
1097 // Whether the next OnReadCompleted call or OnWillRead call completes
1098 // asynchronously depends on whether or not an intermediary buffer is being
1099 // used by the MojoAsyncResourceHandler.
1025 handler_->set_begin_write_expectation(MOJO_RESULT_SHOULD_WAIT); 1100 handler_->set_begin_write_expectation(MOJO_RESULT_SHOULD_WAIT);
1101 MockResourceLoader::Status result = mock_loader_->OnReadCompleted(
1102 std::string(mock_loader_->io_buffer_size() - 1, 'A'));
1103 if (result == MockResourceLoader::Status::CALLBACK_PENDING)
1104 return;
1105
1026 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, 1106 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING,
1027 mock_loader_->OnReadCompleted( 1107 mock_loader_->OnWillRead());
1028 std::string(mock_loader_->io_buffer_size(), 'A')));
1029 } 1108 }
1030 1109
1031 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1110 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1032 BeginWriteFailsOnResume) { 1111 BeginWriteFailsOnResume) {
1033 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 1112 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
1034 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
1035 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnReadCompleted(0));
1036 1113
1037 while (true) { 1114 while (true) {
1038 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 1115 // Whether the next OnReadCompleted call or OnWillRead call completes
1039 MockResourceLoader::Status result = mock_loader_->OnReadCompleted( 1116 // asynchronously depends on whether or not an intermediary buffer is being
1117 // used by the MojoAsyncResourceHandler.
1118 MockResourceLoader::Status result = mock_loader_->OnWillRead();
1119 if (result == MockResourceLoader::Status::CALLBACK_PENDING)
1120 break;
1121 ASSERT_EQ(MockResourceLoader::Status::IDLE, result);
1122 result = mock_loader_->OnReadCompleted(
1040 std::string(mock_loader_->io_buffer_size(), 'A')); 1123 std::string(mock_loader_->io_buffer_size(), 'A'));
1041 if (result == MockResourceLoader::Status::CALLBACK_PENDING) 1124 if (result == MockResourceLoader::Status::CALLBACK_PENDING)
1042 break; 1125 break;
1043 ASSERT_EQ(MockResourceLoader::Status::IDLE, result); 1126 ASSERT_EQ(MockResourceLoader::Status::IDLE, result);
1044 } 1127 }
1045 url_loader_client_.RunUntilResponseBodyArrived(); 1128 url_loader_client_.RunUntilResponseBodyArrived();
1046 ASSERT_TRUE(url_loader_client_.response_body().is_valid()); 1129 ASSERT_TRUE(url_loader_client_.response_body().is_valid());
1047 1130
1048 handler_->set_begin_write_expectation(MOJO_RESULT_UNKNOWN); 1131 handler_->set_begin_write_expectation(MOJO_RESULT_UNKNOWN);
1049 1132
1050 while (mock_loader_->status() != MockResourceLoader::Status::CANCELED) { 1133 while (mock_loader_->status() != MockResourceLoader::Status::CANCELED) {
1051 char buf[256]; 1134 char buf[256];
1052 uint32_t read_size = sizeof(buf); 1135 uint32_t read_size = sizeof(buf);
1053 MojoResult result = 1136 MojoResult result =
1054 mojo::ReadDataRaw(url_loader_client_.response_body(), buf, &read_size, 1137 mojo::ReadDataRaw(url_loader_client_.response_body(), buf, &read_size,
1055 MOJO_READ_DATA_FLAG_NONE); 1138 MOJO_READ_DATA_FLAG_NONE);
1056 ASSERT_TRUE(result == MOJO_RESULT_OK || result == MOJO_RESULT_SHOULD_WAIT); 1139 ASSERT_TRUE(result == MOJO_RESULT_OK || result == MOJO_RESULT_SHOULD_WAIT);
1057 base::RunLoop().RunUntilIdle(); 1140 base::RunLoop().RunUntilIdle();
1058 } 1141 }
1059 1142
1143 if (mock_loader_->status() == MockResourceLoader::Status::IDLE)
1144 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->OnWillRead());
1145
1060 EXPECT_FALSE(url_loader_client_.has_received_completion()); 1146 EXPECT_FALSE(url_loader_client_.has_received_completion());
1061 EXPECT_EQ(net::ERR_FAILED, mock_loader_->error_code()); 1147 EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES, mock_loader_->error_code());
1062 } 1148 }
1063 1149
1064 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, CancelWhileWaiting) { 1150 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, CancelWhileWaiting) {
1065 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 1151 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
1066 1152
1067 while (true) { 1153 while (true) {
1068 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 1154 // Whether the next OnReadCompleted call or OnWillRead call completes
1069 MockResourceLoader::Status result = mock_loader_->OnReadCompleted( 1155 // asynchronously depends on whether or not an intermediary buffer is being
1156 // used by the MojoAsyncResourceHandler.
1157 MockResourceLoader::Status result = mock_loader_->OnWillRead();
1158 if (result == MockResourceLoader::Status::CALLBACK_PENDING)
1159 break;
1160 ASSERT_EQ(MockResourceLoader::Status::IDLE, result);
1161 result = mock_loader_->OnReadCompleted(
1070 std::string(mock_loader_->io_buffer_size(), 'A')); 1162 std::string(mock_loader_->io_buffer_size(), 'A'));
1071 if (result == MockResourceLoader::Status::CALLBACK_PENDING) 1163 if (result == MockResourceLoader::Status::CALLBACK_PENDING)
1072 break; 1164 break;
1073 ASSERT_EQ(MockResourceLoader::Status::IDLE, result); 1165 ASSERT_EQ(MockResourceLoader::Status::IDLE, result);
1074 } 1166 }
1075 1167
1076 url_loader_client_.RunUntilResponseBodyArrived(); 1168 url_loader_client_.RunUntilResponseBodyArrived();
1077 ASSERT_TRUE(url_loader_client_.response_body().is_valid()); 1169 ASSERT_TRUE(url_loader_client_.response_body().is_valid());
1078 1170
1079 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, 1171 net::URLRequestStatus status(net::URLRequestStatus::CANCELED,
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1274 } 1366 }
1275 } 1367 }
1276 EXPECT_EQ("B", body); 1368 EXPECT_EQ("B", body);
1277 } 1369 }
1278 1370
1279 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1371 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1280 MojoAsyncResourceHandlerWithAllocationSizeTest, 1372 MojoAsyncResourceHandlerWithAllocationSizeTest,
1281 ::testing::Values(8, 32 * 2014)); 1373 ::testing::Values(8, 32 * 2014));
1282 } // namespace 1374 } // namespace
1283 } // namespace content 1375 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698