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

Unified Diff: net/disk_cache/blockfile/stats_unittest.cc

Issue 980003002: Disk cache: Re-initialize stats counters if they are zero on-disk. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix typo Created 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/disk_cache/blockfile/stats.cc ('k') | net/net.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/disk_cache/blockfile/stats_unittest.cc
diff --git a/net/disk_cache/blockfile/stats_unittest.cc b/net/disk_cache/blockfile/stats_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fe47bdd4cf634861feb5c74e1e5fdf371840d13f
--- /dev/null
+++ b/net/disk_cache/blockfile/stats_unittest.cc
@@ -0,0 +1,83 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/disk_cache/blockfile/stats.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(DiskCacheStatsTest, Init) {
+ disk_cache::Stats stats;
+ EXPECT_TRUE(stats.Init(nullptr, 0, disk_cache::Addr()));
+ EXPECT_EQ(0, stats.GetCounter(disk_cache::Stats::TRIM_ENTRY));
+}
+
+TEST(DiskCacheStatsTest, InitWithEmptyBuffer) {
+ disk_cache::Stats stats;
+ int required_len = stats.StorageSize();
+ scoped_ptr<char[]> storage(new char[required_len]);
+ memset(storage.get(), 0, required_len);
+
+ ASSERT_TRUE(stats.Init(storage.get(), required_len, disk_cache::Addr()));
+ EXPECT_EQ(0, stats.GetCounter(disk_cache::Stats::TRIM_ENTRY));
+}
+
+TEST(DiskCacheStatsTest, FailsInit) {
+ disk_cache::Stats stats;
+ int required_len = stats.StorageSize();
+ scoped_ptr<char[]> storage(new char[required_len]);
+ memset(storage.get(), 0, required_len);
+
+ // Try a small buffer.
+ EXPECT_LT(200, required_len);
+ disk_cache::Addr addr;
+ EXPECT_FALSE(stats.Init(storage.get(), 200, addr));
+
+ // Try a buffer with garbage.
+ memset(storage.get(), 'a', required_len);
+ EXPECT_FALSE(stats.Init(storage.get(), required_len, addr));
+}
+
+TEST(DiskCacheStatsTest, SaveRestore) {
+ scoped_ptr<disk_cache::Stats> stats(new disk_cache::Stats);
+
+ disk_cache::Addr addr(5);
+ ASSERT_TRUE(stats->Init(nullptr, 0, addr));
+ stats->SetCounter(disk_cache::Stats::CREATE_ERROR, 11);
+ stats->SetCounter(disk_cache::Stats::DOOM_ENTRY, 13);
+ stats->OnEvent(disk_cache::Stats::MIN_COUNTER);
+ stats->OnEvent(disk_cache::Stats::TRIM_ENTRY);
+ stats->OnEvent(disk_cache::Stats::DOOM_RECENT);
+
+ int required_len = stats->StorageSize();
+ scoped_ptr<char[]> storage(new char[required_len]);
+ disk_cache::Addr out_addr;
+ int real_len = stats->SerializeStats(storage.get(), required_len, &out_addr);
+ EXPECT_GE(required_len, real_len);
+ EXPECT_EQ(out_addr, addr);
+
+ stats.reset(new disk_cache::Stats);
+ ASSERT_TRUE(stats->Init(storage.get(), real_len, addr));
+ EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::MIN_COUNTER));
+ EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::TRIM_ENTRY));
+ EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::DOOM_RECENT));
+ EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::OPEN_HIT));
+ EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::READ_DATA));
+ EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::LAST_REPORT_TIMER));
+ EXPECT_EQ(11, stats->GetCounter(disk_cache::Stats::CREATE_ERROR));
+ EXPECT_EQ(13, stats->GetCounter(disk_cache::Stats::DOOM_ENTRY));
+
+ // Now pass the whole buffer. It shoulod not matter that there is unused
+ // space at the end.
+ stats.reset(new disk_cache::Stats);
+ ASSERT_TRUE(stats->Init(storage.get(), required_len, addr));
+ EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::MIN_COUNTER));
+ EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::TRIM_ENTRY));
+ EXPECT_EQ(1, stats->GetCounter(disk_cache::Stats::DOOM_RECENT));
+ EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::OPEN_HIT));
+ EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::READ_DATA));
+ EXPECT_EQ(0, stats->GetCounter(disk_cache::Stats::LAST_REPORT_TIMER));
+ EXPECT_EQ(11, stats->GetCounter(disk_cache::Stats::CREATE_ERROR));
+ EXPECT_EQ(13, stats->GetCounter(disk_cache::Stats::DOOM_ENTRY));
+}
« no previous file with comments | « net/disk_cache/blockfile/stats.cc ('k') | net/net.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698