summaryrefslogtreecommitdiffstats
path: root/net/disk_cache
diff options
context:
space:
mode:
authorrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-30 21:41:54 +0000
committerrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-30 21:41:54 +0000
commita9da16d0e7aa10ad2f4ee3290edc361b862e6314 (patch)
treea5ecba94190bd01ae479f7274d450296ad241f11 /net/disk_cache
parent01e9c8a645db02edb5b0c8b33ff091f55b47dbd5 (diff)
downloadchromium_src-a9da16d0e7aa10ad2f4ee3290edc361b862e6314.zip
chromium_src-a9da16d0e7aa10ad2f4ee3290edc361b862e6314.tar.gz
chromium_src-a9da16d0e7aa10ad2f4ee3290edc361b862e6314.tar.bz2
Add unit tests to the disk cache to verify that a failure during cache reinitialization is handled properly.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@137 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache')
-rw-r--r--net/disk_cache/backend_impl.cc5
-rw-r--r--net/disk_cache/backend_unittest.cc100
-rw-r--r--net/disk_cache/disk_cache_test_base.cc11
-rw-r--r--net/disk_cache/disk_cache_test_base.h9
4 files changed, 86 insertions, 39 deletions
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc
index 790b618..7810a97 100644
--- a/net/disk_cache/backend_impl.cc
+++ b/net/disk_cache/backend_impl.cc
@@ -1037,7 +1037,10 @@ void BackendImpl::RestartCache() {
init_ = false;
restarted_ = true;
int64 errors = stats_.GetCounter(Stats::FATAL_ERROR);
- if (Init())
+
+ // Don't call Init() if directed by the unit test: we are simulating a failure
+ // trying to re-enable the cache.
+ if (!unit_test_ && Init())
stats_.SetCounter(Stats::FATAL_ERROR, errors + 1);
}
diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc
index 0514758..cbdc450 100644
--- a/net/disk_cache/backend_unittest.cc
+++ b/net/disk_cache/backend_unittest.cc
@@ -111,6 +111,9 @@ class DiskCacheBackendTest : public DiskCacheTestBase {
void BackendDoomRecent();
void BackendDoomBetween();
void BackendDoomAll();
+ void BackendInvalidRankings();
+ void BackendDisable();
+ void BackendDisable2();
};
void DiskCacheBackendTest::BackendBasics() {
@@ -809,71 +812,98 @@ TEST(DiskCacheTest, Backend_InvalidRankings) {
}
// If the LRU is corrupt, we delete the cache.
-TEST(DiskCacheTest, Backend_InvalidRankings2) {
- ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
- std::wstring path = GetCachePath();
- disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0);
- ASSERT_TRUE(NULL != cache);
-
+void DiskCacheBackendTest::BackendInvalidRankings() {
disk_cache::Entry* entry;
void* iter = NULL;
- ASSERT_TRUE(cache->OpenNextEntry(&iter, &entry));
+ ASSERT_TRUE(cache_->OpenNextEntry(&iter, &entry));
entry->Close();
- EXPECT_EQ(2, cache->GetEntryCount());
+ EXPECT_EQ(2, cache_->GetEntryCount());
- EXPECT_FALSE(cache->OpenNextEntry(&iter, &entry));
- EXPECT_EQ(0, cache->GetEntryCount());
+ EXPECT_FALSE(cache_->OpenNextEntry(&iter, &entry));
+ EXPECT_EQ(0, cache_->GetEntryCount());
+}
- delete cache;
- EXPECT_TRUE(CheckCacheIntegrity(path));
+TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) {
+ ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
+ DisableFirstCleanup();
+ SetDirectMode();
+ InitCache();
+ BackendInvalidRankings();
}
-// If the LRU is corrupt and we have open entries, we disable the cache.
-TEST(DiskCacheTest, Backend_Disable) {
+TEST_F(DiskCacheBackendTest, InvalidRankingsFailure) {
ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
- std::wstring path = GetCachePath();
- disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0);
- ASSERT_TRUE(NULL != cache);
+ DisableFirstCleanup();
+ SetDirectMode();
+ InitCache();
+ SetTestMode(); // Fail cache reinitialization.
+ BackendInvalidRankings();
+}
+// If the LRU is corrupt and we have open entries, we disable the cache.
+void DiskCacheBackendTest::BackendDisable() {
disk_cache::Entry *entry1, *entry2;
void* iter = NULL;
- ASSERT_TRUE(cache->OpenNextEntry(&iter, &entry1));
+ ASSERT_TRUE(cache_->OpenNextEntry(&iter, &entry1));
- EXPECT_FALSE(cache->OpenNextEntry(&iter, &entry2));
- EXPECT_EQ(2, cache->GetEntryCount());
- EXPECT_FALSE(cache->CreateEntry("Something new", &entry2));
+ EXPECT_FALSE(cache_->OpenNextEntry(&iter, &entry2));
+ EXPECT_EQ(2, cache_->GetEntryCount());
+ EXPECT_FALSE(cache_->CreateEntry("Something new", &entry2));
entry1->Close();
- EXPECT_EQ(0, cache->GetEntryCount());
+ EXPECT_EQ(0, cache_->GetEntryCount());
+}
- delete cache;
- EXPECT_TRUE(CheckCacheIntegrity(path));
+TEST_F(DiskCacheBackendTest, DisableSuccess) {
+ ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
+ DisableFirstCleanup();
+ SetDirectMode();
+ InitCache();
+ BackendDisable();
}
-// This is another type of corruption on the LRU; disable the cache.
-TEST(DiskCacheTest, Backend_Disable2) {
- ASSERT_TRUE(CopyTestCache(L"list_loop"));
- std::wstring path = GetCachePath();
- disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0);
- ASSERT_TRUE(NULL != cache);
+TEST_F(DiskCacheBackendTest, DisableFailure) {
+ ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
+ DisableFirstCleanup();
+ SetDirectMode();
+ InitCache();
+ SetTestMode(); // Fail cache reinitialization.
+ BackendDisable();
+}
- EXPECT_EQ(8, cache->GetEntryCount());
+// This is another type of corruption on the LRU; disable the cache.
+void DiskCacheBackendTest::BackendDisable2() {
+ EXPECT_EQ(8, cache_->GetEntryCount());
disk_cache::Entry* entry;
void* iter = NULL;
int count = 0;
- while (cache->OpenNextEntry(&iter, &entry)) {
+ while (cache_->OpenNextEntry(&iter, &entry)) {
ASSERT_TRUE(NULL != entry);
entry->Close();
count++;
ASSERT_LT(count, 9);
};
- EXPECT_EQ(0, cache->GetEntryCount());
+ EXPECT_EQ(0, cache_->GetEntryCount());
+}
- delete cache;
- EXPECT_TRUE(CheckCacheIntegrity(path));
+TEST_F(DiskCacheBackendTest, DisableSuccess2) {
+ ASSERT_TRUE(CopyTestCache(L"list_loop"));
+ DisableFirstCleanup();
+ SetDirectMode();
+ InitCache();
+ BackendDisable2();
+}
+
+TEST_F(DiskCacheBackendTest, DisableFailure2) {
+ ASSERT_TRUE(CopyTestCache(L"list_loop"));
+ DisableFirstCleanup();
+ SetDirectMode();
+ InitCache();
+ SetTestMode(); // Fail cache reinitialization.
+ BackendDisable2();
}
TEST(DiskCacheTest, Backend_UsageStats) {
diff --git a/net/disk_cache/disk_cache_test_base.cc b/net/disk_cache/disk_cache_test_base.cc
index 57ab31f..1caa95a 100644
--- a/net/disk_cache/disk_cache_test_base.cc
+++ b/net/disk_cache/disk_cache_test_base.cc
@@ -52,7 +52,8 @@ void DiskCacheTestBase::InitCache() {
InitDiskCache();
ASSERT_TRUE(NULL != cache_);
- ASSERT_EQ(0, cache_->GetEntryCount());
+ if (first_cleanup_)
+ ASSERT_EQ(0, cache_->GetEntryCount());
}
void DiskCacheTestBase::InitMemoryCache() {
@@ -73,7 +74,8 @@ void DiskCacheTestBase::InitMemoryCache() {
void DiskCacheTestBase::InitDiskCache() {
std::wstring path = GetCachePath();
- ASSERT_TRUE(DeleteCache(path.c_str()));
+ if (first_cleanup_)
+ ASSERT_TRUE(DeleteCache(path.c_str()));
if (!implementation_) {
cache_ = disk_cache::CreateCacheBackend(path, force_creation_, size_);
@@ -124,3 +126,8 @@ void DiskCacheTestBase::SimulateCrash() {
cache_impl_->SetMaxSize(size_);
ASSERT_TRUE(cache_impl_->Init());
}
+
+void DiskCacheTestBase::SetTestMode() {
+ ASSERT_TRUE(implementation_ && !memory_only_);
+ cache_impl_->SetUnitTestMode();
+}
diff --git a/net/disk_cache/disk_cache_test_base.h b/net/disk_cache/disk_cache_test_base.h
index feb22b2..1c18afe 100644
--- a/net/disk_cache/disk_cache_test_base.h
+++ b/net/disk_cache/disk_cache_test_base.h
@@ -46,11 +46,13 @@ class DiskCacheTestBase : public testing::Test {
protected:
DiskCacheTestBase()
: cache_(NULL), cache_impl_(NULL), mem_cache_(NULL), mask_(0), size_(0),
- memory_only_(false), implementation_(false), force_creation_(false) {}
+ memory_only_(false), implementation_(false), force_creation_(false),
+ first_cleanup_(true) {}
void InitCache();
virtual void TearDown();
void SimulateCrash();
+ void SetTestMode();
void SetMemoryOnlyMode() {
memory_only_ = true;
@@ -72,6 +74,10 @@ class DiskCacheTestBase : public testing::Test {
force_creation_ = true;
}
+ void DisableFirstCleanup() {
+ first_cleanup_ = false;
+ }
+
// cache_ will always have a valid object, regardless of how the cache was
// initialized. The implementation pointers can be NULL.
disk_cache::Backend* cache_;
@@ -83,6 +89,7 @@ class DiskCacheTestBase : public testing::Test {
bool memory_only_;
bool implementation_;
bool force_creation_;
+ bool first_cleanup_;
private:
void InitMemoryCache();