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/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 int64 total_bytes_avail = stream.Available(); | 238 int64 total_bytes_avail = stream.Available(); |
239 EXPECT_EQ(file_size, total_bytes_avail); | 239 EXPECT_EQ(file_size, total_bytes_avail); |
240 | 240 |
241 TestCompletionCallback callback; | 241 TestCompletionCallback callback; |
242 | 242 |
243 int total_bytes_read = 0; | 243 int total_bytes_read = 0; |
244 | 244 |
245 std::string data_read; | 245 std::string data_read; |
246 for (;;) { | 246 for (;;) { |
247 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 247 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
248 rv = stream.Read(buf, buf->size(), callback.callback()); | 248 rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
249 if (rv == ERR_IO_PENDING) | 249 if (rv == ERR_IO_PENDING) |
250 rv = callback.WaitForResult(); | 250 rv = callback.WaitForResult(); |
251 EXPECT_LE(0, rv); | 251 EXPECT_LE(0, rv); |
252 if (rv <= 0) | 252 if (rv <= 0) |
253 break; | 253 break; |
254 total_bytes_read += rv; | 254 total_bytes_read += rv; |
255 data_read.append(buf->data(), rv); | 255 data_read.append(buf->data(), rv); |
256 } | 256 } |
257 EXPECT_EQ(file_size, total_bytes_read); | 257 EXPECT_EQ(file_size, total_bytes_read); |
258 EXPECT_EQ(kTestData, data_read); | 258 EXPECT_EQ(kTestData, data_read); |
(...skipping 10 matching lines...) Expand all Loading... |
269 base::PLATFORM_FILE_ASYNC; | 269 base::PLATFORM_FILE_ASYNC; |
270 int rv = stream.OpenSync(temp_file_path(), flags); | 270 int rv = stream.OpenSync(temp_file_path(), flags); |
271 EXPECT_EQ(OK, rv); | 271 EXPECT_EQ(OK, rv); |
272 | 272 |
273 int64 total_bytes_avail = stream.Available(); | 273 int64 total_bytes_avail = stream.Available(); |
274 EXPECT_EQ(file_size, total_bytes_avail); | 274 EXPECT_EQ(file_size, total_bytes_avail); |
275 | 275 |
276 TestCompletionCallback callback; | 276 TestCompletionCallback callback; |
277 | 277 |
278 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 278 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
279 rv = stream.Read(buf, buf->size(), callback.callback()); | 279 rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
280 stream.CloseSync(); | 280 stream.CloseSync(); |
281 if (rv < 0) { | 281 if (rv < 0) { |
282 EXPECT_EQ(ERR_IO_PENDING, rv); | 282 EXPECT_EQ(ERR_IO_PENDING, rv); |
283 // The callback should not be called if the request is cancelled. | 283 // The callback should not be called if the request is cancelled. |
284 MessageLoop::current()->RunAllPending(); | 284 MessageLoop::current()->RunAllPending(); |
285 EXPECT_FALSE(callback.have_result()); | 285 EXPECT_FALSE(callback.have_result()); |
286 } else { | 286 } else { |
287 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); | 287 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
288 } | 288 } |
289 } | 289 } |
(...skipping 11 matching lines...) Expand all Loading... |
301 base::PLATFORM_FILE_ASYNC; | 301 base::PLATFORM_FILE_ASYNC; |
302 TestCompletionCallback callback; | 302 TestCompletionCallback callback; |
303 int rv = stream->Open(temp_file_path(), flags, callback.callback()); | 303 int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
304 EXPECT_EQ(ERR_IO_PENDING, rv); | 304 EXPECT_EQ(ERR_IO_PENDING, rv); |
305 EXPECT_EQ(OK, callback.WaitForResult()); | 305 EXPECT_EQ(OK, callback.WaitForResult()); |
306 | 306 |
307 int64 total_bytes_avail = stream->Available(); | 307 int64 total_bytes_avail = stream->Available(); |
308 EXPECT_EQ(file_size, total_bytes_avail); | 308 EXPECT_EQ(file_size, total_bytes_avail); |
309 | 309 |
310 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 310 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
311 rv = stream->Read(buf, buf->size(), callback.callback()); | 311 rv = stream->Read(buf.get(), buf->size(), callback.callback()); |
312 stream.reset(); // Delete instead of closing it. | 312 stream.reset(); // Delete instead of closing it. |
313 if (rv < 0) { | 313 if (rv < 0) { |
314 EXPECT_EQ(ERR_IO_PENDING, rv); | 314 EXPECT_EQ(ERR_IO_PENDING, rv); |
315 // The callback should not be called if the request is cancelled. | 315 // The callback should not be called if the request is cancelled. |
316 MessageLoop::current()->RunAllPending(); | 316 MessageLoop::current()->RunAllPending(); |
317 EXPECT_FALSE(callback.have_result()); | 317 EXPECT_FALSE(callback.have_result()); |
318 } else { | 318 } else { |
319 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); | 319 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
320 } | 320 } |
321 } | 321 } |
(...skipping 14 matching lines...) Expand all Loading... |
336 base::PlatformFile file = base::CreatePlatformFile( | 336 base::PlatformFile file = base::CreatePlatformFile( |
337 temp_file_path(), flags, &created, &error_code); | 337 temp_file_path(), flags, &created, &error_code); |
338 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | 338 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); |
339 | 339 |
340 scoped_ptr<FileStream> stream(new FileStream(file, flags, NULL)); | 340 scoped_ptr<FileStream> stream(new FileStream(file, flags, NULL)); |
341 int64 total_bytes_avail = stream->Available(); | 341 int64 total_bytes_avail = stream->Available(); |
342 EXPECT_EQ(file_size, total_bytes_avail); | 342 EXPECT_EQ(file_size, total_bytes_avail); |
343 | 343 |
344 TestCompletionCallback callback; | 344 TestCompletionCallback callback; |
345 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 345 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
346 int rv = stream->Read(buf, buf->size(), callback.callback()); | 346 int rv = stream->Read(buf.get(), buf->size(), callback.callback()); |
347 stream.reset(); // Delete instead of closing it. | 347 stream.reset(); // Delete instead of closing it. |
348 if (rv < 0) { | 348 if (rv < 0) { |
349 EXPECT_EQ(ERR_IO_PENDING, rv); | 349 EXPECT_EQ(ERR_IO_PENDING, rv); |
350 // The callback should not be called if the request is cancelled. | 350 // The callback should not be called if the request is cancelled. |
351 MessageLoop::current()->RunAllPending(); | 351 MessageLoop::current()->RunAllPending(); |
352 EXPECT_FALSE(callback.have_result()); | 352 EXPECT_FALSE(callback.have_result()); |
353 } else { | 353 } else { |
354 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); | 354 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
355 } | 355 } |
356 | 356 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 int64 total_bytes_avail = stream.Available(); | 418 int64 total_bytes_avail = stream.Available(); |
419 EXPECT_EQ(file_size - kOffset, total_bytes_avail); | 419 EXPECT_EQ(file_size - kOffset, total_bytes_avail); |
420 | 420 |
421 TestCompletionCallback callback; | 421 TestCompletionCallback callback; |
422 | 422 |
423 int total_bytes_read = 0; | 423 int total_bytes_read = 0; |
424 | 424 |
425 std::string data_read; | 425 std::string data_read; |
426 for (;;) { | 426 for (;;) { |
427 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 427 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
428 rv = stream.Read(buf, buf->size(), callback.callback()); | 428 rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
429 if (rv == ERR_IO_PENDING) | 429 if (rv == ERR_IO_PENDING) |
430 rv = callback.WaitForResult(); | 430 rv = callback.WaitForResult(); |
431 EXPECT_LE(0, rv); | 431 EXPECT_LE(0, rv); |
432 if (rv <= 0) | 432 if (rv <= 0) |
433 break; | 433 break; |
434 total_bytes_read += rv; | 434 total_bytes_read += rv; |
435 data_read.append(buf->data(), rv); | 435 data_read.append(buf->data(), rv); |
436 } | 436 } |
437 EXPECT_EQ(file_size - kOffset, total_bytes_read); | 437 EXPECT_EQ(file_size - kOffset, total_bytes_read); |
438 EXPECT_EQ(kTestData + kOffset, data_read); | 438 EXPECT_EQ(kTestData + kOffset, data_read); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 int64 file_size; | 527 int64 file_size; |
528 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); | 528 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
529 EXPECT_TRUE(ok); | 529 EXPECT_TRUE(ok); |
530 EXPECT_EQ(0, file_size); | 530 EXPECT_EQ(0, file_size); |
531 | 531 |
532 TestCompletionCallback callback; | 532 TestCompletionCallback callback; |
533 int total_bytes_written = 0; | 533 int total_bytes_written = 0; |
534 | 534 |
535 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 535 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
536 scoped_refptr<DrainableIOBuffer> drainable = | 536 scoped_refptr<DrainableIOBuffer> drainable = |
537 new DrainableIOBuffer(buf, buf->size()); | 537 new DrainableIOBuffer(buf.get(), buf->size()); |
538 while (total_bytes_written != kTestDataSize) { | 538 while (total_bytes_written != kTestDataSize) { |
539 rv = stream.Write(drainable, drainable->BytesRemaining(), | 539 rv = stream.Write(drainable.get(), drainable->BytesRemaining(), |
540 callback.callback()); | 540 callback.callback()); |
541 if (rv == ERR_IO_PENDING) | 541 if (rv == ERR_IO_PENDING) |
542 rv = callback.WaitForResult(); | 542 rv = callback.WaitForResult(); |
543 EXPECT_LT(0, rv); | 543 EXPECT_LT(0, rv); |
544 if (rv <= 0) | 544 if (rv <= 0) |
545 break; | 545 break; |
546 drainable->DidConsume(rv); | 546 drainable->DidConsume(rv); |
547 total_bytes_written += rv; | 547 total_bytes_written += rv; |
548 } | 548 } |
549 ok = file_util::GetFileSize(temp_file_path(), &file_size); | 549 ok = file_util::GetFileSize(temp_file_path(), &file_size); |
(...skipping 10 matching lines...) Expand all Loading... |
560 EXPECT_EQ(OK, rv); | 560 EXPECT_EQ(OK, rv); |
561 | 561 |
562 int64 file_size; | 562 int64 file_size; |
563 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); | 563 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
564 EXPECT_TRUE(ok); | 564 EXPECT_TRUE(ok); |
565 EXPECT_EQ(0, file_size); | 565 EXPECT_EQ(0, file_size); |
566 | 566 |
567 TestCompletionCallback callback; | 567 TestCompletionCallback callback; |
568 | 568 |
569 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 569 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
570 rv = stream.Write(buf, buf->size(), callback.callback()); | 570 rv = stream.Write(buf.get(), buf->size(), callback.callback()); |
571 stream.CloseSync(); | 571 stream.CloseSync(); |
572 if (rv < 0) { | 572 if (rv < 0) { |
573 EXPECT_EQ(ERR_IO_PENDING, rv); | 573 EXPECT_EQ(ERR_IO_PENDING, rv); |
574 // The callback should not be called if the request is cancelled. | 574 // The callback should not be called if the request is cancelled. |
575 MessageLoop::current()->RunAllPending(); | 575 MessageLoop::current()->RunAllPending(); |
576 EXPECT_FALSE(callback.have_result()); | 576 EXPECT_FALSE(callback.have_result()); |
577 } else { | 577 } else { |
578 ok = file_util::GetFileSize(temp_file_path(), &file_size); | 578 ok = file_util::GetFileSize(temp_file_path(), &file_size); |
579 EXPECT_TRUE(ok); | 579 EXPECT_TRUE(ok); |
580 EXPECT_EQ(file_size, rv); | 580 EXPECT_EQ(file_size, rv); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 rv = stream.Seek(FROM_END, kOffset, callback64.callback()); | 623 rv = stream.Seek(FROM_END, kOffset, callback64.callback()); |
624 ASSERT_EQ(ERR_IO_PENDING, rv); | 624 ASSERT_EQ(ERR_IO_PENDING, rv); |
625 int64 new_offset = callback64.WaitForResult(); | 625 int64 new_offset = callback64.WaitForResult(); |
626 EXPECT_EQ(kTestDataSize, new_offset); | 626 EXPECT_EQ(kTestDataSize, new_offset); |
627 | 627 |
628 TestCompletionCallback callback; | 628 TestCompletionCallback callback; |
629 int total_bytes_written = 0; | 629 int total_bytes_written = 0; |
630 | 630 |
631 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 631 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
632 scoped_refptr<DrainableIOBuffer> drainable = | 632 scoped_refptr<DrainableIOBuffer> drainable = |
633 new DrainableIOBuffer(buf, buf->size()); | 633 new DrainableIOBuffer(buf.get(), buf->size()); |
634 while (total_bytes_written != kTestDataSize) { | 634 while (total_bytes_written != kTestDataSize) { |
635 rv = stream.Write(drainable, drainable->BytesRemaining(), | 635 rv = stream.Write(drainable.get(), drainable->BytesRemaining(), |
636 callback.callback()); | 636 callback.callback()); |
637 if (rv == ERR_IO_PENDING) | 637 if (rv == ERR_IO_PENDING) |
638 rv = callback.WaitForResult(); | 638 rv = callback.WaitForResult(); |
639 EXPECT_LT(0, rv); | 639 EXPECT_LT(0, rv); |
640 if (rv <= 0) | 640 if (rv <= 0) |
641 break; | 641 break; |
642 drainable->DidConsume(rv); | 642 drainable->DidConsume(rv); |
643 total_bytes_written += rv; | 643 total_bytes_written += rv; |
644 } | 644 } |
645 ok = file_util::GetFileSize(temp_file_path(), &file_size); | 645 ok = file_util::GetFileSize(temp_file_path(), &file_size); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 | 749 |
750 int64 total_bytes_avail = stream.Available(); | 750 int64 total_bytes_avail = stream.Available(); |
751 EXPECT_EQ(file_size, total_bytes_avail); | 751 EXPECT_EQ(file_size, total_bytes_avail); |
752 | 752 |
753 TestCompletionCallback callback; | 753 TestCompletionCallback callback; |
754 int64 total_bytes_read = 0; | 754 int64 total_bytes_read = 0; |
755 | 755 |
756 std::string data_read; | 756 std::string data_read; |
757 for (;;) { | 757 for (;;) { |
758 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 758 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
759 rv = stream.Read(buf, buf->size(), callback.callback()); | 759 rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
760 if (rv == ERR_IO_PENDING) | 760 if (rv == ERR_IO_PENDING) |
761 rv = callback.WaitForResult(); | 761 rv = callback.WaitForResult(); |
762 EXPECT_LE(0, rv); | 762 EXPECT_LE(0, rv); |
763 if (rv <= 0) | 763 if (rv <= 0) |
764 break; | 764 break; |
765 total_bytes_read += rv; | 765 total_bytes_read += rv; |
766 data_read.append(buf->data(), rv); | 766 data_read.append(buf->data(), rv); |
767 } | 767 } |
768 EXPECT_EQ(file_size, total_bytes_read); | 768 EXPECT_EQ(file_size, total_bytes_read); |
769 EXPECT_TRUE(data_read == kTestData); | 769 EXPECT_TRUE(data_read == kTestData); |
770 | 770 |
771 int total_bytes_written = 0; | 771 int total_bytes_written = 0; |
772 | 772 |
773 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 773 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
774 scoped_refptr<DrainableIOBuffer> drainable = | 774 scoped_refptr<DrainableIOBuffer> drainable = |
775 new DrainableIOBuffer(buf, buf->size()); | 775 new DrainableIOBuffer(buf.get(), buf->size()); |
776 while (total_bytes_written != kTestDataSize) { | 776 while (total_bytes_written != kTestDataSize) { |
777 rv = stream.Write(drainable, drainable->BytesRemaining(), | 777 rv = stream.Write(drainable.get(), drainable->BytesRemaining(), |
778 callback.callback()); | 778 callback.callback()); |
779 if (rv == ERR_IO_PENDING) | 779 if (rv == ERR_IO_PENDING) |
780 rv = callback.WaitForResult(); | 780 rv = callback.WaitForResult(); |
781 EXPECT_LT(0, rv); | 781 EXPECT_LT(0, rv); |
782 if (rv <= 0) | 782 if (rv <= 0) |
783 break; | 783 break; |
784 drainable->DidConsume(rv); | 784 drainable->DidConsume(rv); |
785 total_bytes_written += rv; | 785 total_bytes_written += rv; |
786 } | 786 } |
787 | 787 |
(...skipping 24 matching lines...) Expand all Loading... |
812 rv = stream.Seek(FROM_END, 0, callback64.callback()); | 812 rv = stream.Seek(FROM_END, 0, callback64.callback()); |
813 ASSERT_EQ(ERR_IO_PENDING, rv); | 813 ASSERT_EQ(ERR_IO_PENDING, rv); |
814 int64 offset = callback64.WaitForResult(); | 814 int64 offset = callback64.WaitForResult(); |
815 EXPECT_EQ(offset, file_size); | 815 EXPECT_EQ(offset, file_size); |
816 | 816 |
817 TestCompletionCallback callback; | 817 TestCompletionCallback callback; |
818 int total_bytes_written = 0; | 818 int total_bytes_written = 0; |
819 | 819 |
820 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 820 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
821 scoped_refptr<DrainableIOBuffer> drainable = | 821 scoped_refptr<DrainableIOBuffer> drainable = |
822 new DrainableIOBuffer(buf, buf->size()); | 822 new DrainableIOBuffer(buf.get(), buf->size()); |
823 while (total_bytes_written != kTestDataSize) { | 823 while (total_bytes_written != kTestDataSize) { |
824 rv = stream.Write(drainable, drainable->BytesRemaining(), | 824 rv = stream.Write(drainable.get(), drainable->BytesRemaining(), |
825 callback.callback()); | 825 callback.callback()); |
826 if (rv == ERR_IO_PENDING) | 826 if (rv == ERR_IO_PENDING) |
827 rv = callback.WaitForResult(); | 827 rv = callback.WaitForResult(); |
828 EXPECT_LT(0, rv); | 828 EXPECT_LT(0, rv); |
829 if (rv <= 0) | 829 if (rv <= 0) |
830 break; | 830 break; |
831 drainable->DidConsume(rv); | 831 drainable->DidConsume(rv); |
832 total_bytes_written += rv; | 832 total_bytes_written += rv; |
833 } | 833 } |
834 | 834 |
835 EXPECT_EQ(kTestDataSize, total_bytes_written); | 835 EXPECT_EQ(kTestDataSize, total_bytes_written); |
836 | 836 |
837 rv = stream.Seek(FROM_BEGIN, 0, callback64.callback()); | 837 rv = stream.Seek(FROM_BEGIN, 0, callback64.callback()); |
838 ASSERT_EQ(ERR_IO_PENDING, rv); | 838 ASSERT_EQ(ERR_IO_PENDING, rv); |
839 offset = callback64.WaitForResult(); | 839 offset = callback64.WaitForResult(); |
840 EXPECT_EQ(0, offset); | 840 EXPECT_EQ(0, offset); |
841 | 841 |
842 int total_bytes_read = 0; | 842 int total_bytes_read = 0; |
843 | 843 |
844 std::string data_read; | 844 std::string data_read; |
845 for (;;) { | 845 for (;;) { |
846 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 846 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
847 rv = stream.Read(buf, buf->size(), callback.callback()); | 847 rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
848 if (rv == ERR_IO_PENDING) | 848 if (rv == ERR_IO_PENDING) |
849 rv = callback.WaitForResult(); | 849 rv = callback.WaitForResult(); |
850 EXPECT_LE(0, rv); | 850 EXPECT_LE(0, rv); |
851 if (rv <= 0) | 851 if (rv <= 0) |
852 break; | 852 break; |
853 total_bytes_read += rv; | 853 total_bytes_read += rv; |
854 data_read.append(buf->data(), rv); | 854 data_read.append(buf->data(), rv); |
855 } | 855 } |
856 stream.CloseSync(); | 856 stream.CloseSync(); |
857 | 857 |
(...skipping 17 matching lines...) Expand all Loading... |
875 : result_(0), | 875 : result_(0), |
876 have_result_(false), | 876 have_result_(false), |
877 waiting_for_result_(false), | 877 waiting_for_result_(false), |
878 stream_(stream), | 878 stream_(stream), |
879 total_bytes_written_(total_bytes_written), | 879 total_bytes_written_(total_bytes_written), |
880 total_bytes_read_(total_bytes_read), | 880 total_bytes_read_(total_bytes_read), |
881 data_read_(data_read), | 881 data_read_(data_read), |
882 callback_(base::Bind(&TestWriteReadCompletionCallback::OnComplete, | 882 callback_(base::Bind(&TestWriteReadCompletionCallback::OnComplete, |
883 base::Unretained(this))), | 883 base::Unretained(this))), |
884 test_data_(CreateTestDataBuffer()), | 884 test_data_(CreateTestDataBuffer()), |
885 drainable_(new DrainableIOBuffer(test_data_, kTestDataSize)) { | 885 drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) { |
886 } | 886 } |
887 | 887 |
888 int WaitForResult() { | 888 int WaitForResult() { |
889 DCHECK(!waiting_for_result_); | 889 DCHECK(!waiting_for_result_); |
890 while (!have_result_) { | 890 while (!have_result_) { |
891 waiting_for_result_ = true; | 891 waiting_for_result_ = true; |
892 MessageLoop::current()->Run(); | 892 MessageLoop::current()->Run(); |
893 waiting_for_result_ = false; | 893 waiting_for_result_ = false; |
894 } | 894 } |
895 have_result_ = false; // auto-reset for next callback | 895 have_result_ = false; // auto-reset for next callback |
896 return result_; | 896 return result_; |
897 } | 897 } |
898 | 898 |
899 const CompletionCallback& callback() const { return callback_; } | 899 const CompletionCallback& callback() const { return callback_; } |
900 | 900 |
901 private: | 901 private: |
902 void OnComplete(int result) { | 902 void OnComplete(int result) { |
903 DCHECK_LT(0, result); | 903 DCHECK_LT(0, result); |
904 *total_bytes_written_ += result; | 904 *total_bytes_written_ += result; |
905 | 905 |
906 int rv; | 906 int rv; |
907 | 907 |
908 if (*total_bytes_written_ != kTestDataSize) { | 908 if (*total_bytes_written_ != kTestDataSize) { |
909 // Recurse to finish writing all data. | 909 // Recurse to finish writing all data. |
910 int total_bytes_written = 0, total_bytes_read = 0; | 910 int total_bytes_written = 0, total_bytes_read = 0; |
911 std::string data_read; | 911 std::string data_read; |
912 TestWriteReadCompletionCallback callback( | 912 TestWriteReadCompletionCallback callback( |
913 stream_, &total_bytes_written, &total_bytes_read, &data_read); | 913 stream_, &total_bytes_written, &total_bytes_read, &data_read); |
914 rv = stream_->Write(drainable_, drainable_->BytesRemaining(), | 914 rv = stream_->Write(drainable_.get(), drainable_->BytesRemaining(), |
915 callback.callback()); | 915 callback.callback()); |
916 DCHECK_EQ(ERR_IO_PENDING, rv); | 916 DCHECK_EQ(ERR_IO_PENDING, rv); |
917 rv = callback.WaitForResult(); | 917 rv = callback.WaitForResult(); |
918 drainable_->DidConsume(total_bytes_written); | 918 drainable_->DidConsume(total_bytes_written); |
919 *total_bytes_written_ += total_bytes_written; | 919 *total_bytes_written_ += total_bytes_written; |
920 *total_bytes_read_ += total_bytes_read; | 920 *total_bytes_read_ += total_bytes_read; |
921 *data_read_ += data_read; | 921 *data_read_ += data_read; |
922 } else { // We're done writing all data. Start reading the data. | 922 } else { // We're done writing all data. Start reading the data. |
923 stream_->SeekSync(FROM_BEGIN, 0); | 923 stream_->SeekSync(FROM_BEGIN, 0); |
924 | 924 |
925 TestCompletionCallback callback; | 925 TestCompletionCallback callback; |
926 for (;;) { | 926 for (;;) { |
927 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 927 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
928 rv = stream_->Read(buf, buf->size(), callback.callback()); | 928 rv = stream_->Read(buf.get(), buf->size(), callback.callback()); |
929 if (rv == ERR_IO_PENDING) { | 929 if (rv == ERR_IO_PENDING) { |
930 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 930 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
931 rv = callback.WaitForResult(); | 931 rv = callback.WaitForResult(); |
932 } | 932 } |
933 EXPECT_LE(0, rv); | 933 EXPECT_LE(0, rv); |
934 if (rv <= 0) | 934 if (rv <= 0) |
935 break; | 935 break; |
936 *total_bytes_read_ += rv; | 936 *total_bytes_read_ += rv; |
937 data_read_->append(buf->data(), rv); | 937 data_read_->append(buf->data(), rv); |
938 } | 938 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 int64 offset = stream.SeekSync(FROM_END, 0); | 977 int64 offset = stream.SeekSync(FROM_END, 0); |
978 EXPECT_EQ(offset, file_size); | 978 EXPECT_EQ(offset, file_size); |
979 | 979 |
980 int total_bytes_written = 0; | 980 int total_bytes_written = 0; |
981 int total_bytes_read = 0; | 981 int total_bytes_read = 0; |
982 std::string data_read; | 982 std::string data_read; |
983 TestWriteReadCompletionCallback callback(&stream, &total_bytes_written, | 983 TestWriteReadCompletionCallback callback(&stream, &total_bytes_written, |
984 &total_bytes_read, &data_read); | 984 &total_bytes_read, &data_read); |
985 | 985 |
986 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 986 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
987 rv = stream.Write(buf, buf->size(), callback.callback()); | 987 rv = stream.Write(buf.get(), buf->size(), callback.callback()); |
988 if (rv == ERR_IO_PENDING) | 988 if (rv == ERR_IO_PENDING) |
989 rv = callback.WaitForResult(); | 989 rv = callback.WaitForResult(); |
990 EXPECT_LT(0, rv); | 990 EXPECT_LT(0, rv); |
991 EXPECT_EQ(kTestDataSize, total_bytes_written); | 991 EXPECT_EQ(kTestDataSize, total_bytes_written); |
992 | 992 |
993 stream.CloseSync(); | 993 stream.CloseSync(); |
994 | 994 |
995 ok = file_util::GetFileSize(temp_file_path(), &file_size); | 995 ok = file_util::GetFileSize(temp_file_path(), &file_size); |
996 EXPECT_TRUE(ok); | 996 EXPECT_TRUE(ok); |
997 EXPECT_EQ(kTestDataSize * 2, file_size); | 997 EXPECT_EQ(kTestDataSize * 2, file_size); |
998 | 998 |
999 EXPECT_EQ(kTestDataSize * 2, total_bytes_read); | 999 EXPECT_EQ(kTestDataSize * 2, total_bytes_read); |
1000 const std::string kExpectedFileData = | 1000 const std::string kExpectedFileData = |
1001 std::string(kTestData) + std::string(kTestData); | 1001 std::string(kTestData) + std::string(kTestData); |
1002 EXPECT_EQ(kExpectedFileData, data_read); | 1002 EXPECT_EQ(kExpectedFileData, data_read); |
1003 } | 1003 } |
1004 | 1004 |
1005 class TestWriteCloseCompletionCallback { | 1005 class TestWriteCloseCompletionCallback { |
1006 public: | 1006 public: |
1007 TestWriteCloseCompletionCallback(FileStream* stream, int* total_bytes_written) | 1007 TestWriteCloseCompletionCallback(FileStream* stream, int* total_bytes_written) |
1008 : result_(0), | 1008 : result_(0), |
1009 have_result_(false), | 1009 have_result_(false), |
1010 waiting_for_result_(false), | 1010 waiting_for_result_(false), |
1011 stream_(stream), | 1011 stream_(stream), |
1012 total_bytes_written_(total_bytes_written), | 1012 total_bytes_written_(total_bytes_written), |
1013 callback_(base::Bind(&TestWriteCloseCompletionCallback::OnComplete, | 1013 callback_(base::Bind(&TestWriteCloseCompletionCallback::OnComplete, |
1014 base::Unretained(this))), | 1014 base::Unretained(this))), |
1015 test_data_(CreateTestDataBuffer()), | 1015 test_data_(CreateTestDataBuffer()), |
1016 drainable_(new DrainableIOBuffer(test_data_, kTestDataSize)) { | 1016 drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) { |
1017 } | 1017 } |
1018 | 1018 |
1019 int WaitForResult() { | 1019 int WaitForResult() { |
1020 DCHECK(!waiting_for_result_); | 1020 DCHECK(!waiting_for_result_); |
1021 while (!have_result_) { | 1021 while (!have_result_) { |
1022 waiting_for_result_ = true; | 1022 waiting_for_result_ = true; |
1023 MessageLoop::current()->Run(); | 1023 MessageLoop::current()->Run(); |
1024 waiting_for_result_ = false; | 1024 waiting_for_result_ = false; |
1025 } | 1025 } |
1026 have_result_ = false; // auto-reset for next callback | 1026 have_result_ = false; // auto-reset for next callback |
1027 return result_; | 1027 return result_; |
1028 } | 1028 } |
1029 | 1029 |
1030 const CompletionCallback& callback() const { return callback_; } | 1030 const CompletionCallback& callback() const { return callback_; } |
1031 | 1031 |
1032 private: | 1032 private: |
1033 void OnComplete(int result) { | 1033 void OnComplete(int result) { |
1034 DCHECK_LT(0, result); | 1034 DCHECK_LT(0, result); |
1035 *total_bytes_written_ += result; | 1035 *total_bytes_written_ += result; |
1036 | 1036 |
1037 int rv; | 1037 int rv; |
1038 | 1038 |
1039 if (*total_bytes_written_ != kTestDataSize) { | 1039 if (*total_bytes_written_ != kTestDataSize) { |
1040 // Recurse to finish writing all data. | 1040 // Recurse to finish writing all data. |
1041 int total_bytes_written = 0; | 1041 int total_bytes_written = 0; |
1042 TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written); | 1042 TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written); |
1043 rv = stream_->Write(drainable_, drainable_->BytesRemaining(), | 1043 rv = stream_->Write(drainable_.get(), drainable_->BytesRemaining(), |
1044 callback.callback()); | 1044 callback.callback()); |
1045 DCHECK_EQ(ERR_IO_PENDING, rv); | 1045 DCHECK_EQ(ERR_IO_PENDING, rv); |
1046 rv = callback.WaitForResult(); | 1046 rv = callback.WaitForResult(); |
1047 drainable_->DidConsume(total_bytes_written); | 1047 drainable_->DidConsume(total_bytes_written); |
1048 *total_bytes_written_ += total_bytes_written; | 1048 *total_bytes_written_ += total_bytes_written; |
1049 } else { // We're done writing all data. Close the file. | 1049 } else { // We're done writing all data. Close the file. |
1050 stream_->CloseSync(); | 1050 stream_->CloseSync(); |
1051 } | 1051 } |
1052 | 1052 |
1053 result_ = *total_bytes_written_; | 1053 result_ = *total_bytes_written_; |
(...skipping 30 matching lines...) Expand all Loading... |
1084 int64 total_bytes_avail = stream.Available(); | 1084 int64 total_bytes_avail = stream.Available(); |
1085 EXPECT_EQ(file_size, total_bytes_avail); | 1085 EXPECT_EQ(file_size, total_bytes_avail); |
1086 | 1086 |
1087 int64 offset = stream.SeekSync(FROM_END, 0); | 1087 int64 offset = stream.SeekSync(FROM_END, 0); |
1088 EXPECT_EQ(offset, file_size); | 1088 EXPECT_EQ(offset, file_size); |
1089 | 1089 |
1090 int total_bytes_written = 0; | 1090 int total_bytes_written = 0; |
1091 TestWriteCloseCompletionCallback callback(&stream, &total_bytes_written); | 1091 TestWriteCloseCompletionCallback callback(&stream, &total_bytes_written); |
1092 | 1092 |
1093 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 1093 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
1094 rv = stream.Write(buf, buf->size(), callback.callback()); | 1094 rv = stream.Write(buf.get(), buf->size(), callback.callback()); |
1095 if (rv == ERR_IO_PENDING) | 1095 if (rv == ERR_IO_PENDING) |
1096 total_bytes_written = callback.WaitForResult(); | 1096 total_bytes_written = callback.WaitForResult(); |
1097 EXPECT_LT(0, total_bytes_written); | 1097 EXPECT_LT(0, total_bytes_written); |
1098 EXPECT_EQ(kTestDataSize, total_bytes_written); | 1098 EXPECT_EQ(kTestDataSize, total_bytes_written); |
1099 | 1099 |
1100 ok = file_util::GetFileSize(temp_file_path(), &file_size); | 1100 ok = file_util::GetFileSize(temp_file_path(), &file_size); |
1101 EXPECT_TRUE(ok); | 1101 EXPECT_TRUE(ok); |
1102 EXPECT_EQ(kTestDataSize * 2, file_size); | 1102 EXPECT_EQ(kTestDataSize * 2, file_size); |
1103 } | 1103 } |
1104 | 1104 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1190 base::PLATFORM_FILE_WRITE | | 1190 base::PLATFORM_FILE_WRITE | |
1191 base::PLATFORM_FILE_ASYNC; | 1191 base::PLATFORM_FILE_ASYNC; |
1192 TestCompletionCallback callback; | 1192 TestCompletionCallback callback; |
1193 int rv = stream.Open(temp_file_path(), flags, callback.callback()); | 1193 int rv = stream.Open(temp_file_path(), flags, callback.callback()); |
1194 EXPECT_EQ(ERR_IO_PENDING, rv); | 1194 EXPECT_EQ(ERR_IO_PENDING, rv); |
1195 EXPECT_EQ(OK, callback.WaitForResult()); | 1195 EXPECT_EQ(OK, callback.WaitForResult()); |
1196 EXPECT_TRUE(stream.IsOpen()); | 1196 EXPECT_TRUE(stream.IsOpen()); |
1197 | 1197 |
1198 // Write some data asynchronously. | 1198 // Write some data asynchronously. |
1199 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 1199 scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); |
1200 stream.Write(buf, buf->size(), callback.callback()); | 1200 stream.Write(buf.get(), buf->size(), callback.callback()); |
1201 | 1201 |
1202 // Close the stream without waiting for the completion. | 1202 // Close the stream without waiting for the completion. |
1203 stream.CloseSync(); | 1203 stream.CloseSync(); |
1204 } | 1204 } |
1205 | 1205 |
1206 // TODO(satorux): This should be gone once all once all async clients are | 1206 // TODO(satorux): This should be gone once all once all async clients are |
1207 // migrated to use Close(). crbug.com/114783 | 1207 // migrated to use Close(). crbug.com/114783 |
1208 TEST_F(FileStreamTest, AsyncOpenAndCloseSync) { | 1208 TEST_F(FileStreamTest, AsyncOpenAndCloseSync) { |
1209 FileStream stream(NULL); | 1209 FileStream stream(NULL); |
1210 int flags = base::PLATFORM_FILE_OPEN | | 1210 int flags = base::PLATFORM_FILE_OPEN | |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1253 // Delete the stream without waiting for the close operation to be | 1253 // Delete the stream without waiting for the close operation to be |
1254 // complete. Should be safe. | 1254 // complete. Should be safe. |
1255 stream.reset(); | 1255 stream.reset(); |
1256 // close_callback won't be called. | 1256 // close_callback won't be called. |
1257 EXPECT_FALSE(close_callback.have_result()); | 1257 EXPECT_FALSE(close_callback.have_result()); |
1258 } | 1258 } |
1259 | 1259 |
1260 } // namespace | 1260 } // namespace |
1261 | 1261 |
1262 } // namespace net | 1262 } // namespace net |
OLD | NEW |