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 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 base::Bind(&DownloadFileTest::SetRenameResult, | 388 base::Bind(&DownloadFileTest::SetRenameResult, |
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. If |
399 void PrepareMultipleStreams(int64_t second_stream_length) { | 399 // |first_stream_completes_early| is true, the first stream will complete |
| 400 // before the second stream starts. |
| 401 void PrepareMultipleStreams(bool first_stream_completes_early, |
| 402 int64_t second_stream_length) { |
400 // Create a sparse file. | 403 // Create a sparse file. |
401 ASSERT_TRUE(CreateDownloadFile(0, true, true)); | 404 ASSERT_TRUE(CreateDownloadFile(0, true, true)); |
402 base::FilePath initial_path(download_file_->FullPath()); | 405 base::FilePath initial_path(download_file_->FullPath()); |
403 EXPECT_TRUE(base::PathExists(initial_path)); | 406 EXPECT_TRUE(base::PathExists(initial_path)); |
404 DCHECK(download_file_); | 407 DCHECK(download_file_); |
405 | 408 |
406 const char* stream_0_data[] = {kTestData1, kTestData2}; | 409 const char* stream_0_data[] = {kTestData1, kTestData2}; |
407 const char* stream_1_data[] = {kTestData4, kTestData5}; | 410 const char* stream_1_data[] = {kTestData4, kTestData5}; |
408 size_t stream_1_offset = strlen(kTestData1) + strlen(kTestData2); | 411 size_t stream_1_offset = strlen(kTestData1) + strlen(kTestData2); |
409 | 412 |
410 // Register second SourceStream entry for the second stream. | 413 // Register second SourceStream entry for the second stream. |
411 // The first stream should be registered in ctor of DownloadFile. | 414 // The first stream should be registered in ctor of DownloadFile. |
412 DownloadFileImpl::SourceStreams& source_streams = | 415 DownloadFileImpl::SourceStreams& source_streams = |
413 download_file_->source_streams_; | 416 download_file_->source_streams_; |
414 EXPECT_EQ(static_cast<size_t>(1), source_streams.size()); | 417 EXPECT_EQ(static_cast<size_t>(1), source_streams.size()); |
415 source_streams[stream_1_offset] = | 418 source_streams[stream_1_offset] = |
416 base::MakeUnique<DownloadFileImpl::SourceStream>(stream_1_offset, | 419 base::MakeUnique<DownloadFileImpl::SourceStream>(stream_1_offset, |
417 second_stream_length); | 420 second_stream_length); |
418 | 421 |
419 // Create the second byte stream. Will be moved to DownloadFile. | 422 // Create the second byte stream. Will be moved to DownloadFile. |
420 input_stream_1_ = new MockByteStreamReader(); | 423 input_stream_1_ = new MockByteStreamReader(); |
421 | 424 |
422 ::testing::Sequence s0; | 425 ::testing::Sequence s0; |
423 ::testing::Sequence s1; | 426 ::testing::Sequence s1; |
424 SetupDataAppend(stream_1_data, 2, input_stream_1_, s1, stream_1_offset); | 427 SetupDataAppend(stream_1_data, 2, input_stream_1_, s1, stream_1_offset); |
425 SetupDataAppend(stream_0_data, 2, input_stream_, s0, 0); | 428 SetupDataAppend(stream_0_data, 2, input_stream_, s0, 0); |
426 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, input_stream_, s0); | 429 // If the first stream doesn't finish before the second stream starts |
| 430 // writing, its length will be cut short by the second stream. So |
| 431 // STREAM_COMPLETE will never get called. |
| 432 if (first_stream_completes_early) |
| 433 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, input_stream_, s0); |
| 434 else |
| 435 EXPECT_CALL(*input_stream_, RegisterCallback(_)).RetiresOnSaturation(); |
427 | 436 |
428 // Expectation on MockByteStreamReader for MultipleStreams tests: | 437 // Expectation on MockByteStreamReader for MultipleStreams tests: |
429 // 1. RegisterCallback: Must called twice. One to set the callback, the | 438 // 1. RegisterCallback: Must called twice. One to set the callback, the |
430 // other to release the stream. | 439 // other to release the stream. |
431 // 2. Read: If filled with N buffer, called (N+1) times, where the last Read | 440 // 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. | 441 // call doesn't read any data but returns STREAM_COMPLETE. |
433 // The stream may terminate in the middle and less Read calls are expected. | 442 // 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 | 443 // 3. GetStatus: Only called if the stream is completed and last Read call |
435 // returns STREAM_COMPLETE. | 444 // returns STREAM_COMPLETE. |
436 if (second_stream_length == 0) | 445 if (second_stream_length == 0) |
437 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, input_stream_1_, s1); | 446 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, input_stream_1_, s1); |
438 else | 447 else |
439 EXPECT_CALL(*input_stream_1_, RegisterCallback(_)).RetiresOnSaturation(); | 448 EXPECT_CALL(*input_stream_1_, RegisterCallback(_)).RetiresOnSaturation(); |
440 | 449 |
441 EXPECT_CALL(*input_stream_1_, RegisterCallback(_)).RetiresOnSaturation(); | 450 EXPECT_CALL(*input_stream_1_, RegisterCallback(_)).RetiresOnSaturation(); |
442 } | 451 } |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 sink_callback_.Run(); | 905 sink_callback_.Run(); |
897 base::RunLoop().RunUntilIdle(); | 906 base::RunLoop().RunUntilIdle(); |
898 VerifyStreamAndSize(); | 907 VerifyStreamAndSize(); |
899 DestroyDownloadFile(0); | 908 DestroyDownloadFile(0); |
900 } | 909 } |
901 | 910 |
902 // Tests for concurrent streams handling, used for parallel download. | 911 // Tests for concurrent streams handling, used for parallel download. |
903 // | 912 // |
904 // Activate both streams at the same time. | 913 // Activate both streams at the same time. |
905 TEST_F(DownloadFileTest, MutipleStreamsWrite) { | 914 TEST_F(DownloadFileTest, MutipleStreamsWrite) { |
906 PrepareMultipleStreams(0); | 915 PrepareMultipleStreams(false, 0); |
907 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); | 916 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); |
908 | 917 |
909 int64_t stream_0_length = | 918 int64_t stream_0_length = |
910 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); | 919 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); |
911 int64_t stream_1_length = | 920 int64_t stream_1_length = |
912 static_cast<int64_t>(strlen(kTestData4) + strlen(kTestData5)); | 921 static_cast<int64_t>(strlen(kTestData4) + strlen(kTestData5)); |
913 | 922 |
914 download_file_->AddByteStream( | 923 download_file_->AddByteStream( |
915 std::unique_ptr<MockByteStreamReader>(input_stream_1_), stream_0_length); | 924 std::unique_ptr<MockByteStreamReader>(input_stream_1_), stream_0_length); |
916 sink_callback_.Run(); | 925 sink_callback_.Run(); |
917 base::RunLoop().RunUntilIdle(); | 926 base::RunLoop().RunUntilIdle(); |
918 | 927 |
919 SourceStreamTestData stream_data_0(0, stream_0_length, true); | 928 SourceStreamTestData stream_data_0(0, stream_0_length, true); |
920 SourceStreamTestData stream_data_1(stream_0_length, stream_1_length, true); | 929 SourceStreamTestData stream_data_1(stream_0_length, stream_1_length, true); |
921 VerifySourceStreamsStates(stream_data_0); | 930 VerifySourceStreamsStates(stream_data_0); |
922 VerifySourceStreamsStates(stream_data_1); | 931 VerifySourceStreamsStates(stream_data_1); |
923 EXPECT_EQ(stream_0_length + stream_1_length, TotalBytesReceived()); | 932 EXPECT_EQ(stream_0_length + stream_1_length, TotalBytesReceived()); |
924 | 933 |
925 DestroyDownloadFile(0); | 934 DestroyDownloadFile(0); |
926 } | 935 } |
927 | 936 |
928 // Activate and deplete one stream, later add the second stream. | 937 // Activate and deplete one stream, later add the second stream. |
929 TEST_F(DownloadFileTest, MutipleStreamsOneStreamFirst) { | 938 TEST_F(DownloadFileTest, MutipleStreamsOneStreamFirst) { |
930 PrepareMultipleStreams(0); | 939 PrepareMultipleStreams(true, 0); |
931 | 940 |
932 int64_t stream_0_length = | 941 int64_t stream_0_length = |
933 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); | 942 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); |
934 int64_t stream_1_length = | 943 int64_t stream_1_length = |
935 static_cast<int64_t>(strlen(kTestData4) + strlen(kTestData5)); | 944 static_cast<int64_t>(strlen(kTestData4) + strlen(kTestData5)); |
936 | 945 |
937 // Deplete the first stream. | 946 // Deplete the first stream. |
938 sink_callback_.Run(); | 947 sink_callback_.Run(); |
939 base::RunLoop().RunUntilIdle(); | 948 base::RunLoop().RunUntilIdle(); |
940 | 949 |
(...skipping 22 matching lines...) Expand all Loading... |
963 | 972 |
964 // Two streams write to one sink, the second stream has a limited length. | 973 // Two streams write to one sink, the second stream has a limited length. |
965 TEST_F(DownloadFileTest, MutipleStreamsLimitedLength) { | 974 TEST_F(DownloadFileTest, MutipleStreamsLimitedLength) { |
966 // The second stream has two buffers, kTestData4 and kTestData5. | 975 // The second stream has two buffers, kTestData4 and kTestData5. |
967 // The length limit is set to less than the length of kTestData4. | 976 // The length limit is set to less than the length of kTestData4. |
968 // kTestData4 should be partially written to disk, where kTestData5 should be | 977 // kTestData4 should be partially written to disk, where kTestData5 should be |
969 // ignored. | 978 // ignored. |
970 int64_t stream_0_length = | 979 int64_t stream_0_length = |
971 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); | 980 static_cast<int64_t>(strlen(kTestData1) + strlen(kTestData2)); |
972 int64_t stream_1_length = static_cast<int64_t>(strlen(kTestData4)) - 1; | 981 int64_t stream_1_length = static_cast<int64_t>(strlen(kTestData4)) - 1; |
973 PrepareMultipleStreams(stream_1_length); | 982 PrepareMultipleStreams(false, stream_1_length); |
974 | 983 |
975 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); | 984 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); |
976 | 985 |
977 download_file_->AddByteStream( | 986 download_file_->AddByteStream( |
978 std::unique_ptr<MockByteStreamReader>(input_stream_1_), stream_0_length); | 987 std::unique_ptr<MockByteStreamReader>(input_stream_1_), stream_0_length); |
979 sink_callback_.Run(); | 988 sink_callback_.Run(); |
980 base::RunLoop().RunUntilIdle(); | 989 base::RunLoop().RunUntilIdle(); |
981 | 990 |
982 SourceStreamTestData stream_data_0(0, stream_0_length, true); | 991 SourceStreamTestData stream_data_0(0, stream_0_length, true); |
983 SourceStreamTestData stream_data_1(stream_0_length, stream_1_length, true); | 992 SourceStreamTestData stream_data_1(stream_0_length, stream_1_length, true); |
(...skipping 10 matching lines...) Expand all Loading... |
994 // Finish the second stream. | 1003 // Finish the second stream. |
995 // TODO(xingliu): Refactor test code to deal with unfinished streams. | 1004 // TODO(xingliu): Refactor test code to deal with unfinished streams. |
996 scoped_refptr<net::IOBuffer> data = new net::IOBuffer(strlen(kTestData5)); | 1005 scoped_refptr<net::IOBuffer> data = new net::IOBuffer(strlen(kTestData5)); |
997 size_t size; | 1006 size_t size; |
998 input_stream_1_->Read(&data, &size); | 1007 input_stream_1_->Read(&data, &size); |
999 | 1008 |
1000 DestroyDownloadFile(0, false); | 1009 DestroyDownloadFile(0, false); |
1001 } | 1010 } |
1002 | 1011 |
1003 } // namespace content | 1012 } // namespace content |
OLD | NEW |