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 "net/base/file_stream.h" | 5 #include "net/base/file_stream.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 waiting_for_result_ = true; | 539 waiting_for_result_ = true; |
540 base::RunLoop().Run(); | 540 base::RunLoop().Run(); |
541 waiting_for_result_ = false; | 541 waiting_for_result_ = false; |
542 } | 542 } |
543 have_result_ = false; // auto-reset for next callback | 543 have_result_ = false; // auto-reset for next callback |
544 return result_; | 544 return result_; |
545 } | 545 } |
546 | 546 |
547 const CompletionCallback& callback() const { return callback_; } | 547 const CompletionCallback& callback() const { return callback_; } |
548 | 548 |
| 549 void ValidateWrittenData() { |
| 550 TestCompletionCallback callback; |
| 551 int rv = 0; |
| 552 for (;;) { |
| 553 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 554 rv = stream_->Read(buf.get(), buf->size(), callback.callback()); |
| 555 if (rv == ERR_IO_PENDING) { |
| 556 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 557 base::MessageLoop::current()); |
| 558 rv = callback.WaitForResult(); |
| 559 } |
| 560 EXPECT_LE(0, rv); |
| 561 if (rv <= 0) |
| 562 break; |
| 563 *total_bytes_read_ += rv; |
| 564 data_read_->append(buf->data(), rv); |
| 565 } |
| 566 } |
| 567 |
549 private: | 568 private: |
550 void OnComplete(int result) { | 569 void OnComplete(int result) { |
551 DCHECK_LT(0, result); | 570 DCHECK_LT(0, result); |
552 *total_bytes_written_ += result; | 571 *total_bytes_written_ += result; |
553 | 572 |
554 int rv; | 573 int rv; |
555 | 574 |
556 if (*total_bytes_written_ != kTestDataSize) { | 575 if (*total_bytes_written_ != kTestDataSize) { |
557 // Recurse to finish writing all data. | 576 // Recurse to finish writing all data. |
558 int total_bytes_written = 0, total_bytes_read = 0; | 577 int total_bytes_written = 0, total_bytes_read = 0; |
(...skipping 11 matching lines...) Expand all Loading... |
570 } else { // We're done writing all data. Start reading the data. | 589 } else { // We're done writing all data. Start reading the data. |
571 TestInt64CompletionCallback callback64; | 590 TestInt64CompletionCallback callback64; |
572 EXPECT_EQ(ERR_IO_PENDING, | 591 EXPECT_EQ(ERR_IO_PENDING, |
573 stream_->Seek(base::File::FROM_BEGIN, 0, | 592 stream_->Seek(base::File::FROM_BEGIN, 0, |
574 callback64.callback())); | 593 callback64.callback())); |
575 { | 594 { |
576 base::MessageLoop::ScopedNestableTaskAllower allow( | 595 base::MessageLoop::ScopedNestableTaskAllower allow( |
577 base::MessageLoop::current()); | 596 base::MessageLoop::current()); |
578 EXPECT_LE(0, callback64.WaitForResult()); | 597 EXPECT_LE(0, callback64.WaitForResult()); |
579 } | 598 } |
580 | |
581 TestCompletionCallback callback; | |
582 for (;;) { | |
583 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | |
584 rv = stream_->Read(buf.get(), buf->size(), callback.callback()); | |
585 if (rv == ERR_IO_PENDING) { | |
586 base::MessageLoop::ScopedNestableTaskAllower allow( | |
587 base::MessageLoop::current()); | |
588 rv = callback.WaitForResult(); | |
589 } | |
590 EXPECT_LE(0, rv); | |
591 if (rv <= 0) | |
592 break; | |
593 *total_bytes_read_ += rv; | |
594 data_read_->append(buf->data(), rv); | |
595 } | |
596 } | 599 } |
597 | 600 |
598 result_ = *total_bytes_written_; | 601 result_ = *total_bytes_written_; |
599 have_result_ = true; | 602 have_result_ = true; |
600 if (waiting_for_result_) | 603 if (waiting_for_result_) |
601 base::MessageLoop::current()->Quit(); | 604 base::MessageLoop::current()->Quit(); |
602 } | 605 } |
603 | 606 |
604 int result_; | 607 int result_; |
605 bool have_result_; | 608 bool have_result_; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 TestWriteReadCompletionCallback callback(stream.get(), &total_bytes_written, | 642 TestWriteReadCompletionCallback callback(stream.get(), &total_bytes_written, |
640 &total_bytes_read, &data_read); | 643 &total_bytes_read, &data_read); |
641 | 644 |
642 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 645 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
643 rv = stream->Write(buf.get(), buf->size(), callback.callback()); | 646 rv = stream->Write(buf.get(), buf->size(), callback.callback()); |
644 if (rv == ERR_IO_PENDING) | 647 if (rv == ERR_IO_PENDING) |
645 rv = callback.WaitForResult(); | 648 rv = callback.WaitForResult(); |
646 EXPECT_LT(0, rv); | 649 EXPECT_LT(0, rv); |
647 EXPECT_EQ(kTestDataSize, total_bytes_written); | 650 EXPECT_EQ(kTestDataSize, total_bytes_written); |
648 | 651 |
| 652 callback.ValidateWrittenData(); |
| 653 |
649 stream.reset(); | 654 stream.reset(); |
650 | 655 |
651 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 656 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
652 EXPECT_EQ(kTestDataSize * 2, file_size); | 657 EXPECT_EQ(kTestDataSize * 2, file_size); |
653 | 658 |
654 EXPECT_EQ(kTestDataSize * 2, total_bytes_read); | 659 EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
655 const std::string kExpectedFileData = | 660 const std::string kExpectedFileData = |
656 std::string(kTestData) + std::string(kTestData); | 661 std::string(kTestData) + std::string(kTestData); |
657 EXPECT_EQ(kExpectedFileData, data_read); | 662 EXPECT_EQ(kExpectedFileData, data_read); |
658 } | 663 } |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 total_bytes_read += rv; | 879 total_bytes_read += rv; |
875 data_read.append(buf->data(), rv); | 880 data_read.append(buf->data(), rv); |
876 } | 881 } |
877 EXPECT_EQ(file_size, total_bytes_read); | 882 EXPECT_EQ(file_size, total_bytes_read); |
878 } | 883 } |
879 #endif | 884 #endif |
880 | 885 |
881 } // namespace | 886 } // namespace |
882 | 887 |
883 } // namespace net | 888 } // namespace net |
OLD | NEW |