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

Side by Side Diff: net/base/file_stream_unittest.cc

Issue 15829004: Update net/ to use scoped_refptr<T>::get() rather than implicit "operator T*" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: license twerk Created 7 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 | Annotate | Revision Log
« no previous file with comments | « net/base/file_stream_context_posix.cc ('k') | net/base/test_completion_callback_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/file_util.h" 9 #include "base/file_util.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 EXPECT_EQ(OK, callback.WaitForResult()); 196 EXPECT_EQ(OK, callback.WaitForResult());
197 197
198 int64 total_bytes_avail = stream.Available(); 198 int64 total_bytes_avail = stream.Available();
199 EXPECT_EQ(file_size, total_bytes_avail); 199 EXPECT_EQ(file_size, total_bytes_avail);
200 200
201 int total_bytes_read = 0; 201 int total_bytes_read = 0;
202 202
203 std::string data_read; 203 std::string data_read;
204 for (;;) { 204 for (;;) {
205 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 205 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
206 rv = stream.Read(buf, buf->size(), callback.callback()); 206 rv = stream.Read(buf.get(), buf->size(), callback.callback());
207 if (rv == ERR_IO_PENDING) 207 if (rv == ERR_IO_PENDING)
208 rv = callback.WaitForResult(); 208 rv = callback.WaitForResult();
209 EXPECT_LE(0, rv); 209 EXPECT_LE(0, rv);
210 if (rv <= 0) 210 if (rv <= 0)
211 break; 211 break;
212 total_bytes_read += rv; 212 total_bytes_read += rv;
213 data_read.append(buf->data(), rv); 213 data_read.append(buf->data(), rv);
214 } 214 }
215 EXPECT_EQ(file_size, total_bytes_read); 215 EXPECT_EQ(file_size, total_bytes_read);
216 EXPECT_EQ(kTestData, data_read); 216 EXPECT_EQ(kTestData, data_read);
(...skipping 10 matching lines...) Expand all
227 base::PLATFORM_FILE_ASYNC; 227 base::PLATFORM_FILE_ASYNC;
228 TestCompletionCallback callback; 228 TestCompletionCallback callback;
229 int rv = stream->Open(temp_file_path(), flags, callback.callback()); 229 int rv = stream->Open(temp_file_path(), flags, callback.callback());
230 EXPECT_EQ(ERR_IO_PENDING, rv); 230 EXPECT_EQ(ERR_IO_PENDING, rv);
231 EXPECT_EQ(OK, callback.WaitForResult()); 231 EXPECT_EQ(OK, callback.WaitForResult());
232 232
233 int64 total_bytes_avail = stream->Available(); 233 int64 total_bytes_avail = stream->Available();
234 EXPECT_EQ(file_size, total_bytes_avail); 234 EXPECT_EQ(file_size, total_bytes_avail);
235 235
236 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 236 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
237 rv = stream->Read(buf, buf->size(), callback.callback()); 237 rv = stream->Read(buf.get(), buf->size(), callback.callback());
238 stream.reset(); // Delete instead of closing it. 238 stream.reset(); // Delete instead of closing it.
239 if (rv < 0) { 239 if (rv < 0) {
240 EXPECT_EQ(ERR_IO_PENDING, rv); 240 EXPECT_EQ(ERR_IO_PENDING, rv);
241 // The callback should not be called if the request is cancelled. 241 // The callback should not be called if the request is cancelled.
242 base::MessageLoop::current()->RunUntilIdle(); 242 base::MessageLoop::current()->RunUntilIdle();
243 EXPECT_FALSE(callback.have_result()); 243 EXPECT_FALSE(callback.have_result());
244 } else { 244 } else {
245 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); 245 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv));
246 } 246 }
247 } 247 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 EXPECT_EQ(kOffset, new_offset); 303 EXPECT_EQ(kOffset, new_offset);
304 304
305 int64 total_bytes_avail = stream.Available(); 305 int64 total_bytes_avail = stream.Available();
306 EXPECT_EQ(file_size - kOffset, total_bytes_avail); 306 EXPECT_EQ(file_size - kOffset, total_bytes_avail);
307 307
308 int total_bytes_read = 0; 308 int total_bytes_read = 0;
309 309
310 std::string data_read; 310 std::string data_read;
311 for (;;) { 311 for (;;) {
312 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 312 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
313 rv = stream.Read(buf, buf->size(), callback.callback()); 313 rv = stream.Read(buf.get(), buf->size(), callback.callback());
314 if (rv == ERR_IO_PENDING) 314 if (rv == ERR_IO_PENDING)
315 rv = callback.WaitForResult(); 315 rv = callback.WaitForResult();
316 EXPECT_LE(0, rv); 316 EXPECT_LE(0, rv);
317 if (rv <= 0) 317 if (rv <= 0)
318 break; 318 break;
319 total_bytes_read += rv; 319 total_bytes_read += rv;
320 data_read.append(buf->data(), rv); 320 data_read.append(buf->data(), rv);
321 } 321 }
322 EXPECT_EQ(file_size - kOffset, total_bytes_read); 322 EXPECT_EQ(file_size - kOffset, total_bytes_read);
323 EXPECT_EQ(kTestData + kOffset, data_read); 323 EXPECT_EQ(kTestData + kOffset, data_read);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 415
416 int64 file_size; 416 int64 file_size;
417 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); 417 bool ok = file_util::GetFileSize(temp_file_path(), &file_size);
418 EXPECT_TRUE(ok); 418 EXPECT_TRUE(ok);
419 EXPECT_EQ(0, file_size); 419 EXPECT_EQ(0, file_size);
420 420
421 int total_bytes_written = 0; 421 int total_bytes_written = 0;
422 422
423 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); 423 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
424 scoped_refptr<DrainableIOBuffer> drainable = 424 scoped_refptr<DrainableIOBuffer> drainable =
425 new DrainableIOBuffer(buf, buf->size()); 425 new DrainableIOBuffer(buf.get(), buf->size());
426 while (total_bytes_written != kTestDataSize) { 426 while (total_bytes_written != kTestDataSize) {
427 rv = stream.Write(drainable, drainable->BytesRemaining(), 427 rv = stream.Write(
428 callback.callback()); 428 drainable.get(), drainable->BytesRemaining(), callback.callback());
429 if (rv == ERR_IO_PENDING) 429 if (rv == ERR_IO_PENDING)
430 rv = callback.WaitForResult(); 430 rv = callback.WaitForResult();
431 EXPECT_LT(0, rv); 431 EXPECT_LT(0, rv);
432 if (rv <= 0) 432 if (rv <= 0)
433 break; 433 break;
434 drainable->DidConsume(rv); 434 drainable->DidConsume(rv);
435 total_bytes_written += rv; 435 total_bytes_written += rv;
436 } 436 }
437 ok = file_util::GetFileSize(temp_file_path(), &file_size); 437 ok = file_util::GetFileSize(temp_file_path(), &file_size);
438 EXPECT_TRUE(ok); 438 EXPECT_TRUE(ok);
439 EXPECT_EQ(file_size, total_bytes_written); 439 EXPECT_EQ(file_size, total_bytes_written);
440 } 440 }
441 441
442 TEST_F(FileStreamTest, AsyncWrite_EarlyDelete) { 442 TEST_F(FileStreamTest, AsyncWrite_EarlyDelete) {
443 scoped_ptr<FileStream> stream(new FileStream(NULL)); 443 scoped_ptr<FileStream> stream(new FileStream(NULL));
444 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | 444 int flags = base::PLATFORM_FILE_CREATE_ALWAYS |
445 base::PLATFORM_FILE_WRITE | 445 base::PLATFORM_FILE_WRITE |
446 base::PLATFORM_FILE_ASYNC; 446 base::PLATFORM_FILE_ASYNC;
447 TestCompletionCallback callback; 447 TestCompletionCallback callback;
448 int rv = stream->Open(temp_file_path(), flags, callback.callback()); 448 int rv = stream->Open(temp_file_path(), flags, callback.callback());
449 EXPECT_EQ(ERR_IO_PENDING, rv); 449 EXPECT_EQ(ERR_IO_PENDING, rv);
450 EXPECT_EQ(OK, callback.WaitForResult()); 450 EXPECT_EQ(OK, callback.WaitForResult());
451 451
452 int64 file_size; 452 int64 file_size;
453 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); 453 bool ok = file_util::GetFileSize(temp_file_path(), &file_size);
454 EXPECT_TRUE(ok); 454 EXPECT_TRUE(ok);
455 EXPECT_EQ(0, file_size); 455 EXPECT_EQ(0, file_size);
456 456
457 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); 457 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
458 rv = stream->Write(buf, buf->size(), callback.callback()); 458 rv = stream->Write(buf.get(), buf->size(), callback.callback());
459 stream.reset(); 459 stream.reset();
460 if (rv < 0) { 460 if (rv < 0) {
461 EXPECT_EQ(ERR_IO_PENDING, rv); 461 EXPECT_EQ(ERR_IO_PENDING, rv);
462 // The callback should not be called if the request is cancelled. 462 // The callback should not be called if the request is cancelled.
463 base::MessageLoop::current()->RunUntilIdle(); 463 base::MessageLoop::current()->RunUntilIdle();
464 EXPECT_FALSE(callback.have_result()); 464 EXPECT_FALSE(callback.have_result());
465 } else { 465 } else {
466 ok = file_util::GetFileSize(temp_file_path(), &file_size); 466 ok = file_util::GetFileSize(temp_file_path(), &file_size);
467 EXPECT_TRUE(ok); 467 EXPECT_TRUE(ok);
468 EXPECT_EQ(file_size, rv); 468 EXPECT_EQ(file_size, rv);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 const int64 kOffset = 0; 512 const int64 kOffset = 0;
513 rv = stream.Seek(FROM_END, kOffset, callback64.callback()); 513 rv = stream.Seek(FROM_END, kOffset, callback64.callback());
514 ASSERT_EQ(ERR_IO_PENDING, rv); 514 ASSERT_EQ(ERR_IO_PENDING, rv);
515 int64 new_offset = callback64.WaitForResult(); 515 int64 new_offset = callback64.WaitForResult();
516 EXPECT_EQ(kTestDataSize, new_offset); 516 EXPECT_EQ(kTestDataSize, new_offset);
517 517
518 int total_bytes_written = 0; 518 int total_bytes_written = 0;
519 519
520 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); 520 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
521 scoped_refptr<DrainableIOBuffer> drainable = 521 scoped_refptr<DrainableIOBuffer> drainable =
522 new DrainableIOBuffer(buf, buf->size()); 522 new DrainableIOBuffer(buf.get(), buf->size());
523 while (total_bytes_written != kTestDataSize) { 523 while (total_bytes_written != kTestDataSize) {
524 rv = stream.Write(drainable, drainable->BytesRemaining(), 524 rv = stream.Write(
525 callback.callback()); 525 drainable.get(), drainable->BytesRemaining(), callback.callback());
526 if (rv == ERR_IO_PENDING) 526 if (rv == ERR_IO_PENDING)
527 rv = callback.WaitForResult(); 527 rv = callback.WaitForResult();
528 EXPECT_LT(0, rv); 528 EXPECT_LT(0, rv);
529 if (rv <= 0) 529 if (rv <= 0)
530 break; 530 break;
531 drainable->DidConsume(rv); 531 drainable->DidConsume(rv);
532 total_bytes_written += rv; 532 total_bytes_written += rv;
533 } 533 }
534 ok = file_util::GetFileSize(temp_file_path(), &file_size); 534 ok = file_util::GetFileSize(temp_file_path(), &file_size);
535 EXPECT_TRUE(ok); 535 EXPECT_TRUE(ok);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 EXPECT_EQ(OK, callback.WaitForResult()); 639 EXPECT_EQ(OK, callback.WaitForResult());
640 640
641 int64 total_bytes_avail = stream->Available(); 641 int64 total_bytes_avail = stream->Available();
642 EXPECT_EQ(file_size, total_bytes_avail); 642 EXPECT_EQ(file_size, total_bytes_avail);
643 643
644 int64 total_bytes_read = 0; 644 int64 total_bytes_read = 0;
645 645
646 std::string data_read; 646 std::string data_read;
647 for (;;) { 647 for (;;) {
648 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 648 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
649 rv = stream->Read(buf, buf->size(), callback.callback()); 649 rv = stream->Read(buf.get(), buf->size(), callback.callback());
650 if (rv == ERR_IO_PENDING) 650 if (rv == ERR_IO_PENDING)
651 rv = callback.WaitForResult(); 651 rv = callback.WaitForResult();
652 EXPECT_LE(0, rv); 652 EXPECT_LE(0, rv);
653 if (rv <= 0) 653 if (rv <= 0)
654 break; 654 break;
655 total_bytes_read += rv; 655 total_bytes_read += rv;
656 data_read.append(buf->data(), rv); 656 data_read.append(buf->data(), rv);
657 } 657 }
658 EXPECT_EQ(file_size, total_bytes_read); 658 EXPECT_EQ(file_size, total_bytes_read);
659 EXPECT_TRUE(data_read == kTestData); 659 EXPECT_TRUE(data_read == kTestData);
660 660
661 int total_bytes_written = 0; 661 int total_bytes_written = 0;
662 662
663 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); 663 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
664 scoped_refptr<DrainableIOBuffer> drainable = 664 scoped_refptr<DrainableIOBuffer> drainable =
665 new DrainableIOBuffer(buf, buf->size()); 665 new DrainableIOBuffer(buf.get(), buf->size());
666 while (total_bytes_written != kTestDataSize) { 666 while (total_bytes_written != kTestDataSize) {
667 rv = stream->Write(drainable, drainable->BytesRemaining(), 667 rv = stream->Write(
668 callback.callback()); 668 drainable.get(), drainable->BytesRemaining(), callback.callback());
669 if (rv == ERR_IO_PENDING) 669 if (rv == ERR_IO_PENDING)
670 rv = callback.WaitForResult(); 670 rv = callback.WaitForResult();
671 EXPECT_LT(0, rv); 671 EXPECT_LT(0, rv);
672 if (rv <= 0) 672 if (rv <= 0)
673 break; 673 break;
674 drainable->DidConsume(rv); 674 drainable->DidConsume(rv);
675 total_bytes_written += rv; 675 total_bytes_written += rv;
676 } 676 }
677 677
678 stream.reset(); 678 stream.reset();
(...skipping 24 matching lines...) Expand all
703 TestInt64CompletionCallback callback64; 703 TestInt64CompletionCallback callback64;
704 rv = stream->Seek(FROM_END, 0, callback64.callback()); 704 rv = stream->Seek(FROM_END, 0, callback64.callback());
705 ASSERT_EQ(ERR_IO_PENDING, rv); 705 ASSERT_EQ(ERR_IO_PENDING, rv);
706 int64 offset = callback64.WaitForResult(); 706 int64 offset = callback64.WaitForResult();
707 EXPECT_EQ(offset, file_size); 707 EXPECT_EQ(offset, file_size);
708 708
709 int total_bytes_written = 0; 709 int total_bytes_written = 0;
710 710
711 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); 711 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
712 scoped_refptr<DrainableIOBuffer> drainable = 712 scoped_refptr<DrainableIOBuffer> drainable =
713 new DrainableIOBuffer(buf, buf->size()); 713 new DrainableIOBuffer(buf.get(), buf->size());
714 while (total_bytes_written != kTestDataSize) { 714 while (total_bytes_written != kTestDataSize) {
715 rv = stream->Write(drainable, drainable->BytesRemaining(), 715 rv = stream->Write(
716 callback.callback()); 716 drainable.get(), drainable->BytesRemaining(), callback.callback());
717 if (rv == ERR_IO_PENDING) 717 if (rv == ERR_IO_PENDING)
718 rv = callback.WaitForResult(); 718 rv = callback.WaitForResult();
719 EXPECT_LT(0, rv); 719 EXPECT_LT(0, rv);
720 if (rv <= 0) 720 if (rv <= 0)
721 break; 721 break;
722 drainable->DidConsume(rv); 722 drainable->DidConsume(rv);
723 total_bytes_written += rv; 723 total_bytes_written += rv;
724 } 724 }
725 725
726 EXPECT_EQ(kTestDataSize, total_bytes_written); 726 EXPECT_EQ(kTestDataSize, total_bytes_written);
727 727
728 rv = stream->Seek(FROM_BEGIN, 0, callback64.callback()); 728 rv = stream->Seek(FROM_BEGIN, 0, callback64.callback());
729 ASSERT_EQ(ERR_IO_PENDING, rv); 729 ASSERT_EQ(ERR_IO_PENDING, rv);
730 offset = callback64.WaitForResult(); 730 offset = callback64.WaitForResult();
731 EXPECT_EQ(0, offset); 731 EXPECT_EQ(0, offset);
732 732
733 int total_bytes_read = 0; 733 int total_bytes_read = 0;
734 734
735 std::string data_read; 735 std::string data_read;
736 for (;;) { 736 for (;;) {
737 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 737 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
738 rv = stream->Read(buf, buf->size(), callback.callback()); 738 rv = stream->Read(buf.get(), buf->size(), callback.callback());
739 if (rv == ERR_IO_PENDING) 739 if (rv == ERR_IO_PENDING)
740 rv = callback.WaitForResult(); 740 rv = callback.WaitForResult();
741 EXPECT_LE(0, rv); 741 EXPECT_LE(0, rv);
742 if (rv <= 0) 742 if (rv <= 0)
743 break; 743 break;
744 total_bytes_read += rv; 744 total_bytes_read += rv;
745 data_read.append(buf->data(), rv); 745 data_read.append(buf->data(), rv);
746 } 746 }
747 stream.reset(); 747 stream.reset();
748 748
749 ok = file_util::GetFileSize(temp_file_path(), &file_size); 749 ok = file_util::GetFileSize(temp_file_path(), &file_size);
750 EXPECT_TRUE(ok); 750 EXPECT_TRUE(ok);
751 EXPECT_EQ(kTestDataSize * 2, file_size); 751 EXPECT_EQ(kTestDataSize * 2, file_size);
752 752
753 EXPECT_EQ(kTestDataSize * 2, total_bytes_read); 753 EXPECT_EQ(kTestDataSize * 2, total_bytes_read);
754 const std::string kExpectedFileData = 754 const std::string kExpectedFileData =
755 std::string(kTestData) + std::string(kTestData); 755 std::string(kTestData) + std::string(kTestData);
756 EXPECT_EQ(kExpectedFileData, data_read); 756 EXPECT_EQ(kExpectedFileData, data_read);
757 } 757 }
758 758
759 class TestWriteReadCompletionCallback { 759 class TestWriteReadCompletionCallback {
760 public: 760 public:
761 TestWriteReadCompletionCallback( 761 TestWriteReadCompletionCallback(FileStream* stream,
762 FileStream* stream, 762 int* total_bytes_written,
763 int* total_bytes_written, 763 int* total_bytes_read,
764 int* total_bytes_read, 764 std::string* data_read)
765 std::string* data_read)
766 : result_(0), 765 : result_(0),
767 have_result_(false), 766 have_result_(false),
768 waiting_for_result_(false), 767 waiting_for_result_(false),
769 stream_(stream), 768 stream_(stream),
770 total_bytes_written_(total_bytes_written), 769 total_bytes_written_(total_bytes_written),
771 total_bytes_read_(total_bytes_read), 770 total_bytes_read_(total_bytes_read),
772 data_read_(data_read), 771 data_read_(data_read),
773 callback_(base::Bind(&TestWriteReadCompletionCallback::OnComplete, 772 callback_(base::Bind(&TestWriteReadCompletionCallback::OnComplete,
774 base::Unretained(this))), 773 base::Unretained(this))),
775 test_data_(CreateTestDataBuffer()), 774 test_data_(CreateTestDataBuffer()),
776 drainable_(new DrainableIOBuffer(test_data_, kTestDataSize)) { 775 drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) {}
777 }
778 776
779 int WaitForResult() { 777 int WaitForResult() {
780 DCHECK(!waiting_for_result_); 778 DCHECK(!waiting_for_result_);
781 while (!have_result_) { 779 while (!have_result_) {
782 waiting_for_result_ = true; 780 waiting_for_result_ = true;
783 base::MessageLoop::current()->Run(); 781 base::MessageLoop::current()->Run();
784 waiting_for_result_ = false; 782 waiting_for_result_ = false;
785 } 783 }
786 have_result_ = false; // auto-reset for next callback 784 have_result_ = false; // auto-reset for next callback
787 return result_; 785 return result_;
788 } 786 }
789 787
790 const CompletionCallback& callback() const { return callback_; } 788 const CompletionCallback& callback() const { return callback_; }
791 789
792 private: 790 private:
793 void OnComplete(int result) { 791 void OnComplete(int result) {
794 DCHECK_LT(0, result); 792 DCHECK_LT(0, result);
795 *total_bytes_written_ += result; 793 *total_bytes_written_ += result;
796 794
797 int rv; 795 int rv;
798 796
799 if (*total_bytes_written_ != kTestDataSize) { 797 if (*total_bytes_written_ != kTestDataSize) {
800 // Recurse to finish writing all data. 798 // Recurse to finish writing all data.
801 int total_bytes_written = 0, total_bytes_read = 0; 799 int total_bytes_written = 0, total_bytes_read = 0;
802 std::string data_read; 800 std::string data_read;
803 TestWriteReadCompletionCallback callback( 801 TestWriteReadCompletionCallback callback(
804 stream_, &total_bytes_written, &total_bytes_read, &data_read); 802 stream_, &total_bytes_written, &total_bytes_read, &data_read);
805 rv = stream_->Write(drainable_, drainable_->BytesRemaining(), 803 rv = stream_->Write(
806 callback.callback()); 804 drainable_.get(), drainable_->BytesRemaining(), callback.callback());
807 DCHECK_EQ(ERR_IO_PENDING, rv); 805 DCHECK_EQ(ERR_IO_PENDING, rv);
808 rv = callback.WaitForResult(); 806 rv = callback.WaitForResult();
809 drainable_->DidConsume(total_bytes_written); 807 drainable_->DidConsume(total_bytes_written);
810 *total_bytes_written_ += total_bytes_written; 808 *total_bytes_written_ += total_bytes_written;
811 *total_bytes_read_ += total_bytes_read; 809 *total_bytes_read_ += total_bytes_read;
812 *data_read_ += data_read; 810 *data_read_ += data_read;
813 } else { // We're done writing all data. Start reading the data. 811 } else { // We're done writing all data. Start reading the data.
814 stream_->SeekSync(FROM_BEGIN, 0); 812 stream_->SeekSync(FROM_BEGIN, 0);
815 813
816 TestCompletionCallback callback; 814 TestCompletionCallback callback;
817 for (;;) { 815 for (;;) {
818 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 816 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
819 rv = stream_->Read(buf, buf->size(), callback.callback()); 817 rv = stream_->Read(buf.get(), buf->size(), callback.callback());
820 if (rv == ERR_IO_PENDING) { 818 if (rv == ERR_IO_PENDING) {
821 base::MessageLoop::ScopedNestableTaskAllower allow( 819 base::MessageLoop::ScopedNestableTaskAllower allow(
822 base::MessageLoop::current()); 820 base::MessageLoop::current());
823 rv = callback.WaitForResult(); 821 rv = callback.WaitForResult();
824 } 822 }
825 EXPECT_LE(0, rv); 823 EXPECT_LE(0, rv);
826 if (rv <= 0) 824 if (rv <= 0)
827 break; 825 break;
828 *total_bytes_read_ += rv; 826 *total_bytes_read_ += rv;
829 data_read_->append(buf->data(), rv); 827 data_read_->append(buf->data(), rv);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 int64 offset = stream->SeekSync(FROM_END, 0); 869 int64 offset = stream->SeekSync(FROM_END, 0);
872 EXPECT_EQ(offset, file_size); 870 EXPECT_EQ(offset, file_size);
873 871
874 int total_bytes_written = 0; 872 int total_bytes_written = 0;
875 int total_bytes_read = 0; 873 int total_bytes_read = 0;
876 std::string data_read; 874 std::string data_read;
877 TestWriteReadCompletionCallback callback(stream.get(), &total_bytes_written, 875 TestWriteReadCompletionCallback callback(stream.get(), &total_bytes_written,
878 &total_bytes_read, &data_read); 876 &total_bytes_read, &data_read);
879 877
880 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); 878 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
881 rv = stream->Write(buf, buf->size(), callback.callback()); 879 rv = stream->Write(buf.get(), buf->size(), callback.callback());
882 if (rv == ERR_IO_PENDING) 880 if (rv == ERR_IO_PENDING)
883 rv = callback.WaitForResult(); 881 rv = callback.WaitForResult();
884 EXPECT_LT(0, rv); 882 EXPECT_LT(0, rv);
885 EXPECT_EQ(kTestDataSize, total_bytes_written); 883 EXPECT_EQ(kTestDataSize, total_bytes_written);
886 884
887 stream.reset(); 885 stream.reset();
888 886
889 ok = file_util::GetFileSize(temp_file_path(), &file_size); 887 ok = file_util::GetFileSize(temp_file_path(), &file_size);
890 EXPECT_TRUE(ok); 888 EXPECT_TRUE(ok);
891 EXPECT_EQ(kTestDataSize * 2, file_size); 889 EXPECT_EQ(kTestDataSize * 2, file_size);
892 890
893 EXPECT_EQ(kTestDataSize * 2, total_bytes_read); 891 EXPECT_EQ(kTestDataSize * 2, total_bytes_read);
894 const std::string kExpectedFileData = 892 const std::string kExpectedFileData =
895 std::string(kTestData) + std::string(kTestData); 893 std::string(kTestData) + std::string(kTestData);
896 EXPECT_EQ(kExpectedFileData, data_read); 894 EXPECT_EQ(kExpectedFileData, data_read);
897 } 895 }
898 896
899 class TestWriteCloseCompletionCallback { 897 class TestWriteCloseCompletionCallback {
900 public: 898 public:
901 TestWriteCloseCompletionCallback(FileStream* stream, int* total_bytes_written) 899 TestWriteCloseCompletionCallback(FileStream* stream, int* total_bytes_written)
902 : result_(0), 900 : result_(0),
903 have_result_(false), 901 have_result_(false),
904 waiting_for_result_(false), 902 waiting_for_result_(false),
905 stream_(stream), 903 stream_(stream),
906 total_bytes_written_(total_bytes_written), 904 total_bytes_written_(total_bytes_written),
907 callback_(base::Bind(&TestWriteCloseCompletionCallback::OnComplete, 905 callback_(base::Bind(&TestWriteCloseCompletionCallback::OnComplete,
908 base::Unretained(this))), 906 base::Unretained(this))),
909 test_data_(CreateTestDataBuffer()), 907 test_data_(CreateTestDataBuffer()),
910 drainable_(new DrainableIOBuffer(test_data_, kTestDataSize)) { 908 drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) {}
911 }
912 909
913 int WaitForResult() { 910 int WaitForResult() {
914 DCHECK(!waiting_for_result_); 911 DCHECK(!waiting_for_result_);
915 while (!have_result_) { 912 while (!have_result_) {
916 waiting_for_result_ = true; 913 waiting_for_result_ = true;
917 base::MessageLoop::current()->Run(); 914 base::MessageLoop::current()->Run();
918 waiting_for_result_ = false; 915 waiting_for_result_ = false;
919 } 916 }
920 have_result_ = false; // auto-reset for next callback 917 have_result_ = false; // auto-reset for next callback
921 return result_; 918 return result_;
922 } 919 }
923 920
924 const CompletionCallback& callback() const { return callback_; } 921 const CompletionCallback& callback() const { return callback_; }
925 922
926 private: 923 private:
927 void OnComplete(int result) { 924 void OnComplete(int result) {
928 DCHECK_LT(0, result); 925 DCHECK_LT(0, result);
929 *total_bytes_written_ += result; 926 *total_bytes_written_ += result;
930 927
931 int rv; 928 int rv;
932 929
933 if (*total_bytes_written_ != kTestDataSize) { 930 if (*total_bytes_written_ != kTestDataSize) {
934 // Recurse to finish writing all data. 931 // Recurse to finish writing all data.
935 int total_bytes_written = 0; 932 int total_bytes_written = 0;
936 TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written); 933 TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written);
937 rv = stream_->Write(drainable_, drainable_->BytesRemaining(), 934 rv = stream_->Write(
938 callback.callback()); 935 drainable_.get(), drainable_->BytesRemaining(), callback.callback());
939 DCHECK_EQ(ERR_IO_PENDING, rv); 936 DCHECK_EQ(ERR_IO_PENDING, rv);
940 rv = callback.WaitForResult(); 937 rv = callback.WaitForResult();
941 drainable_->DidConsume(total_bytes_written); 938 drainable_->DidConsume(total_bytes_written);
942 *total_bytes_written_ += total_bytes_written; 939 *total_bytes_written_ += total_bytes_written;
943 } 940 }
944 941
945 result_ = *total_bytes_written_; 942 result_ = *total_bytes_written_;
946 have_result_ = true; 943 have_result_ = true;
947 if (waiting_for_result_) 944 if (waiting_for_result_)
948 base::MessageLoop::current()->Quit(); 945 base::MessageLoop::current()->Quit();
(...skipping 29 matching lines...) Expand all
978 int64 total_bytes_avail = stream->Available(); 975 int64 total_bytes_avail = stream->Available();
979 EXPECT_EQ(file_size, total_bytes_avail); 976 EXPECT_EQ(file_size, total_bytes_avail);
980 977
981 int64 offset = stream->SeekSync(FROM_END, 0); 978 int64 offset = stream->SeekSync(FROM_END, 0);
982 EXPECT_EQ(offset, file_size); 979 EXPECT_EQ(offset, file_size);
983 980
984 int total_bytes_written = 0; 981 int total_bytes_written = 0;
985 TestWriteCloseCompletionCallback callback(stream.get(), &total_bytes_written); 982 TestWriteCloseCompletionCallback callback(stream.get(), &total_bytes_written);
986 983
987 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); 984 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
988 rv = stream->Write(buf, buf->size(), callback.callback()); 985 rv = stream->Write(buf.get(), buf->size(), callback.callback());
989 if (rv == ERR_IO_PENDING) 986 if (rv == ERR_IO_PENDING)
990 total_bytes_written = callback.WaitForResult(); 987 total_bytes_written = callback.WaitForResult();
991 EXPECT_LT(0, total_bytes_written); 988 EXPECT_LT(0, total_bytes_written);
992 EXPECT_EQ(kTestDataSize, total_bytes_written); 989 EXPECT_EQ(kTestDataSize, total_bytes_written);
993 990
994 stream.reset(); 991 stream.reset();
995 992
996 ok = file_util::GetFileSize(temp_file_path(), &file_size); 993 ok = file_util::GetFileSize(temp_file_path(), &file_size);
997 EXPECT_TRUE(ok); 994 EXPECT_TRUE(ok);
998 EXPECT_EQ(kTestDataSize * 2, file_size); 995 EXPECT_EQ(kTestDataSize * 2, file_size);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1048 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | 1045 int flags = base::PLATFORM_FILE_CREATE_ALWAYS |
1049 base::PLATFORM_FILE_WRITE | 1046 base::PLATFORM_FILE_WRITE |
1050 base::PLATFORM_FILE_ASYNC; 1047 base::PLATFORM_FILE_ASYNC;
1051 TestCompletionCallback callback; 1048 TestCompletionCallback callback;
1052 int rv = stream->Open(temp_file_path(), flags, callback.callback()); 1049 int rv = stream->Open(temp_file_path(), flags, callback.callback());
1053 EXPECT_EQ(ERR_IO_PENDING, rv); 1050 EXPECT_EQ(ERR_IO_PENDING, rv);
1054 EXPECT_EQ(OK, callback.WaitForResult()); 1051 EXPECT_EQ(OK, callback.WaitForResult());
1055 1052
1056 // Try passing NULL buffer to Write() and check that it fails. 1053 // Try passing NULL buffer to Write() and check that it fails.
1057 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(NULL); 1054 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(NULL);
1058 rv = stream->Write(buf, 1, callback.callback()); 1055 rv = stream->Write(buf.get(), 1, callback.callback());
1059 if (rv == ERR_IO_PENDING) 1056 if (rv == ERR_IO_PENDING)
1060 rv = callback.WaitForResult(); 1057 rv = callback.WaitForResult();
1061 EXPECT_LT(rv, 0); 1058 EXPECT_LT(rv, 0);
1062 } 1059 }
1063 1060
1064 // Verify that async Read() errors are mapped correctly. 1061 // Verify that async Read() errors are mapped correctly.
1065 TEST_F(FileStreamTest, AsyncReadError) { 1062 TEST_F(FileStreamTest, AsyncReadError) {
1066 scoped_ptr<FileStream> stream(new FileStream(NULL)); 1063 scoped_ptr<FileStream> stream(new FileStream(NULL));
1067 int flags = base::PLATFORM_FILE_OPEN | 1064 int flags = base::PLATFORM_FILE_OPEN |
1068 base::PLATFORM_FILE_READ | 1065 base::PLATFORM_FILE_READ |
1069 base::PLATFORM_FILE_ASYNC; 1066 base::PLATFORM_FILE_ASYNC;
1070 TestCompletionCallback callback; 1067 TestCompletionCallback callback;
1071 int rv = stream->Open(temp_file_path(), flags, callback.callback()); 1068 int rv = stream->Open(temp_file_path(), flags, callback.callback());
1072 EXPECT_EQ(ERR_IO_PENDING, rv); 1069 EXPECT_EQ(ERR_IO_PENDING, rv);
1073 EXPECT_EQ(OK, callback.WaitForResult()); 1070 EXPECT_EQ(OK, callback.WaitForResult());
1074 1071
1075 // Try passing NULL buffer to Read() and check that it fails. 1072 // Try passing NULL buffer to Read() and check that it fails.
1076 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(NULL); 1073 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(NULL);
1077 rv = stream->Read(buf, 1, callback.callback()); 1074 rv = stream->Read(buf.get(), 1, callback.callback());
1078 if (rv == ERR_IO_PENDING) 1075 if (rv == ERR_IO_PENDING)
1079 rv = callback.WaitForResult(); 1076 rv = callback.WaitForResult();
1080 EXPECT_LT(rv, 0); 1077 EXPECT_LT(rv, 0);
1081 } 1078 }
1082 1079
1083 } // namespace 1080 } // namespace
1084 1081
1085 } // namespace net 1082 } // namespace net
OLDNEW
« no previous file with comments | « net/base/file_stream_context_posix.cc ('k') | net/base/test_completion_callback_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698