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

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

Issue 12224017: Create a new disk_cache type, SHADER_CACHE. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Better fix for OnExternalCacheHit Created 7 years, 10 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/entry_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 TEST_F(DiskCacheBackendTest, MemoryOnlyBasics) { 131 TEST_F(DiskCacheBackendTest, MemoryOnlyBasics) {
132 SetMemoryOnlyMode(); 132 SetMemoryOnlyMode();
133 BackendBasics(); 133 BackendBasics();
134 } 134 }
135 135
136 TEST_F(DiskCacheBackendTest, AppCacheBasics) { 136 TEST_F(DiskCacheBackendTest, AppCacheBasics) {
137 SetCacheType(net::APP_CACHE); 137 SetCacheType(net::APP_CACHE);
138 BackendBasics(); 138 BackendBasics();
139 } 139 }
140 140
141 TEST_F(DiskCacheBackendTest, ShaderCacheBasics) {
142 SetCacheType(net::SHADER_CACHE);
143 BackendBasics();
144 }
145
141 void DiskCacheBackendTest::BackendKeying() { 146 void DiskCacheBackendTest::BackendKeying() {
142 InitCache(); 147 InitCache();
143 const char* kName1 = "the first key"; 148 const char* kName1 = "the first key";
144 const char* kName2 = "the first Key"; 149 const char* kName2 = "the first Key";
145 disk_cache::Entry *entry1, *entry2; 150 disk_cache::Entry *entry1, *entry2;
146 ASSERT_EQ(net::OK, CreateEntry(kName1, &entry1)); 151 ASSERT_EQ(net::OK, CreateEntry(kName1, &entry1));
147 152
148 ASSERT_EQ(net::OK, CreateEntry(kName2, &entry2)); 153 ASSERT_EQ(net::OK, CreateEntry(kName2, &entry2));
149 EXPECT_TRUE(entry1 != entry2) << "Case sensitive"; 154 EXPECT_TRUE(entry1 != entry2) << "Case sensitive";
150 entry2->Close(); 155 entry2->Close();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 TEST_F(DiskCacheBackendTest, MemoryOnlyKeying) { 196 TEST_F(DiskCacheBackendTest, MemoryOnlyKeying) {
192 SetMemoryOnlyMode(); 197 SetMemoryOnlyMode();
193 BackendKeying(); 198 BackendKeying();
194 } 199 }
195 200
196 TEST_F(DiskCacheBackendTest, AppCacheKeying) { 201 TEST_F(DiskCacheBackendTest, AppCacheKeying) {
197 SetCacheType(net::APP_CACHE); 202 SetCacheType(net::APP_CACHE);
198 BackendKeying(); 203 BackendKeying();
199 } 204 }
200 205
206 TEST_F(DiskCacheBackendTest, ShaderCacheKeying) {
207 SetCacheType(net::SHADER_CACHE);
208 BackendKeying();
209 }
210
201 TEST_F(DiskCacheTest, CreateBackend) { 211 TEST_F(DiskCacheTest, CreateBackend) {
202 net::TestCompletionCallback cb; 212 net::TestCompletionCallback cb;
203 213
204 { 214 {
205 ASSERT_TRUE(CleanupCacheDir()); 215 ASSERT_TRUE(CleanupCacheDir());
206 base::Thread cache_thread("CacheThread"); 216 base::Thread cache_thread("CacheThread");
207 ASSERT_TRUE(cache_thread.StartWithOptions( 217 ASSERT_TRUE(cache_thread.StartWithOptions(
208 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 218 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
209 219
210 // Test the private factory methods. 220 // Test the private factory methods.
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 } 595 }
586 596
587 TEST_F(DiskCacheBackendTest, AppCacheLoad) { 597 TEST_F(DiskCacheBackendTest, AppCacheLoad) {
588 SetCacheType(net::APP_CACHE); 598 SetCacheType(net::APP_CACHE);
589 // Work with a tiny index table (16 entries) 599 // Work with a tiny index table (16 entries)
590 SetMask(0xf); 600 SetMask(0xf);
591 SetMaxSize(0x100000); 601 SetMaxSize(0x100000);
592 BackendLoad(); 602 BackendLoad();
593 } 603 }
594 604
605 TEST_F(DiskCacheBackendTest, ShaderCacheLoad) {
606 SetCacheType(net::SHADER_CACHE);
607 // Work with a tiny index table (16 entries)
608 SetMask(0xf);
609 SetMaxSize(0x100000);
610 BackendLoad();
611 }
612
595 // Tests the chaining of an entry to the current head. 613 // Tests the chaining of an entry to the current head.
596 void DiskCacheBackendTest::BackendChain() { 614 void DiskCacheBackendTest::BackendChain() {
597 SetMask(0x1); // 2-entry table. 615 SetMask(0x1); // 2-entry table.
598 SetMaxSize(0x3000); // 12 kB. 616 SetMaxSize(0x3000); // 12 kB.
599 InitCache(); 617 InitCache();
600 618
601 disk_cache::Entry* entry; 619 disk_cache::Entry* entry;
602 ASSERT_EQ(net::OK, CreateEntry("The first key", &entry)); 620 ASSERT_EQ(net::OK, CreateEntry("The first key", &entry));
603 entry->Close(); 621 entry->Close();
604 ASSERT_EQ(net::OK, CreateEntry("The Second key", &entry)); 622 ASSERT_EQ(net::OK, CreateEntry("The Second key", &entry));
605 entry->Close(); 623 entry->Close();
606 } 624 }
607 625
608 TEST_F(DiskCacheBackendTest, Chain) { 626 TEST_F(DiskCacheBackendTest, Chain) {
609 BackendChain(); 627 BackendChain();
610 } 628 }
611 629
612 TEST_F(DiskCacheBackendTest, NewEvictionChain) { 630 TEST_F(DiskCacheBackendTest, NewEvictionChain) {
613 SetNewEviction(); 631 SetNewEviction();
614 BackendChain(); 632 BackendChain();
615 } 633 }
616 634
617 TEST_F(DiskCacheBackendTest, AppCacheChain) { 635 TEST_F(DiskCacheBackendTest, AppCacheChain) {
618 SetCacheType(net::APP_CACHE); 636 SetCacheType(net::APP_CACHE);
619 BackendChain(); 637 BackendChain();
620 } 638 }
621 639
640 TEST_F(DiskCacheBackendTest, ShaderCacheChain) {
641 SetCacheType(net::SHADER_CACHE);
642 BackendChain();
643 }
644
622 TEST_F(DiskCacheBackendTest, NewEvictionTrim) { 645 TEST_F(DiskCacheBackendTest, NewEvictionTrim) {
623 SetNewEviction(); 646 SetNewEviction();
624 SetDirectMode(); 647 SetDirectMode();
625 InitCache(); 648 InitCache();
626 649
627 disk_cache::Entry* entry; 650 disk_cache::Entry* entry;
628 for (int i = 0; i < 100; i++) { 651 for (int i = 0; i < 100; i++) {
629 std::string name(StringPrintf("Key %d", i)); 652 std::string name(StringPrintf("Key %d", i));
630 ASSERT_EQ(net::OK, CreateEntry(name, &entry)); 653 ASSERT_EQ(net::OK, CreateEntry(name, &entry));
631 entry->Close(); 654 entry->Close();
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 SetNewEviction(); 745 SetNewEviction();
723 BackendInvalidEntry(); 746 BackendInvalidEntry();
724 } 747 }
725 748
726 // We'll be leaking memory from this test. 749 // We'll be leaking memory from this test.
727 TEST_F(DiskCacheBackendTest, AppCacheInvalidEntry) { 750 TEST_F(DiskCacheBackendTest, AppCacheInvalidEntry) {
728 SetCacheType(net::APP_CACHE); 751 SetCacheType(net::APP_CACHE);
729 BackendInvalidEntry(); 752 BackendInvalidEntry();
730 } 753 }
731 754
755 // We'll be leaking memory from this test.
756 TEST_F(DiskCacheBackendTest, ShaderCacheInvalidEntry) {
757 SetCacheType(net::SHADER_CACHE);
758 BackendInvalidEntry();
759 }
760
732 // Almost the same test, but this time crash the cache after reading an entry. 761 // Almost the same test, but this time crash the cache after reading an entry.
733 // We'll be leaking memory from this test. 762 // We'll be leaking memory from this test.
734 void DiskCacheBackendTest::BackendInvalidEntryRead() { 763 void DiskCacheBackendTest::BackendInvalidEntryRead() {
735 // Use the implementation directly... we need to simulate a crash. 764 // Use the implementation directly... we need to simulate a crash.
736 SetDirectMode(); 765 SetDirectMode();
737 InitCache(); 766 InitCache();
738 767
739 std::string key("Some key"); 768 std::string key("Some key");
740 disk_cache::Entry* entry; 769 disk_cache::Entry* entry;
741 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 770 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 BackendInvalidEntryRead(); 802 BackendInvalidEntryRead();
774 } 803 }
775 804
776 // We'll be leaking memory from this test. 805 // We'll be leaking memory from this test.
777 TEST_F(DiskCacheBackendTest, AppCacheInvalidEntryRead) { 806 TEST_F(DiskCacheBackendTest, AppCacheInvalidEntryRead) {
778 SetCacheType(net::APP_CACHE); 807 SetCacheType(net::APP_CACHE);
779 BackendInvalidEntryRead(); 808 BackendInvalidEntryRead();
780 } 809 }
781 810
782 // We'll be leaking memory from this test. 811 // We'll be leaking memory from this test.
812 TEST_F(DiskCacheBackendTest, ShaderCacheInvalidEntryRead) {
813 SetCacheType(net::SHADER_CACHE);
814 BackendInvalidEntryRead();
815 }
816
817 // We'll be leaking memory from this test.
783 void DiskCacheBackendTest::BackendInvalidEntryWithLoad() { 818 void DiskCacheBackendTest::BackendInvalidEntryWithLoad() {
784 // Work with a tiny index table (16 entries) 819 // Work with a tiny index table (16 entries)
785 SetMask(0xf); 820 SetMask(0xf);
786 SetMaxSize(0x100000); 821 SetMaxSize(0x100000);
787 InitCache(); 822 InitCache();
788 823
789 int seed = static_cast<int>(Time::Now().ToInternalValue()); 824 int seed = static_cast<int>(Time::Now().ToInternalValue());
790 srand(seed); 825 srand(seed);
791 826
792 const int kNumEntries = 100; 827 const int kNumEntries = 100;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
839 BackendInvalidEntryWithLoad(); 874 BackendInvalidEntryWithLoad();
840 } 875 }
841 876
842 // We'll be leaking memory from this test. 877 // We'll be leaking memory from this test.
843 TEST_F(DiskCacheBackendTest, AppCacheInvalidEntryWithLoad) { 878 TEST_F(DiskCacheBackendTest, AppCacheInvalidEntryWithLoad) {
844 SetCacheType(net::APP_CACHE); 879 SetCacheType(net::APP_CACHE);
845 BackendInvalidEntryWithLoad(); 880 BackendInvalidEntryWithLoad();
846 } 881 }
847 882
848 // We'll be leaking memory from this test. 883 // We'll be leaking memory from this test.
884 TEST_F(DiskCacheBackendTest, ShaderCacheInvalidEntryWithLoad) {
885 SetCacheType(net::SHADER_CACHE);
886 BackendInvalidEntryWithLoad();
887 }
888
889 // We'll be leaking memory from this test.
849 void DiskCacheBackendTest::BackendTrimInvalidEntry() { 890 void DiskCacheBackendTest::BackendTrimInvalidEntry() {
850 // Use the implementation directly... we need to simulate a crash. 891 // Use the implementation directly... we need to simulate a crash.
851 SetDirectMode(); 892 SetDirectMode();
852 893
853 const int kSize = 0x3000; // 12 kB 894 const int kSize = 0x3000; // 12 kB
854 SetMaxSize(kSize * 10); 895 SetMaxSize(kSize * 10);
855 InitCache(); 896 InitCache();
856 897
857 std::string first("some key"); 898 std::string first("some key");
858 std::string second("something else"); 899 std::string second("something else");
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
1021 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations) { 1062 TEST_F(DiskCacheBackendTest, NewEvictionEnumerations) {
1022 SetNewEviction(); 1063 SetNewEviction();
1023 BackendEnumerations(); 1064 BackendEnumerations();
1024 } 1065 }
1025 1066
1026 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations) { 1067 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations) {
1027 SetMemoryOnlyMode(); 1068 SetMemoryOnlyMode();
1028 BackendEnumerations(); 1069 BackendEnumerations();
1029 } 1070 }
1030 1071
1072 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerations) {
1073 SetCacheType(net::SHADER_CACHE);
1074 BackendEnumerations();
1075 }
1076
1031 TEST_F(DiskCacheBackendTest, AppCacheEnumerations) { 1077 TEST_F(DiskCacheBackendTest, AppCacheEnumerations) {
1032 SetCacheType(net::APP_CACHE); 1078 SetCacheType(net::APP_CACHE);
1033 BackendEnumerations(); 1079 BackendEnumerations();
1034 } 1080 }
1035 1081
1036 // Verifies enumerations while entries are open. 1082 // Verifies enumerations while entries are open.
1037 void DiskCacheBackendTest::BackendEnumerations2() { 1083 void DiskCacheBackendTest::BackendEnumerations2() {
1038 InitCache(); 1084 InitCache();
1039 const std::string first("first"); 1085 const std::string first("first");
1040 const std::string second("second"); 1086 const std::string second("second");
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1090 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations2) { 1136 TEST_F(DiskCacheBackendTest, MemoryOnlyEnumerations2) {
1091 SetMemoryOnlyMode(); 1137 SetMemoryOnlyMode();
1092 BackendEnumerations2(); 1138 BackendEnumerations2();
1093 } 1139 }
1094 1140
1095 TEST_F(DiskCacheBackendTest, AppCacheEnumerations2) { 1141 TEST_F(DiskCacheBackendTest, AppCacheEnumerations2) {
1096 SetCacheType(net::APP_CACHE); 1142 SetCacheType(net::APP_CACHE);
1097 BackendEnumerations2(); 1143 BackendEnumerations2();
1098 } 1144 }
1099 1145
1146 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerations2) {
1147 SetCacheType(net::SHADER_CACHE);
1148 BackendEnumerations2();
1149 }
1150
1151 // Verify that ReadData calls do not update the LRU cache
1152 // when using the SHADER_CACHE type.
1153 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerationReadData) {
1154 SetCacheType(net::SHADER_CACHE);
1155 InitCache();
1156 const std::string first("first");
1157 const std::string second("second");
1158 disk_cache::Entry *entry1, *entry2;
1159 const int kSize = 50;
1160 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
1161
1162 ASSERT_EQ(net::OK, CreateEntry(first, &entry1));
1163 memset(buffer1->data(), 0, kSize);
1164 base::strlcpy(buffer1->data(), "And the data to save", kSize);
1165 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1, kSize, false));
1166
1167 ASSERT_EQ(net::OK, CreateEntry(second, &entry2));
1168 entry2->Close();
1169
1170 FlushQueueForTest();
1171
1172 // Make sure that the timestamp is not the same.
1173 AddDelay();
1174
1175 // Read from the last item in the LRU.
1176 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1, kSize));
1177 entry1->Close();
1178
1179 void* iter = NULL;
1180 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2));
1181 EXPECT_EQ(entry2->GetKey(), second);
1182 entry2->Close();
1183 cache_->EndEnumeration(&iter);
1184 }
1185
1100 // Verify handling of invalid entries while doing enumerations. 1186 // Verify handling of invalid entries while doing enumerations.
1101 // We'll be leaking memory from this test. 1187 // We'll be leaking memory from this test.
1102 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { 1188 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() {
1103 // Use the implementation directly... we need to simulate a crash. 1189 // Use the implementation directly... we need to simulate a crash.
1104 SetDirectMode(); 1190 SetDirectMode();
1105 InitCache(); 1191 InitCache();
1106 1192
1107 std::string key("Some key"); 1193 std::string key("Some key");
1108 disk_cache::Entry *entry, *entry1, *entry2; 1194 disk_cache::Entry *entry, *entry1, *entry2;
1109 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); 1195 ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
(...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after
2318 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) { 2404 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) {
2319 SetMemoryOnlyMode(); 2405 SetMemoryOnlyMode();
2320 BackendDoomAll(); 2406 BackendDoomAll();
2321 } 2407 }
2322 2408
2323 TEST_F(DiskCacheBackendTest, AppCacheOnlyDoomAll) { 2409 TEST_F(DiskCacheBackendTest, AppCacheOnlyDoomAll) {
2324 SetCacheType(net::APP_CACHE); 2410 SetCacheType(net::APP_CACHE);
2325 BackendDoomAll(); 2411 BackendDoomAll();
2326 } 2412 }
2327 2413
2414 TEST_F(DiskCacheBackendTest, ShaderCacheOnlyDoomAll) {
2415 SetCacheType(net::SHADER_CACHE);
2416 BackendDoomAll();
2417 }
2418
2328 // If the index size changes when we doom the cache, we should not crash. 2419 // If the index size changes when we doom the cache, we should not crash.
2329 void DiskCacheBackendTest::BackendDoomAll2() { 2420 void DiskCacheBackendTest::BackendDoomAll2() {
2330 EXPECT_EQ(2, cache_->GetEntryCount()); 2421 EXPECT_EQ(2, cache_->GetEntryCount());
2331 EXPECT_EQ(net::OK, DoomAllEntries()); 2422 EXPECT_EQ(net::OK, DoomAllEntries());
2332 2423
2333 disk_cache::Entry* entry; 2424 disk_cache::Entry* entry;
2334 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry)); 2425 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry));
2335 entry->Close(); 2426 entry->Close();
2336 2427
2337 EXPECT_EQ(1, cache_->GetEntryCount()); 2428 EXPECT_EQ(1, cache_->GetEntryCount());
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
2569 // Ping the oldest entry. 2660 // Ping the oldest entry.
2570 cache_->OnExternalCacheHit("key0"); 2661 cache_->OnExternalCacheHit("key0");
2571 2662
2572 TrimForTest(false); 2663 TrimForTest(false);
2573 2664
2574 // Make sure the older key remains. 2665 // Make sure the older key remains.
2575 EXPECT_EQ(1, cache_->GetEntryCount()); 2666 EXPECT_EQ(1, cache_->GetEntryCount());
2576 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); 2667 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
2577 entry->Close(); 2668 entry->Close();
2578 } 2669 }
2670
2671 TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) {
2672 SetCacheType(net::SHADER_CACHE);
2673 SetDirectMode();
2674 InitCache();
2675
2676 disk_cache::Entry* entry;
2677
2678 for (int i = 0; i < 2; ++i) {
2679 std::string key = StringPrintf("key%d", i);
2680 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2681 entry->Close();
2682 }
2683
2684 // Ping the oldest entry.
2685 cache_->OnExternalCacheHit("key0");
2686
2687 TrimForTest(false);
2688
2689 // Make sure the older key remains.
2690 EXPECT_EQ(1, cache_->GetEntryCount());
2691 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
2692 entry->Close();
2693 }
OLDNEW
« no previous file with comments | « net/disk_cache/backend_impl.cc ('k') | net/disk_cache/entry_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698