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

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

Issue 12794003: Initialize the simple cache backend at runtime. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 9 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 "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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 210
211 TEST_F(DiskCacheTest, CreateBackend) { 211 TEST_F(DiskCacheTest, CreateBackend) {
212 net::TestCompletionCallback cb; 212 net::TestCompletionCallback cb;
213 213
214 { 214 {
215 ASSERT_TRUE(CleanupCacheDir()); 215 ASSERT_TRUE(CleanupCacheDir());
216 base::Thread cache_thread("CacheThread"); 216 base::Thread cache_thread("CacheThread");
217 ASSERT_TRUE(cache_thread.StartWithOptions( 217 ASSERT_TRUE(cache_thread.StartWithOptions(
218 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 218 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
219 219
220 // Test the private factory methods. 220 // Test the private factory method(s).
221 disk_cache::Backend* cache = NULL; 221 disk_cache::Backend* cache = NULL;
222 int rv = disk_cache::BackendImpl::CreateBackend(
223 cache_path_, false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
224 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback());
225 ASSERT_EQ(net::OK, cb.GetResult(rv));
226 ASSERT_TRUE(cache);
227 delete cache;
228
229 cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL); 222 cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL);
230 ASSERT_TRUE(cache); 223 ASSERT_TRUE(cache);
231 delete cache; 224 delete cache;
232 cache = NULL; 225 cache = NULL;
233 226
234 // Now test the public API. 227 // Now test the public API.
235 rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, cache_path_, 0, false, 228 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, cache_path_, 0,
236 cache_thread.message_loop_proxy(), 229 false,
237 NULL, &cache, cb.callback()); 230 cache_thread.message_loop_proxy(),
231 NULL, &cache, cb.callback());
238 ASSERT_EQ(net::OK, cb.GetResult(rv)); 232 ASSERT_EQ(net::OK, cb.GetResult(rv));
239 ASSERT_TRUE(cache); 233 ASSERT_TRUE(cache);
240 delete cache; 234 delete cache;
241 cache = NULL; 235 cache = NULL;
242 236
243 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, base::FilePath(), 0, 237 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, base::FilePath(), 0,
244 false, NULL, NULL, &cache, 238 false, NULL, NULL, &cache,
245 cb.callback()); 239 cb.callback());
246 ASSERT_EQ(net::OK, cb.GetResult(rv)); 240 ASSERT_EQ(net::OK, cb.GetResult(rv));
247 ASSERT_TRUE(cache); 241 ASSERT_TRUE(cache);
248 delete cache; 242 delete cache;
249 } 243 }
250 244
251 MessageLoop::current()->RunUntilIdle(); 245 MessageLoop::current()->RunUntilIdle();
252 } 246 }
253 247
254 // Testst that re-creating the cache performs the expected cleanup. 248 // Testst that re-creating the cache performs the expected cleanup.
255 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) { 249 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) {
256 ASSERT_TRUE(CopyTestCache("bad_entry")); 250 ASSERT_TRUE(CopyTestCache("bad_entry"));
257 base::FilePath filename = cache_path_.AppendASCII("data_1"); 251 base::FilePath filename = cache_path_.AppendASCII("data_1");
258 file_util::Delete(filename, false); 252 file_util::Delete(filename, false);
259 DisableFirstCleanup(); 253 DisableFirstCleanup();
260 SetForceCreation(); 254 SetForceCreation();
261 255
262 bool prev = base::ThreadRestrictions::SetIOAllowed(false); 256 bool prev = base::ThreadRestrictions::SetIOAllowed(false);
263 InitCache(); 257 InitDefaultCacheViaCreator();
264 base::ThreadRestrictions::SetIOAllowed(prev); 258 base::ThreadRestrictions::SetIOAllowed(prev);
265 } 259 }
266 260
267 TEST_F(DiskCacheBackendTest, ExternalFiles) { 261 TEST_F(DiskCacheBackendTest, ExternalFiles) {
268 InitCache(); 262 InitCache();
269 // First, let's create a file on the folder. 263 // First, let's create a file on the folder.
270 base::FilePath filename = cache_path_.AppendASCII("f_000001"); 264 base::FilePath filename = cache_path_.AppendASCII("f_000001");
271 265
272 const int kSize = 50; 266 const int kSize = 50;
273 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 267 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
(...skipping 20 matching lines...) Expand all
294 { 288 {
295 ASSERT_TRUE(CleanupCacheDir()); 289 ASSERT_TRUE(CleanupCacheDir());
296 base::Thread cache_thread("CacheThread"); 290 base::Thread cache_thread("CacheThread");
297 ASSERT_TRUE(cache_thread.StartWithOptions( 291 ASSERT_TRUE(cache_thread.StartWithOptions(
298 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 292 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
299 293
300 disk_cache::Backend* cache; 294 disk_cache::Backend* cache;
301 uint32 flags = disk_cache::kNoBuffering; 295 uint32 flags = disk_cache::kNoBuffering;
302 if (!fast) 296 if (!fast)
303 flags |= disk_cache::kNoRandom; 297 flags |= disk_cache::kNoRandom;
304 rv = disk_cache::BackendImpl::CreateBackend( 298
305 cache_path_, false, 0, net::DISK_CACHE, flags, 299 cache = CreateBackendAsModeSuggests(flags, NULL);
306 base::MessageLoopProxy::current(), NULL,
307 &cache, cb.callback());
308 ASSERT_EQ(net::OK, cb.GetResult(rv));
309 300
310 disk_cache::EntryImpl* entry; 301 disk_cache::EntryImpl* entry;
311 rv = cache->CreateEntry( 302 rv = cache->CreateEntry(
312 "some key", reinterpret_cast<disk_cache::Entry**>(&entry), 303 "some key", reinterpret_cast<disk_cache::Entry**>(&entry),
313 cb.callback()); 304 cb.callback());
314 ASSERT_EQ(net::OK, cb.GetResult(rv)); 305 ASSERT_EQ(net::OK, cb.GetResult(rv));
315 306
316 const int kSize = 25000; 307 const int kSize = 25000;
317 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 308 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
318 CacheTestFillBuffer(buffer->data(), kSize, false); 309 CacheTestFillBuffer(buffer->data(), kSize, false);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 } 357 }
367 358
368 // Tests that we deal with background-thread pending operations. 359 // Tests that we deal with background-thread pending operations.
369 void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) { 360 void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) {
370 net::TestCompletionCallback cb; 361 net::TestCompletionCallback cb;
371 362
372 { 363 {
373 ASSERT_TRUE(CleanupCacheDir()); 364 ASSERT_TRUE(CleanupCacheDir());
374 base::Thread cache_thread("CacheThread"); 365 base::Thread cache_thread("CacheThread");
375 ASSERT_TRUE(cache_thread.StartWithOptions( 366 ASSERT_TRUE(cache_thread.StartWithOptions(
376 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 367 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
377 368
378 disk_cache::Backend* cache; 369 disk_cache::Backend* cache;
379 uint32 flags = disk_cache::kNoBuffering; 370 uint32 flags = disk_cache::kNoBuffering;
380 if (!fast) 371 if (!fast)
381 flags |= disk_cache::kNoRandom; 372 flags |= disk_cache::kNoRandom;
382 int rv = disk_cache::BackendImpl::CreateBackend( 373
383 cache_path_, false, 0, net::DISK_CACHE, flags, 374 cache = CreateBackendAsModeSuggests(flags, &cache_thread);
384 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback());
385 ASSERT_EQ(net::OK, cb.GetResult(rv));
386 375
387 disk_cache::Entry* entry; 376 disk_cache::Entry* entry;
388 rv = cache->CreateEntry("some key", &entry, cb.callback()); 377 int rv = cache->CreateEntry("some key", &entry, cb.callback());
389 ASSERT_EQ(net::OK, cb.GetResult(rv)); 378 ASSERT_EQ(net::OK, cb.GetResult(rv));
390 379
391 entry->Close(); 380 entry->Close();
392 381
393 // The cache destructor will see one pending operation here. 382 // The cache destructor will see one pending operation here.
394 delete cache; 383 delete cache;
395 } 384 }
396 385
397 MessageLoop::current()->RunUntilIdle(); 386 MessageLoop::current()->RunUntilIdle();
398 } 387 }
(...skipping 16 matching lines...) Expand all
415 404
416 { 405 {
417 ASSERT_TRUE(CleanupCacheDir()); 406 ASSERT_TRUE(CleanupCacheDir());
418 base::Thread cache_thread("CacheThread"); 407 base::Thread cache_thread("CacheThread");
419 ASSERT_TRUE(cache_thread.StartWithOptions( 408 ASSERT_TRUE(cache_thread.StartWithOptions(
420 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 409 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
421 410
422 disk_cache::Backend* cache; 411 disk_cache::Backend* cache;
423 disk_cache::BackendFlags flags = 412 disk_cache::BackendFlags flags =
424 fast ? disk_cache::kNone : disk_cache::kNoRandom; 413 fast ? disk_cache::kNone : disk_cache::kNoRandom;
425 int rv = disk_cache::BackendImpl::CreateBackend( 414 cache = CreateBackendAsModeSuggests(flags, &cache_thread);
426 cache_path_, false, 0, net::DISK_CACHE, flags,
427 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback());
428 ASSERT_EQ(net::OK, cb.GetResult(rv));
429 415
430 disk_cache::Entry* entry; 416 disk_cache::Entry* entry;
431 rv = cache->CreateEntry("some key", &entry, cb.callback()); 417 int rv = cache->CreateEntry("some key", &entry, cb.callback());
432 ASSERT_EQ(net::ERR_IO_PENDING, rv); 418 ASSERT_EQ(net::ERR_IO_PENDING, rv);
433 419
434 delete cache; 420 delete cache;
435 EXPECT_FALSE(cb.have_result()); 421 EXPECT_FALSE(cb.have_result());
436 } 422 }
437 423
438 MessageLoop::current()->RunUntilIdle(); 424 MessageLoop::current()->RunUntilIdle();
439 } 425 }
440 426
441 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate) { 427 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate) {
(...skipping 12 matching lines...) Expand all
454 ASSERT_TRUE(CleanupCacheDir()); 440 ASSERT_TRUE(CleanupCacheDir());
455 base::FilePath index = cache_path_.AppendASCII("index"); 441 base::FilePath index = cache_path_.AppendASCII("index");
456 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); 442 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5));
457 443
458 base::Thread cache_thread("CacheThread"); 444 base::Thread cache_thread("CacheThread");
459 ASSERT_TRUE(cache_thread.StartWithOptions( 445 ASSERT_TRUE(cache_thread.StartWithOptions(
460 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 446 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
461 net::TestCompletionCallback cb; 447 net::TestCompletionCallback cb;
462 448
463 disk_cache::Backend* backend = NULL; 449 disk_cache::Backend* backend = NULL;
464 int rv = disk_cache::BackendImpl::CreateBackend( 450 int rv = disk_cache::CreateCacheBackend(
465 cache_path_, false, 0, net::DISK_CACHE, disk_cache::kNone, 451 net::DISK_CACHE, cache_path_, 0, false,
466 cache_thread.message_loop_proxy(), NULL, &backend, cb.callback()); 452 cache_thread.message_loop_proxy(), NULL, &backend, cb.callback());
467 ASSERT_NE(net::OK, cb.GetResult(rv)); 453 ASSERT_NE(net::OK, cb.GetResult(rv));
468 454
469 ASSERT_TRUE(backend == NULL); 455 ASSERT_TRUE(backend == NULL);
470 delete backend; 456 delete backend;
471 } 457 }
472 458
473 void DiskCacheBackendTest::BackendSetSize() { 459 void DiskCacheBackendTest::BackendSetSize() {
474 SetDirectMode();
475 const int cache_size = 0x10000; // 64 kB 460 const int cache_size = 0x10000; // 64 kB
476 SetMaxSize(cache_size); 461 SetMaxSize(cache_size);
477 InitCache(); 462 InitCache();
478 463
479 std::string first("some key"); 464 std::string first("some key");
480 std::string second("something else"); 465 std::string second("something else");
481 disk_cache::Entry* entry; 466 disk_cache::Entry* entry;
482 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 467 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
483 468
484 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(cache_size)); 469 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(cache_size));
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 BackendChain(); 622 BackendChain();
638 } 623 }
639 624
640 TEST_F(DiskCacheBackendTest, ShaderCacheChain) { 625 TEST_F(DiskCacheBackendTest, ShaderCacheChain) {
641 SetCacheType(net::SHADER_CACHE); 626 SetCacheType(net::SHADER_CACHE);
642 BackendChain(); 627 BackendChain();
643 } 628 }
644 629
645 TEST_F(DiskCacheBackendTest, NewEvictionTrim) { 630 TEST_F(DiskCacheBackendTest, NewEvictionTrim) {
646 SetNewEviction(); 631 SetNewEviction();
647 SetDirectMode();
648 InitCache(); 632 InitCache();
649 633
650 disk_cache::Entry* entry; 634 disk_cache::Entry* entry;
651 for (int i = 0; i < 100; i++) { 635 for (int i = 0; i < 100; i++) {
652 std::string name(StringPrintf("Key %d", i)); 636 std::string name(StringPrintf("Key %d", i));
653 ASSERT_EQ(net::OK, CreateEntry(name, &entry)); 637 ASSERT_EQ(net::OK, CreateEntry(name, &entry));
654 entry->Close(); 638 entry->Close();
655 if (i < 90) { 639 if (i < 90) {
656 // Entries 0 to 89 are in list 1; 90 to 99 are in list 0. 640 // Entries 0 to 89 are in list 1; 90 to 99 are in list 0.
657 ASSERT_EQ(net::OK, OpenEntry(name, &entry)); 641 ASSERT_EQ(net::OK, OpenEntry(name, &entry));
(...skipping 10 matching lines...) Expand all
668 652
669 // Double check that we still have the list tails. 653 // Double check that we still have the list tails.
670 ASSERT_EQ(net::OK, OpenEntry("Key 1", &entry)); 654 ASSERT_EQ(net::OK, OpenEntry("Key 1", &entry));
671 entry->Close(); 655 entry->Close();
672 ASSERT_EQ(net::OK, OpenEntry("Key 91", &entry)); 656 ASSERT_EQ(net::OK, OpenEntry("Key 91", &entry));
673 entry->Close(); 657 entry->Close();
674 } 658 }
675 659
676 // Before looking for invalid entries, let's check a valid entry. 660 // Before looking for invalid entries, let's check a valid entry.
677 void DiskCacheBackendTest::BackendValidEntry() { 661 void DiskCacheBackendTest::BackendValidEntry() {
678 SetDirectMode();
679 InitCache(); 662 InitCache();
680 663
681 std::string key("Some key"); 664 std::string key("Some key");
682 disk_cache::Entry* entry; 665 disk_cache::Entry* entry;
683 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 666 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
684 667
685 const int kSize = 50; 668 const int kSize = 50;
686 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 669 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
687 memset(buffer1->data(), 0, kSize); 670 memset(buffer1->data(), 0, kSize);
688 base::strlcpy(buffer1->data(), "And the data to save", kSize); 671 base::strlcpy(buffer1->data(), "And the data to save", kSize);
(...skipping 16 matching lines...) Expand all
705 688
706 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) { 689 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) {
707 SetNewEviction(); 690 SetNewEviction();
708 BackendValidEntry(); 691 BackendValidEntry();
709 } 692 }
710 693
711 // The same logic of the previous test (ValidEntry), but this time force the 694 // The same logic of the previous test (ValidEntry), but this time force the
712 // entry to be invalid, simulating a crash in the middle. 695 // entry to be invalid, simulating a crash in the middle.
713 // We'll be leaking memory from this test. 696 // We'll be leaking memory from this test.
714 void DiskCacheBackendTest::BackendInvalidEntry() { 697 void DiskCacheBackendTest::BackendInvalidEntry() {
715 // Use the implementation directly... we need to simulate a crash.
716 SetDirectMode();
717 InitCache(); 698 InitCache();
718 699
719 std::string key("Some key"); 700 std::string key("Some key");
720 disk_cache::Entry* entry; 701 disk_cache::Entry* entry;
721 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 702 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
722 703
723 const int kSize = 50; 704 const int kSize = 50;
724 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 705 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
725 memset(buffer->data(), 0, kSize); 706 memset(buffer->data(), 0, kSize);
726 base::strlcpy(buffer->data(), "And the data to save", kSize); 707 base::strlcpy(buffer->data(), "And the data to save", kSize);
(...skipping 27 matching lines...) Expand all
754 735
755 // We'll be leaking memory from this test. 736 // We'll be leaking memory from this test.
756 TEST_F(DiskCacheBackendTest, ShaderCacheInvalidEntry) { 737 TEST_F(DiskCacheBackendTest, ShaderCacheInvalidEntry) {
757 SetCacheType(net::SHADER_CACHE); 738 SetCacheType(net::SHADER_CACHE);
758 BackendInvalidEntry(); 739 BackendInvalidEntry();
759 } 740 }
760 741
761 // Almost the same test, but this time crash the cache after reading an entry. 742 // Almost the same test, but this time crash the cache after reading an entry.
762 // We'll be leaking memory from this test. 743 // We'll be leaking memory from this test.
763 void DiskCacheBackendTest::BackendInvalidEntryRead() { 744 void DiskCacheBackendTest::BackendInvalidEntryRead() {
764 // Use the implementation directly... we need to simulate a crash.
765 SetDirectMode();
766 InitCache(); 745 InitCache();
767 746
768 std::string key("Some key"); 747 std::string key("Some key");
769 disk_cache::Entry* entry; 748 disk_cache::Entry* entry;
770 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 749 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
771 750
772 const int kSize = 50; 751 const int kSize = 50;
773 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 752 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
774 memset(buffer->data(), 0, kSize); 753 memset(buffer->data(), 0, kSize);
775 base::strlcpy(buffer->data(), "And the data to save", kSize); 754 base::strlcpy(buffer->data(), "And the data to save", kSize);
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 } 860 }
882 861
883 // We'll be leaking memory from this test. 862 // We'll be leaking memory from this test.
884 TEST_F(DiskCacheBackendTest, ShaderCacheInvalidEntryWithLoad) { 863 TEST_F(DiskCacheBackendTest, ShaderCacheInvalidEntryWithLoad) {
885 SetCacheType(net::SHADER_CACHE); 864 SetCacheType(net::SHADER_CACHE);
886 BackendInvalidEntryWithLoad(); 865 BackendInvalidEntryWithLoad();
887 } 866 }
888 867
889 // We'll be leaking memory from this test. 868 // We'll be leaking memory from this test.
890 void DiskCacheBackendTest::BackendTrimInvalidEntry() { 869 void DiskCacheBackendTest::BackendTrimInvalidEntry() {
891 // Use the implementation directly... we need to simulate a crash.
892 SetDirectMode();
893
894 const int kSize = 0x3000; // 12 kB 870 const int kSize = 0x3000; // 12 kB
895 SetMaxSize(kSize * 10); 871 SetMaxSize(kSize * 10);
896 InitCache(); 872 InitCache();
897 873
898 std::string first("some key"); 874 std::string first("some key");
899 std::string second("something else"); 875 std::string second("something else");
900 disk_cache::Entry* entry; 876 disk_cache::Entry* entry;
901 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 877 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
902 878
903 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 879 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 } 912 }
937 913
938 // We'll be leaking memory from this test. 914 // We'll be leaking memory from this test.
939 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry) { 915 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry) {
940 SetNewEviction(); 916 SetNewEviction();
941 BackendTrimInvalidEntry(); 917 BackendTrimInvalidEntry();
942 } 918 }
943 919
944 // We'll be leaking memory from this test. 920 // We'll be leaking memory from this test.
945 void DiskCacheBackendTest::BackendTrimInvalidEntry2() { 921 void DiskCacheBackendTest::BackendTrimInvalidEntry2() {
946 // Use the implementation directly... we need to simulate a crash.
947 SetDirectMode();
948 SetMask(0xf); // 16-entry table. 922 SetMask(0xf); // 16-entry table.
949 923
950 const int kSize = 0x3000; // 12 kB 924 const int kSize = 0x3000; // 12 kB
951 SetMaxSize(kSize * 40); 925 SetMaxSize(kSize * 40);
952 InitCache(); 926 InitCache();
953 927
954 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 928 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
955 memset(buffer->data(), 0, kSize); 929 memset(buffer->data(), 0, kSize);
956 disk_cache::Entry* entry; 930 disk_cache::Entry* entry;
957 931
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 void* iter = NULL; 1153 void* iter = NULL;
1180 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); 1154 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2));
1181 EXPECT_EQ(entry2->GetKey(), second); 1155 EXPECT_EQ(entry2->GetKey(), second);
1182 entry2->Close(); 1156 entry2->Close();
1183 cache_->EndEnumeration(&iter); 1157 cache_->EndEnumeration(&iter);
1184 } 1158 }
1185 1159
1186 // Verify handling of invalid entries while doing enumerations. 1160 // Verify handling of invalid entries while doing enumerations.
1187 // We'll be leaking memory from this test. 1161 // We'll be leaking memory from this test.
1188 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { 1162 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() {
1189 // Use the implementation directly... we need to simulate a crash.
1190 SetDirectMode();
1191 InitCache(); 1163 InitCache();
1192 1164
1193 std::string key("Some key"); 1165 std::string key("Some key");
1194 disk_cache::Entry *entry, *entry1, *entry2; 1166 disk_cache::Entry *entry, *entry1, *entry2;
1195 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); 1167 ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
1196 1168
1197 const int kSize = 50; 1169 const int kSize = 50;
1198 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 1170 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
1199 memset(buffer1->data(), 0, kSize); 1171 memset(buffer1->data(), 0, kSize);
1200 base::strlcpy(buffer1->data(), "And the data to save", kSize); 1172 base::strlcpy(buffer1->data(), "And the data to save", kSize);
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1539 1511
1540 TEST_F(DiskCacheBackendTest, RecoverWithEviction) { 1512 TEST_F(DiskCacheBackendTest, RecoverWithEviction) {
1541 BackendRecoverWithEviction(); 1513 BackendRecoverWithEviction();
1542 } 1514 }
1543 1515
1544 TEST_F(DiskCacheBackendTest, NewEvictionRecoverWithEviction) { 1516 TEST_F(DiskCacheBackendTest, NewEvictionRecoverWithEviction) {
1545 SetNewEviction(); 1517 SetNewEviction();
1546 BackendRecoverWithEviction(); 1518 BackendRecoverWithEviction();
1547 } 1519 }
1548 1520
1549 // Tests dealing with cache files that cannot be recovered. 1521 // Tests that the |BackendImpl| fails to start with the wrong cache version.
1550 TEST_F(DiskCacheTest, DeleteOld) { 1522 TEST_F(DiskCacheTest, WrongVersion) {
1551 ASSERT_TRUE(CopyTestCache("wrong_version")); 1523 ASSERT_TRUE(CopyTestCache("wrong_version"));
1552 base::Thread cache_thread("CacheThread"); 1524 base::Thread cache_thread("CacheThread");
1553 ASSERT_TRUE(cache_thread.StartWithOptions( 1525 ASSERT_TRUE(cache_thread.StartWithOptions(
1554 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 1526 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
1555 net::TestCompletionCallback cb; 1527 net::TestCompletionCallback cb;
1556 1528
1557 disk_cache::Backend* cache; 1529 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
1558 int rv = disk_cache::BackendImpl::CreateBackend( 1530 cache_path_, cache_thread.message_loop_proxy(), NULL);
1559 cache_path_, true, 0, net::DISK_CACHE, disk_cache::kNoRandom, 1531 int rv = cache->Init(cb.callback());
1560 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); 1532 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv));
1561 ASSERT_EQ(net::OK, cb.GetResult(rv));
1562
1563 MessageLoopHelper helper;
1564
1565 ASSERT_TRUE(NULL != cache);
1566 ASSERT_EQ(0, cache->GetEntryCount());
1567 1533
1568 delete cache; 1534 delete cache;
1569 } 1535 }
1570 1536
1537 // Tests that the cache is properly restarted on recovery error.
1538 TEST_F(DiskCacheBackendTest, DeleteOld) {
1539 ASSERT_TRUE(CopyTestCache("wrong_version"));
1540 InitDefaultCacheViaCreator();
1541 }
1542
1571 // We want to be able to deal with messed up entries on disk. 1543 // We want to be able to deal with messed up entries on disk.
1572 void DiskCacheBackendTest::BackendInvalidEntry2() { 1544 void DiskCacheBackendTest::BackendInvalidEntry2() {
1573 ASSERT_TRUE(CopyTestCache("bad_entry")); 1545 ASSERT_TRUE(CopyTestCache("bad_entry"));
1574 DisableFirstCleanup(); 1546 DisableFirstCleanup();
1575 InitCache(); 1547 InitCache();
1576 1548
1577 disk_cache::Entry *entry1, *entry2; 1549 disk_cache::Entry *entry1, *entry2;
1578 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); 1550 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1));
1579 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2)); 1551 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2));
1580 entry1->Close(); 1552 entry1->Close();
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
1712 FlushQueueForTest(); 1684 FlushQueueForTest();
1713 entry->Close(); 1685 entry->Close();
1714 1686
1715 ASSERT_EQ(net::OK, OpenEntry("some other key", &entry)); 1687 ASSERT_EQ(net::OK, OpenEntry("some other key", &entry));
1716 entry->Close(); 1688 entry->Close();
1717 } 1689 }
1718 1690
1719 // Tests handling of corrupt entries by keeping the rankings node around, with 1691 // Tests handling of corrupt entries by keeping the rankings node around, with
1720 // a fatal failure. 1692 // a fatal failure.
1721 void DiskCacheBackendTest::BackendInvalidEntry7() { 1693 void DiskCacheBackendTest::BackendInvalidEntry7() {
1722 SetDirectMode();
1723 const int kSize = 0x3000; // 12 kB. 1694 const int kSize = 0x3000; // 12 kB.
1724 SetMaxSize(kSize * 10); 1695 SetMaxSize(kSize * 10);
1725 InitCache(); 1696 InitCache();
1726 1697
1727 std::string first("some key"); 1698 std::string first("some key");
1728 std::string second("something else"); 1699 std::string second("something else");
1729 disk_cache::Entry* entry; 1700 disk_cache::Entry* entry;
1730 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 1701 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
1731 entry->Close(); 1702 entry->Close();
1732 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 1703 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
(...skipping 24 matching lines...) Expand all
1757 } 1728 }
1758 1729
1759 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry7) { 1730 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry7) {
1760 SetNewEviction(); 1731 SetNewEviction();
1761 BackendInvalidEntry7(); 1732 BackendInvalidEntry7();
1762 } 1733 }
1763 1734
1764 // Tests handling of corrupt entries by keeping the rankings node around, with 1735 // Tests handling of corrupt entries by keeping the rankings node around, with
1765 // a non fatal failure. 1736 // a non fatal failure.
1766 void DiskCacheBackendTest::BackendInvalidEntry8() { 1737 void DiskCacheBackendTest::BackendInvalidEntry8() {
1767 SetDirectMode();
1768 const int kSize = 0x3000; // 12 kB 1738 const int kSize = 0x3000; // 12 kB
1769 SetMaxSize(kSize * 10); 1739 SetMaxSize(kSize * 10);
1770 InitCache(); 1740 InitCache();
1771 1741
1772 std::string first("some key"); 1742 std::string first("some key");
1773 std::string second("something else"); 1743 std::string second("something else");
1774 disk_cache::Entry* entry; 1744 disk_cache::Entry* entry;
1775 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 1745 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
1776 entry->Close(); 1746 entry->Close();
1777 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 1747 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
(...skipping 27 matching lines...) Expand all
1805 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry8) { 1775 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry8) {
1806 SetNewEviction(); 1776 SetNewEviction();
1807 BackendInvalidEntry8(); 1777 BackendInvalidEntry8();
1808 } 1778 }
1809 1779
1810 // Tests handling of corrupt entries detected by enumerations. Note that these 1780 // Tests handling of corrupt entries detected by enumerations. Note that these
1811 // tests (xx9 to xx11) are basically just going though slightly different 1781 // tests (xx9 to xx11) are basically just going though slightly different
1812 // codepaths so they are tighlty coupled with the code, but that is better than 1782 // codepaths so they are tighlty coupled with the code, but that is better than
1813 // not testing error handling code. 1783 // not testing error handling code.
1814 void DiskCacheBackendTest::BackendInvalidEntry9(bool eviction) { 1784 void DiskCacheBackendTest::BackendInvalidEntry9(bool eviction) {
1815 SetDirectMode();
1816 const int kSize = 0x3000; // 12 kB. 1785 const int kSize = 0x3000; // 12 kB.
1817 SetMaxSize(kSize * 10); 1786 SetMaxSize(kSize * 10);
1818 InitCache(); 1787 InitCache();
1819 1788
1820 std::string first("some key"); 1789 std::string first("some key");
1821 std::string second("something else"); 1790 std::string second("something else");
1822 disk_cache::Entry* entry; 1791 disk_cache::Entry* entry;
1823 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 1792 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
1824 entry->Close(); 1793 entry->Close();
1825 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 1794 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1870 BackendInvalidEntry9(true); 1839 BackendInvalidEntry9(true);
1871 } 1840 }
1872 1841
1873 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry9) { 1842 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry9) {
1874 SetNewEviction(); 1843 SetNewEviction();
1875 BackendInvalidEntry9(true); 1844 BackendInvalidEntry9(true);
1876 } 1845 }
1877 1846
1878 // Tests handling of corrupt entries detected by enumerations. 1847 // Tests handling of corrupt entries detected by enumerations.
1879 void DiskCacheBackendTest::BackendInvalidEntry10(bool eviction) { 1848 void DiskCacheBackendTest::BackendInvalidEntry10(bool eviction) {
1880 SetDirectMode();
1881 const int kSize = 0x3000; // 12 kB. 1849 const int kSize = 0x3000; // 12 kB.
1882 SetMaxSize(kSize * 10); 1850 SetMaxSize(kSize * 10);
1883 SetNewEviction(); 1851 SetNewEviction();
1884 InitCache(); 1852 InitCache();
1885 1853
1886 std::string first("some key"); 1854 std::string first("some key");
1887 std::string second("something else"); 1855 std::string second("something else");
1888 disk_cache::Entry* entry; 1856 disk_cache::Entry* entry;
1889 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 1857 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
1890 entry->Close(); 1858 entry->Close();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1934 TEST_F(DiskCacheBackendTest, InvalidEntry10) { 1902 TEST_F(DiskCacheBackendTest, InvalidEntry10) {
1935 BackendInvalidEntry10(false); 1903 BackendInvalidEntry10(false);
1936 } 1904 }
1937 1905
1938 TEST_F(DiskCacheBackendTest, TrimInvalidEntry10) { 1906 TEST_F(DiskCacheBackendTest, TrimInvalidEntry10) {
1939 BackendInvalidEntry10(true); 1907 BackendInvalidEntry10(true);
1940 } 1908 }
1941 1909
1942 // Tests handling of corrupt entries detected by enumerations. 1910 // Tests handling of corrupt entries detected by enumerations.
1943 void DiskCacheBackendTest::BackendInvalidEntry11(bool eviction) { 1911 void DiskCacheBackendTest::BackendInvalidEntry11(bool eviction) {
1944 SetDirectMode();
1945 const int kSize = 0x3000; // 12 kB. 1912 const int kSize = 0x3000; // 12 kB.
1946 SetMaxSize(kSize * 10); 1913 SetMaxSize(kSize * 10);
1947 SetNewEviction(); 1914 SetNewEviction();
1948 InitCache(); 1915 InitCache();
1949 1916
1950 std::string first("some key"); 1917 std::string first("some key");
1951 std::string second("something else"); 1918 std::string second("something else");
1952 disk_cache::Entry* entry; 1919 disk_cache::Entry* entry;
1953 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 1920 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
1954 entry->Close(); 1921 entry->Close();
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
2006 TEST_F(DiskCacheBackendTest, InvalidEntry11) { 1973 TEST_F(DiskCacheBackendTest, InvalidEntry11) {
2007 BackendInvalidEntry11(false); 1974 BackendInvalidEntry11(false);
2008 } 1975 }
2009 1976
2010 TEST_F(DiskCacheBackendTest, TrimInvalidEntry11) { 1977 TEST_F(DiskCacheBackendTest, TrimInvalidEntry11) {
2011 BackendInvalidEntry11(true); 1978 BackendInvalidEntry11(true);
2012 } 1979 }
2013 1980
2014 // Tests handling of corrupt entries in the middle of a long eviction run. 1981 // Tests handling of corrupt entries in the middle of a long eviction run.
2015 void DiskCacheBackendTest::BackendTrimInvalidEntry12() { 1982 void DiskCacheBackendTest::BackendTrimInvalidEntry12() {
2016 SetDirectMode();
2017 const int kSize = 0x3000; // 12 kB 1983 const int kSize = 0x3000; // 12 kB
2018 SetMaxSize(kSize * 10); 1984 SetMaxSize(kSize * 10);
2019 InitCache(); 1985 InitCache();
2020 1986
2021 std::string first("some key"); 1987 std::string first("some key");
2022 std::string second("something else"); 1988 std::string second("something else");
2023 disk_cache::Entry* entry; 1989 disk_cache::Entry* entry;
2024 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 1990 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
2025 entry->Close(); 1991 entry->Close();
2026 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 1992 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
2083 EXPECT_EQ(2, cache_->GetEntryCount()); 2049 EXPECT_EQ(2, cache_->GetEntryCount());
2084 2050
2085 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry)); 2051 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry));
2086 FlushQueueForTest(); // Allow the restart to finish. 2052 FlushQueueForTest(); // Allow the restart to finish.
2087 EXPECT_EQ(0, cache_->GetEntryCount()); 2053 EXPECT_EQ(0, cache_->GetEntryCount());
2088 } 2054 }
2089 2055
2090 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) { 2056 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) {
2091 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2057 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2092 DisableFirstCleanup(); 2058 DisableFirstCleanup();
2093 SetDirectMode();
2094 InitCache(); 2059 InitCache();
2095 BackendInvalidRankings(); 2060 BackendInvalidRankings();
2096 } 2061 }
2097 2062
2098 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankingsSuccess) { 2063 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankingsSuccess) {
2099 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2064 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2100 DisableFirstCleanup(); 2065 DisableFirstCleanup();
2101 SetDirectMode();
2102 SetNewEviction(); 2066 SetNewEviction();
2103 InitCache(); 2067 InitCache();
2104 BackendInvalidRankings(); 2068 BackendInvalidRankings();
2105 } 2069 }
2106 2070
2107 TEST_F(DiskCacheBackendTest, InvalidRankingsFailure) { 2071 TEST_F(DiskCacheBackendTest, InvalidRankingsFailure) {
2108 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2072 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2109 DisableFirstCleanup(); 2073 DisableFirstCleanup();
2110 SetDirectMode();
2111 InitCache(); 2074 InitCache();
2112 SetTestMode(); // Fail cache reinitialization. 2075 SetTestMode(); // Fail cache reinitialization.
2113 BackendInvalidRankings(); 2076 BackendInvalidRankings();
2114 } 2077 }
2115 2078
2116 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankingsFailure) { 2079 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankingsFailure) {
2117 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2080 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2118 DisableFirstCleanup(); 2081 DisableFirstCleanup();
2119 SetDirectMode();
2120 SetNewEviction(); 2082 SetNewEviction();
2121 InitCache(); 2083 InitCache();
2122 SetTestMode(); // Fail cache reinitialization. 2084 SetTestMode(); // Fail cache reinitialization.
2123 BackendInvalidRankings(); 2085 BackendInvalidRankings();
2124 } 2086 }
2125 2087
2126 // If the LRU is corrupt and we have open entries, we disable the cache. 2088 // If the LRU is corrupt and we have open entries, we disable the cache.
2127 void DiskCacheBackendTest::BackendDisable() { 2089 void DiskCacheBackendTest::BackendDisable() {
2128 disk_cache::Entry *entry1, *entry2; 2090 disk_cache::Entry *entry1, *entry2;
2129 void* iter = NULL; 2091 void* iter = NULL;
2130 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1)); 2092 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry1));
2131 2093
2132 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2)); 2094 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry2));
2133 EXPECT_EQ(0, cache_->GetEntryCount()); 2095 EXPECT_EQ(0, cache_->GetEntryCount());
2134 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2)); 2096 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2));
2135 2097
2136 entry1->Close(); 2098 entry1->Close();
2137 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. 2099 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache.
2138 FlushQueueForTest(); // This one actually allows that task to complete. 2100 FlushQueueForTest(); // This one actually allows that task to complete.
2139 2101
2140 EXPECT_EQ(0, cache_->GetEntryCount()); 2102 EXPECT_EQ(0, cache_->GetEntryCount());
2141 } 2103 }
2142 2104
2143 TEST_F(DiskCacheBackendTest, DisableSuccess) { 2105 TEST_F(DiskCacheBackendTest, DisableSuccess) {
2144 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2106 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2145 DisableFirstCleanup(); 2107 DisableFirstCleanup();
2146 SetDirectMode();
2147 InitCache(); 2108 InitCache();
2148 BackendDisable(); 2109 BackendDisable();
2149 } 2110 }
2150 2111
2151 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess) { 2112 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess) {
2152 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2113 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2153 DisableFirstCleanup(); 2114 DisableFirstCleanup();
2154 SetDirectMode();
2155 SetNewEviction(); 2115 SetNewEviction();
2156 InitCache(); 2116 InitCache();
2157 BackendDisable(); 2117 BackendDisable();
2158 } 2118 }
2159 2119
2160 TEST_F(DiskCacheBackendTest, DisableFailure) { 2120 TEST_F(DiskCacheBackendTest, DisableFailure) {
2161 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2121 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2162 DisableFirstCleanup(); 2122 DisableFirstCleanup();
2163 SetDirectMode();
2164 InitCache(); 2123 InitCache();
2165 SetTestMode(); // Fail cache reinitialization. 2124 SetTestMode(); // Fail cache reinitialization.
2166 BackendDisable(); 2125 BackendDisable();
2167 } 2126 }
2168 2127
2169 TEST_F(DiskCacheBackendTest, NewEvictionDisableFailure) { 2128 TEST_F(DiskCacheBackendTest, NewEvictionDisableFailure) {
2170 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2129 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2171 DisableFirstCleanup(); 2130 DisableFirstCleanup();
2172 SetDirectMode();
2173 SetNewEviction(); 2131 SetNewEviction();
2174 InitCache(); 2132 InitCache();
2175 SetTestMode(); // Fail cache reinitialization. 2133 SetTestMode(); // Fail cache reinitialization.
2176 BackendDisable(); 2134 BackendDisable();
2177 } 2135 }
2178 2136
2179 // This is another type of corruption on the LRU; disable the cache. 2137 // This is another type of corruption on the LRU; disable the cache.
2180 void DiskCacheBackendTest::BackendDisable2() { 2138 void DiskCacheBackendTest::BackendDisable2() {
2181 EXPECT_EQ(8, cache_->GetEntryCount()); 2139 EXPECT_EQ(8, cache_->GetEntryCount());
2182 2140
2183 disk_cache::Entry* entry; 2141 disk_cache::Entry* entry;
2184 void* iter = NULL; 2142 void* iter = NULL;
2185 int count = 0; 2143 int count = 0;
2186 while (OpenNextEntry(&iter, &entry) == net::OK) { 2144 while (OpenNextEntry(&iter, &entry) == net::OK) {
2187 ASSERT_TRUE(NULL != entry); 2145 ASSERT_TRUE(NULL != entry);
2188 entry->Close(); 2146 entry->Close();
2189 count++; 2147 count++;
2190 ASSERT_LT(count, 9); 2148 ASSERT_LT(count, 9);
2191 }; 2149 };
2192 2150
2193 FlushQueueForTest(); 2151 FlushQueueForTest();
2194 EXPECT_EQ(0, cache_->GetEntryCount()); 2152 EXPECT_EQ(0, cache_->GetEntryCount());
2195 } 2153 }
2196 2154
2197 TEST_F(DiskCacheBackendTest, DisableSuccess2) { 2155 TEST_F(DiskCacheBackendTest, DisableSuccess2) {
2198 ASSERT_TRUE(CopyTestCache("list_loop")); 2156 ASSERT_TRUE(CopyTestCache("list_loop"));
2199 DisableFirstCleanup(); 2157 DisableFirstCleanup();
2200 SetDirectMode();
2201 InitCache(); 2158 InitCache();
2202 BackendDisable2(); 2159 BackendDisable2();
2203 } 2160 }
2204 2161
2205 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess2) { 2162 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess2) {
2206 ASSERT_TRUE(CopyTestCache("list_loop")); 2163 ASSERT_TRUE(CopyTestCache("list_loop"));
2207 DisableFirstCleanup(); 2164 DisableFirstCleanup();
2208 SetNewEviction(); 2165 SetNewEviction();
2209 SetDirectMode();
2210 InitCache(); 2166 InitCache();
2211 BackendDisable2(); 2167 BackendDisable2();
2212 } 2168 }
2213 2169
2214 TEST_F(DiskCacheBackendTest, DisableFailure2) { 2170 TEST_F(DiskCacheBackendTest, DisableFailure2) {
2215 ASSERT_TRUE(CopyTestCache("list_loop")); 2171 ASSERT_TRUE(CopyTestCache("list_loop"));
2216 DisableFirstCleanup(); 2172 DisableFirstCleanup();
2217 SetDirectMode();
2218 InitCache(); 2173 InitCache();
2219 SetTestMode(); // Fail cache reinitialization. 2174 SetTestMode(); // Fail cache reinitialization.
2220 BackendDisable2(); 2175 BackendDisable2();
2221 } 2176 }
2222 2177
2223 TEST_F(DiskCacheBackendTest, NewEvictionDisableFailure2) { 2178 TEST_F(DiskCacheBackendTest, NewEvictionDisableFailure2) {
2224 ASSERT_TRUE(CopyTestCache("list_loop")); 2179 ASSERT_TRUE(CopyTestCache("list_loop"));
2225 DisableFirstCleanup(); 2180 DisableFirstCleanup();
2226 SetDirectMode();
2227 SetNewEviction(); 2181 SetNewEviction();
2228 InitCache(); 2182 InitCache();
2229 SetTestMode(); // Fail cache reinitialization. 2183 SetTestMode(); // Fail cache reinitialization.
2230 BackendDisable2(); 2184 BackendDisable2();
2231 } 2185 }
2232 2186
2233 // If the index size changes when we disable the cache, we should not crash. 2187 // If the index size changes when we disable the cache, we should not crash.
2234 void DiskCacheBackendTest::BackendDisable3() { 2188 void DiskCacheBackendTest::BackendDisable3() {
2235 disk_cache::Entry *entry1, *entry2; 2189 disk_cache::Entry *entry1, *entry2;
2236 void* iter = NULL; 2190 void* iter = NULL;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
2309 entry3->Close(); 2263 entry3->Close();
2310 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. 2264 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache.
2311 FlushQueueForTest(); // This one actually allows that task to complete. 2265 FlushQueueForTest(); // This one actually allows that task to complete.
2312 2266
2313 EXPECT_EQ(0, cache_->GetEntryCount()); 2267 EXPECT_EQ(0, cache_->GetEntryCount());
2314 } 2268 }
2315 2269
2316 TEST_F(DiskCacheBackendTest, DisableSuccess4) { 2270 TEST_F(DiskCacheBackendTest, DisableSuccess4) {
2317 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2271 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2318 DisableFirstCleanup(); 2272 DisableFirstCleanup();
2319 SetDirectMode();
2320 InitCache(); 2273 InitCache();
2321 BackendDisable4(); 2274 BackendDisable4();
2322 } 2275 }
2323 2276
2324 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess4) { 2277 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess4) {
2325 ASSERT_TRUE(CopyTestCache("bad_rankings")); 2278 ASSERT_TRUE(CopyTestCache("bad_rankings"));
2326 DisableFirstCleanup(); 2279 DisableFirstCleanup();
2327 SetDirectMode();
2328 SetNewEviction(); 2280 SetNewEviction();
2329 InitCache(); 2281 InitCache();
2330 BackendDisable4(); 2282 BackendDisable4();
2331 } 2283 }
2332 2284
2333 TEST_F(DiskCacheTest, Backend_UsageStats) { 2285 TEST_F(DiskCacheTest, Backend_UsageStats) {
2334 MessageLoopHelper helper; 2286 MessageLoopHelper helper;
2335 2287
2336 ASSERT_TRUE(CleanupCacheDir()); 2288 ASSERT_TRUE(CleanupCacheDir());
2337 scoped_ptr<disk_cache::BackendImpl> cache; 2289 scoped_ptr<disk_cache::BackendImpl> cache;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
2453 ASSERT_TRUE(store2.CreateUniqueTempDir()); 2405 ASSERT_TRUE(store2.CreateUniqueTempDir());
2454 2406
2455 base::Thread cache_thread("CacheThread"); 2407 base::Thread cache_thread("CacheThread");
2456 ASSERT_TRUE(cache_thread.StartWithOptions( 2408 ASSERT_TRUE(cache_thread.StartWithOptions(
2457 base::Thread::Options(MessageLoop::TYPE_IO, 0))); 2409 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
2458 net::TestCompletionCallback cb; 2410 net::TestCompletionCallback cb;
2459 2411
2460 const int kNumberOfCaches = 2; 2412 const int kNumberOfCaches = 2;
2461 disk_cache::Backend* cache[kNumberOfCaches]; 2413 disk_cache::Backend* cache[kNumberOfCaches];
2462 2414
2463 int rv = disk_cache::BackendImpl::CreateBackend( 2415 int rv = disk_cache::CreateCacheBackend(
2464 store1.path(), false, 0, net::DISK_CACHE, disk_cache::kNone, 2416 net::DISK_CACHE, store1.path(), 0, false,
2465 cache_thread.message_loop_proxy(), NULL, &cache[0], cb.callback()); 2417 cache_thread.message_loop_proxy(), NULL, &cache[0], cb.callback());
2466 ASSERT_EQ(net::OK, cb.GetResult(rv)); 2418 ASSERT_EQ(net::OK, cb.GetResult(rv));
2467 rv = disk_cache::BackendImpl::CreateBackend( 2419 rv = disk_cache::CreateCacheBackend(
2468 store2.path(), false, 0, net::MEDIA_CACHE, disk_cache::kNone, 2420 net::MEDIA_CACHE, store2.path(), 0, false,
2469 cache_thread.message_loop_proxy(), NULL, &cache[1], cb.callback()); 2421 cache_thread.message_loop_proxy(), NULL, &cache[1], cb.callback());
2470 ASSERT_EQ(net::OK, cb.GetResult(rv)); 2422 ASSERT_EQ(net::OK, cb.GetResult(rv));
2471 2423
2472 ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL); 2424 ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL);
2473 2425
2474 std::string key("the first key"); 2426 std::string key("the first key");
2475 disk_cache::Entry* entry; 2427 disk_cache::Entry* entry;
2476 for (int i = 0; i < kNumberOfCaches; i++) { 2428 for (int i = 0; i < kNumberOfCaches; i++) {
2477 rv = cache[i]->CreateEntry(key, &entry, cb.callback()); 2429 rv = cache[i]->CreateEntry(key, &entry, cb.callback());
2478 ASSERT_EQ(net::OK, cb.GetResult(rv)); 2430 ASSERT_EQ(net::OK, cb.GetResult(rv));
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
2527 // Region 6: expected = kint32max 2479 // Region 6: expected = kint32max
2528 EXPECT_EQ(kint32max, 2480 EXPECT_EQ(kint32max,
2529 disk_cache::PreferedCacheSize(largest_size * 100)); 2481 disk_cache::PreferedCacheSize(largest_size * 100));
2530 EXPECT_EQ(kint32max, 2482 EXPECT_EQ(kint32max,
2531 disk_cache::PreferedCacheSize(largest_size * 10000)); 2483 disk_cache::PreferedCacheSize(largest_size * 10000));
2532 } 2484 }
2533 2485
2534 // Tests that we can "migrate" a running instance from one experiment group to 2486 // Tests that we can "migrate" a running instance from one experiment group to
2535 // another. 2487 // another.
2536 TEST_F(DiskCacheBackendTest, Histograms) { 2488 TEST_F(DiskCacheBackendTest, Histograms) {
2537 SetDirectMode();
2538 InitCache(); 2489 InitCache();
2539 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro. 2490 disk_cache::BackendImpl* backend_ = cache_impl_; // Needed be the macro.
2540 2491
2541 for (int i = 1; i < 3; i++) { 2492 for (int i = 1; i < 3; i++) {
2542 CACHE_UMA(HOURS, "FillupTime", i, 28); 2493 CACHE_UMA(HOURS, "FillupTime", i, 28);
2543 } 2494 }
2544 } 2495 }
2545 2496
2546 // Make sure that we keep the total memory used by the internal buffers under 2497 // Make sure that we keep the total memory used by the internal buffers under
2547 // control. 2498 // control.
2548 TEST_F(DiskCacheBackendTest, TotalBuffersSize1) { 2499 TEST_F(DiskCacheBackendTest, TotalBuffersSize1) {
2549 SetDirectMode();
2550 InitCache(); 2500 InitCache();
2551 std::string key("the first key"); 2501 std::string key("the first key");
2552 disk_cache::Entry* entry; 2502 disk_cache::Entry* entry;
2553 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 2503 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2554 2504
2555 const int kSize = 200; 2505 const int kSize = 200;
2556 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 2506 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
2557 CacheTestFillBuffer(buffer->data(), kSize, true); 2507 CacheTestFillBuffer(buffer->data(), kSize, true);
2558 2508
2559 for (int i = 0; i < 10; i++) { 2509 for (int i = 0; i < 10; i++) {
(...skipping 12 matching lines...) Expand all
2572 entry->Close(); 2522 entry->Close();
2573 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 2523 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
2574 } 2524 }
2575 2525
2576 entry->Close(); 2526 entry->Close();
2577 EXPECT_EQ(0, cache_impl_->GetTotalBuffersSize()); 2527 EXPECT_EQ(0, cache_impl_->GetTotalBuffersSize());
2578 } 2528 }
2579 2529
2580 // This test assumes at least 150MB of system memory. 2530 // This test assumes at least 150MB of system memory.
2581 TEST_F(DiskCacheBackendTest, TotalBuffersSize2) { 2531 TEST_F(DiskCacheBackendTest, TotalBuffersSize2) {
2582 SetDirectMode();
2583 InitCache(); 2532 InitCache();
2584 2533
2585 const int kOneMB = 1024 * 1024; 2534 const int kOneMB = 1024 * 1024;
2586 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB)); 2535 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB));
2587 EXPECT_EQ(kOneMB, cache_impl_->GetTotalBuffersSize()); 2536 EXPECT_EQ(kOneMB, cache_impl_->GetTotalBuffersSize());
2588 2537
2589 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB)); 2538 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB));
2590 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize()); 2539 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize());
2591 2540
2592 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB)); 2541 EXPECT_TRUE(cache_impl_->IsAllocAllowed(0, kOneMB));
2593 EXPECT_EQ(kOneMB * 3, cache_impl_->GetTotalBuffersSize()); 2542 EXPECT_EQ(kOneMB * 3, cache_impl_->GetTotalBuffersSize());
2594 2543
2595 cache_impl_->BufferDeleted(kOneMB); 2544 cache_impl_->BufferDeleted(kOneMB);
2596 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize()); 2545 EXPECT_EQ(kOneMB * 2, cache_impl_->GetTotalBuffersSize());
2597 2546
2598 // Check the upper limit. 2547 // Check the upper limit.
2599 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, 30 * kOneMB)); 2548 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, 30 * kOneMB));
2600 2549
2601 for (int i = 0; i < 30; i++) 2550 for (int i = 0; i < 30; i++)
2602 cache_impl_->IsAllocAllowed(0, kOneMB); // Ignore the result. 2551 cache_impl_->IsAllocAllowed(0, kOneMB); // Ignore the result.
2603 2552
2604 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, kOneMB)); 2553 EXPECT_FALSE(cache_impl_->IsAllocAllowed(0, kOneMB));
2605 } 2554 }
2606 2555
2607 // Tests that sharing of external files works and we are able to delete the 2556 // Tests that sharing of external files works and we are able to delete the
2608 // files when we need to. 2557 // files when we need to.
2609 TEST_F(DiskCacheBackendTest, FileSharing) { 2558 TEST_F(DiskCacheBackendTest, FileSharing) {
2610 SetDirectMode();
2611 InitCache(); 2559 InitCache();
2612 2560
2613 disk_cache::Addr address(0x80000001); 2561 disk_cache::Addr address(0x80000001);
2614 ASSERT_TRUE(cache_impl_->CreateExternalFile(&address)); 2562 ASSERT_TRUE(cache_impl_->CreateExternalFile(&address));
2615 base::FilePath name = cache_impl_->GetFileName(address); 2563 base::FilePath name = cache_impl_->GetFileName(address);
2616 2564
2617 scoped_refptr<disk_cache::File> file(new disk_cache::File(false)); 2565 scoped_refptr<disk_cache::File> file(new disk_cache::File(false));
2618 file->Init(name); 2566 file->Init(name);
2619 2567
2620 #if defined(OS_WIN) 2568 #if defined(OS_WIN)
(...skipping 18 matching lines...) Expand all
2639 memset(buffer1, 't', kSize); 2587 memset(buffer1, 't', kSize);
2640 memset(buffer2, 0, kSize); 2588 memset(buffer2, 0, kSize);
2641 EXPECT_TRUE(file->Write(buffer1, kSize, 0)); 2589 EXPECT_TRUE(file->Write(buffer1, kSize, 0));
2642 EXPECT_TRUE(file->Read(buffer2, kSize, 0)); 2590 EXPECT_TRUE(file->Read(buffer2, kSize, 0));
2643 EXPECT_EQ(0, memcmp(buffer1, buffer2, kSize)); 2591 EXPECT_EQ(0, memcmp(buffer1, buffer2, kSize));
2644 2592
2645 EXPECT_TRUE(disk_cache::DeleteCacheFile(name)); 2593 EXPECT_TRUE(disk_cache::DeleteCacheFile(name));
2646 } 2594 }
2647 2595
2648 TEST_F(DiskCacheBackendTest, UpdateRankForExternalCacheHit) { 2596 TEST_F(DiskCacheBackendTest, UpdateRankForExternalCacheHit) {
2649 SetDirectMode();
2650 InitCache(); 2597 InitCache();
2651 2598
2652 disk_cache::Entry* entry; 2599 disk_cache::Entry* entry;
2653 2600
2654 for (int i = 0; i < 2; ++i) { 2601 for (int i = 0; i < 2; ++i) {
2655 std::string key = StringPrintf("key%d", i); 2602 std::string key = StringPrintf("key%d", i);
2656 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 2603 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2657 entry->Close(); 2604 entry->Close();
2658 } 2605 }
2659 2606
2660 // Ping the oldest entry. 2607 // Ping the oldest entry.
2661 cache_->OnExternalCacheHit("key0"); 2608 cache_->OnExternalCacheHit("key0");
2662 2609
2663 TrimForTest(false); 2610 TrimForTest(false);
2664 2611
2665 // Make sure the older key remains. 2612 // Make sure the older key remains.
2666 EXPECT_EQ(1, cache_->GetEntryCount()); 2613 EXPECT_EQ(1, cache_->GetEntryCount());
2667 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); 2614 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
2668 entry->Close(); 2615 entry->Close();
2669 } 2616 }
2670 2617
2671 TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) { 2618 TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) {
2672 SetCacheType(net::SHADER_CACHE); 2619 SetCacheType(net::SHADER_CACHE);
2673 SetDirectMode();
2674 InitCache(); 2620 InitCache();
2675 2621
2676 disk_cache::Entry* entry; 2622 disk_cache::Entry* entry;
2677 2623
2678 for (int i = 0; i < 2; ++i) { 2624 for (int i = 0; i < 2; ++i) {
2679 std::string key = StringPrintf("key%d", i); 2625 std::string key = StringPrintf("key%d", i);
2680 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 2626 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2681 entry->Close(); 2627 entry->Close();
2682 } 2628 }
2683 2629
2684 // Ping the oldest entry. 2630 // Ping the oldest entry.
2685 cache_->OnExternalCacheHit("key0"); 2631 cache_->OnExternalCacheHit("key0");
2686 2632
2687 TrimForTest(false); 2633 TrimForTest(false);
2688 2634
2689 // Make sure the older key remains. 2635 // Make sure the older key remains.
2690 EXPECT_EQ(1, cache_->GetEntryCount()); 2636 EXPECT_EQ(1, cache_->GetEntryCount());
2691 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); 2637 ASSERT_EQ(net::OK, OpenEntry("key0", &entry));
2692 entry->Close(); 2638 entry->Close();
2693 } 2639 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698