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

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

Issue 15203004: Disk cache: Reference CL for the implementation of file format version 3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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/disk_cache/backend_impl.cc ('k') | net/disk_cache/block_files.h » ('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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/port.h" 7 #include "base/port.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
(...skipping 27 matching lines...) Expand all
38 class DiskCacheBackendTest : public DiskCacheTestWithCache { 38 class DiskCacheBackendTest : public DiskCacheTestWithCache {
39 protected: 39 protected:
40 void BackendBasics(); 40 void BackendBasics();
41 void BackendKeying(); 41 void BackendKeying();
42 void BackendShutdownWithPendingFileIO(bool fast); 42 void BackendShutdownWithPendingFileIO(bool fast);
43 void BackendShutdownWithPendingIO(bool fast); 43 void BackendShutdownWithPendingIO(bool fast);
44 void BackendShutdownWithPendingCreate(bool fast); 44 void BackendShutdownWithPendingCreate(bool fast);
45 void BackendSetSize(); 45 void BackendSetSize();
46 void BackendLoad(); 46 void BackendLoad();
47 void BackendChain(); 47 void BackendChain();
48 void BucketUse();
49 void BackendNewEvictionTrim();
48 void BackendValidEntry(); 50 void BackendValidEntry();
49 void BackendInvalidEntry(); 51 void BackendInvalidEntry();
50 void BackendInvalidEntryRead(); 52 void BackendInvalidEntryRead();
51 void BackendInvalidEntryWithLoad(); 53 void BackendInvalidEntryWithLoad();
52 void BackendTrimInvalidEntry(); 54 void BackendTrimInvalidEntry();
53 void BackendTrimInvalidEntry2(); 55 void BackendTrimInvalidEntry2();
54 void BackendEnumerations(); 56 void BackendEnumerations();
55 void BackendEnumerations2(); 57 void BackendEnumerations2();
56 void BackendInvalidEntryEnumeration(); 58 void BackendInvalidEntryEnumeration();
57 void BackendFixEnumerators(); 59 void BackendFixEnumerators();
(...skipping 18 matching lines...) Expand all
76 void BackendInvalidEntry11(bool eviction); 78 void BackendInvalidEntry11(bool eviction);
77 void BackendTrimInvalidEntry12(); 79 void BackendTrimInvalidEntry12();
78 void BackendDoomAll(); 80 void BackendDoomAll();
79 void BackendDoomAll2(); 81 void BackendDoomAll2();
80 void BackendInvalidRankings(); 82 void BackendInvalidRankings();
81 void BackendInvalidRankings2(); 83 void BackendInvalidRankings2();
82 void BackendDisable(); 84 void BackendDisable();
83 void BackendDisable2(); 85 void BackendDisable2();
84 void BackendDisable3(); 86 void BackendDisable3();
85 void BackendDisable4(); 87 void BackendDisable4();
88 void BackendTotalBuffersSize1();
89 void BackendTotalBuffersSize2();
90 void BackendUpdateRankForExternalCacheHit();
86 void TracingBackendBasics(); 91 void TracingBackendBasics();
87 }; 92 };
88 93
89 void DiskCacheBackendTest::BackendBasics() { 94 void DiskCacheBackendTest::BackendBasics() {
90 InitCache(); 95 InitCache();
91 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; 96 disk_cache::Entry *entry1 = NULL, *entry2 = NULL;
92 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); 97 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1));
93 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry1)); 98 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry1));
94 ASSERT_TRUE(NULL != entry1); 99 ASSERT_TRUE(NULL != entry1);
95 entry1->Close(); 100 entry1->Close();
96 entry1 = NULL; 101 entry1 = NULL;
97 102
98 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); 103 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1));
99 ASSERT_TRUE(NULL != entry1); 104 ASSERT_TRUE(NULL != entry1);
100 entry1->Close(); 105 entry1->Close();
101 entry1 = NULL; 106 entry1 = NULL;
102 107
103 EXPECT_NE(net::OK, CreateEntry("the first key", &entry1)); 108 EXPECT_NE(net::OK, CreateEntry("the first key", &entry1));
104 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); 109 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1));
105 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2)); 110 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2));
106 ASSERT_EQ(net::OK, CreateEntry("some other key", &entry2)); 111 ASSERT_EQ(net::OK, CreateEntry("some other key", &entry2));
107 ASSERT_TRUE(NULL != entry1); 112 ASSERT_TRUE(NULL != entry1);
108 ASSERT_TRUE(NULL != entry2); 113 ASSERT_TRUE(NULL != entry2);
109 EXPECT_EQ(2, cache_->GetEntryCount()); 114 EXPECT_EQ(2, cache_->GetEntryCount());
110 115
111 disk_cache::Entry* entry3 = NULL; 116 disk_cache::Entry* entry3 = NULL;
112 ASSERT_EQ(net::OK, OpenEntry("some other key", &entry3)); 117 ASSERT_EQ(net::OK, OpenEntry("some other key", &entry3));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 TEST_F(DiskCacheBackendTest, AppCacheBasics) { 154 TEST_F(DiskCacheBackendTest, AppCacheBasics) {
150 SetCacheType(net::APP_CACHE); 155 SetCacheType(net::APP_CACHE);
151 BackendBasics(); 156 BackendBasics();
152 } 157 }
153 158
154 TEST_F(DiskCacheBackendTest, ShaderCacheBasics) { 159 TEST_F(DiskCacheBackendTest, ShaderCacheBasics) {
155 SetCacheType(net::SHADER_CACHE); 160 SetCacheType(net::SHADER_CACHE);
156 BackendBasics(); 161 BackendBasics();
157 } 162 }
158 163
164 TEST_F(DiskCacheBackendTest, V3Basics) {
165 UseVersion3();
166 BackendBasics();
167 }
168
159 void DiskCacheBackendTest::BackendKeying() { 169 void DiskCacheBackendTest::BackendKeying() {
160 InitCache(); 170 InitCache();
161 const char* kName1 = "the first key"; 171 const char* kName1 = "the first key";
162 const char* kName2 = "the first Key"; 172 const char* kName2 = "the first Key";
163 disk_cache::Entry *entry1, *entry2; 173 disk_cache::Entry *entry1, *entry2;
164 ASSERT_EQ(net::OK, CreateEntry(kName1, &entry1)); 174 ASSERT_EQ(net::OK, CreateEntry(kName1, &entry1));
165 175
166 ASSERT_EQ(net::OK, CreateEntry(kName2, &entry2)); 176 ASSERT_EQ(net::OK, CreateEntry(kName2, &entry2));
167 EXPECT_TRUE(entry1 != entry2) << "Case sensitive"; 177 EXPECT_TRUE(entry1 != entry2) << "Case sensitive";
168 entry2->Close(); 178 entry2->Close();
(...skipping 25 matching lines...) Expand all
194 buffer2[19999] = '\0'; 204 buffer2[19999] = '\0';
195 ASSERT_EQ(net::OK, CreateEntry(buffer2, &entry2)) << "key on external file"; 205 ASSERT_EQ(net::OK, CreateEntry(buffer2, &entry2)) << "key on external file";
196 entry2->Close(); 206 entry2->Close();
197 entry1->Close(); 207 entry1->Close();
198 } 208 }
199 209
200 TEST_F(DiskCacheBackendTest, Keying) { 210 TEST_F(DiskCacheBackendTest, Keying) {
201 BackendKeying(); 211 BackendKeying();
202 } 212 }
203 213
214 TEST_F(DiskCacheBackendTest, V3Keying) {
215 UseVersion3();
216 BackendKeying();
217 }
218
204 TEST_F(DiskCacheBackendTest, NewEvictionKeying) { 219 TEST_F(DiskCacheBackendTest, NewEvictionKeying) {
205 SetNewEviction(); 220 SetNewEviction();
206 BackendKeying(); 221 BackendKeying();
207 } 222 }
208 223
224 TEST_F(DiskCacheBackendTest, V3NewEvictionKeying) {
225 UseVersion3();
226 SetNewEviction();
227 BackendKeying();
228 }
229
209 TEST_F(DiskCacheBackendTest, MemoryOnlyKeying) { 230 TEST_F(DiskCacheBackendTest, MemoryOnlyKeying) {
210 SetMemoryOnlyMode(); 231 SetMemoryOnlyMode();
211 BackendKeying(); 232 BackendKeying();
212 } 233 }
213 234
214 TEST_F(DiskCacheBackendTest, AppCacheKeying) { 235 TEST_F(DiskCacheBackendTest, AppCacheKeying) {
215 SetCacheType(net::APP_CACHE); 236 SetCacheType(net::APP_CACHE);
216 BackendKeying(); 237 BackendKeying();
217 } 238 }
218 239
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 void DiskCacheBackendTest::BackendShutdownWithPendingFileIO(bool fast) { 329 void DiskCacheBackendTest::BackendShutdownWithPendingFileIO(bool fast) {
309 net::TestCompletionCallback cb; 330 net::TestCompletionCallback cb;
310 int rv; 331 int rv;
311 332
312 { 333 {
313 ASSERT_TRUE(CleanupCacheDir()); 334 ASSERT_TRUE(CleanupCacheDir());
314 base::Thread cache_thread("CacheThread"); 335 base::Thread cache_thread("CacheThread");
315 ASSERT_TRUE(cache_thread.StartWithOptions( 336 ASSERT_TRUE(cache_thread.StartWithOptions(
316 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 337 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
317 338
318 uint32 flags = disk_cache::kNoBuffering; 339 if (fast)
319 if (!fast) 340 AvoidTestFlag();
320 flags |= disk_cache::kNoRandom;
321 341
322 UseCurrentThread(); 342 UseCurrentThread();
323 CreateBackend(flags, NULL); 343 CreateBackend(NULL);
344 SetNoBuffering();
324 345
325 disk_cache::EntryImpl* entry; 346 disk_cache::EntryImpl* entry;
326 rv = cache_->CreateEntry( 347 rv = cache_->CreateEntry(
327 "some key", reinterpret_cast<disk_cache::Entry**>(&entry), 348 "some key", reinterpret_cast<disk_cache::Entry**>(&entry),
328 cb.callback()); 349 cb.callback());
329 ASSERT_EQ(net::OK, cb.GetResult(rv)); 350 ASSERT_EQ(net::OK, cb.GetResult(rv));
330 351
331 const int kSize = 25000; 352 const int kSize = 25000;
332 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 353 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
333 CacheTestFillBuffer(buffer->data(), kSize, false); 354 CacheTestFillBuffer(buffer->data(), kSize, false);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 // Tests that we deal with background-thread pending operations. 406 // Tests that we deal with background-thread pending operations.
386 void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) { 407 void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) {
387 net::TestCompletionCallback cb; 408 net::TestCompletionCallback cb;
388 409
389 { 410 {
390 ASSERT_TRUE(CleanupCacheDir()); 411 ASSERT_TRUE(CleanupCacheDir());
391 base::Thread cache_thread("CacheThread"); 412 base::Thread cache_thread("CacheThread");
392 ASSERT_TRUE(cache_thread.StartWithOptions( 413 ASSERT_TRUE(cache_thread.StartWithOptions(
393 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 414 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
394 415
395 uint32 flags = disk_cache::kNoBuffering; 416 if (fast)
396 if (!fast) 417 AvoidTestFlag();
397 flags |= disk_cache::kNoRandom;
398 418
399 CreateBackend(flags, &cache_thread); 419 CreateBackend(&cache_thread);
420 SetNoBuffering();
400 421
401 disk_cache::Entry* entry; 422 disk_cache::Entry* entry;
402 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); 423 int rv = cache_->CreateEntry("some key", &entry, cb.callback());
403 ASSERT_EQ(net::OK, cb.GetResult(rv)); 424 ASSERT_EQ(net::OK, cb.GetResult(rv));
404 425
405 entry->Close(); 426 entry->Close();
406 427
407 // The cache destructor will see one pending operation here. 428 // The cache destructor will see one pending operation here.
408 delete cache_; 429 delete cache_;
409 // Prevent the TearDown() to delete the backend again. 430 // Prevent the TearDown() to delete the backend again.
(...skipping 18 matching lines...) Expand all
428 // Tests that we deal with create-type pending operations. 449 // Tests that we deal with create-type pending operations.
429 void DiskCacheBackendTest::BackendShutdownWithPendingCreate(bool fast) { 450 void DiskCacheBackendTest::BackendShutdownWithPendingCreate(bool fast) {
430 net::TestCompletionCallback cb; 451 net::TestCompletionCallback cb;
431 452
432 { 453 {
433 ASSERT_TRUE(CleanupCacheDir()); 454 ASSERT_TRUE(CleanupCacheDir());
434 base::Thread cache_thread("CacheThread"); 455 base::Thread cache_thread("CacheThread");
435 ASSERT_TRUE(cache_thread.StartWithOptions( 456 ASSERT_TRUE(cache_thread.StartWithOptions(
436 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 457 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
437 458
438 disk_cache::BackendFlags flags = 459 if (fast)
439 fast ? disk_cache::kNone : disk_cache::kNoRandom; 460 AvoidTestFlag();
440 CreateBackend(flags, &cache_thread); 461 CreateBackend(&cache_thread);
441 462
442 disk_cache::Entry* entry; 463 disk_cache::Entry* entry;
443 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); 464 int rv = cache_->CreateEntry("some key", &entry, cb.callback());
444 ASSERT_EQ(net::ERR_IO_PENDING, rv); 465 ASSERT_EQ(net::ERR_IO_PENDING, rv);
445 466
446 delete cache_; 467 delete cache_;
447 // Prevent the TearDown() to delete the backend again. 468 // Prevent the TearDown() to delete the backend again.
448 cache_ = NULL; 469 cache_ = NULL;
449 EXPECT_FALSE(cb.have_result()); 470 EXPECT_FALSE(cb.have_result());
450 } 471 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 537
517 SetMaxSize(cache_size); 538 SetMaxSize(cache_size);
518 539
519 // The cache is 95% full. 540 // The cache is 95% full.
520 541
521 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 542 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
522 EXPECT_EQ(cache_size / 10, WriteData(entry, 0, 0, buffer, cache_size / 10, 543 EXPECT_EQ(cache_size / 10, WriteData(entry, 0, 0, buffer, cache_size / 10,
523 false)); 544 false));
524 545
525 disk_cache::Entry* entry2; 546 disk_cache::Entry* entry2;
526 ASSERT_EQ(net::OK, CreateEntry("an extra key", &entry2)); 547 std::string extra_key("an extra key");
548 ASSERT_EQ(net::OK, CreateEntry(extra_key, &entry2));
527 EXPECT_EQ(cache_size / 10, WriteData(entry2, 0, 0, buffer, cache_size / 10, 549 EXPECT_EQ(cache_size / 10, WriteData(entry2, 0, 0, buffer, cache_size / 10,
528 false)); 550 false));
529 entry2->Close(); // This will trigger the cache trim. 551 entry2->Close(); // This will trigger the cache trim.
530 552 WaitForEntryToClose(extra_key);
531 EXPECT_NE(net::OK, OpenEntry(first, &entry2));
532 553
533 FlushQueueForTest(); // Make sure that we are done trimming the cache. 554 FlushQueueForTest(); // Make sure that we are done trimming the cache.
534 FlushQueueForTest(); // We may have posted two tasks to evict stuff. 555 FlushQueueForTest(); // We may have posted two tasks to evict stuff.
535 556
536 entry->Close(); 557 entry->Close();
558 WaitForEntryToClose(second);
559
560 EXPECT_NE(net::OK, OpenEntry(first, &entry2));
537 ASSERT_EQ(net::OK, OpenEntry(second, &entry)); 561 ASSERT_EQ(net::OK, OpenEntry(second, &entry));
538 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0)); 562 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0));
539 entry->Close(); 563 entry->Close();
540 } 564 }
541 565
542 TEST_F(DiskCacheBackendTest, SetSize) { 566 TEST_F(DiskCacheBackendTest, SetSize) {
543 BackendSetSize(); 567 BackendSetSize();
544 } 568 }
545 569
570 TEST_F(DiskCacheBackendTest, V3SetSize) {
571 UseVersion3();
572 UseVersion3();
573 SetNewEviction();
574 BackendSetSize();
575 }
576
546 TEST_F(DiskCacheBackendTest, NewEvictionSetSize) { 577 TEST_F(DiskCacheBackendTest, NewEvictionSetSize) {
547 SetNewEviction(); 578 SetNewEviction();
548 BackendSetSize(); 579 BackendSetSize();
549 } 580 }
550 581
582 TEST_F(DiskCacheBackendTest, V3NewEvictionSetSize) {
583 UseVersion3();
584 SetNewEviction();
585 BackendSetSize();
586 }
587
551 TEST_F(DiskCacheBackendTest, MemoryOnlySetSize) { 588 TEST_F(DiskCacheBackendTest, MemoryOnlySetSize) {
552 SetMemoryOnlyMode(); 589 SetMemoryOnlyMode();
553 BackendSetSize(); 590 BackendSetSize();
554 } 591 }
555 592
556 void DiskCacheBackendTest::BackendLoad() { 593 void DiskCacheBackendTest::BackendLoad() {
557 InitCache(); 594 InitCache();
558 int seed = static_cast<int>(Time::Now().ToInternalValue()); 595 int seed = static_cast<int>(Time::Now().ToInternalValue());
559 srand(seed); 596 srand(seed);
560 597
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 TEST_F(DiskCacheBackendTest, AppCacheChain) { 684 TEST_F(DiskCacheBackendTest, AppCacheChain) {
648 SetCacheType(net::APP_CACHE); 685 SetCacheType(net::APP_CACHE);
649 BackendChain(); 686 BackendChain();
650 } 687 }
651 688
652 TEST_F(DiskCacheBackendTest, ShaderCacheChain) { 689 TEST_F(DiskCacheBackendTest, ShaderCacheChain) {
653 SetCacheType(net::SHADER_CACHE); 690 SetCacheType(net::SHADER_CACHE);
654 BackendChain(); 691 BackendChain();
655 } 692 }
656 693
657 TEST_F(DiskCacheBackendTest, NewEvictionTrim) { 694 // Tests the proper use of cell buckets while the index grow.
695 void DiskCacheBackendTest::BucketUse() {
696 UseVersion3();
697 InitCache();
698 const int kNumEntries = 20;
699 disk_cache::Entry* entries[kNumEntries];
700 std::string key("The first key");
701
702 // This generates kNumEntries collisions so the extra table has to be used.
703 // There are only 2 extra buckets on the test setup, so the index has to grow.
704 for (int i = 0; i < kNumEntries; i++) {
705 ASSERT_EQ(net::OK, CreateEntry(key, &entries[i]));
706 entries[i]->Doom();
707 FlushQueueForTest();
708 }
709
710 for (int i = 0; i < kNumEntries / 2; i++)
711 entries[i]->Close();
712
713 for (int i = 0; i < kNumEntries / 2; i++) {
714 ASSERT_EQ(net::OK, CreateEntry(key, &entries[i]));
715 entries[i]->Doom();
716 }
717
718 for (int i = 0; i < kNumEntries; i++)
719 entries[i]->Close();
720 }
721
722 TEST_F(DiskCacheBackendTest, V3BucketUse) {
723 BucketUse();
724 }
725
726 TEST_F(DiskCacheBackendTest, V3DoubleIndexBucketUse) {
727 PresetTestMode(); // Doubles the index instead of a slow growth.
728 BucketUse();
729 }
730
731 TEST_F(DiskCacheBackendTest, V3DoubleIndex) {
732 UseVersion3();
733 PresetTestMode();
734 InitCache();
735
736 disk_cache::Entry* entry;
737 const int kNumEntries = 200;
738 for (int i = 0; i < kNumEntries; i++) {
739 std::string name(base::StringPrintf("Key %d", i));
740 ASSERT_EQ(net::OK, CreateEntry(name, &entry));
741 entry->Close();
742 FlushQueueForTest();
743 }
744
745 // Generate enough collisions to force growing the extra table (and with it,
746 // the index).
747 const int kNumSavedEntries = 5;
748 disk_cache::Entry* entries[kNumSavedEntries];
749 std::string key("The first key");
750 for (int i = 0; i < kNumSavedEntries; i++) {
751 ASSERT_EQ(net::OK, CreateEntry(key, &entries[i]));
752 entries[i]->Doom();
753 FlushQueueForTest();
754 }
755
756 for (int i = 0; i < kNumSavedEntries; i++)
757 entries[i]->Close();
758
759 // Verify that all entries are there.
760 for (int i = 0; i < kNumEntries; i++) {
761 std::string name(base::StringPrintf("Key %d", i));
762 ASSERT_EQ(net::OK, OpenEntry(name, &entry));
763 entry->Close();
764 }
765 }
766
767 // This test leaks memory because it simulates a crash.
768 TEST_F(DiskCacheBackendTest, DISABLED_V3Backup) {
769 UseVersion3();
770 PresetTestMode();
771 InitCache();
772
773 disk_cache::Entry* entry;
774 std::string name1("First entry");
775 ASSERT_EQ(net::OK, CreateEntry(name1, &entry));
776 entry->Close();
777 WaitForEntryToClose(name1);
778 AddDelayForTest(40);
779
780 std::string name2("Another entry");
781 ASSERT_EQ(net::OK, CreateEntry(name2, &entry));
782
783 SimulateCrash();
784 EXPECT_EQ(net::OK, OpenEntry(name1, &entry));
785 entry->Close();
786 EXPECT_NE(net::OK, OpenEntry(name2, &entry));
787 }
788
789 void DiskCacheBackendTest::BackendNewEvictionTrim() {
658 SetNewEviction(); 790 SetNewEviction();
659 InitCache(); 791 InitCache();
660 792
661 disk_cache::Entry* entry; 793 disk_cache::Entry* entry;
662 for (int i = 0; i < 100; i++) { 794 for (int i = 0; i < 100; i++) {
663 std::string name(base::StringPrintf("Key %d", i)); 795 std::string name(base::StringPrintf("Key %d", i));
664 ASSERT_EQ(net::OK, CreateEntry(name, &entry)); 796 ASSERT_EQ(net::OK, CreateEntry(name, &entry));
665 entry->Close(); 797 entry->Close();
666 if (i < 90) { 798 if (i < 90) {
667 // Entries 0 to 89 are in list 1; 90 to 99 are in list 0. 799 // Entries 0 to 89 are in list 1; 90 to 99 are in list 0.
668 ASSERT_EQ(net::OK, OpenEntry(name, &entry)); 800 ASSERT_EQ(net::OK, OpenEntry(name, &entry));
669 entry->Close(); 801 entry->Close();
670 } 802 }
803 if (!(i % 10))
804 FlushQueueForTest();
805
806 if (i == 0 || i == 90) {
807 WaitForEntryToClose(name);
808 AddDelayForTest(70);
809 }
671 } 810 }
672 811
673 // The first eviction must come from list 1 (10% limit), the second must come 812 // The first eviction must come from list 1 (10% limit), the second must come
674 // from list 0. 813 // from list 0.
675 TrimForTest(false); 814 TrimForTest(false);
676 EXPECT_NE(net::OK, OpenEntry("Key 0", &entry)); 815 EXPECT_NE(net::OK, OpenEntry("Key 0", &entry));
677 TrimForTest(false); 816 TrimForTest(false);
678 EXPECT_NE(net::OK, OpenEntry("Key 90", &entry)); 817 EXPECT_NE(net::OK, OpenEntry("Key 90", &entry));
679 818
680 // Double check that we still have the list tails. 819 // Double check that we still have the list tails.
681 ASSERT_EQ(net::OK, OpenEntry("Key 1", &entry)); 820 ASSERT_EQ(net::OK, OpenEntry("Key 1", &entry));
682 entry->Close(); 821 entry->Close();
683 ASSERT_EQ(net::OK, OpenEntry("Key 91", &entry)); 822 ASSERT_EQ(net::OK, OpenEntry("Key 91", &entry));
684 entry->Close(); 823 entry->Close();
685 } 824 }
686 825
826 TEST_F(DiskCacheBackendTest, NewEvictionTrim) {
827 BackendNewEvictionTrim();
828 }
829
830 TEST_F(DiskCacheBackendTest, V3NewEvictionTrim) {
831 UseVersion3();
832 BackendNewEvictionTrim();
833 }
834
687 // Before looking for invalid entries, let's check a valid entry. 835 // Before looking for invalid entries, let's check a valid entry.
688 void DiskCacheBackendTest::BackendValidEntry() { 836 void DiskCacheBackendTest::BackendValidEntry() {
689 InitCache(); 837 InitCache();
690 838
691 std::string key("Some key"); 839 std::string key("Some key");
692 disk_cache::Entry* entry; 840 disk_cache::Entry* entry;
693 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 841 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
694 842
695 const int kSize = 50; 843 const int kSize = 50;
696 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 844 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
1011 Time initial = Time::Now(); 1159 Time initial = Time::Now();
1012 int seed = static_cast<int>(initial.ToInternalValue()); 1160 int seed = static_cast<int>(initial.ToInternalValue());
1013 srand(seed); 1161 srand(seed);
1014 1162
1015 const int kNumEntries = 100; 1163 const int kNumEntries = 100;
1016 for (int i = 0; i < kNumEntries; i++) { 1164 for (int i = 0; i < kNumEntries; i++) {
1017 std::string key = GenerateKey(true); 1165 std::string key = GenerateKey(true);
1018 disk_cache::Entry* entry; 1166 disk_cache::Entry* entry;
1019 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 1167 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1020 entry->Close(); 1168 entry->Close();
1169 if (!(i % 10))
1170 FlushQueueForTest();
1021 } 1171 }
1022 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); 1172 EXPECT_EQ(kNumEntries, cache_->GetEntryCount());
1023 Time final = Time::Now(); 1173 Time final = Time::Now();
1024 1174
1025 disk_cache::Entry* entry; 1175 disk_cache::Entry* entry;
1026 void* iter = NULL; 1176 void* iter = NULL;
1027 int count = 0; 1177 int count = 0;
1028 Time last_modified[kNumEntries]; 1178 Time last_modified[kNumEntries];
1029 Time last_used[kNumEntries]; 1179 Time last_used[kNumEntries];
1030 while (OpenNextEntry(&iter, &entry) == net::OK) { 1180 while (OpenNextEntry(&iter, &entry) == net::OK) {
(...skipping 22 matching lines...) Expand all
1053 entry->Close(); 1203 entry->Close();
1054 count++; 1204 count++;
1055 }; 1205 };
1056 EXPECT_EQ(kNumEntries, count); 1206 EXPECT_EQ(kNumEntries, count);
1057 } 1207 }
1058 1208
1059 TEST_F(DiskCacheBackendTest, Enumerations) { 1209 TEST_F(DiskCacheBackendTest, Enumerations) {
1060 BackendEnumerations(); 1210 BackendEnumerations();
1061 } 1211 }
1062 1212
1213 TEST_F(DiskCacheBackendTest, V3Enumerations) {
1214 UseVersion3();
1215 BackendEnumerations();
1216 }
1217
1063 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations) { 1218 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations) {
1064 SetNewEviction(); 1219 SetNewEviction();
1065 BackendEnumerations(); 1220 BackendEnumerations();
1066 } 1221 }
1067 1222
1223 TEST_F(DiskCacheBackendTest, V3NewEvictionEnumerations) {
1224 UseVersion3();
1225 SetNewEviction();
1226 BackendEnumerations();
1227 }
1228
1068 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations) { 1229 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations) {
1069 SetMemoryOnlyMode(); 1230 SetMemoryOnlyMode();
1070 BackendEnumerations(); 1231 BackendEnumerations();
1071 } 1232 }
1072 1233
1073 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerations) { 1234 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerations) {
1074 SetCacheType(net::SHADER_CACHE); 1235 SetCacheType(net::SHADER_CACHE);
1075 BackendEnumerations(); 1236 BackendEnumerations();
1076 } 1237 }
1077 1238
1078 TEST_F(DiskCacheBackendTest, AppCacheEnumerations) { 1239 TEST_F(DiskCacheBackendTest, AppCacheEnumerations) {
1079 SetCacheType(net::APP_CACHE); 1240 SetCacheType(net::APP_CACHE);
1080 BackendEnumerations(); 1241 BackendEnumerations();
1081 } 1242 }
1082 1243
1083 // Verifies enumerations while entries are open. 1244 // Verifies enumerations while entries are open.
1084 void DiskCacheBackendTest::BackendEnumerations2() { 1245 void DiskCacheBackendTest::BackendEnumerations2() {
1085 InitCache(); 1246 InitCache();
1086 const std::string first("first"); 1247 const std::string first("first");
1087 const std::string second("second"); 1248 const std::string second("second");
1088 disk_cache::Entry *entry1, *entry2; 1249 disk_cache::Entry *entry1, *entry2;
1089 ASSERT_EQ(net::OK, CreateEntry(first, &entry1)); 1250 ASSERT_EQ(net::OK, CreateEntry(first, &entry1));
1090 entry1->Close(); 1251 entry1->Close();
1091 ASSERT_EQ(net::OK, CreateEntry(second, &entry2)); 1252 ASSERT_EQ(net::OK, CreateEntry(second, &entry2));
1092 entry2->Close(); 1253 entry2->Close();
1093 FlushQueueForTest(); 1254 FlushQueueForTest();
1094 1255
1095 // Make sure that the timestamp is not the same. 1256 // Make sure that the timestamp is not the same.
1096 AddDelay(); 1257 AddDelayForTest(70);
1097 ASSERT_EQ(net::OK, OpenEntry(second, &entry1)); 1258 ASSERT_EQ(net::OK, OpenEntry(second, &entry1));
1098 void* iter = NULL; 1259 void* iter = NULL;
1099 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); 1260 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2));
1100 EXPECT_EQ(entry2->GetKey(), second); 1261 EXPECT_EQ(entry2->GetKey(), second);
1101 1262
1102 // Two entries and the iterator pointing at "first". 1263 // Two entries and the iterator pointing at "first".
1103 entry1->Close(); 1264 entry1->Close();
1104 entry2->Close(); 1265 entry2->Close();
1105 1266
1106 // The iterator should still be valid, so we should not crash. 1267 // The iterator should still be valid, so we should not crash.
(...skipping 15 matching lines...) Expand all
1122 1283
1123 entry1->Close(); 1284 entry1->Close();
1124 entry2->Close(); 1285 entry2->Close();
1125 cache_->EndEnumeration(&iter); 1286 cache_->EndEnumeration(&iter);
1126 } 1287 }
1127 1288
1128 TEST_F(DiskCacheBackendTest, Enumerations2) { 1289 TEST_F(DiskCacheBackendTest, Enumerations2) {
1129 BackendEnumerations2(); 1290 BackendEnumerations2();
1130 } 1291 }
1131 1292
1293 TEST_F(DiskCacheBackendTest, V3Enumerations2) {
1294 UseVersion3();
1295 BackendEnumerations2();
1296 }
1297
1132 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations2) { 1298 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations2) {
1133 SetNewEviction(); 1299 SetNewEviction();
1134 BackendEnumerations2(); 1300 BackendEnumerations2();
1135 } 1301 }
1136 1302
1303 TEST_F(DiskCacheBackendTest, V3NewEvictionEnumerations2) {
1304 UseVersion3();
1305 SetNewEviction();
1306 BackendEnumerations2();
1307 }
1308
1137 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations2) { 1309 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations2) {
1138 SetMemoryOnlyMode(); 1310 SetMemoryOnlyMode();
1139 BackendEnumerations2(); 1311 BackendEnumerations2();
1140 } 1312 }
1141 1313
1142 TEST_F(DiskCacheBackendTest, AppCacheEnumerations2) { 1314 TEST_F(DiskCacheBackendTest, AppCacheEnumerations2) {
1143 SetCacheType(net::APP_CACHE); 1315 SetCacheType(net::APP_CACHE);
1144 BackendEnumerations2(); 1316 BackendEnumerations2();
1145 } 1317 }
1146 1318
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
1300 void DiskCacheBackendTest::BackendDoomRecent() { 1472 void DiskCacheBackendTest::BackendDoomRecent() {
1301 InitCache(); 1473 InitCache();
1302 1474
1303 disk_cache::Entry *entry; 1475 disk_cache::Entry *entry;
1304 ASSERT_EQ(net::OK, CreateEntry("first", &entry)); 1476 ASSERT_EQ(net::OK, CreateEntry("first", &entry));
1305 entry->Close(); 1477 entry->Close();
1306 ASSERT_EQ(net::OK, CreateEntry("second", &entry)); 1478 ASSERT_EQ(net::OK, CreateEntry("second", &entry));
1307 entry->Close(); 1479 entry->Close();
1308 FlushQueueForTest(); 1480 FlushQueueForTest();
1309 1481
1310 AddDelay(); 1482 AddDelayForTest(70);
1311 Time middle = Time::Now(); 1483 Time middle = GetCurrentTime();
1312 1484
1313 ASSERT_EQ(net::OK, CreateEntry("third", &entry)); 1485 ASSERT_EQ(net::OK, CreateEntry("third", &entry));
1314 entry->Close(); 1486 entry->Close();
1315 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry)); 1487 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry));
1316 entry->Close(); 1488 entry->Close();
1317 FlushQueueForTest(); 1489 FlushQueueForTest();
1318 1490
1319 AddDelay(); 1491 AddDelayForTest(70);
1320 Time final = Time::Now(); 1492 Time final = GetCurrentTime();
1321 1493
1322 ASSERT_EQ(4, cache_->GetEntryCount()); 1494 ASSERT_EQ(4, cache_->GetEntryCount());
1323 EXPECT_EQ(net::OK, DoomEntriesSince(final)); 1495 EXPECT_EQ(net::OK, DoomEntriesSince(final));
1324 ASSERT_EQ(4, cache_->GetEntryCount()); 1496 ASSERT_EQ(4, cache_->GetEntryCount());
1325 1497
1326 EXPECT_EQ(net::OK, DoomEntriesSince(middle)); 1498 EXPECT_EQ(net::OK, DoomEntriesSince(middle));
1327 ASSERT_EQ(2, cache_->GetEntryCount()); 1499 ASSERT_EQ(2, cache_->GetEntryCount());
1328 1500
1329 ASSERT_EQ(net::OK, OpenEntry("second", &entry)); 1501 ASSERT_EQ(net::OK, OpenEntry("second", &entry));
1330 entry->Close(); 1502 entry->Close();
1331 } 1503 }
1332 1504
1333 TEST_F(DiskCacheBackendTest, DoomRecent) { 1505 TEST_F(DiskCacheBackendTest, DoomRecent) {
1334 BackendDoomRecent(); 1506 BackendDoomRecent();
1335 } 1507 }
1336 1508
1509 TEST_F(DiskCacheBackendTest, V3DoomRecent) {
1510 UseVersion3();
1511 BackendDoomRecent();
1512 }
1513
1337 TEST_F(DiskCacheBackendTest, NewEvictionDoomRecent) { 1514 TEST_F(DiskCacheBackendTest, NewEvictionDoomRecent) {
1338 SetNewEviction(); 1515 SetNewEviction();
1339 BackendDoomRecent(); 1516 BackendDoomRecent();
1340 } 1517 }
1341 1518
1519 TEST_F(DiskCacheBackendTest, V3NewEvictionDoomRecent) {
1520 UseVersion3();
1521 SetNewEviction();
1522 BackendDoomRecent();
1523 }
1524
1342 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomRecent) { 1525 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomRecent) {
1343 SetMemoryOnlyMode(); 1526 SetMemoryOnlyMode();
1344 BackendDoomRecent(); 1527 BackendDoomRecent();
1345 } 1528 }
1346 1529
1347 void DiskCacheBackendTest::InitSparseCache(base::Time* doomed_start, 1530 void DiskCacheBackendTest::InitSparseCache(base::Time* doomed_start,
1348 base::Time* doomed_end) { 1531 base::Time* doomed_end) {
1349 InitCache(); 1532 InitCache();
1350 1533
1351 const int kSize = 50; 1534 const int kSize = 50;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1419 TEST_F(DiskCacheBackendTest, DoomEntriesSinceSparse) { 1602 TEST_F(DiskCacheBackendTest, DoomEntriesSinceSparse) {
1420 base::Time start; 1603 base::Time start;
1421 InitSparseCache(&start, NULL); 1604 InitSparseCache(&start, NULL);
1422 DoomEntriesSince(start); 1605 DoomEntriesSince(start);
1423 // NOTE: BackendImpl counts child entries in its GetEntryCount(), while 1606 // NOTE: BackendImpl counts child entries in its GetEntryCount(), while
1424 // MemBackendImpl does not. Thats why expected value differs here from 1607 // MemBackendImpl does not. Thats why expected value differs here from
1425 // MemoryOnlyDoomEntriesSinceSparse. 1608 // MemoryOnlyDoomEntriesSinceSparse.
1426 EXPECT_EQ(3, cache_->GetEntryCount()); 1609 EXPECT_EQ(3, cache_->GetEntryCount());
1427 } 1610 }
1428 1611
1612 TEST_F(DiskCacheBackendTest, V3DoomEntriesSinceSparse) {
1613 base::Time start;
1614 UseVersion3();
1615 InitSparseCache(&start, NULL);
1616 DoomEntriesSince(start);
1617 // NOTE: BackendImpl counts child entries in its GetEntryCount(), while
1618 // MemBackendImpl does not. Thats why expected value differs here from
1619 // MemoryOnlyDoomEntriesSinceSparse.
1620 EXPECT_EQ(3, cache_->GetEntryCount());
1621 }
1622
1429 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAllSparse) { 1623 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAllSparse) {
1430 SetMemoryOnlyMode(); 1624 SetMemoryOnlyMode();
1431 InitSparseCache(NULL, NULL); 1625 InitSparseCache(NULL, NULL);
1432 EXPECT_EQ(net::OK, DoomAllEntries()); 1626 EXPECT_EQ(net::OK, DoomAllEntries());
1433 EXPECT_EQ(0, cache_->GetEntryCount()); 1627 EXPECT_EQ(0, cache_->GetEntryCount());
1434 } 1628 }
1435 1629
1436 TEST_F(DiskCacheBackendTest, DoomAllSparse) { 1630 TEST_F(DiskCacheBackendTest, DoomAllSparse) {
1437 InitSparseCache(NULL, NULL); 1631 InitSparseCache(NULL, NULL);
1438 EXPECT_EQ(net::OK, DoomAllEntries()); 1632 EXPECT_EQ(net::OK, DoomAllEntries());
1439 EXPECT_EQ(0, cache_->GetEntryCount()); 1633 EXPECT_EQ(0, cache_->GetEntryCount());
1440 } 1634 }
1441 1635
1636 TEST_F(DiskCacheBackendTest, V3DoomAllSparse) {
1637 UseVersion3();
1638 InitSparseCache(NULL, NULL);
1639 EXPECT_EQ(net::OK, DoomAllEntries());
1640 EXPECT_EQ(0, cache_->GetEntryCount());
1641 }
1642
1442 void DiskCacheBackendTest::BackendDoomBetween() { 1643 void DiskCacheBackendTest::BackendDoomBetween() {
1443 InitCache(); 1644 InitCache();
1444 1645
1445 disk_cache::Entry *entry; 1646 disk_cache::Entry *entry;
1446 ASSERT_EQ(net::OK, CreateEntry("first", &entry)); 1647 ASSERT_EQ(net::OK, CreateEntry("first", &entry));
1447 entry->Close(); 1648 entry->Close();
1448 FlushQueueForTest(); 1649 FlushQueueForTest();
1449 1650
1450 AddDelay(); 1651 AddDelay();
1451 Time middle_start = Time::Now(); 1652 Time middle_start = Time::Now();
(...skipping 27 matching lines...) Expand all
1479 ASSERT_EQ(1, cache_->GetEntryCount()); 1680 ASSERT_EQ(1, cache_->GetEntryCount());
1480 1681
1481 ASSERT_EQ(net::OK, OpenEntry("first", &entry)); 1682 ASSERT_EQ(net::OK, OpenEntry("first", &entry));
1482 entry->Close(); 1683 entry->Close();
1483 } 1684 }
1484 1685
1485 TEST_F(DiskCacheBackendTest, DoomBetween) { 1686 TEST_F(DiskCacheBackendTest, DoomBetween) {
1486 BackendDoomBetween(); 1687 BackendDoomBetween();
1487 } 1688 }
1488 1689
1690 TEST_F(DiskCacheBackendTest, V3DoomBetween) {
1691 UseVersion3();
1692 BackendDoomBetween();
1693 }
1694
1489 TEST_F(DiskCacheBackendTest, NewEvictionDoomBetween) { 1695 TEST_F(DiskCacheBackendTest, NewEvictionDoomBetween) {
1490 SetNewEviction(); 1696 SetNewEviction();
1491 BackendDoomBetween(); 1697 BackendDoomBetween();
1492 } 1698 }
1493 1699
1700 TEST_F(DiskCacheBackendTest, V3NewEvictionDoomBetween) {
1701 UseVersion3();
1702 SetNewEviction();
1703 BackendDoomBetween();
1704 }
1705
1494 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomBetween) { 1706 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomBetween) {
1495 SetMemoryOnlyMode(); 1707 SetMemoryOnlyMode();
1496 BackendDoomBetween(); 1708 BackendDoomBetween();
1497 } 1709 }
1498 1710
1499 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomEntriesBetweenSparse) { 1711 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomEntriesBetweenSparse) {
1500 SetMemoryOnlyMode(); 1712 SetMemoryOnlyMode();
1501 base::Time start, end; 1713 base::Time start, end;
1502 InitSparseCache(&start, &end); 1714 InitSparseCache(&start, &end);
1503 DoomEntriesBetween(start, end); 1715 DoomEntriesBetween(start, end);
(...skipping 10 matching lines...) Expand all
1514 InitSparseCache(&start, &end); 1726 InitSparseCache(&start, &end);
1515 DoomEntriesBetween(start, end); 1727 DoomEntriesBetween(start, end);
1516 EXPECT_EQ(9, cache_->GetEntryCount()); 1728 EXPECT_EQ(9, cache_->GetEntryCount());
1517 1729
1518 start = end; 1730 start = end;
1519 end = base::Time::Now(); 1731 end = base::Time::Now();
1520 DoomEntriesBetween(start, end); 1732 DoomEntriesBetween(start, end);
1521 EXPECT_EQ(3, cache_->GetEntryCount()); 1733 EXPECT_EQ(3, cache_->GetEntryCount());
1522 } 1734 }
1523 1735
1736 TEST_F(DiskCacheBackendTest, V3DoomEntriesBetweenSparse) {
1737 base::Time start, end;
1738 UseVersion3();
1739 InitSparseCache(&start, &end);
1740 DoomEntriesBetween(start, end);
1741 EXPECT_EQ(9, cache_->GetEntryCount());
1742
1743 start = end;
1744 end = base::Time::Now();
1745 DoomEntriesBetween(start, end);
1746 EXPECT_EQ(3, cache_->GetEntryCount());
1747 }
1748
1524 void DiskCacheBackendTest::BackendTransaction(const std::string& name, 1749 void DiskCacheBackendTest::BackendTransaction(const std::string& name,
1525 int num_entries, bool load) { 1750 int num_entries, bool load) {
1526 success_ = false; 1751 success_ = false;
1527 ASSERT_TRUE(CopyTestCache(name)); 1752 ASSERT_TRUE(CopyTestCache(name));
1528 DisableFirstCleanup(); 1753 DisableFirstCleanup();
1529 1754
1530 uint32 mask; 1755 uint32 mask;
1531 if (load) { 1756 if (load) {
1532 mask = 0xf; 1757 mask = 0xf;
1533 SetMaxSize(0x100000); 1758 SetMaxSize(0x100000);
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after
2507 EXPECT_EQ(net::OK, DoomAllEntries()); 2732 EXPECT_EQ(net::OK, DoomAllEntries());
2508 ASSERT_EQ(0, cache_->GetEntryCount()); 2733 ASSERT_EQ(0, cache_->GetEntryCount());
2509 2734
2510 EXPECT_EQ(net::OK, DoomAllEntries()); 2735 EXPECT_EQ(net::OK, DoomAllEntries());
2511 } 2736 }
2512 2737
2513 TEST_F(DiskCacheBackendTest, DoomAll) { 2738 TEST_F(DiskCacheBackendTest, DoomAll) {
2514 BackendDoomAll(); 2739 BackendDoomAll();
2515 } 2740 }
2516 2741
2742 TEST_F(DiskCacheBackendTest, V3DoomAll) {
2743 UseVersion3();
2744 BackendDoomAll();
2745 }
2746
2517 TEST_F(DiskCacheBackendTest, NewEvictionDoomAll) { 2747 TEST_F(DiskCacheBackendTest, NewEvictionDoomAll) {
2518 SetNewEviction(); 2748 SetNewEviction();
2519 BackendDoomAll(); 2749 BackendDoomAll();
2520 } 2750 }
2521 2751
2752 TEST_F(DiskCacheBackendTest, V3NewEvictionDoomAll) {
2753 UseVersion3();
2754 SetNewEviction();
2755 BackendDoomAll();
2756 }
2757
2522 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) { 2758 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) {
2523 SetMemoryOnlyMode(); 2759 SetMemoryOnlyMode();
2524 BackendDoomAll(); 2760 BackendDoomAll();
2525 } 2761 }
2526 2762
2527 TEST_F(DiskCacheBackendTest, AppCacheOnlyDoomAll) { 2763 TEST_F(DiskCacheBackendTest, AppCacheOnlyDoomAll) {
2528 SetCacheType(net::APP_CACHE); 2764 SetCacheType(net::APP_CACHE);
2529 BackendDoomAll(); 2765 BackendDoomAll();
2530 } 2766 }
2531 2767
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
2655 InitCache(); 2891 InitCache();
2656 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro. 2892 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro.
2657 2893
2658 for (int i = 1; i < 3; i++) { 2894 for (int i = 1; i < 3; i++) {
2659 CACHE_UMA(HOURS, "FillupTime", i, 28); 2895 CACHE_UMA(HOURS, "FillupTime", i, 28);
2660 } 2896 }
2661 } 2897 }
2662 2898
2663 // Make sure that we keep the total memory used by the internal buffers under 2899 // Make sure that we keep the total memory used by the internal buffers under
2664 // control. 2900 // control.
2665 TEST_F(DiskCacheBackendTest, TotalBuffersSize1) { 2901 void DiskCacheBackendTest::BackendTotalBuffersSize1() {
2666 InitCache(); 2902 InitCache();
2667 std::string key("the first key"); 2903 std::string key("the first key");
2668 disk_cache::Entry* entry; 2904 disk_cache::Entry* entry;
2669 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 2905 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2670 2906
2671 const int kSize = 200; 2907 const int kSize = 200;
2672 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 2908 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
2673 CacheTestFillBuffer(buffer->data(), kSize, true); 2909 CacheTestFillBuffer(buffer->data(), kSize, true);
2674 2910
2675 for (int i = 0; i < 10; i++) { 2911 for (int i = 0; i < 10; i++) {
2676 SCOPED_TRACE(i); 2912 SCOPED_TRACE(i);
2677 // Allocate 2MB for this entry. 2913 // Allocate 2MB for this entry.
2678 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, true)); 2914 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, true));
2679 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer, kSize, true)); 2915 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer, kSize, true));
2680 EXPECT_EQ(kSize, WriteData(entry, 0, 1024 * 1024, buffer, kSize, false)); 2916 EXPECT_EQ(kSize, WriteData(entry, 0, 1024 * 1024, buffer, kSize, false));
2681 EXPECT_EQ(kSize, WriteData(entry, 1, 1024 * 1024, buffer, kSize, false)); 2917 EXPECT_EQ(kSize, WriteData(entry, 1, 1024 * 1024, buffer, kSize, false));
2682 2918
2683 // Delete one of the buffers and truncate the other. 2919 // Delete one of the buffers and truncate the other.
2684 EXPECT_EQ(0, WriteData(entry, 0, 0, buffer, 0, true)); 2920 EXPECT_EQ(0, WriteData(entry, 0, 0, buffer, 0, true));
2685 EXPECT_EQ(0, WriteData(entry, 1, 10, buffer, 0, true)); 2921 EXPECT_EQ(0, WriteData(entry, 1, 10, buffer, 0, true));
2686 2922
2687 // Delete the second buffer, writing 10 bytes to disk. 2923 // Delete the second buffer, writing 10 bytes to disk.
2688 entry->Close(); 2924 entry->Close();
2689 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 2925 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
2690 } 2926 }
2691 2927
2692 entry->Close(); 2928 entry->Close();
2693 EXPECT_EQ(0, cache_impl_->GetTotalBuffersSize()); 2929 EXPECT_EQ(0, GetTotalBuffersSize());
2930 }
2931
2932 TEST_F(DiskCacheBackendTest, TotalBuffersSize1) {
2933 BackendTotalBuffersSize1();
2934 }
2935
2936 TEST_F(DiskCacheBackendTest, V3TotalBuffersSize1) {
2937 UseVersion3();
2938 BackendTotalBuffersSize1();
2694 } 2939 }
2695 2940
2696 // This test assumes at least 150MB of system memory. 2941 // This test assumes at least 150MB of system memory.
2697 TEST_F(DiskCacheBackendTest, TotalBuffersSize2) { 2942 void DiskCacheBackendTest::BackendTotalBuffersSize2() {
2698 InitCache(); 2943 InitCache();
2699 2944
2700 const int kOneMB = 1024 * 1024; 2945 const int kOneMB = 1024 * 1024;
2701 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB)); 2946 EXPECT_TRUE(IsAllocAllowed(0, kOneMB));
2702 EXPECT_EQ(kOneMB, cache_impl_->GetTotalBuffersSize()); 2947 EXPECT_EQ(kOneMB, GetTotalBuffersSize());
2703 2948
2704 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB)); 2949 EXPECT_TRUE(IsAllocAllowed(0, kOneMB));
2705 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize()); 2950 EXPECT_EQ(kOneMB * 2, GetTotalBuffersSize());
2706 2951
2707 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB)); 2952 EXPECT_TRUE(IsAllocAllowed(0, kOneMB));
2708 EXPECT_EQ(kOneMB * 3, cache_impl_->GetTotalBuffersSize()); 2953 EXPECT_EQ(kOneMB * 3, GetTotalBuffersSize());
2709 2954
2710 cache_impl_->BufferDeleted(kOneMB); 2955 BufferDeleted(kOneMB);
2711 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize()); 2956 EXPECT_EQ(kOneMB * 2, GetTotalBuffersSize());
2712 2957
2713 // Check the upper limit. 2958 // Check the upper limit.
2714 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, 30 * kOneMB)); 2959 EXPECT_FALSE(IsAllocAllowed(0, 30 * kOneMB));
2715 2960
2716 for (int i = 0; i < 30; i++) 2961 for (int i = 0; i < 30; i++)
2717 cache_impl_->IsAllocAllowed(0, kOneMB); // Ignore the result. 2962 IsAllocAllowed(0, kOneMB); // Ignore the result.
2718 2963
2719 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, kOneMB)); 2964 EXPECT_FALSE(IsAllocAllowed(0, kOneMB));
2965 }
2966
2967 TEST_F(DiskCacheBackendTest, TotalBuffersSize2) {
2968 BackendTotalBuffersSize2();
2969 }
2970
2971 TEST_F(DiskCacheBackendTest, V3TotalBuffersSize2) {
2972 UseVersion3();
2973 BackendTotalBuffersSize2();
2720 } 2974 }
2721 2975
2722 // Tests that sharing of external files works and we are able to delete the 2976 // Tests that sharing of external files works and we are able to delete the
2723 // files when we need to. 2977 // files when we need to.
2724 TEST_F(DiskCacheBackendTest, FileSharing) { 2978 TEST_F(DiskCacheBackendTest, FileSharing) {
2725 InitCache(); 2979 InitCache();
2726 2980
2727 disk_cache::Addr address(0x80000001); 2981 disk_cache::Addr address(0x80000001);
2728 ASSERT_TRUE(cache_impl_->CreateExternalFile(&address)); 2982 ASSERT_TRUE(cache_impl_->CreateExternalFile(&address));
2729 base::FilePath name = cache_impl_->GetFileName(address); 2983 base::FilePath name = cache_impl_->GetFileName(address);
(...skipping 22 matching lines...) Expand all
2752 char buffer2[kSize]; 3006 char buffer2[kSize];
2753 memset(buffer1, 't', kSize); 3007 memset(buffer1, 't', kSize);
2754 memset(buffer2, 0, kSize); 3008 memset(buffer2, 0, kSize);
2755 EXPECT_TRUE(file->Write(buffer1, kSize, 0)); 3009 EXPECT_TRUE(file->Write(buffer1, kSize, 0));
2756 EXPECT_TRUE(file->Read(buffer2, kSize, 0)); 3010 EXPECT_TRUE(file->Read(buffer2, kSize, 0));
2757 EXPECT_EQ(0, memcmp(buffer1, buffer2, kSize)); 3011 EXPECT_EQ(0, memcmp(buffer1, buffer2, kSize));
2758 3012
2759 EXPECT_TRUE(disk_cache::DeleteCacheFile(name)); 3013 EXPECT_TRUE(disk_cache::DeleteCacheFile(name));
2760 } 3014 }
2761 3015
2762 TEST_F(DiskCacheBackendTest, UpdateRankForExternalCacheHit) { 3016 void DiskCacheBackendTest::BackendUpdateRankForExternalCacheHit() {
2763 InitCache(); 3017 InitCache();
2764 3018
2765 disk_cache::Entry* entry; 3019 disk_cache::Entry* entry;
2766 3020
2767 for (int i = 0; i < 2; ++i) { 3021 for (int i = 0; i < 2; ++i) {
2768 std::string key = base::StringPrintf("key%d", i); 3022 std::string key = base::StringPrintf("key%d", i);
2769 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 3023 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2770 entry->Close(); 3024 entry->Close();
3025 AddDelayForTest(70);
2771 } 3026 }
3027 FlushQueueForTest();
2772 3028
2773 // Ping the oldest entry. 3029 // Ping the oldest entry.
3030 SetTestMode();
2774 cache_->OnExternalCacheHit("key0"); 3031 cache_->OnExternalCacheHit("key0");
3032 FlushQueueForTest();
2775 3033
2776 TrimForTest(false); 3034 TrimForTest(false);
2777 3035
2778 // Make sure the older key remains. 3036 // Make sure the older key remains.
2779 EXPECT_EQ(1, cache_->GetEntryCount()); 3037 EXPECT_NE(net::OK, OpenEntry("key1", &entry));
2780 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); 3038 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
2781 entry->Close(); 3039 entry->Close();
2782 } 3040 }
2783 3041
3042 TEST_F(DiskCacheBackendTest, UpdateRankForExternalCacheHit) {
3043 BackendUpdateRankForExternalCacheHit();
3044 }
3045
3046 TEST_F(DiskCacheBackendTest, V3UpdateRankForExternalCacheHit) {
3047 UseVersion3();
3048 BackendUpdateRankForExternalCacheHit();
3049 }
3050
2784 TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) { 3051 TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) {
2785 SetCacheType(net::SHADER_CACHE); 3052 SetCacheType(net::SHADER_CACHE);
2786 InitCache(); 3053 BackendUpdateRankForExternalCacheHit();
3054 }
2787 3055
2788 disk_cache::Entry* entry; 3056 TEST_F(DiskCacheBackendTest, V3ShaderCacheUpdateRankForExternalCacheHit) {
2789 3057 UseVersion3();
2790 for (int i = 0; i < 2; ++i) { 3058 SetCacheType(net::SHADER_CACHE);
2791 std::string key = base::StringPrintf("key%d", i); 3059 BackendUpdateRankForExternalCacheHit();
2792 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2793 entry->Close();
2794 }
2795
2796 // Ping the oldest entry.
2797 cache_->OnExternalCacheHit("key0");
2798
2799 TrimForTest(false);
2800
2801 // Make sure the older key remains.
2802 EXPECT_EQ(1, cache_->GetEntryCount());
2803 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
2804 entry->Close();
2805 } 3060 }
2806 3061
2807 void DiskCacheBackendTest::TracingBackendBasics() { 3062 void DiskCacheBackendTest::TracingBackendBasics() {
2808 InitCache(); 3063 InitCache();
2809 cache_ = new disk_cache::TracingCacheBackend(cache_); 3064 cache_ = new disk_cache::TracingCacheBackend(cache_);
2810 cache_impl_ = NULL; 3065 cache_impl_ = NULL;
2811 EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType()); 3066 EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType());
2812 if (!simple_cache_mode_) { 3067 if (!simple_cache_mode_) {
2813 EXPECT_EQ(0, cache_->GetEntryCount()); 3068 EXPECT_EQ(0, cache_->GetEntryCount());
2814 } 3069 }
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
3020 NULL); 3275 NULL);
3021 cache->SetUnitTestMode(); 3276 cache->SetUnitTestMode();
3022 net::TestCompletionCallback cb; 3277 net::TestCompletionCallback cb;
3023 int rv = cache->Init(cb.callback()); 3278 int rv = cache->Init(cb.callback());
3024 EXPECT_NE(net::OK, cb.GetResult(rv)); 3279 EXPECT_NE(net::OK, cb.GetResult(rv));
3025 delete cache; 3280 delete cache;
3026 DisableIntegrityCheck(); 3281 DisableIntegrityCheck();
3027 } 3282 }
3028 3283
3029 #endif // !defined(OS_WIN) 3284 #endif // !defined(OS_WIN)
OLDNEW
« no previous file with comments | « net/disk_cache/backend_impl.cc ('k') | net/disk_cache/block_files.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698