| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/platform_thread.h" | 8 #include "base/platform_thread.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 protected: | 41 protected: |
| 42 void BackendBasics(); | 42 void BackendBasics(); |
| 43 void BackendKeying(); | 43 void BackendKeying(); |
| 44 void BackendSetSize(); | 44 void BackendSetSize(); |
| 45 void BackendLoad(); | 45 void BackendLoad(); |
| 46 void BackendValidEntry(); | 46 void BackendValidEntry(); |
| 47 void BackendInvalidEntry(); | 47 void BackendInvalidEntry(); |
| 48 void BackendInvalidEntryRead(); | 48 void BackendInvalidEntryRead(); |
| 49 void BackendInvalidEntryWithLoad(); | 49 void BackendInvalidEntryWithLoad(); |
| 50 void BackendTrimInvalidEntry(); | 50 void BackendTrimInvalidEntry(); |
| 51 void BackendTrimInvalidEntry2(); |
| 51 void BackendEnumerations(); | 52 void BackendEnumerations(); |
| 52 void BackendInvalidEntryEnumeration(); | 53 void BackendInvalidEntryEnumeration(); |
| 53 void BackendFixEnumerators(); | 54 void BackendFixEnumerators(); |
| 54 void BackendDoomRecent(); | 55 void BackendDoomRecent(); |
| 55 void BackendDoomBetween(); | 56 void BackendDoomBetween(); |
| 56 void BackendTransaction(const std::wstring& name, int num_entries, bool load); | 57 void BackendTransaction(const std::wstring& name, int num_entries, bool load); |
| 57 void BackendRecoverInsert(); | 58 void BackendRecoverInsert(); |
| 58 void BackendRecoverRemove(); | 59 void BackendRecoverRemove(); |
| 59 void BackendInvalidEntry2(); | 60 void BackendInvalidEntry2(); |
| 60 void BackendNotMarkedButDirty(const std::wstring& name); | 61 void BackendNotMarkedButDirty(const std::wstring& name); |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntryWithLoad) { | 487 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntryWithLoad) { |
| 487 SetNewEviction(); | 488 SetNewEviction(); |
| 488 BackendInvalidEntryWithLoad(); | 489 BackendInvalidEntryWithLoad(); |
| 489 } | 490 } |
| 490 | 491 |
| 491 // We'll be leaking memory from this test. | 492 // We'll be leaking memory from this test. |
| 492 void DiskCacheBackendTest::BackendTrimInvalidEntry() { | 493 void DiskCacheBackendTest::BackendTrimInvalidEntry() { |
| 493 // Use the implementation directly... we need to simulate a crash. | 494 // Use the implementation directly... we need to simulate a crash. |
| 494 SetDirectMode(); | 495 SetDirectMode(); |
| 495 | 496 |
| 496 const int cache_size = 0x4000; // 16 kB | 497 const int kSize = 0x3000; // 12 kB |
| 497 SetMaxSize(cache_size * 10); | 498 SetMaxSize(kSize * 10); |
| 498 InitCache(); | 499 InitCache(); |
| 499 | 500 |
| 500 std::string first("some key"); | 501 std::string first("some key"); |
| 501 std::string second("something else"); | 502 std::string second("something else"); |
| 502 disk_cache::Entry* entry; | 503 disk_cache::Entry* entry; |
| 503 ASSERT_TRUE(cache_->CreateEntry(first, &entry)); | 504 ASSERT_TRUE(cache_->CreateEntry(first, &entry)); |
| 504 | 505 |
| 505 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(cache_size); | 506 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize); |
| 506 memset(buffer->data(), 0, cache_size); | 507 memset(buffer->data(), 0, kSize); |
| 507 EXPECT_EQ(cache_size * 19 / 20, entry->WriteData(0, 0, buffer, | 508 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false)); |
| 508 cache_size * 19 / 20, NULL, false)); | |
| 509 | 509 |
| 510 // Simulate a crash. | 510 // Simulate a crash. |
| 511 SimulateCrash(); | 511 SimulateCrash(); |
| 512 | 512 |
| 513 ASSERT_TRUE(cache_->CreateEntry(second, &entry)); | 513 ASSERT_TRUE(cache_->CreateEntry(second, &entry)); |
| 514 EXPECT_EQ(cache_size / 10, entry->WriteData(0, 0, buffer, cache_size / 10, | 514 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false)); |
| 515 NULL, false)) << "trim the cache"; | |
| 516 entry->Close(); | |
| 517 | 515 |
| 516 EXPECT_EQ(2, cache_->GetEntryCount()); |
| 517 SetMaxSize(kSize); |
| 518 entry->Close(); // Trim the cache. |
| 519 |
| 520 // If we evicted the entry in less than 20mS, we have one entry in the cache; |
| 521 // if it took more than that, we posted a task and we'll delete the second |
| 522 // entry too. |
| 523 MessageLoop::current()->RunAllPending(); |
| 524 EXPECT_GE(1, cache_->GetEntryCount()); |
| 518 EXPECT_FALSE(cache_->OpenEntry(first, &entry)); | 525 EXPECT_FALSE(cache_->OpenEntry(first, &entry)); |
| 519 EXPECT_EQ(1, cache_->GetEntryCount()); | |
| 520 } | 526 } |
| 521 | 527 |
| 522 // We'll be leaking memory from this test. | 528 // We'll be leaking memory from this test. |
| 523 TEST_F(DiskCacheBackendTest, TrimInvalidEntry) { | 529 TEST_F(DiskCacheBackendTest, TrimInvalidEntry) { |
| 524 BackendTrimInvalidEntry(); | 530 BackendTrimInvalidEntry(); |
| 525 } | 531 } |
| 526 | 532 |
| 527 // We'll be leaking memory from this test. | 533 // We'll be leaking memory from this test. |
| 528 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry) { | 534 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry) { |
| 529 SetNewEviction(); | 535 SetNewEviction(); |
| 530 BackendTrimInvalidEntry(); | 536 BackendTrimInvalidEntry(); |
| 531 } | 537 } |
| 532 | 538 |
| 539 // We'll be leaking memory from this test. |
| 540 void DiskCacheBackendTest::BackendTrimInvalidEntry2() { |
| 541 // Use the implementation directly... we need to simulate a crash. |
| 542 SetDirectMode(); |
| 543 SetMask(0xf); // 16-entry table. |
| 544 |
| 545 const int kSize = 0x3000; // 12 kB |
| 546 SetMaxSize(kSize * 40); |
| 547 InitCache(); |
| 548 |
| 549 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kSize); |
| 550 memset(buffer->data(), 0, kSize); |
| 551 disk_cache::Entry* entry; |
| 552 |
| 553 // Writing 32 entries to this cache chains most of them. |
| 554 for (int i = 0; i < 32; i++) { |
| 555 std::string key(StringPrintf("some key %d", i)); |
| 556 ASSERT_TRUE(cache_->CreateEntry(key, &entry)); |
| 557 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false)); |
| 558 entry->Close(); |
| 559 ASSERT_TRUE(cache_->OpenEntry(key, &entry)); |
| 560 // Note that we are not closing the entries. |
| 561 } |
| 562 |
| 563 // Simulate a crash. |
| 564 SimulateCrash(); |
| 565 |
| 566 ASSERT_TRUE(cache_->CreateEntry("Something else", &entry)); |
| 567 EXPECT_EQ(kSize, entry->WriteData(0, 0, buffer, kSize, NULL, false)); |
| 568 |
| 569 EXPECT_EQ(33, cache_->GetEntryCount()); |
| 570 SetMaxSize(kSize); |
| 571 |
| 572 // For the new eviction code, all corrupt entries are on the second list so |
| 573 // they are not going away that easy. |
| 574 if (new_eviction_) |
| 575 cache_->DoomAllEntries(); |
| 576 |
| 577 entry->Close(); // Trim the cache. |
| 578 |
| 579 // We may abort the eviction before cleaning up everything. |
| 580 MessageLoop::current()->RunAllPending(); |
| 581 EXPECT_GE(30, cache_->GetEntryCount()); |
| 582 } |
| 583 |
| 584 // We'll be leaking memory from this test. |
| 585 TEST_F(DiskCacheBackendTest, TrimInvalidEntry2) { |
| 586 BackendTrimInvalidEntry2(); |
| 587 } |
| 588 |
| 589 // We'll be leaking memory from this test. |
| 590 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry2) { |
| 591 SetNewEviction(); |
| 592 BackendTrimInvalidEntry2(); |
| 593 } |
| 594 |
| 533 void DiskCacheBackendTest::BackendEnumerations() { | 595 void DiskCacheBackendTest::BackendEnumerations() { |
| 534 InitCache(); | 596 InitCache(); |
| 535 Time initial = Time::Now(); | 597 Time initial = Time::Now(); |
| 536 int seed = static_cast<int>(initial.ToInternalValue()); | 598 int seed = static_cast<int>(initial.ToInternalValue()); |
| 537 srand(seed); | 599 srand(seed); |
| 538 | 600 |
| 539 const int kNumEntries = 100; | 601 const int kNumEntries = 100; |
| 540 for (int i = 0; i < kNumEntries; i++) { | 602 for (int i = 0; i < kNumEntries; i++) { |
| 541 std::string key = GenerateKey(true); | 603 std::string key = GenerateKey(true); |
| 542 disk_cache::Entry* entry; | 604 disk_cache::Entry* entry; |
| (...skipping 816 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1359 EXPECT_EQ(kDefaultSize * 5 / 2, | 1421 EXPECT_EQ(kDefaultSize * 5 / 2, |
| 1360 disk_cache::PreferedCacheSize(large_size * 100 / 2)); | 1422 disk_cache::PreferedCacheSize(large_size * 100 / 2)); |
| 1361 EXPECT_EQ(kDefaultSize * 5 / 2, | 1423 EXPECT_EQ(kDefaultSize * 5 / 2, |
| 1362 disk_cache::PreferedCacheSize(large_size * 500 / 2)); | 1424 disk_cache::PreferedCacheSize(large_size * 500 / 2)); |
| 1363 | 1425 |
| 1364 EXPECT_EQ(kDefaultSize * 6 / 2, | 1426 EXPECT_EQ(kDefaultSize * 6 / 2, |
| 1365 disk_cache::PreferedCacheSize(large_size * 600 / 2)); | 1427 disk_cache::PreferedCacheSize(large_size * 600 / 2)); |
| 1366 EXPECT_EQ(kDefaultSize * 7 / 2, | 1428 EXPECT_EQ(kDefaultSize * 7 / 2, |
| 1367 disk_cache::PreferedCacheSize(large_size * 700 / 2)); | 1429 disk_cache::PreferedCacheSize(large_size * 700 / 2)); |
| 1368 } | 1430 } |
| OLD | NEW |