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

Side by Side Diff: net/disk_cache/entry_unittest.cc

Issue 2874833005: SimpleCache: read small files all at once. (Closed)
Patch Set: Add some metrics and an experiment knob. Not really happy with coverage, though. Created 3 years, 5 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
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 <utility> 5 #include <utility>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 void DoomNormalEntry(); 67 void DoomNormalEntry();
68 void DoomEntryNextToOpenEntry(); 68 void DoomEntryNextToOpenEntry();
69 void DoomedEntry(int stream_index); 69 void DoomedEntry(int stream_index);
70 void BasicSparseIO(); 70 void BasicSparseIO();
71 void HugeSparseIO(); 71 void HugeSparseIO();
72 void GetAvailableRange(); 72 void GetAvailableRange();
73 void CouldBeSparse(); 73 void CouldBeSparse();
74 void UpdateSparseEntry(); 74 void UpdateSparseEntry();
75 void DoomSparseEntry(); 75 void DoomSparseEntry();
76 void PartialSparseEntry(); 76 void PartialSparseEntry();
77 bool SimpleCacheMakeBadChecksumEntry(const std::string& key, int* data_size); 77 bool SimpleCacheMakeBadChecksumEntry(const std::string& key, int data_size);
78 bool SimpleCacheThirdStreamFileExists(const char* key); 78 bool SimpleCacheThirdStreamFileExists(const char* key);
79 void SyncDoomEntry(const char* key); 79 void SyncDoomEntry(const char* key);
80 }; 80 };
81 81
82 // This part of the test runs on the background thread. 82 // This part of the test runs on the background thread.
83 void DiskCacheEntryTest::InternalSyncIOBackground(disk_cache::Entry* entry) { 83 void DiskCacheEntryTest::InternalSyncIOBackground(disk_cache::Entry* entry) {
84 const int kSize1 = 10; 84 const int kSize1 = 10;
85 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); 85 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
86 CacheTestFillBuffer(buffer1->data(), kSize1, false); 86 CacheTestFillBuffer(buffer1->data(), kSize1, false);
87 EXPECT_EQ( 87 EXPECT_EQ(
(...skipping 2536 matching lines...) Expand 10 before | Expand all | Expand 10 after
2624 // it on a doomed entry, if it was previously lazily omitted. 2624 // it on a doomed entry, if it was previously lazily omitted.
2625 for (int i = 0; i < disk_cache::kSimpleEntryStreamCount - 1; ++i) { 2625 for (int i = 0; i < disk_cache::kSimpleEntryStreamCount - 1; ++i) {
2626 EXPECT_THAT(DoomAllEntries(), IsOk()); 2626 EXPECT_THAT(DoomAllEntries(), IsOk());
2627 DoomedEntry(i); 2627 DoomedEntry(i);
2628 } 2628 }
2629 } 2629 }
2630 2630
2631 // Creates an entry with corrupted last byte in stream 0. 2631 // Creates an entry with corrupted last byte in stream 0.
2632 // Requires SimpleCacheMode. 2632 // Requires SimpleCacheMode.
2633 bool DiskCacheEntryTest::SimpleCacheMakeBadChecksumEntry(const std::string& key, 2633 bool DiskCacheEntryTest::SimpleCacheMakeBadChecksumEntry(const std::string& key,
2634 int* data_size) { 2634 int data_size) {
2635 disk_cache::Entry* entry = NULL; 2635 disk_cache::Entry* entry = NULL;
2636 2636
2637 if (CreateEntry(key, &entry) != net::OK || !entry) { 2637 if (CreateEntry(key, &entry) != net::OK || !entry) {
2638 LOG(ERROR) << "Could not create entry"; 2638 LOG(ERROR) << "Could not create entry";
2639 return false; 2639 return false;
2640 } 2640 }
2641 2641
2642 const char data[] = "this is very good data"; 2642 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(data_size));
2643 const int kDataSize = arraysize(data); 2643 memset(buffer->data(), 'A', data_size);
2644 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kDataSize));
2645 base::strlcpy(buffer->data(), data, kDataSize);
2646 2644
2647 EXPECT_EQ(kDataSize, WriteData(entry, 1, 0, buffer.get(), kDataSize, false)); 2645 EXPECT_EQ(data_size, WriteData(entry, 1, 0, buffer.get(), data_size, false));
2648 entry->Close(); 2646 entry->Close();
2649 entry = NULL; 2647 entry = NULL;
2650 2648
2651 // Corrupt the last byte of the data. 2649 // Corrupt the last byte of the data.
2652 base::FilePath entry_file0_path = cache_path_.AppendASCII( 2650 base::FilePath entry_file0_path = cache_path_.AppendASCII(
2653 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); 2651 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
2654 base::File entry_file0(entry_file0_path, 2652 base::File entry_file0(entry_file0_path,
2655 base::File::FLAG_WRITE | base::File::FLAG_OPEN); 2653 base::File::FLAG_WRITE | base::File::FLAG_OPEN);
2656 if (!entry_file0.IsValid()) 2654 if (!entry_file0.IsValid())
2657 return false; 2655 return false;
2658 2656
2659 int64_t file_offset = 2657 int64_t file_offset =
2660 sizeof(disk_cache::SimpleFileHeader) + key.size() + kDataSize - 2; 2658 sizeof(disk_cache::SimpleFileHeader) + key.size() + data_size - 2;
2661 EXPECT_EQ(1, entry_file0.Write(file_offset, "X", 1)); 2659 EXPECT_EQ(1, entry_file0.Write(file_offset, "X", 1));
2662 *data_size = kDataSize;
2663 return true; 2660 return true;
2664 } 2661 }
2665 2662
2666 // Tests that the simple cache can detect entries that have bad data. 2663 // Tests that the simple cache can detect entries that have bad data.
2664 TEST_F(DiskCacheEntryTest, SimpleCacheBadChecksumSmall) {
2665 base::HistogramTester histogram_tester;
2666 SetSimpleCacheMode();
2667 InitCache();
2668
2669 const char key[] = "the first key";
2670 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, 10));
2671
2672 disk_cache::Entry* entry = NULL;
2673
2674 // Open the entry. Since we made a small entry, we will detect the CRC
2675 // problem at open.
2676 EXPECT_THAT(OpenEntry(key, &entry), IsError(net::ERR_FAILED));
2677 }
2678
2667 TEST_F(DiskCacheEntryTest, SimpleCacheBadChecksum) { 2679 TEST_F(DiskCacheEntryTest, SimpleCacheBadChecksum) {
2668 base::HistogramTester histogram_tester; 2680 base::HistogramTester histogram_tester;
2669 SetSimpleCacheMode(); 2681 SetSimpleCacheMode();
2670 InitCache(); 2682 InitCache();
2671 2683
2672 const char key[] = "the first key"; 2684 const char key[] = "the first key";
2673 int size_unused; 2685 const int kLargeSize = 50000;
2674 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size_unused)); 2686 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, kLargeSize));
2675 2687
2676 disk_cache::Entry* entry = NULL; 2688 disk_cache::Entry* entry = NULL;
2677 2689
2678 // Open the entry. 2690 // Open the entry. Can't spot the checksum that quickly with it so
2691 // huge.
2679 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); 2692 ASSERT_THAT(OpenEntry(key, &entry), IsOk());
2680 ScopedEntryPtr entry_closer(entry); 2693 ScopedEntryPtr entry_closer(entry);
2681 2694
2682 const int kReadBufferSize = 200; 2695 EXPECT_GE(kLargeSize, entry->GetDataSize(1));
2683 EXPECT_GE(kReadBufferSize, entry->GetDataSize(1)); 2696 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kLargeSize));
2684 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize));
2685 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH, 2697 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH,
2686 ReadData(entry, 1, 0, read_buffer.get(), kReadBufferSize)); 2698 ReadData(entry, 1, 0, read_buffer.get(), kLargeSize));
2687 histogram_tester.ExpectUniqueSample( 2699 histogram_tester.ExpectUniqueSample(
2688 "SimpleCache.Http.ReadResult", 2700 "SimpleCache.Http.ReadResult",
2689 disk_cache::READ_RESULT_SYNC_CHECKSUM_FAILURE, 1); 2701 disk_cache::READ_RESULT_SYNC_CHECKSUM_FAILURE, 1);
2690 } 2702 }
2691 2703
2692 // Tests that an entry that has had an IO error occur can still be Doomed(). 2704 // Tests that an entry that has had an IO error occur can still be Doomed().
2693 TEST_F(DiskCacheEntryTest, SimpleCacheErrorThenDoom) { 2705 TEST_F(DiskCacheEntryTest, SimpleCacheErrorThenDoom) {
2694 base::HistogramTester histogram_tester; 2706 base::HistogramTester histogram_tester;
2695 SetSimpleCacheMode(); 2707 SetSimpleCacheMode();
2696 InitCache(); 2708 InitCache();
2697 2709
2698 const char key[] = "the first key"; 2710 const char key[] = "the first key";
2699 int size_unused; 2711 const int kLargeSize = 50000;
2700 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size_unused)); 2712 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, kLargeSize));
2701 2713
2702 disk_cache::Entry* entry = NULL; 2714 disk_cache::Entry* entry = NULL;
2703 2715
2704 // Open the entry, forcing an IO error. 2716 // Open the entry, forcing an IO error.
2705 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); 2717 ASSERT_THAT(OpenEntry(key, &entry), IsOk());
2706 ScopedEntryPtr entry_closer(entry); 2718 ScopedEntryPtr entry_closer(entry);
2707 2719
2708 const int kReadBufferSize = 200; 2720 EXPECT_GE(kLargeSize, entry->GetDataSize(1));
2709 EXPECT_GE(kReadBufferSize, entry->GetDataSize(1)); 2721 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kLargeSize));
2710 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize));
2711 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH, 2722 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH,
2712 ReadData(entry, 1, 0, read_buffer.get(), kReadBufferSize)); 2723 ReadData(entry, 1, 0, read_buffer.get(), kLargeSize));
2713 histogram_tester.ExpectUniqueSample( 2724 histogram_tester.ExpectUniqueSample(
2714 "SimpleCache.Http.ReadResult", 2725 "SimpleCache.Http.ReadResult",
2715 disk_cache::READ_RESULT_SYNC_CHECKSUM_FAILURE, 1); 2726 disk_cache::READ_RESULT_SYNC_CHECKSUM_FAILURE, 1);
2716 entry->Doom(); // Should not crash. 2727 entry->Doom(); // Should not crash.
2717 } 2728 }
2718 2729
2719 bool TruncatePath(const base::FilePath& file_path, int64_t length) { 2730 bool TruncatePath(const base::FilePath& file_path, int64_t length) {
2720 base::File file(file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN); 2731 base::File file(file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN);
2721 if (!file.IsValid()) 2732 if (!file.IsValid())
2722 return false; 2733 return false;
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after
3438 3449
3439 // Tests that if a read and a following in-flight truncate are both in progress 3450 // Tests that if a read and a following in-flight truncate are both in progress
3440 // simultaniously that they both can occur successfully. See 3451 // simultaniously that they both can occur successfully. See
3441 // http://crbug.com/239223 3452 // http://crbug.com/239223
3442 TEST_F(DiskCacheEntryTest, SimpleCacheInFlightTruncate) { 3453 TEST_F(DiskCacheEntryTest, SimpleCacheInFlightTruncate) {
3443 SetSimpleCacheMode(); 3454 SetSimpleCacheMode();
3444 InitCache(); 3455 InitCache();
3445 3456
3446 const char key[] = "the first key"; 3457 const char key[] = "the first key";
3447 3458
3448 const int kBufferSize = 1024; 3459 const int kBufferSize = 50000; // to avoid quick read
3449 scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize)); 3460 scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize));
3450 CacheTestFillBuffer(write_buffer->data(), kBufferSize, false); 3461 CacheTestFillBuffer(write_buffer->data(), kBufferSize, false);
3451 3462
3452 disk_cache::Entry* entry = NULL; 3463 disk_cache::Entry* entry = NULL;
3453 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); 3464 ASSERT_THAT(CreateEntry(key, &entry), IsOk());
3454 3465
3455 EXPECT_EQ(kBufferSize, 3466 EXPECT_EQ(kBufferSize,
3456 WriteData(entry, 1, 0, write_buffer.get(), kBufferSize, false)); 3467 WriteData(entry, 1, 0, write_buffer.get(), kBufferSize, false));
3457 entry->Close(); 3468 entry->Close();
3458 entry = NULL; 3469 entry = NULL;
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
3569 } 3580 }
3570 3581
3571 // Checking one more scenario of overlapped reading of a bad entry. 3582 // Checking one more scenario of overlapped reading of a bad entry.
3572 // Differs from the |SimpleCacheMultipleReadersCheckCRC| only by the order of 3583 // Differs from the |SimpleCacheMultipleReadersCheckCRC| only by the order of
3573 // last two reads. 3584 // last two reads.
3574 TEST_F(DiskCacheEntryTest, SimpleCacheMultipleReadersCheckCRC2) { 3585 TEST_F(DiskCacheEntryTest, SimpleCacheMultipleReadersCheckCRC2) {
3575 SetSimpleCacheMode(); 3586 SetSimpleCacheMode();
3576 InitCache(); 3587 InitCache();
3577 3588
3578 const char key[] = "key"; 3589 const char key[] = "key";
3579 int size; 3590 int size = 50000;
3580 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size)); 3591 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, size));
3581 3592
3582 scoped_refptr<net::IOBuffer> read_buffer1(new net::IOBuffer(size)); 3593 scoped_refptr<net::IOBuffer> read_buffer1(new net::IOBuffer(size));
3583 scoped_refptr<net::IOBuffer> read_buffer2(new net::IOBuffer(size)); 3594 scoped_refptr<net::IOBuffer> read_buffer2(new net::IOBuffer(size));
3584 3595
3585 // Advance the first reader a little. 3596 // Advance the first reader a little.
3586 disk_cache::Entry* entry = NULL; 3597 disk_cache::Entry* entry = NULL;
3587 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); 3598 ASSERT_THAT(OpenEntry(key, &entry), IsOk());
3588 ScopedEntryPtr entry_closer(entry); 3599 ScopedEntryPtr entry_closer(entry);
3589 EXPECT_EQ(1, ReadData(entry, 1, 0, read_buffer1.get(), 1)); 3600 EXPECT_EQ(1, ReadData(entry, 1, 0, read_buffer1.get(), 1));
3590 3601
(...skipping 724 matching lines...) Expand 10 before | Expand all | Expand 10 after
4315 // CHECK(temp_dir_.CreateUniqueTempDir()); 4326 // CHECK(temp_dir_.CreateUniqueTempDir());
4316 // cache_path_ = temp_dir_.GetPath(); 4327 // cache_path_ = temp_dir_.GetPath();
4317 disk_cache::DeleteCache(cache_path_, 4328 disk_cache::DeleteCache(cache_path_,
4318 true /* delete the dir, what we really want*/); 4329 true /* delete the dir, what we really want*/);
4319 4330
4320 disk_cache::Entry* entry; 4331 disk_cache::Entry* entry;
4321 std::string key("a key"); 4332 std::string key("a key");
4322 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); 4333 ASSERT_THAT(CreateEntry(key, &entry), IsOk());
4323 entry->Close(); 4334 entry->Close();
4324 } 4335 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698