summaryrefslogtreecommitdiffstats
path: root/net/disk_cache
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-22 11:32:53 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-22 11:32:53 +0000
commitc86d350e584b00bd5df71e82b5774ef245a14bef (patch)
treefd28cd57045ae2862e37621e24453612443d8029 /net/disk_cache
parent0407998a8dbe022e1ae9677935a0bc822bcd61ed (diff)
downloadchromium_src-c86d350e584b00bd5df71e82b5774ef245a14bef.zip
chromium_src-c86d350e584b00bd5df71e82b5774ef245a14bef.tar.gz
chromium_src-c86d350e584b00bd5df71e82b5774ef245a14bef.tar.bz2
GTTF: Make net_unittests run successfully with parallel test launcher
by using a unique directory for disk cache tests. BUG=54098 TEST=run net_unittests using tools/parallel_launcher/parallel_launcher.py Review URL: http://codereview.chromium.org/3410008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60165 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache')
-rw-r--r--net/disk_cache/backend_unittest.cc103
-rw-r--r--net/disk_cache/block_files_unittest.cc49
-rw-r--r--net/disk_cache/disk_cache_test_base.cc17
-rw-r--r--net/disk_cache/disk_cache_test_base.h7
-rw-r--r--net/disk_cache/disk_cache_test_util.cc37
-rw-r--r--net/disk_cache/disk_cache_test_util.h14
-rw-r--r--net/disk_cache/mapped_file_unittest.cc8
-rw-r--r--net/disk_cache/storage_block_unittest.cc12
-rw-r--r--net/disk_cache/stress_cache.cc13
9 files changed, 121 insertions, 139 deletions
diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc
index 3f0018c..7794e51 100644
--- a/net/disk_cache/backend_unittest.cc
+++ b/net/disk_cache/backend_unittest.cc
@@ -180,11 +180,10 @@ TEST_F(DiskCacheBackendTest, AppCacheKeying) {
}
TEST_F(DiskCacheTest, CreateBackend) {
+ ScopedTestCache test_cache;
TestCompletionCallback cb;
{
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
base::Thread cache_thread("CacheThread");
ASSERT_TRUE(cache_thread.StartWithOptions(
base::Thread::Options(MessageLoop::TYPE_IO, 0)));
@@ -192,8 +191,8 @@ TEST_F(DiskCacheTest, CreateBackend) {
// Test the private factory methods.
disk_cache::Backend* cache = NULL;
int rv = disk_cache::BackendImpl::CreateBackend(
- path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
- cache_thread.message_loop_proxy(), &cache, &cb);
+ test_cache.path(), false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
+ cache_thread.message_loop_proxy(), &cache, &cb);
ASSERT_EQ(net::OK, cb.GetResult(rv));
ASSERT_TRUE(cache);
delete cache;
@@ -204,7 +203,8 @@ TEST_F(DiskCacheTest, CreateBackend) {
cache = NULL;
// Now test the public API.
- rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, path, 0, false,
+ rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, test_cache.path(),
+ 0, false,
cache_thread.message_loop_proxy(),
&cache, &cb);
ASSERT_EQ(net::OK, cb.GetResult(rv));
@@ -225,7 +225,7 @@ TEST_F(DiskCacheTest, CreateBackend) {
TEST_F(DiskCacheBackendTest, ExternalFiles) {
InitCache();
// First, lets create a file on the folder.
- FilePath filename = GetCacheFilePath().AppendASCII("f_000001");
+ FilePath filename = path().AppendASCII("f_000001");
const int kSize = 50;
scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize);
@@ -246,19 +246,18 @@ TEST_F(DiskCacheBackendTest, ExternalFiles) {
// Tests that we deal with file-level pending operations at destruction time.
TEST_F(DiskCacheTest, ShutdownWithPendingIO) {
+ ScopedTestCache test_cache;
TestCompletionCallback cb;
{
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
base::Thread cache_thread("CacheThread");
ASSERT_TRUE(cache_thread.StartWithOptions(
base::Thread::Options(MessageLoop::TYPE_IO, 0)));
disk_cache::Backend* cache;
int rv = disk_cache::BackendImpl::CreateBackend(
- path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
- base::MessageLoopProxy::CreateForCurrentThread(), &cache, &cb);
+ test_cache.path(), false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
+ base::MessageLoopProxy::CreateForCurrentThread(), &cache, &cb);
ASSERT_EQ(net::OK, cb.GetResult(rv));
disk_cache::EntryImpl* entry;
@@ -297,19 +296,18 @@ TEST_F(DiskCacheTest, ShutdownWithPendingIO) {
// Tests that we deal with background-thread pending operations.
TEST_F(DiskCacheTest, ShutdownWithPendingIO2) {
+ ScopedTestCache test_cache;
TestCompletionCallback cb;
{
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
base::Thread cache_thread("CacheThread");
ASSERT_TRUE(cache_thread.StartWithOptions(
base::Thread::Options(MessageLoop::TYPE_IO, 0)));
disk_cache::Backend* cache;
int rv = disk_cache::BackendImpl::CreateBackend(
- path, false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
- cache_thread.message_loop_proxy(), &cache, &cb);
+ test_cache.path(), false, 0, net::DISK_CACHE, disk_cache::kNoRandom,
+ cache_thread.message_loop_proxy(), &cache, &cb);
ASSERT_EQ(net::OK, cb.GetResult(rv));
disk_cache::Entry* entry;
@@ -333,9 +331,9 @@ TEST_F(DiskCacheTest, ShutdownWithPendingIO2) {
}
TEST_F(DiskCacheTest, TruncatedIndex) {
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
- FilePath index = path.AppendASCII("index");
+ ScopedTestCache test_cache;
+
+ FilePath index = test_cache.path().AppendASCII("index");
ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5));
base::Thread cache_thread("CacheThread");
@@ -345,8 +343,8 @@ TEST_F(DiskCacheTest, TruncatedIndex) {
disk_cache::Backend* backend = NULL;
int rv = disk_cache::BackendImpl::CreateBackend(
- path, false, 0, net::DISK_CACHE, disk_cache::kNone,
- cache_thread.message_loop_proxy(), &backend, &cb);
+ test_cache.path(), false, 0, net::DISK_CACHE, disk_cache::kNone,
+ cache_thread.message_loop_proxy(), &backend, &cb);
ASSERT_NE(net::OK, cb.GetResult(rv));
ASSERT_TRUE(backend == NULL);
@@ -1145,7 +1143,7 @@ TEST_F(DiskCacheBackendTest, MemoryOnlyDoomBetween) {
void DiskCacheBackendTest::BackendTransaction(const std::string& name,
int num_entries, bool load) {
success_ = false;
- ASSERT_TRUE(CopyTestCache(name));
+ ASSERT_TRUE(CopyTestCache(name, path()));
DisableFirstCleanup();
if (load) {
@@ -1176,7 +1174,7 @@ void DiskCacheBackendTest::BackendTransaction(const std::string& name,
cache_ = NULL;
cache_impl_ = NULL;
- ASSERT_TRUE(CheckCacheIntegrity(GetCacheFilePath(), new_eviction_));
+ ASSERT_TRUE(CheckCacheIntegrity(path(), new_eviction_));
success_ = true;
}
@@ -1266,8 +1264,9 @@ TEST_F(DiskCacheBackendTest, NewEvictionRecoverRemove) {
// Tests dealing with cache files that cannot be recovered.
TEST_F(DiskCacheTest, DeleteOld) {
- ASSERT_TRUE(CopyTestCache("wrong_version"));
- FilePath path = GetCacheFilePath();
+ ScopedTestCache test_cache;
+
+ ASSERT_TRUE(CopyTestCache("wrong_version", test_cache.path()));
base::Thread cache_thread("CacheThread");
ASSERT_TRUE(cache_thread.StartWithOptions(
base::Thread::Options(MessageLoop::TYPE_IO, 0)));
@@ -1275,8 +1274,8 @@ TEST_F(DiskCacheTest, DeleteOld) {
disk_cache::Backend* cache;
int rv = disk_cache::BackendImpl::CreateBackend(
- path, true, 0, net::DISK_CACHE, disk_cache::kNoRandom,
- cache_thread.message_loop_proxy(), &cache, &cb);
+ test_cache.path(), true, 0, net::DISK_CACHE, disk_cache::kNoRandom,
+ cache_thread.message_loop_proxy(), &cache, &cb);
ASSERT_EQ(net::OK, cb.GetResult(rv));
MessageLoopHelper helper;
@@ -1289,7 +1288,7 @@ TEST_F(DiskCacheTest, DeleteOld) {
// We want to be able to deal with messed up entries on disk.
void DiskCacheBackendTest::BackendInvalidEntry2() {
- ASSERT_TRUE(CopyTestCache("bad_entry"));
+ ASSERT_TRUE(CopyTestCache("bad_entry", path()));
DisableFirstCleanup();
InitCache();
@@ -1313,7 +1312,7 @@ TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry2) {
// We want to be able to deal with abnormal dirty entries.
void DiskCacheBackendTest::BackendNotMarkedButDirty(const std::string& name) {
- ASSERT_TRUE(CopyTestCache(name));
+ ASSERT_TRUE(CopyTestCache(name, path()));
DisableFirstCleanup();
InitCache();
@@ -1343,8 +1342,7 @@ TEST_F(DiskCacheBackendTest, NewEvictionNotMarkedButDirty2) {
// We want to be able to deal with messed up entries on disk.
void DiskCacheBackendTest::BackendInvalidRankings2() {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
- FilePath path = GetCacheFilePath();
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
InitCache();
@@ -1380,7 +1378,7 @@ void DiskCacheBackendTest::BackendInvalidRankings() {
}
TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
InitCache();
@@ -1388,7 +1386,7 @@ TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) {
}
TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankingsSuccess) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
SetNewEviction();
@@ -1397,7 +1395,7 @@ TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankingsSuccess) {
}
TEST_F(DiskCacheBackendTest, InvalidRankingsFailure) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
InitCache();
@@ -1406,7 +1404,7 @@ TEST_F(DiskCacheBackendTest, InvalidRankingsFailure) {
}
TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankingsFailure) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
SetNewEviction();
@@ -1433,7 +1431,7 @@ void DiskCacheBackendTest::BackendDisable() {
}
TEST_F(DiskCacheBackendTest, DisableSuccess) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
InitCache();
@@ -1441,7 +1439,7 @@ TEST_F(DiskCacheBackendTest, DisableSuccess) {
}
TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
SetNewEviction();
@@ -1450,7 +1448,7 @@ TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess) {
}
TEST_F(DiskCacheBackendTest, DisableFailure) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
InitCache();
@@ -1459,7 +1457,7 @@ TEST_F(DiskCacheBackendTest, DisableFailure) {
}
TEST_F(DiskCacheBackendTest, NewEvictionDisableFailure) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
SetNewEviction();
@@ -1487,7 +1485,7 @@ void DiskCacheBackendTest::BackendDisable2() {
}
TEST_F(DiskCacheBackendTest, DisableSuccess2) {
- ASSERT_TRUE(CopyTestCache("list_loop"));
+ ASSERT_TRUE(CopyTestCache("list_loop", path()));
DisableFirstCleanup();
SetDirectMode();
InitCache();
@@ -1495,7 +1493,7 @@ TEST_F(DiskCacheBackendTest, DisableSuccess2) {
}
TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess2) {
- ASSERT_TRUE(CopyTestCache("list_loop"));
+ ASSERT_TRUE(CopyTestCache("list_loop", path()));
DisableFirstCleanup();
SetNewEviction();
SetDirectMode();
@@ -1504,7 +1502,7 @@ TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess2) {
}
TEST_F(DiskCacheBackendTest, DisableFailure2) {
- ASSERT_TRUE(CopyTestCache("list_loop"));
+ ASSERT_TRUE(CopyTestCache("list_loop", path()));
DisableFirstCleanup();
SetDirectMode();
InitCache();
@@ -1513,7 +1511,7 @@ TEST_F(DiskCacheBackendTest, DisableFailure2) {
}
TEST_F(DiskCacheBackendTest, NewEvictionDisableFailure2) {
- ASSERT_TRUE(CopyTestCache("list_loop"));
+ ASSERT_TRUE(CopyTestCache("list_loop", path()));
DisableFirstCleanup();
SetDirectMode();
SetNewEviction();
@@ -1540,7 +1538,7 @@ void DiskCacheBackendTest::BackendDisable3() {
}
TEST_F(DiskCacheBackendTest, DisableSuccess3) {
- ASSERT_TRUE(CopyTestCache("bad_rankings2"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings2", path()));
DisableFirstCleanup();
SetMaxSize(20 * 1024 * 1024);
InitCache();
@@ -1548,7 +1546,7 @@ TEST_F(DiskCacheBackendTest, DisableSuccess3) {
}
TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess3) {
- ASSERT_TRUE(CopyTestCache("bad_rankings2"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings2", path()));
DisableFirstCleanup();
SetMaxSize(20 * 1024 * 1024);
SetNewEviction();
@@ -1606,7 +1604,7 @@ void DiskCacheBackendTest::BackendDisable4() {
}
TEST_F(DiskCacheBackendTest, DisableSuccess4) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
InitCache();
@@ -1614,7 +1612,7 @@ TEST_F(DiskCacheBackendTest, DisableSuccess4) {
}
TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess4) {
- ASSERT_TRUE(CopyTestCache("bad_rankings"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings", path()));
DisableFirstCleanup();
SetDirectMode();
SetNewEviction();
@@ -1623,13 +1621,14 @@ TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess4) {
}
TEST_F(DiskCacheTest, Backend_UsageStats) {
+ ScopedTestCache test_cache;
+
MessageLoopHelper helper;
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
scoped_ptr<disk_cache::BackendImpl> cache;
cache.reset(new disk_cache::BackendImpl(
- path, base::MessageLoopProxy::CreateForCurrentThread()));
+ test_cache.path(),
+ base::MessageLoopProxy::CreateForCurrentThread()));
ASSERT_TRUE(NULL != cache.get());
cache->SetUnitTestMode();
ASSERT_EQ(net::OK, cache->SyncInit());
@@ -1715,7 +1714,7 @@ void DiskCacheBackendTest::BackendDoomAll2() {
}
TEST_F(DiskCacheBackendTest, DoomAll2) {
- ASSERT_TRUE(CopyTestCache("bad_rankings2"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings2", path()));
DisableFirstCleanup();
SetMaxSize(20 * 1024 * 1024);
InitCache();
@@ -1723,7 +1722,7 @@ TEST_F(DiskCacheBackendTest, DoomAll2) {
}
TEST_F(DiskCacheBackendTest, NewEvictionDoomAll2) {
- ASSERT_TRUE(CopyTestCache("bad_rankings2"));
+ ASSERT_TRUE(CopyTestCache("bad_rankings2", path()));
DisableFirstCleanup();
SetMaxSize(20 * 1024 * 1024);
SetNewEviction();
@@ -1735,8 +1734,8 @@ TEST_F(DiskCacheBackendTest, NewEvictionDoomAll2) {
// of the cache.
TEST_F(DiskCacheTest, MultipleInstances) {
ScopedTestCache store1;
- ScopedTestCache store2("cache_test2");
- ScopedTestCache store3("cache_test3");
+ ScopedTestCache store2;
+ ScopedTestCache store3;
base::Thread cache_thread("CacheThread");
ASSERT_TRUE(cache_thread.StartWithOptions(
base::Thread::Options(MessageLoop::TYPE_IO, 0)));
diff --git a/net/disk_cache/block_files_unittest.cc b/net/disk_cache/block_files_unittest.cc
index 90bf048..4455dab 100644
--- a/net/disk_cache/block_files_unittest.cc
+++ b/net/disk_cache/block_files_unittest.cc
@@ -28,11 +28,9 @@ int NumberOfFiles(const FilePath& path) {
namespace disk_cache {
TEST_F(DiskCacheTest, BlockFiles_Grow) {
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
- ASSERT_TRUE(file_util::CreateDirectory(path));
+ ScopedTestCache test_cache;
- BlockFiles files(path);
+ BlockFiles files(test_cache.path());
ASSERT_TRUE(files.Init(true));
const int kMaxSize = 35000;
@@ -42,7 +40,7 @@ TEST_F(DiskCacheTest, BlockFiles_Grow) {
for (int i = 0; i < kMaxSize; i++) {
EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[i]));
}
- EXPECT_EQ(6, NumberOfFiles(path));
+ EXPECT_EQ(6, NumberOfFiles(test_cache.path()));
// Make sure we don't keep adding files.
for (int i = 0; i < kMaxSize * 4; i += 2) {
@@ -50,16 +48,14 @@ TEST_F(DiskCacheTest, BlockFiles_Grow) {
files.DeleteBlock(address[target], false);
EXPECT_TRUE(files.CreateBlock(RANKINGS, 4, &address[target]));
}
- EXPECT_EQ(6, NumberOfFiles(path));
+ EXPECT_EQ(6, NumberOfFiles(test_cache.path()));
}
// We should be able to delete empty block files.
TEST_F(DiskCacheTest, BlockFiles_Shrink) {
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
- ASSERT_TRUE(file_util::CreateDirectory(path));
+ ScopedTestCache test_cache;
- BlockFiles files(path);
+ BlockFiles files(test_cache.path());
ASSERT_TRUE(files.Init(true));
const int kMaxSize = 35000;
@@ -74,16 +70,14 @@ TEST_F(DiskCacheTest, BlockFiles_Shrink) {
for (int i = 0; i < kMaxSize; i++) {
files.DeleteBlock(address[i], false);
}
- EXPECT_EQ(4, NumberOfFiles(path));
+ EXPECT_EQ(4, NumberOfFiles(test_cache.path()));
}
// Handling of block files not properly closed.
TEST_F(DiskCacheTest, BlockFiles_Recover) {
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
- ASSERT_TRUE(file_util::CreateDirectory(path));
+ ScopedTestCache test_cache;
- BlockFiles files(path);
+ BlockFiles files(test_cache.path());
ASSERT_TRUE(files.Init(true));
const int kNumEntries = 2000;
@@ -157,11 +151,9 @@ TEST_F(DiskCacheTest, BlockFiles_Recover) {
// Handling of truncated files.
TEST_F(DiskCacheTest, BlockFiles_ZeroSizeFile) {
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
- ASSERT_TRUE(file_util::CreateDirectory(path));
+ ScopedTestCache test_cache;
- BlockFiles files(path);
+ BlockFiles files(test_cache.path());
ASSERT_TRUE(files.Init(true));
FilePath filename = files.Name(0);
@@ -179,11 +171,9 @@ TEST_F(DiskCacheTest, BlockFiles_ZeroSizeFile) {
// An invalid file can be detected after init.
TEST_F(DiskCacheTest, BlockFiles_InvalidFile) {
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
- ASSERT_TRUE(file_util::CreateDirectory(path));
+ ScopedTestCache test_cache;
- BlockFiles files(path);
+ BlockFiles files(test_cache.path());
ASSERT_TRUE(files.Init(true));
// Let's access block 10 of file 5. (There is no file).
@@ -205,10 +195,11 @@ TEST_F(DiskCacheTest, BlockFiles_InvalidFile) {
// Tests that we generate the correct file stats.
TEST_F(DiskCacheTest, BlockFiles_Stats) {
- ASSERT_TRUE(CopyTestCache("remove_load1"));
- FilePath path = GetCacheFilePath();
+ ScopedTestCache test_cache;
- BlockFiles files(path);
+ ASSERT_TRUE(CopyTestCache("remove_load1", test_cache.path()));
+
+ BlockFiles files(test_cache.path());
ASSERT_TRUE(files.Init(false));
int used, load;
@@ -227,11 +218,9 @@ TEST_F(DiskCacheTest, BlockFiles_Stats) {
// Tests that we add and remove blocks correctly.
TEST_F(DiskCacheTest, AllocationMap) {
- FilePath path = GetCacheFilePath();
- ASSERT_TRUE(DeleteCache(path));
- ASSERT_TRUE(file_util::CreateDirectory(path));
+ ScopedTestCache test_cache;
- BlockFiles files(path);
+ BlockFiles files(test_cache.path());
ASSERT_TRUE(files.Init(true));
// Create a bunch of entries.
diff --git a/net/disk_cache/disk_cache_test_base.cc b/net/disk_cache/disk_cache_test_base.cc
index cffe3fa..2d3d376 100644
--- a/net/disk_cache/disk_cache_test_base.cc
+++ b/net/disk_cache/disk_cache_test_base.cc
@@ -55,9 +55,8 @@ void DiskCacheTestWithCache::InitMemoryCache() {
}
void DiskCacheTestWithCache::InitDiskCache() {
- FilePath path = GetCacheFilePath();
if (first_cleanup_)
- ASSERT_TRUE(DeleteCache(path));
+ ASSERT_TRUE(DeleteCache(test_cache_.path()));
if (!cache_thread_.IsRunning()) {
EXPECT_TRUE(cache_thread_.StartWithOptions(
@@ -66,7 +65,7 @@ void DiskCacheTestWithCache::InitDiskCache() {
ASSERT_TRUE(cache_thread_.message_loop() != NULL);
if (implementation_)
- return InitDiskCacheImpl(path);
+ return InitDiskCacheImpl(test_cache_.path());
scoped_refptr<base::MessageLoopProxy> thread =
use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() :
@@ -74,8 +73,8 @@ void DiskCacheTestWithCache::InitDiskCache() {
TestCompletionCallback cb;
int rv = disk_cache::BackendImpl::CreateBackend(
- path, force_creation_, size_, type_,
- disk_cache::kNoRandom, thread, &cache_, &cb);
+ test_cache_.path(), force_creation_, size_, type_,
+ disk_cache::kNoRandom, thread, &cache_, &cb);
ASSERT_EQ(net::OK, cb.GetResult(rv));
}
@@ -111,8 +110,7 @@ void DiskCacheTestWithCache::TearDown() {
cache_thread_.Stop();
if (!memory_only_ && integrity_) {
- FilePath path = GetCacheFilePath();
- EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_));
+ EXPECT_TRUE(CheckCacheIntegrity(path(), new_eviction_));
}
PlatformTest::TearDown();
@@ -127,10 +125,9 @@ void DiskCacheTestWithCache::SimulateCrash() {
cache_impl_->ClearRefCountForTest();
delete cache_impl_;
- FilePath path = GetCacheFilePath();
- EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_));
+ EXPECT_TRUE(CheckCacheIntegrity(path(), new_eviction_));
- InitDiskCacheImpl(path);
+ InitDiskCacheImpl(path());
}
void DiskCacheTestWithCache::SetTestMode() {
diff --git a/net/disk_cache/disk_cache_test_base.h b/net/disk_cache/disk_cache_test_base.h
index faecc17..0121191 100644
--- a/net/disk_cache/disk_cache_test_base.h
+++ b/net/disk_cache/disk_cache_test_base.h
@@ -9,6 +9,7 @@
#include "base/basictypes.h"
#include "base/thread.h"
#include "net/base/cache_type.h"
+#include "net/disk_cache/disk_cache_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@@ -92,6 +93,10 @@ class DiskCacheTestWithCache : public DiskCacheTest {
type_ = type;
}
+ FilePath path() const {
+ return test_cache_.path();
+ }
+
// Utility methods to access the cache and wait for each operation to finish.
int OpenEntry(const std::string& key, disk_cache::Entry** entry);
int CreateEntry(const std::string& key, disk_cache::Entry** entry);
@@ -136,7 +141,9 @@ class DiskCacheTestWithCache : public DiskCacheTest {
void InitDiskCache();
void InitDiskCacheImpl(const FilePath& path);
+ ScopedTestCache test_cache_;
base::Thread cache_thread_;
+
DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache);
};
diff --git a/net/disk_cache/disk_cache_test_util.cc b/net/disk_cache/disk_cache_test_util.cc
index 7dd618a..095b693 100644
--- a/net/disk_cache/disk_cache_test_util.cc
+++ b/net/disk_cache/disk_cache_test_util.cc
@@ -16,20 +16,6 @@
using base::Time;
using base::TimeDelta;
-namespace {
-
-FilePath BuildCachePath(const std::string& name) {
- FilePath path;
- PathService::Get(base::DIR_TEMP, &path); // Ignore return value;
- path = path.AppendASCII(name);
- if (!file_util::PathExists(path))
- file_util::CreateDirectory(path);
-
- return path;
-}
-
-} // namespace.
-
std::string GenerateKey(bool same_length) {
char key[200];
CacheTestFillBuffer(key, sizeof(key), same_length);
@@ -55,10 +41,6 @@ void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls) {
buffer[0] = 'g';
}
-FilePath GetCacheFilePath() {
- return BuildCachePath("cache_test");
-}
-
bool CreateCacheTestFile(const FilePath& name) {
int flags = base::PLATFORM_FILE_CREATE_ALWAYS |
base::PLATFORM_FILE_READ |
@@ -78,7 +60,7 @@ bool DeleteCache(const FilePath& path) {
return true;
}
-bool CopyTestCache(const std::string& name) {
+bool CopyTestCache(const std::string& name, const FilePath& destination) {
FilePath path;
PathService::Get(base::DIR_SOURCE_ROOT, &path);
path = path.AppendASCII("net");
@@ -86,10 +68,9 @@ bool CopyTestCache(const std::string& name) {
path = path.AppendASCII("cache_tests");
path = path.AppendASCII(name);
- FilePath dest = GetCacheFilePath();
- if (!DeleteCache(dest))
+ if (!DeleteCache(destination))
return false;
- return file_util::CopyDirectory(path, dest, false);
+ return file_util::CopyDirectory(path, destination, false);
}
bool CheckCacheIntegrity(const FilePath& path, bool new_eviction) {
@@ -105,19 +86,11 @@ bool CheckCacheIntegrity(const FilePath& path, bool new_eviction) {
return cache->SelfCheck() >= 0;
}
-ScopedTestCache::ScopedTestCache() : path_(GetCacheFilePath()) {
- bool result = DeleteCache(path_);
- DCHECK(result);
-}
-
-ScopedTestCache::ScopedTestCache(const std::string& name)
- : path_(BuildCachePath(name)) {
- bool result = DeleteCache(path_);
- DCHECK(result);
+ScopedTestCache::ScopedTestCache() {
+ temp_dir_.CreateUniqueTempDir();
}
ScopedTestCache::~ScopedTestCache() {
- file_util::Delete(path(), true);
}
// -----------------------------------------------------------------------
diff --git a/net/disk_cache/disk_cache_test_util.h b/net/disk_cache/disk_cache_test_util.h
index f6348e9..f4864bb 100644
--- a/net/disk_cache/disk_cache_test_util.h
+++ b/net/disk_cache/disk_cache_test_util.h
@@ -11,6 +11,7 @@
#include "base/callback.h"
#include "base/file_path.h"
#include "base/message_loop.h"
+#include "base/scoped_temp_dir.h"
#include "base/timer.h"
#include "build/build_config.h"
@@ -20,11 +21,8 @@ bool CreateCacheTestFile(const FilePath& name);
// Deletes all file son the cache.
bool DeleteCache(const FilePath& path);
-// Copies a set of cache files from the data folder to the test folder.
-bool CopyTestCache(const std::string& name);
-
-// Gets the path to the cache test folder.
-FilePath GetCacheFilePath();
+// Copies a set of cache files from the data folder to the destination folder.
+bool CopyTestCache(const std::string& name, const FilePath& destination);
// Fills buffer with random values (may contain nulls unless no_nulls is true).
void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls);
@@ -40,14 +38,12 @@ bool CheckCacheIntegrity(const FilePath& path, bool new_eviction);
class ScopedTestCache {
public:
ScopedTestCache();
- // Use a specific folder name.
- explicit ScopedTestCache(const std::string& name);
~ScopedTestCache();
- FilePath path() const { return path_; }
+ FilePath path() const { return temp_dir_.path(); }
private:
- const FilePath path_; // Path to the cache test folder.
+ ScopedTempDir temp_dir_;
DISALLOW_COPY_AND_ASSIGN(ScopedTestCache);
};
diff --git a/net/disk_cache/mapped_file_unittest.cc b/net/disk_cache/mapped_file_unittest.cc
index 8b01a7d..4404be6 100644
--- a/net/disk_cache/mapped_file_unittest.cc
+++ b/net/disk_cache/mapped_file_unittest.cc
@@ -61,7 +61,9 @@ void WaitForCallbacks(int expected) {
} // namespace
TEST_F(DiskCacheTest, MappedFile_SyncIO) {
- FilePath filename = GetCacheFilePath().AppendASCII("a_test");
+ ScopedTestCache test_cache;
+ FilePath filename = test_cache.path().AppendASCII("a_test");
+
scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile);
ASSERT_TRUE(CreateCacheTestFile(filename));
ASSERT_TRUE(file->Init(filename, 8192));
@@ -76,7 +78,9 @@ TEST_F(DiskCacheTest, MappedFile_SyncIO) {
}
TEST_F(DiskCacheTest, MappedFile_AsyncIO) {
- FilePath filename = GetCacheFilePath().AppendASCII("a_test");
+ ScopedTestCache test_cache;
+ FilePath filename = test_cache.path().AppendASCII("a_test");
+
scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile);
ASSERT_TRUE(CreateCacheTestFile(filename));
ASSERT_TRUE(file->Init(filename, 8192));
diff --git a/net/disk_cache/storage_block_unittest.cc b/net/disk_cache/storage_block_unittest.cc
index c9406b6..abaa4a7 100644
--- a/net/disk_cache/storage_block_unittest.cc
+++ b/net/disk_cache/storage_block_unittest.cc
@@ -10,7 +10,9 @@
#include "testing/gtest/include/gtest/gtest.h"
TEST_F(DiskCacheTest, StorageBlock_LoadStore) {
- FilePath filename = GetCacheFilePath().AppendASCII("a_test");
+ ScopedTestCache test_cache;
+ FilePath filename = test_cache.path().AppendASCII("a_test");
+
scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile);
ASSERT_TRUE(CreateCacheTestFile(filename));
ASSERT_TRUE(file->Init(filename, 8192));
@@ -30,7 +32,9 @@ TEST_F(DiskCacheTest, StorageBlock_LoadStore) {
}
TEST_F(DiskCacheTest, StorageBlock_SetData) {
- FilePath filename = GetCacheFilePath().AppendASCII("a_test");
+ ScopedTestCache test_cache;
+ FilePath filename = test_cache.path().AppendASCII("a_test");
+
scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile);
ASSERT_TRUE(CreateCacheTestFile(filename));
ASSERT_TRUE(file->Init(filename, 8192));
@@ -50,7 +54,9 @@ TEST_F(DiskCacheTest, StorageBlock_SetData) {
}
TEST_F(DiskCacheTest, StorageBlock_SetModified) {
- FilePath filename = GetCacheFilePath().AppendASCII("a_test");
+ ScopedTestCache test_cache;
+ FilePath filename = test_cache.path().AppendASCII("a_test");
+
scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile);
ASSERT_TRUE(CreateCacheTestFile(filename));
ASSERT_TRUE(file->Init(filename, 8192));
diff --git a/net/disk_cache/stress_cache.cc b/net/disk_cache/stress_cache.cc
index 98dcbe1..51fda4c 100644
--- a/net/disk_cache/stress_cache.cc
+++ b/net/disk_cache/stress_cache.cc
@@ -29,6 +29,7 @@
#include "base/command_line.h"
#include "base/debug_util.h"
#include "base/file_path.h"
+#include "base/file_util.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/path_service.h"
@@ -50,6 +51,16 @@ using base::Time;
const int kError = -1;
const int kExpectedCrash = 100;
+FilePath GetStressCacheFilePath() {
+ FilePath path;
+ PathService::Get(base::DIR_TEMP, &path); // Ignore return value;
+ path = path.AppendASCII("cache_test_stress");
+ if (!file_util::PathExists(path))
+ file_util::CreateDirectory(path);
+
+ return path;
+}
+
// Starts a new process.
int RunSlave(int iteration) {
FilePath exe;
@@ -92,7 +103,7 @@ int MasterCode() {
// to know which instance of the application wrote them.
void StressTheCache(int iteration) {
int cache_size = 0x800000; // 8MB
- FilePath path = GetCacheFilePath().InsertBeforeExtensionASCII("_stress");
+ FilePath path = GetStressCacheFilePath();
base::Thread cache_thread("CacheThread");
if (!cache_thread.StartWithOptions(