Commit a9da16d0 authored by rvargas@google.com's avatar rvargas@google.com

Add unit tests to the disk cache to verify that a failure during cache...

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
parent 01e9c8a6
...@@ -1037,7 +1037,10 @@ void BackendImpl::RestartCache() { ...@@ -1037,7 +1037,10 @@ void BackendImpl::RestartCache() {
init_ = false; init_ = false;
restarted_ = true; restarted_ = true;
int64 errors = stats_.GetCounter(Stats::FATAL_ERROR); 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); stats_.SetCounter(Stats::FATAL_ERROR, errors + 1);
} }
......
...@@ -111,6 +111,9 @@ class DiskCacheBackendTest : public DiskCacheTestBase { ...@@ -111,6 +111,9 @@ class DiskCacheBackendTest : public DiskCacheTestBase {
void BackendDoomRecent(); void BackendDoomRecent();
void BackendDoomBetween(); void BackendDoomBetween();
void BackendDoomAll(); void BackendDoomAll();
void BackendInvalidRankings();
void BackendDisable();
void BackendDisable2();
}; };
void DiskCacheBackendTest::BackendBasics() { void DiskCacheBackendTest::BackendBasics() {
...@@ -809,71 +812,98 @@ TEST(DiskCacheTest, Backend_InvalidRankings) { ...@@ -809,71 +812,98 @@ TEST(DiskCacheTest, Backend_InvalidRankings) {
} }
// If the LRU is corrupt, we delete the cache. // If the LRU is corrupt, we delete the cache.
TEST(DiskCacheTest, Backend_InvalidRankings2) { void DiskCacheBackendTest::BackendInvalidRankings() {
ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
std::wstring path = GetCachePath();
disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0);
ASSERT_TRUE(NULL != cache);
disk_cache::Entry* entry; disk_cache::Entry* entry;
void* iter = NULL; void* iter = NULL;
ASSERT_TRUE(cache->OpenNextEntry(&iter, &entry)); ASSERT_TRUE(cache_->OpenNextEntry(&iter, &entry));
entry->Close(); entry->Close();
EXPECT_EQ(2, cache->GetEntryCount()); EXPECT_EQ(2, cache_->GetEntryCount());
EXPECT_FALSE(cache->OpenNextEntry(&iter, &entry)); EXPECT_FALSE(cache_->OpenNextEntry(&iter, &entry));
EXPECT_EQ(0, cache->GetEntryCount()); EXPECT_EQ(0, cache_->GetEntryCount());
}
delete cache; TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) {
EXPECT_TRUE(CheckCacheIntegrity(path)); 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_F(DiskCacheBackendTest, InvalidRankingsFailure) {
TEST(DiskCacheTest, Backend_Disable) {
ASSERT_TRUE(CopyTestCache(L"bad_rankings")); ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
std::wstring path = GetCachePath(); DisableFirstCleanup();
disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0); SetDirectMode();
ASSERT_TRUE(NULL != cache); 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; disk_cache::Entry *entry1, *entry2;
void* iter = NULL; void* iter = NULL;
ASSERT_TRUE(cache->OpenNextEntry(&iter, &entry1)); ASSERT_TRUE(cache_->OpenNextEntry(&iter, &entry1));
EXPECT_FALSE(cache->OpenNextEntry(&iter, &entry2)); EXPECT_FALSE(cache_->OpenNextEntry(&iter, &entry2));
EXPECT_EQ(2, cache->GetEntryCount()); EXPECT_EQ(2, cache_->GetEntryCount());
EXPECT_FALSE(cache->CreateEntry("Something new", &entry2)); EXPECT_FALSE(cache_->CreateEntry("Something new", &entry2));
entry1->Close(); entry1->Close();
EXPECT_EQ(0, cache->GetEntryCount()); EXPECT_EQ(0, cache_->GetEntryCount());
}
delete cache; TEST_F(DiskCacheBackendTest, DisableSuccess) {
EXPECT_TRUE(CheckCacheIntegrity(path)); ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
DisableFirstCleanup();
SetDirectMode();
InitCache();
BackendDisable();
} }
// This is another type of corruption on the LRU; disable the cache. TEST_F(DiskCacheBackendTest, DisableFailure) {
TEST(DiskCacheTest, Backend_Disable2) { ASSERT_TRUE(CopyTestCache(L"bad_rankings"));
ASSERT_TRUE(CopyTestCache(L"list_loop")); DisableFirstCleanup();
std::wstring path = GetCachePath(); SetDirectMode();
disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0); InitCache();
ASSERT_TRUE(NULL != cache); 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; disk_cache::Entry* entry;
void* iter = NULL; void* iter = NULL;
int count = 0; int count = 0;
while (cache->OpenNextEntry(&iter, &entry)) { while (cache_->OpenNextEntry(&iter, &entry)) {
ASSERT_TRUE(NULL != entry); ASSERT_TRUE(NULL != entry);
entry->Close(); entry->Close();
count++; count++;
ASSERT_LT(count, 9); ASSERT_LT(count, 9);
}; };
EXPECT_EQ(0, cache->GetEntryCount()); EXPECT_EQ(0, cache_->GetEntryCount());
}
delete cache; TEST_F(DiskCacheBackendTest, DisableSuccess2) {
EXPECT_TRUE(CheckCacheIntegrity(path)); 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) { TEST(DiskCacheTest, Backend_UsageStats) {
......
...@@ -52,7 +52,8 @@ void DiskCacheTestBase::InitCache() { ...@@ -52,7 +52,8 @@ void DiskCacheTestBase::InitCache() {
InitDiskCache(); InitDiskCache();
ASSERT_TRUE(NULL != cache_); ASSERT_TRUE(NULL != cache_);
ASSERT_EQ(0, cache_->GetEntryCount()); if (first_cleanup_)
ASSERT_EQ(0, cache_->GetEntryCount());
} }
void DiskCacheTestBase::InitMemoryCache() { void DiskCacheTestBase::InitMemoryCache() {
...@@ -73,7 +74,8 @@ void DiskCacheTestBase::InitMemoryCache() { ...@@ -73,7 +74,8 @@ void DiskCacheTestBase::InitMemoryCache() {
void DiskCacheTestBase::InitDiskCache() { void DiskCacheTestBase::InitDiskCache() {
std::wstring path = GetCachePath(); std::wstring path = GetCachePath();
ASSERT_TRUE(DeleteCache(path.c_str())); if (first_cleanup_)
ASSERT_TRUE(DeleteCache(path.c_str()));
if (!implementation_) { if (!implementation_) {
cache_ = disk_cache::CreateCacheBackend(path, force_creation_, size_); cache_ = disk_cache::CreateCacheBackend(path, force_creation_, size_);
...@@ -124,3 +126,8 @@ void DiskCacheTestBase::SimulateCrash() { ...@@ -124,3 +126,8 @@ void DiskCacheTestBase::SimulateCrash() {
cache_impl_->SetMaxSize(size_); cache_impl_->SetMaxSize(size_);
ASSERT_TRUE(cache_impl_->Init()); ASSERT_TRUE(cache_impl_->Init());
} }
void DiskCacheTestBase::SetTestMode() {
ASSERT_TRUE(implementation_ && !memory_only_);
cache_impl_->SetUnitTestMode();
}
...@@ -46,11 +46,13 @@ class DiskCacheTestBase : public testing::Test { ...@@ -46,11 +46,13 @@ class DiskCacheTestBase : public testing::Test {
protected: protected:
DiskCacheTestBase() DiskCacheTestBase()
: cache_(NULL), cache_impl_(NULL), mem_cache_(NULL), mask_(0), size_(0), : 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(); void InitCache();
virtual void TearDown(); virtual void TearDown();
void SimulateCrash(); void SimulateCrash();
void SetTestMode();
void SetMemoryOnlyMode() { void SetMemoryOnlyMode() {
memory_only_ = true; memory_only_ = true;
...@@ -72,6 +74,10 @@ class DiskCacheTestBase : public testing::Test { ...@@ -72,6 +74,10 @@ class DiskCacheTestBase : public testing::Test {
force_creation_ = true; force_creation_ = true;
} }
void DisableFirstCleanup() {
first_cleanup_ = false;
}
// cache_ will always have a valid object, regardless of how the cache was // cache_ will always have a valid object, regardless of how the cache was
// initialized. The implementation pointers can be NULL. // initialized. The implementation pointers can be NULL.
disk_cache::Backend* cache_; disk_cache::Backend* cache_;
...@@ -83,6 +89,7 @@ class DiskCacheTestBase : public testing::Test { ...@@ -83,6 +89,7 @@ class DiskCacheTestBase : public testing::Test {
bool memory_only_; bool memory_only_;
bool implementation_; bool implementation_;
bool force_creation_; bool force_creation_;
bool first_cleanup_;
private: private:
void InitMemoryCache(); void InitMemoryCache();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment