summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorpasko@chromium.org <pasko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-07-24 23:46:51 +0000
committerpasko@chromium.org <pasko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-07-24 23:46:51 +0000
commitd7c76d0f1c28fa3634986ffb6602d055835a72fa (patch)
tree2b2580061e6ca23ea9b7d40c19bc445ccd3b2d51 /net
parent11d3c6552bad2d18f2d1d8d96ffbd523f06f95c7 (diff)
downloadchromium_src-d7c76d0f1c28fa3634986ffb6602d055835a72fa.zip
chromium_src-d7c76d0f1c28fa3634986ffb6602d055835a72fa.tar.gz
chromium_src-d7c76d0f1c28fa3634986ffb6602d055835a72fa.tar.bz2
Simple Cache: Get mtime from the cache directory early
Currently the index is loaded asynchronously while the backend is already initialized and is able to modify the cache directory. This may lead to index file being considered stale without a real reason. To overcome this problem we take the mtime of the directory early during backend initialization and then compare it to the index freshness timestamp. The index in memory will continue to be fresh since we update it with latest knowledge from the backend. This is the first step to add precision into index freshness, following steps are: * move the index file into a separate directory to make sure the rename() operation itself does not make the index stale * Save directory mtime to the index file itself to avoid missing updates while the index is being written to the disk * Read the last index modification time without loading the whole index Some preparations were also made here for the next step: moving the index file to a subdirectory. Mainly it is about moving some knowledge of cache directory structure into the SimpleIndexFile and updating the tests accordingly. Hopefully this won't make reviewing too complicated. BUG=261618 Review URL: https://chromiumcodereview.appspot.com/19519010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@213547 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/disk_cache/simple/simple_backend_impl.cc51
-rw-r--r--net/disk_cache/simple/simple_backend_impl.h20
-rw-r--r--net/disk_cache/simple/simple_index.cc8
-rw-r--r--net/disk_cache/simple/simple_index.h2
-rw-r--r--net/disk_cache/simple/simple_index_file.cc86
-rw-r--r--net/disk_cache/simple/simple_index_file.h17
-rw-r--r--net/disk_cache/simple/simple_index_file_unittest.cc89
-rw-r--r--net/disk_cache/simple/simple_index_unittest.cc3
8 files changed, 168 insertions, 108 deletions
diff --git a/net/disk_cache/simple/simple_backend_impl.cc b/net/disk_cache/simple/simple_backend_impl.cc
index 4037e8e..f0f92c6 100644
--- a/net/disk_cache/simple/simple_backend_impl.cc
+++ b/net/disk_cache/simple/simple_backend_impl.cc
@@ -202,17 +202,21 @@ int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) {
index_->ExecuteWhenReady(base::Bind(&RecordIndexLoad,
base::TimeTicks::Now()));
- InitializeIndexCallback initialize_index_callback =
- base::Bind(&SimpleBackendImpl::InitializeIndex,
- base::Unretained(this),
- completion_callback);
- cache_thread_->PostTask(
- FROM_HERE,
+ scoped_ptr<base::Time> cache_dir_mtime(new base::Time());
+ scoped_ptr<uint64> max_size(new uint64());
+ scoped_ptr<int> result(new int());
+ Closure task =
base::Bind(&SimpleBackendImpl::ProvideDirectorySuggestBetterCacheSize,
- MessageLoopProxy::current(), // io_thread
- path_,
- initialize_index_callback,
- orig_max_size_));
+ path_, orig_max_size_,
+ cache_dir_mtime.get(), max_size.get(),
+ result.get());
+ Closure reply = base::Bind(&SimpleBackendImpl::InitializeIndex,
+ AsWeakPtr(),
+ base::Passed(&max_size),
+ base::Passed(&cache_dir_mtime),
+ base::Passed(&result),
+ completion_callback);
+ cache_thread_->PostTaskAndReply(FROM_HERE, task, reply);
return net::ERR_IO_PENDING;
}
@@ -348,21 +352,24 @@ void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) {
index_->UseIfExists(key);
}
-void SimpleBackendImpl::InitializeIndex(
- const CompletionCallback& callback, uint64 suggested_max_size, int result) {
- if (result == net::OK) {
- index_->SetMaxSize(suggested_max_size);
- index_->Initialize();
+void SimpleBackendImpl::InitializeIndex(scoped_ptr<uint64> suggested_max_size,
+ scoped_ptr<base::Time> cache_dir_mtime,
+ scoped_ptr<int> dir_sanity_check_result,
+ const CompletionCallback& callback) {
+ if (*dir_sanity_check_result == net::OK) {
+ index_->SetMaxSize(*suggested_max_size);
+ index_->Initialize(*cache_dir_mtime);
}
- callback.Run(result);
+ callback.Run(*dir_sanity_check_result);
}
// static
void SimpleBackendImpl::ProvideDirectorySuggestBetterCacheSize(
- SingleThreadTaskRunner* io_thread,
const base::FilePath& path,
- const InitializeIndexCallback& initialize_index_callback,
- uint64 suggested_max_size) {
+ uint64 suggested_max_size,
+ base::Time* out_mtime,
+ uint64* out_max_size,
+ int* out_result) {
int rv = net::OK;
uint64 max_size = suggested_max_size;
if (!FileStructureConsistent(path)) {
@@ -370,6 +377,8 @@ void SimpleBackendImpl::ProvideDirectorySuggestBetterCacheSize(
<< path.LossyDisplayName();
rv = net::ERR_FAILED;
} else {
+ bool mtime_result = simple_util::GetMTime(path, out_mtime);
+ DCHECK(mtime_result);
if (!max_size) {
int64 available = base::SysInfo::AmountOfFreeDiskSpace(path);
if (available < 0)
@@ -381,8 +390,8 @@ void SimpleBackendImpl::ProvideDirectorySuggestBetterCacheSize(
}
DCHECK(max_size);
}
- io_thread->PostTask(FROM_HERE,
- base::Bind(initialize_index_callback, max_size, rv));
+ *out_max_size = max_size;
+ *out_result = rv;
}
scoped_refptr<SimpleEntryImpl> SimpleBackendImpl::CreateOrFindActiveEntry(
diff --git a/net/disk_cache/simple/simple_backend_impl.h b/net/disk_cache/simple/simple_backend_impl.h
index a21c3c7..3bf9c30 100644
--- a/net/disk_cache/simple/simple_backend_impl.h
+++ b/net/disk_cache/simple/simple_backend_impl.h
@@ -16,6 +16,7 @@
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task_runner.h"
+#include "base/time/time.h"
#include "net/base/cache_type.h"
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/simple/simple_entry_impl.h"
@@ -90,13 +91,14 @@ class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend,
private:
typedef base::hash_map<uint64, base::WeakPtr<SimpleEntryImpl> > EntryMap;
- typedef base::Callback<void(uint64 max_size, int result)>
+ typedef base::Callback<void(base::Time mtime, uint64 max_size, int result)>
InitializeIndexCallback;
// Must run on IO Thread.
- void InitializeIndex(const CompletionCallback& callback,
- uint64 suggested_max_size,
- int result);
+ void InitializeIndex(scoped_ptr<uint64> suggested_max_size,
+ scoped_ptr<base::Time> cache_dir_mtime,
+ scoped_ptr<int> dir_sanity_check_result,
+ const CompletionCallback& callback);
// Dooms all entries previously accessed between |initial_time| and
// |end_time|. Invoked when the index is ready.
@@ -107,11 +109,11 @@ class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend,
// Try to create the directory if it doesn't exist. Replies with maximum cache
// size adjustment. Must run on Cache Thread.
- static void ProvideDirectorySuggestBetterCacheSize(
- base::SingleThreadTaskRunner* io_thread,
- const base::FilePath& path,
- const InitializeIndexCallback& initialize_index_callback,
- uint64 suggested_max_size);
+ static void ProvideDirectorySuggestBetterCacheSize(const base::FilePath& path,
+ uint64 suggested_max_size,
+ base::Time* out_mtime,
+ uint64* out_max_size,
+ int* out_result);
// Searches |active_entries_| for the entry corresponding to |key|. If found,
// returns the found entry. Otherwise, creates a new entry and returns that.
diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc
index 6d559eb..8dbb88a 100644
--- a/net/disk_cache/simple/simple_index.cc
+++ b/net/disk_cache/simple/simple_index.cc
@@ -126,7 +126,7 @@ SimpleIndex::~SimpleIndex() {
}
}
-void SimpleIndex::Initialize() {
+void SimpleIndex::Initialize(base::Time cache_mtime) {
DCHECK(io_thread_checker_.CalledOnValidThread());
// Take the foreground and background index flush delays from the experiment
@@ -152,8 +152,10 @@ void SimpleIndex::Initialize() {
base::Bind(&SimpleIndex::OnActivityStateChange, AsWeakPtr())));
#endif
- index_file_->LoadIndexEntries(
- io_thread_, base::Bind(&SimpleIndex::MergeInitializingSet, AsWeakPtr()));
+ index_file_->LoadIndexEntries(cache_mtime,
+ io_thread_,
+ base::Bind(&SimpleIndex::MergeInitializingSet,
+ AsWeakPtr()));
}
bool SimpleIndex::SetMaxSize(int max_bytes) {
diff --git a/net/disk_cache/simple/simple_index.h b/net/disk_cache/simple/simple_index.h
index d58d267..08a83bb 100644
--- a/net/disk_cache/simple/simple_index.h
+++ b/net/disk_cache/simple/simple_index.h
@@ -77,7 +77,7 @@ class NET_EXPORT_PRIVATE SimpleIndex
virtual ~SimpleIndex();
- void Initialize();
+ void Initialize(base::Time cache_mtime);
bool SetMaxSize(int max_bytes);
int max_size() const { return max_size_; }
diff --git a/net/disk_cache/simple/simple_index_file.cc b/net/disk_cache/simple/simple_index_file.cc
index ab794079..f25d376 100644
--- a/net/disk_cache/simple/simple_index_file.cc
+++ b/net/disk_cache/simple/simple_index_file.cc
@@ -37,24 +37,23 @@ void DoomEntrySetReply(scoped_ptr<int> result,
}
void WriteToDiskInternal(const base::FilePath& index_filename,
+ const base::FilePath& temp_index_filename,
scoped_ptr<Pickle> pickle,
const base::TimeTicks& start_time,
bool app_on_background) {
- const base::FilePath temp_filename =
- index_filename.DirName().AppendASCII("index_temp");
int bytes_written = file_util::WriteFile(
- temp_filename,
+ temp_index_filename,
reinterpret_cast<const char*>(pickle->data()),
pickle->size());
DCHECK_EQ(bytes_written, implicit_cast<int>(pickle->size()));
if (bytes_written != static_cast<int>(pickle->size())) {
// TODO(felipeg): Add better error handling.
LOG(ERROR) << "Could not write Simple Cache index to temporary file: "
- << temp_filename.value();
- base::DeleteFile(temp_filename, /* recursive = */ false);
+ << temp_index_filename.value();
+ base::DeleteFile(temp_index_filename, /* recursive = */ false);
} else {
// Swap temp and index_file.
- bool result = base::ReplaceFile(temp_filename, index_filename, NULL);
+ bool result = base::ReplaceFile(temp_index_filename, index_filename, NULL);
DCHECK(result);
}
if (app_on_background) {
@@ -72,6 +71,8 @@ namespace disk_cache {
// static
const char SimpleIndexFile::kIndexFileName[] = "the-real-index";
+// static
+const char SimpleIndexFile::kTempIndexFileName[] = "temp-index";
SimpleIndexFile::IndexMetadata::IndexMetadata() :
magic_number_(kSimpleIndexMagicNumber),
@@ -111,21 +112,28 @@ bool SimpleIndexFile::IndexMetadata::CheckIndexMetadata() {
SimpleIndexFile::SimpleIndexFile(
base::SingleThreadTaskRunner* cache_thread,
base::TaskRunner* worker_pool,
- const base::FilePath& index_file_directory)
+ const base::FilePath& cache_directory)
: cache_thread_(cache_thread),
worker_pool_(worker_pool),
- index_file_path_(index_file_directory.AppendASCII(kIndexFileName)) {
+ cache_directory_(cache_directory),
+ index_file_(cache_directory_.AppendASCII(kIndexFileName)),
+ temp_index_file_(cache_directory_.AppendASCII(kTempIndexFileName)) {
}
SimpleIndexFile::~SimpleIndexFile() {}
void SimpleIndexFile::LoadIndexEntries(
+ base::Time cache_last_modified,
scoped_refptr<base::SingleThreadTaskRunner> response_thread,
const IndexCompletionCallback& completion_callback) {
worker_pool_->PostTask(
FROM_HERE,
base::Bind(&SimpleIndexFile::SyncLoadIndexEntries,
- index_file_path_, response_thread, completion_callback));
+ cache_last_modified,
+ cache_directory_,
+ index_file_,
+ response_thread,
+ completion_callback));
}
void SimpleIndexFile::WriteToDisk(const SimpleIndex::EntrySet& entry_set,
@@ -136,7 +144,8 @@ void SimpleIndexFile::WriteToDisk(const SimpleIndex::EntrySet& entry_set,
scoped_ptr<Pickle> pickle = Serialize(index_metadata, entry_set);
cache_thread_->PostTask(FROM_HERE, base::Bind(
&WriteToDiskInternal,
- index_file_path_,
+ index_file_,
+ temp_index_file_,
base::Passed(&pickle),
base::TimeTicks::Now(),
app_on_background));
@@ -151,7 +160,7 @@ void SimpleIndexFile::DoomEntrySet(
worker_pool_->PostTaskAndReply(
FROM_HERE,
base::Bind(&SimpleSynchronousEntry::DoomEntrySet,
- base::Passed(entry_hashes.Pass()), index_file_path_.DirName(),
+ base::Passed(entry_hashes.Pass()), cache_directory_,
result_p),
base::Bind(&DoomEntrySetReply, base::Passed(result.Pass()),
reply_callback));
@@ -159,31 +168,53 @@ void SimpleIndexFile::DoomEntrySet(
// static
void SimpleIndexFile::SyncLoadIndexEntries(
+ base::Time cache_last_modified,
+ const base::FilePath& cache_directory,
const base::FilePath& index_file_path,
scoped_refptr<base::SingleThreadTaskRunner> response_thread,
const IndexCompletionCallback& completion_callback) {
- // TODO(felipeg): probably could load a stale index and use it for something.
scoped_ptr<SimpleIndex::EntrySet> index_file_entries;
-
const bool index_file_exists = base::PathExists(index_file_path);
+ // Used in histograms. Please only add new values at the end.
+ enum {
+ INDEX_STATE_CORRUPT = 0,
+ INDEX_STATE_STALE = 1,
+ INDEX_STATE_FRESH = 2,
+ INDEX_STATE_FRESH_CONCURRENT_UPDATES = 3,
+ INDEX_STATE_MAX = 4,
+ } index_file_state;
+
// Only load if the index is not stale.
- const bool index_stale = IsIndexFileStale(index_file_path);
- if (!index_stale) {
+ if (IsIndexFileStale(cache_last_modified, index_file_path)) {
+ index_file_state = INDEX_STATE_STALE;
+ } else {
+ index_file_state = INDEX_STATE_FRESH;
+ base::Time latest_dir_mtime;
+ if (simple_util::GetMTime(cache_directory, &latest_dir_mtime) &&
+ IsIndexFileStale(latest_dir_mtime, index_file_path)) {
+ // A file operation has updated the directory since we last looked at it
+ // during backend initialization.
+ index_file_state = INDEX_STATE_FRESH_CONCURRENT_UPDATES;
+ }
+
const base::TimeTicks start = base::TimeTicks::Now();
index_file_entries = SyncLoadFromDisk(index_file_path);
UMA_HISTOGRAM_TIMES("SimpleCache.IndexLoadTime",
base::TimeTicks::Now() - start);
UMA_HISTOGRAM_COUNTS("SimpleCache.IndexEntriesLoaded",
index_file_entries ? index_file_entries->size() : 0);
+ if (!index_file_entries)
+ index_file_state = INDEX_STATE_CORRUPT;
}
-
- UMA_HISTOGRAM_BOOLEAN("SimpleCache.IndexStale", index_stale);
+ UMA_HISTOGRAM_ENUMERATION("SimpleCache.IndexFileStateOnLoad",
+ index_file_state,
+ INDEX_STATE_MAX);
bool force_index_flush = false;
if (!index_file_entries) {
const base::TimeTicks start = base::TimeTicks::Now();
- index_file_entries = SyncRestoreFromDisk(index_file_path);
+ index_file_entries = SyncRestoreFromDisk(cache_directory, index_file_path);
UMA_HISTOGRAM_MEDIUM_TIMES("SimpleCache.IndexRestoreTime",
base::TimeTicks::Now() - start);
UMA_HISTOGRAM_COUNTS("SimpleCache.IndexEntriesRestored",
@@ -193,8 +224,6 @@ void SimpleIndexFile::SyncLoadIndexEntries(
// away, this might save us from having to restore again next time.
force_index_flush = true;
}
- UMA_HISTOGRAM_BOOLEAN("SimpleCache.IndexCorrupt",
- (!index_stale && force_index_flush));
// Used in histograms. Please only add new values at the end.
enum {
@@ -313,6 +342,7 @@ scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::Deserialize(const char* data,
// static
scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::SyncRestoreFromDisk(
+ const base::FilePath& cache_directory,
const base::FilePath& index_file_path) {
LOG(INFO) << "Simple Cache Index is being restored from disk.";
@@ -326,7 +356,7 @@ scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::SyncRestoreFromDisk(
const int kFileSuffixLength = sizeof("_0") - 1;
const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
- base::FileEnumerator enumerator(index_file_path.DirName(),
+ base::FileEnumerator enumerator(cache_directory,
false /* recursive */,
base::FileEnumerator::FILES,
file_pattern);
@@ -372,18 +402,12 @@ scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::SyncRestoreFromDisk(
}
// static
-bool SimpleIndexFile::IsIndexFileStale(const base::FilePath& index_filename) {
+bool SimpleIndexFile::IsIndexFileStale(base::Time cache_last_modified,
+ const base::FilePath& index_file_path) {
base::Time index_mtime;
- base::Time dir_mtime;
- if (!simple_util::GetMTime(index_filename.DirName(), &dir_mtime))
- return true;
- if (!simple_util::GetMTime(index_filename, &index_mtime))
+ if (!simple_util::GetMTime(index_file_path, &index_mtime))
return true;
- // Index file last_modified must be equal to the directory last_modified since
- // the last operation we do is ReplaceFile in the
- // SimpleIndexFile::WriteToDisk().
- // If not true, we need to restore the index.
- return index_mtime < dir_mtime;
+ return index_mtime < cache_last_modified;
}
} // namespace disk_cache
diff --git a/net/disk_cache/simple/simple_index_file.h b/net/disk_cache/simple/simple_index_file.h
index 5cb8c7e..e328bc1 100644
--- a/net/disk_cache/simple/simple_index_file.h
+++ b/net/disk_cache/simple/simple_index_file.h
@@ -69,11 +69,12 @@ class NET_EXPORT_PRIVATE SimpleIndexFile {
SimpleIndexFile(base::SingleThreadTaskRunner* cache_thread,
base::TaskRunner* worker_pool,
- const base::FilePath& index_file_directory);
+ const base::FilePath& cache_directory);
virtual ~SimpleIndexFile();
// Get index entries based on current disk context.
virtual void LoadIndexEntries(
+ base::Time cache_last_modified,
scoped_refptr<base::SingleThreadTaskRunner> response_thread,
const SimpleIndexFile::IndexCompletionCallback& completion_callback);
@@ -93,6 +94,8 @@ class NET_EXPORT_PRIVATE SimpleIndexFile {
// Synchronous (IO performing) implementation of LoadIndexEntries.
static void SyncLoadIndexEntries(
+ base::Time cache_last_modified,
+ const base::FilePath& cache_directory,
const base::FilePath& index_file_path,
scoped_refptr<base::SingleThreadTaskRunner> response_thread,
const SimpleIndexFile::IndexCompletionCallback& completion_callback);
@@ -116,10 +119,13 @@ class NET_EXPORT_PRIVATE SimpleIndexFile {
// Scan the index directory for entries, returning an EntrySet of all entries
// found.
static scoped_ptr<SimpleIndex::EntrySet> SyncRestoreFromDisk(
+ const base::FilePath& cache_directory,
const base::FilePath& index_file_path);
- // Determines if an index file is stale relative to the cache directory.
- static bool IsIndexFileStale(const base::FilePath& index_filename);
+ // Determines if an index file is stale relative to the time of last
+ // modification of the cache directory.
+ static bool IsIndexFileStale(base::Time cache_last_modified,
+ const base::FilePath& index_file_path);
struct PickleHeader : public Pickle::Header {
uint32 crc;
@@ -127,9 +133,12 @@ class NET_EXPORT_PRIVATE SimpleIndexFile {
const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_;
const scoped_refptr<base::TaskRunner> worker_pool_;
- const base::FilePath index_file_path_;
+ const base::FilePath cache_directory_;
+ const base::FilePath index_file_;
+ const base::FilePath temp_index_file_;
static const char kIndexFileName[];
+ static const char kTempIndexFileName[];
DISALLOW_COPY_AND_ASSIGN(SimpleIndexFile);
};
diff --git a/net/disk_cache/simple/simple_index_file_unittest.cc b/net/disk_cache/simple/simple_index_file_unittest.cc
index e2493f9..e1267fa 100644
--- a/net/disk_cache/simple/simple_index_file_unittest.cc
+++ b/net/disk_cache/simple/simple_index_file_unittest.cc
@@ -15,6 +15,7 @@
#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_index.h"
#include "net/disk_cache/simple/simple_index_file.h"
+#include "net/disk_cache/simple/simple_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
@@ -57,7 +58,6 @@ class WrappedSimpleIndexFile : public SimpleIndexFile {
public:
using SimpleIndexFile::Deserialize;
using SimpleIndexFile::IsIndexFileStale;
- using SimpleIndexFile::kIndexFileName;
using SimpleIndexFile::Serialize;
explicit WrappedSimpleIndexFile(const base::FilePath& index_file_directory)
@@ -66,6 +66,10 @@ class WrappedSimpleIndexFile : public SimpleIndexFile {
index_file_directory) {}
virtual ~WrappedSimpleIndexFile() {
}
+
+ const base::FilePath& GetIndexFilePath() const {
+ return index_file_;
+ }
};
class SimpleIndexFileTest : public testing::Test {
@@ -143,41 +147,43 @@ TEST_F(SimpleIndexFileTest, Serialize) {
}
TEST_F(SimpleIndexFileTest, IsIndexFileStale) {
- base::ScopedTempDir temp_dir;
- ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
-
- const std::string kIndexFileName = "simple-index";
- const base::FilePath index_path =
- temp_dir.path().AppendASCII(kIndexFileName);
- EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
+ base::ScopedTempDir cache_dir;
+ ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
+ base::Time cache_mtime;
+ const base::FilePath cache_path = cache_dir.path();
+
+ ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime));
+ WrappedSimpleIndexFile simple_index_file(cache_path);
+ const base::FilePath& index_path = simple_index_file.GetIndexFilePath();
+ EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(cache_mtime,
+ index_path));
const std::string kDummyData = "nothing to be seen here";
EXPECT_EQ(static_cast<int>(kDummyData.size()),
file_util::WriteFile(index_path,
kDummyData.data(),
kDummyData.size()));
- EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
+ ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime));
+ EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(cache_mtime,
+ index_path));
const base::Time past_time = base::Time::Now() -
base::TimeDelta::FromSeconds(10);
EXPECT_TRUE(file_util::TouchFile(index_path, past_time, past_time));
- EXPECT_TRUE(file_util::TouchFile(temp_dir.path(), past_time, past_time));
- EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
-
- EXPECT_EQ(static_cast<int>(kDummyData.size()),
- file_util::WriteFile(temp_dir.path().AppendASCII("other_file"),
- kDummyData.data(),
- kDummyData.size()));
+ EXPECT_TRUE(file_util::TouchFile(cache_path, past_time, past_time));
+ ASSERT_TRUE(simple_util::GetMTime(cache_path, &cache_mtime));
+ EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(cache_mtime,
+ index_path));
+ const base::Time even_older =
+ past_time - base::TimeDelta::FromSeconds(10);
+ EXPECT_TRUE(file_util::TouchFile(index_path, even_older, even_older));
+ EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(cache_mtime,
+ index_path));
- EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
}
TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) {
- base::ScopedTempDir temp_dir;
- ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
-
- const base::FilePath index_path =
- temp_dir.path().AppendASCII(WrappedSimpleIndexFile::kIndexFileName);
- EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
+ base::ScopedTempDir cache_dir;
+ ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
SimpleIndex::EntrySet entries;
static const uint64 kHashes[] = { 11, 22, 33 };
@@ -192,19 +198,23 @@ TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) {
const uint64 kCacheSize = 456U;
{
- WrappedSimpleIndexFile simple_index_file(temp_dir.path());
+ WrappedSimpleIndexFile simple_index_file(cache_dir.path());
simple_index_file.WriteToDisk(entries, kCacheSize,
base::TimeTicks(), false);
base::RunLoop().RunUntilIdle();
- EXPECT_TRUE(base::PathExists(index_path));
+ EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
}
- WrappedSimpleIndexFile simple_index_file(temp_dir.path());
- simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current(),
- GetCallback());
+ WrappedSimpleIndexFile simple_index_file2(cache_dir.path());
+ base::Time fake_cache_mtime;
+ ASSERT_TRUE(simple_util::GetMTime(simple_index_file2.GetIndexFilePath(),
+ &fake_cache_mtime));
+ simple_index_file2.LoadIndexEntries(fake_cache_mtime,
+ base::MessageLoopProxy::current(),
+ GetCallback());
base::RunLoop().RunUntilIdle();
- EXPECT_TRUE(base::PathExists(index_path));
+ EXPECT_TRUE(base::PathExists(simple_index_file2.GetIndexFilePath()));
ASSERT_TRUE(callback_result());
EXPECT_FALSE(callback_result()->force_index_flush);
const SimpleIndex::EntrySet* read_entries =
@@ -217,21 +227,24 @@ TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) {
}
TEST_F(SimpleIndexFileTest, LoadCorruptIndex) {
- base::ScopedTempDir temp_dir;
- ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ base::ScopedTempDir cache_dir;
+ ASSERT_TRUE(cache_dir.CreateUniqueTempDir());
- const base::FilePath index_path =
- temp_dir.path().AppendASCII(WrappedSimpleIndexFile::kIndexFileName);
- EXPECT_TRUE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
+ WrappedSimpleIndexFile simple_index_file(cache_dir.path());
+ const base::FilePath& index_path = simple_index_file.GetIndexFilePath();
const std::string kDummyData = "nothing to be seen here";
EXPECT_EQ(static_cast<int>(kDummyData.size()),
file_util::WriteFile(index_path,
kDummyData.data(),
kDummyData.size()));
- EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(index_path));
-
- WrappedSimpleIndexFile simple_index_file(temp_dir.path());
- simple_index_file.LoadIndexEntries(base::MessageLoopProxy::current().get(),
+ base::Time fake_cache_mtime;
+ ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(),
+ &fake_cache_mtime));
+ EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(fake_cache_mtime,
+ index_path));
+
+ simple_index_file.LoadIndexEntries(fake_cache_mtime,
+ base::MessageLoopProxy::current().get(),
GetCallback());
base::RunLoop().RunUntilIdle();
diff --git a/net/disk_cache/simple/simple_index_unittest.cc b/net/disk_cache/simple/simple_index_unittest.cc
index 24675c5..3c76d5e 100644
--- a/net/disk_cache/simple/simple_index_unittest.cc
+++ b/net/disk_cache/simple/simple_index_unittest.cc
@@ -54,6 +54,7 @@ class MockSimpleIndexFile : public SimpleIndexFile,
disk_writes_(0) {}
virtual void LoadIndexEntries(
+ base::Time cache_last_modified,
scoped_refptr<base::SingleThreadTaskRunner> response_thread,
const SimpleIndexFile::IndexCompletionCallback&
completion_callback) OVERRIDE {
@@ -111,7 +112,7 @@ class SimpleIndexTest : public testing::Test {
index_.reset(new SimpleIndex(NULL, base::FilePath(),
index_file.PassAs<SimpleIndexFile>()));
- index_->Initialize();
+ index_->Initialize(base::Time());
}
void WaitForTimeChange() {