OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 base::Unretained(this), | 389 base::Unretained(this), |
390 loop_runner.QuitClosure(), | 390 loop_runner.QuitClosure(), |
391 &result_reason, | 391 &result_reason, |
392 result_path_p); | 392 result_path_p); |
393 InvokeRenameMethod(method, full_path, completion_callback); | 393 InvokeRenameMethod(method, full_path, completion_callback); |
394 loop_runner.Run(); | 394 loop_runner.Run(); |
395 return result_reason; | 395 return result_reason; |
396 } | 396 } |
397 | 397 |
398 // Prepare two byte streams to write to the same file sink. | 398 // Prepare two byte streams to write to the same file sink. |
399 void PrepareMultipleStreams(int64_t second_stream_length) { | 399 void PrepareMultipleStreams(bool first_stream_write_all_data, |
| 400 int64_t second_stream_length) { |
400 // Create a sparse file. | 401 // Create a sparse file. |
401 ASSERT_TRUE(CreateDownloadFile(0, true, true)); | 402 ASSERT_TRUE(CreateDownloadFile(0, true, true)); |
402 base::FilePath initial_path(download_file_->FullPath()); | 403 base::FilePath initial_path(download_file_->FullPath()); |
403 EXPECT_TRUE(base::PathExists(initial_path)); | 404 EXPECT_TRUE(base::PathExists(initial_path)); |
404 DCHECK(download_file_); | 405 DCHECK(download_file_); |
405 | 406 |
406 const char* stream_0_data[] = {kTestData1, kTestData2}; | 407 const char* stream_0_data[] = {kTestData1, kTestData2}; |
407 const char* stream_1_data[] = {kTestData4, kTestData5}; | 408 const char* stream_1_data[] = {kTestData4, kTestData5}; |
| 409 const char* all_data[] = {kTestData1, kTestData2, kTestData4, kTestData5}; |
408 size_t stream_1_offset = strlen(kTestData1) + strlen(kTestData2); | 410 size_t stream_1_offset = strlen(kTestData1) + strlen(kTestData2); |
409 | 411 |
410 // Register second SourceStream entry for the second stream. | 412 // Register second SourceStream entry for the second stream. |
411 // The first stream should be registered in ctor of DownloadFile. | 413 // The first stream should be registered in ctor of DownloadFile. |
412 DownloadFileImpl::SourceStreams& source_streams = | 414 DownloadFileImpl::SourceStreams& source_streams = |
413 download_file_->source_streams_; | 415 download_file_->source_streams_; |
414 EXPECT_EQ(static_cast<size_t>(1), source_streams.size()); | 416 EXPECT_EQ(static_cast<size_t>(1), source_streams.size()); |
415 source_streams[stream_1_offset] = | 417 source_streams[stream_1_offset] = |
416 base::MakeUnique<DownloadFileImpl::SourceStream>(stream_1_offset, | 418 base::MakeUnique<DownloadFileImpl::SourceStream>(stream_1_offset, |
417 second_stream_length); | 419 second_stream_length); |
418 | 420 |
419 // Create the second byte stream. Will be moved to DownloadFile. | 421 // Create the second byte stream. Will be moved to DownloadFile. |
420 input_stream_1_ = new MockByteStreamReader(); | 422 input_stream_1_ = new MockByteStreamReader(); |
421 | 423 |
422 ::testing::Sequence s0; | 424 ::testing::Sequence s0; |
423 ::testing::Sequence s1; | 425 ::testing::Sequence s1; |
424 SetupDataAppend(stream_1_data, 2, input_stream_1_, s1, stream_1_offset); | 426 if (first_stream_write_all_data) { |
425 SetupDataAppend(stream_0_data, 2, input_stream_, s0, 0); | 427 SetupDataAppend(all_data, 4, input_stream_, s0, 0); |
| 428 // The 2nd stream will abort after the first read |
| 429 SetupDataAppend(stream_1_data, 1, input_stream_1_, s1, stream_1_offset); |
| 430 } else { |
| 431 SetupDataAppend(stream_0_data, 2, input_stream_, s0, 0); |
| 432 SetupDataAppend(stream_1_data, 2, input_stream_1_, s1, stream_1_offset); |
| 433 } |
| 434 |
| 435 |
426 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, input_stream_, s0); | 436 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, input_stream_, s0); |
427 | 437 |
428 // Expectation on MockByteStreamReader for MultipleStreams tests: | 438 // Expectation on MockByteStreamReader for MultipleStreams tests: |
429 // 1. RegisterCallback: Must called twice. One to set the callback, the | 439 // 1. RegisterCallback: Must called twice. One to set the callback, the |
430 // other to release the stream. | 440 // other to release the stream. |
431 // 2. Read: If filled with N buffer, called (N+1) times, where the last Read | 441 // 2. Read: If filled with N buffer, called (N+1) times, where the last Read |
432 // call doesn't read any data but returns STRAM_COMPLETE. | 442 // call doesn't read any data but returns STRAM_COMPLETE. |
433 // The stream may terminate in the middle and less Read calls are expected. | 443 // The stream may terminate in the middle and less Read calls are expected. |
434 // 3. GetStatus: Only called if the stream is completed and last Read call | 444 // 3. GetStatus: Only called if the stream is completed and last Read call |
435 // returns STREAM_COMPLETE. | 445 // returns STREAM_COMPLETE. |
436 if (second_stream_length == 0) | 446 if (second_stream_length == 0 && !first_stream_write_all_data) |
437 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, input_stream_1_, s1); | 447 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, input_stream_1_, s1); |
438 else | 448 else |
439 EXPECT_CALL(*input_stream_1_, RegisterCallback(_)).RetiresOnSaturation(); | 449 EXPECT_CALL(*input_stream_1_, RegisterCallback(_)).RetiresOnSaturation(); |
440 | 450 |
441 EXPECT_CALL(*input_stream_1_, RegisterCallback(_)).RetiresOnSaturation(); | 451 EXPECT_CALL(*input_stream_1_, RegisterCallback(_)).RetiresOnSaturation(); |
442 } | 452 } |
443 | 453 |
444 void VerifySourceStreamsStates(const SourceStreamTestData& data) { | 454 void VerifySourceStreamsStates(const SourceStreamTestData& data) { |
445 DCHECK(download_file_->source_streams_.find(data.offset) != | 455 DCHECK(download_file_->source_streams_.find(data.offset) != |
446 download_file_->source_streams_.end()); | 456 download_file_->source_streams_.end()); |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 sink_callback_.Run(); | 906 sink_callback_.Run(); |
897 base::RunLoop().RunUntilIdle(); | 907 base::RunLoop().RunUntilIdle(); |
898 VerifyStreamAndSize(); | 908 VerifyStreamAndSize(); |
899 DestroyDownloadFile(0); | 909 DestroyDownloadFile(0); |
900 } | 910 } |
901 | 911 |
902 // Tests for concurrent streams handling, used for parallel download. | 912 // Tests for concurrent streams handling, used for parallel download. |
903 // | 913 // |
904 // Activate both streams at the same time. | 914 // Activate both streams at the same time. |
905 TEST_F(DownloadFileTest, MutipleStreamsWrite) { | 915 TEST_F(DownloadFileTest, MutipleStreamsWrite) { |
906 PrepareMultipleStreams(0); | 916 PrepareMultipleStreams(false, 0); |
907 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); | 917 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); |
908 | 918 |
909 int64_t stream_0_length = | 919 int64_t stream_0_length = |
910 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); | 920 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); |
911 int64_t stream_1_length = | 921 int64_t stream_1_length = |
912 static_cast<int64_t>(strlen(kTestData4) + strlen(kTestData5)); | 922 static_cast<int64_t>(strlen(kTestData4) + strlen(kTestData5)); |
913 | 923 |
914 download_file_->AddByteStream( | 924 download_file_->AddByteStream( |
915 std::unique_ptr<MockByteStreamReader>(input_stream_1_), stream_0_length); | 925 std::unique_ptr<MockByteStreamReader>(input_stream_1_), stream_0_length); |
916 sink_callback_.Run(); | 926 sink_callback_.Run(); |
917 base::RunLoop().RunUntilIdle(); | 927 base::RunLoop().RunUntilIdle(); |
918 | 928 |
919 SourceStreamTestData stream_data_0(0, stream_0_length, true); | 929 SourceStreamTestData stream_data_0(0, stream_0_length, true); |
920 SourceStreamTestData stream_data_1(stream_0_length, stream_1_length, true); | 930 SourceStreamTestData stream_data_1(stream_0_length, stream_1_length, true); |
921 VerifySourceStreamsStates(stream_data_0); | 931 VerifySourceStreamsStates(stream_data_0); |
922 VerifySourceStreamsStates(stream_data_1); | 932 VerifySourceStreamsStates(stream_data_1); |
923 EXPECT_EQ(stream_0_length + stream_1_length, TotalBytesReceived()); | 933 EXPECT_EQ(stream_0_length + stream_1_length, TotalBytesReceived()); |
924 | 934 |
925 DestroyDownloadFile(0); | 935 DestroyDownloadFile(0); |
926 } | 936 } |
927 | 937 |
928 // Activate and deplete one stream, later add the second stream. | 938 // Activate and deplete one stream, later add the second stream. |
929 TEST_F(DownloadFileTest, MutipleStreamsOneStreamFirst) { | 939 TEST_F(DownloadFileTest, MutipleStreamsOneStreamFirst) { |
930 PrepareMultipleStreams(0); | 940 PrepareMultipleStreams(false, 0); |
931 | 941 |
932 int64_t stream_0_length = | 942 int64_t stream_0_length = |
933 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); | 943 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); |
934 int64_t stream_1_length = | 944 int64_t stream_1_length = |
935 static_cast<int64_t>(strlen(kTestData4) + strlen(kTestData5)); | 945 static_cast<int64_t>(strlen(kTestData4) + strlen(kTestData5)); |
936 | 946 |
937 // Deplete the first stream. | 947 // Deplete the first stream. |
938 sink_callback_.Run(); | 948 sink_callback_.Run(); |
939 base::RunLoop().RunUntilIdle(); | 949 base::RunLoop().RunUntilIdle(); |
940 | 950 |
(...skipping 22 matching lines...) Expand all Loading... |
963 | 973 |
964 // Two streams write to one sink, the second stream has a limited length. | 974 // Two streams write to one sink, the second stream has a limited length. |
965 TEST_F(DownloadFileTest, MutipleStreamsLimitedLength) { | 975 TEST_F(DownloadFileTest, MutipleStreamsLimitedLength) { |
966 // The second stream has two buffers, kTestData4 and kTestData5. | 976 // The second stream has two buffers, kTestData4 and kTestData5. |
967 // The length limit is set to less than the length of kTestData4. | 977 // The length limit is set to less than the length of kTestData4. |
968 // kTestData4 should be partially written to disk, where kTestData5 should be | 978 // kTestData4 should be partially written to disk, where kTestData5 should be |
969 // ignored. | 979 // ignored. |
970 int64_t stream_0_length = | 980 int64_t stream_0_length = |
971 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); | 981 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); |
972 int64_t stream_1_length = static_cast<int64_t>(strlen(kTestData4)) - 1; | 982 int64_t stream_1_length = static_cast<int64_t>(strlen(kTestData4)) - 1; |
973 PrepareMultipleStreams(stream_1_length); | 983 PrepareMultipleStreams(false, stream_1_length); |
974 | 984 |
975 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); | 985 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); |
976 | 986 |
977 download_file_->AddByteStream( | 987 download_file_->AddByteStream( |
978 std::unique_ptr<MockByteStreamReader>(input_stream_1_), stream_0_length); | 988 std::unique_ptr<MockByteStreamReader>(input_stream_1_), stream_0_length); |
979 sink_callback_.Run(); | 989 sink_callback_.Run(); |
980 base::RunLoop().RunUntilIdle(); | 990 base::RunLoop().RunUntilIdle(); |
981 | 991 |
982 SourceStreamTestData stream_data_0(0, stream_0_length, true); | 992 SourceStreamTestData stream_data_0(0, stream_0_length, true); |
983 SourceStreamTestData stream_data_1(stream_0_length, stream_1_length, true); | 993 SourceStreamTestData stream_data_1(stream_0_length, stream_1_length, true); |
984 VerifySourceStreamsStates(stream_data_0); | 994 VerifySourceStreamsStates(stream_data_0); |
985 VerifySourceStreamsStates(stream_data_1); | 995 VerifySourceStreamsStates(stream_data_1); |
986 EXPECT_EQ(stream_0_length + stream_1_length, TotalBytesReceived()); | 996 EXPECT_EQ(stream_0_length + stream_1_length, TotalBytesReceived()); |
987 | 997 |
988 std::string disk_data, expected_data; | 998 std::string disk_data, expected_data; |
989 EXPECT_TRUE(base::ReadFileToString(download_file_->FullPath(), &disk_data)); | 999 EXPECT_TRUE(base::ReadFileToString(download_file_->FullPath(), &disk_data)); |
990 expected_data.append(kTestData1).append(kTestData2).append(kTestData4); | 1000 expected_data.append(kTestData1).append(kTestData2).append(kTestData4); |
991 expected_data = expected_data.substr(0, stream_0_length + stream_1_length); | 1001 expected_data = expected_data.substr(0, stream_0_length + stream_1_length); |
992 EXPECT_EQ(expected_data, disk_data); | 1002 EXPECT_EQ(expected_data, disk_data); |
993 | 1003 |
994 // Finish the second stream. | 1004 // Finish the second stream. |
995 // TODO(xingliu): Refactor test code to deal with unfinished streams. | 1005 // TODO(xingliu): Refactor test code to deal with unfinished streams. |
996 scoped_refptr<net::IOBuffer> data = new net::IOBuffer(strlen(kTestData5)); | 1006 scoped_refptr<net::IOBuffer> data = new net::IOBuffer(strlen(kTestData5)); |
997 size_t size; | 1007 size_t size; |
998 input_stream_1_->Read(&data, &size); | 1008 input_stream_1_->Read(&data, &size); |
999 | 1009 |
1000 DestroyDownloadFile(0, false); | 1010 DestroyDownloadFile(0, false); |
1001 } | 1011 } |
1002 | 1012 |
| 1013 // Two streams write to one sink, the first stream writes the whole file before |
| 1014 // the seconds stream was able to start |
| 1015 TEST_F(DownloadFileTest, MutipleStreamsFirstStreamWriteAllData) { |
| 1016 PrepareMultipleStreams(true, 0); |
| 1017 int64_t stream_0_length = |
| 1018 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2) + |
| 1019 strlen(kTestData4) + strlen(kTestData5)); |
| 1020 int64_t stream_1_length = |
| 1021 static_cast<int64_t>(strlen(kTestData4) + strlen(kTestData5)); |
| 1022 sink_callback_.Run(); |
| 1023 base::RunLoop().RunUntilIdle(); |
| 1024 |
| 1025 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); |
| 1026 |
| 1027 download_file_->AddByteStream( |
| 1028 std::unique_ptr<MockByteStreamReader>(input_stream_1_), |
| 1029 stream_0_length - stream_1_length); |
| 1030 base::RunLoop().RunUntilIdle(); |
| 1031 |
| 1032 SourceStreamTestData stream_data_0(0, stream_0_length, true); |
| 1033 SourceStreamTestData stream_data_1( |
| 1034 stream_0_length - stream_1_length, 0, true); |
| 1035 VerifySourceStreamsStates(stream_data_0); |
| 1036 VerifySourceStreamsStates(stream_data_1); |
| 1037 EXPECT_EQ(stream_0_length, TotalBytesReceived()); |
| 1038 |
| 1039 DestroyDownloadFile(0); |
| 1040 } |
| 1041 |
1003 } // namespace content | 1042 } // namespace content |
OLD | NEW |