summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgavinp <gavinp@chromium.org>2014-11-04 12:20:57 -0800
committerCommit bot <commit-bot@chromium.org>2014-11-04 20:21:35 +0000
commitf379a6efd4f9beb6cc49e7f4e00286dfc2717dcf (patch)
tree43c4d400689d7f4ad8880276b6414c884b4c6f29
parentfa3a3cc37f07130dedf6a017bbca6903d9ef4f7c (diff)
downloadchromium_src-f379a6efd4f9beb6cc49e7f4e00286dfc2717dcf.zip
chromium_src-f379a6efd4f9beb6cc49e7f4e00286dfc2717dcf.tar.gz
chromium_src-f379a6efd4f9beb6cc49e7f4e00286dfc2717dcf.tar.bz2
Use net::TestClosureCallback in SimpleCache tests.
Reduces flakiness; improves test readability. Also introduces a callback on SimpleIndexFile to signal completion of writing the index, useful for deflaking cache serialization. R=jkarlin@chromium.org BUG=255775,429672 Review URL: https://codereview.chromium.org/699743002 Cr-Commit-Position: refs/heads/master@{#302653}
-rw-r--r--net/disk_cache/simple/simple_index.cc2
-rw-r--r--net/disk_cache/simple/simple_index_file.cc20
-rw-r--r--net/disk_cache/simple/simple_index_file.h3
-rw-r--r--net/disk_cache/simple/simple_index_file_unittest.cc39
-rw-r--r--net/disk_cache/simple/simple_index_unittest.cc3
5 files changed, 24 insertions, 43 deletions
diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc
index ca4d58e..66d9f42 100644
--- a/net/disk_cache/simple/simple_index.cc
+++ b/net/disk_cache/simple/simple_index.cc
@@ -470,7 +470,7 @@ void SimpleIndex::WriteToDisk() {
last_write_to_disk_ = start;
index_file_->WriteToDisk(entries_set_, cache_size_,
- start, app_on_background_);
+ start, app_on_background_, base::Closure());
}
} // namespace disk_cache
diff --git a/net/disk_cache/simple/simple_index_file.cc b/net/disk_cache/simple/simple_index_file.cc
index ac71330..95b2bae 100644
--- a/net/disk_cache/simple/simple_index_file.cc
+++ b/net/disk_cache/simple/simple_index_file.cc
@@ -278,18 +278,18 @@ void SimpleIndexFile::LoadIndexEntries(base::Time cache_last_modified,
void SimpleIndexFile::WriteToDisk(const SimpleIndex::EntrySet& entry_set,
uint64 cache_size,
const base::TimeTicks& start,
- bool app_on_background) {
+ bool app_on_background,
+ const base::Closure& callback) {
IndexMetadata index_metadata(entry_set.size(), cache_size);
scoped_ptr<Pickle> pickle = Serialize(index_metadata, entry_set);
- cache_thread_->PostTask(FROM_HERE,
- base::Bind(&SimpleIndexFile::SyncWriteToDisk,
- cache_type_,
- cache_directory_,
- index_file_,
- temp_index_file_,
- base::Passed(&pickle),
- base::TimeTicks::Now(),
- app_on_background));
+ base::Closure task =
+ base::Bind(&SimpleIndexFile::SyncWriteToDisk,
+ cache_type_, cache_directory_, index_file_, temp_index_file_,
+ base::Passed(&pickle), start, app_on_background);
+ if (callback.is_null())
+ cache_thread_->PostTask(FROM_HERE, task);
+ else
+ cache_thread_->PostTaskAndReply(FROM_HERE, task, callback);
}
// static
diff --git a/net/disk_cache/simple/simple_index_file.h b/net/disk_cache/simple/simple_index_file.h
index b1f8961..91f2a48 100644
--- a/net/disk_cache/simple/simple_index_file.h
+++ b/net/disk_cache/simple/simple_index_file.h
@@ -90,7 +90,8 @@ class NET_EXPORT_PRIVATE SimpleIndexFile {
virtual void WriteToDisk(const SimpleIndex::EntrySet& entry_set,
uint64 cache_size,
const base::TimeTicks& start,
- bool app_on_background);
+ bool app_on_background,
+ const base::Closure& callback);
private:
friend class WrappedSimpleIndexFile;
diff --git a/net/disk_cache/simple/simple_index_file_unittest.cc b/net/disk_cache/simple/simple_index_file_unittest.cc
index 77db225..4cedab2 100644
--- a/net/disk_cache/simple/simple_index_file_unittest.cc
+++ b/net/disk_cache/simple/simple_index_file_unittest.cc
@@ -98,24 +98,6 @@ class SimpleIndexFileTest : public testing::Test {
b.last_used_time_seconds_since_epoch_ &&
a.entry_size_ == b.entry_size_;
}
-
- protected:
- SimpleIndexFileTest() : callback_called_(false) {}
-
- base::Closure GetCallback() {
- return base::Bind(&SimpleIndexFileTest::LoadIndexEntriesCallback,
- base::Unretained(this));
- }
-
- bool callback_called() { return callback_called_; }
-
- private:
- void LoadIndexEntriesCallback() {
- EXPECT_FALSE(callback_called_);
- callback_called_ = true;
- }
-
- bool callback_called_;
};
TEST_F(SimpleIndexFileTest, Serialize) {
@@ -203,11 +185,12 @@ TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) {
}
const uint64 kCacheSize = 456U;
+ net::TestClosure closure;
{
WrappedSimpleIndexFile simple_index_file(cache_dir.path());
- simple_index_file.WriteToDisk(entries, kCacheSize,
- base::TimeTicks(), false);
- base::RunLoop().RunUntilIdle();
+ simple_index_file.WriteToDisk(entries, kCacheSize, base::TimeTicks(),
+ false, closure.closure());
+ closure.WaitForResult();
EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
}
@@ -215,13 +198,11 @@ TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) {
base::Time fake_cache_mtime;
ASSERT_TRUE(simple_util::GetMTime(cache_dir.path(), &fake_cache_mtime));
SimpleIndexLoadResult load_index_result;
- simple_index_file.LoadIndexEntries(fake_cache_mtime,
- GetCallback(),
+ simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(),
&load_index_result);
- base::RunLoop().RunUntilIdle();
+ closure.WaitForResult();
EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath()));
- ASSERT_TRUE(callback_called());
EXPECT_TRUE(load_index_result.did_load);
EXPECT_FALSE(load_index_result.flush_required);
@@ -246,15 +227,13 @@ TEST_F(SimpleIndexFileTest, LoadCorruptIndex) {
&fake_cache_mtime));
EXPECT_FALSE(WrappedSimpleIndexFile::LegacyIsIndexFileStale(fake_cache_mtime,
index_path));
-
SimpleIndexLoadResult load_index_result;
- simple_index_file.LoadIndexEntries(fake_cache_mtime,
- GetCallback(),
+ net::TestClosure closure;
+ simple_index_file.LoadIndexEntries(fake_cache_mtime, closure.closure(),
&load_index_result);
- base::RunLoop().RunUntilIdle();
+ closure.WaitForResult();
EXPECT_FALSE(base::PathExists(index_path));
- ASSERT_TRUE(callback_called());
EXPECT_TRUE(load_index_result.did_load);
EXPECT_TRUE(load_index_result.flush_required);
}
diff --git a/net/disk_cache/simple/simple_index_unittest.cc b/net/disk_cache/simple/simple_index_unittest.cc
index e5d0830..1096488 100644
--- a/net/disk_cache/simple/simple_index_unittest.cc
+++ b/net/disk_cache/simple/simple_index_unittest.cc
@@ -68,7 +68,8 @@ class MockSimpleIndexFile : public SimpleIndexFile,
void WriteToDisk(const SimpleIndex::EntrySet& entry_set,
uint64 cache_size,
const base::TimeTicks& start,
- bool app_on_background) override {
+ bool app_on_background,
+ const base::Closure& callback) override {
disk_writes_++;
disk_write_entry_set_ = entry_set;
}