OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/disk_cache/disk_cache_test.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/threading/platform_thread.h" |
| 11 #include "net/base/test_completion_callback.h" |
| 12 #include "net/disk_cache/cache_util.h" |
| 13 #include "net/disk_cache/disk_cache.h" |
| 14 #include "net/disk_cache/disk_cache_test_util.h" |
| 15 |
| 16 namespace disk_cache { |
| 17 |
| 18 BackendTestTraits::CreateBackendExtraData::~CreateBackendExtraData() { |
| 19 } |
| 20 |
| 21 bool BackendTestTraits::WritesUpdateLastUsed() const { |
| 22 return true; |
| 23 } |
| 24 |
| 25 bool BackendTestTraits::ReadsUpdateLastUsed() const { |
| 26 return true; |
| 27 } |
| 28 |
| 29 int BackendTestTraits::SparseRoundingInterval() const { |
| 30 return 1; |
| 31 } |
| 32 |
| 33 bool BackendTestTraits::EntryCountIncludesSparseRanges() const { |
| 34 return false; |
| 35 } |
| 36 |
| 37 bool BackendTestTraits::ImplementsCouldBeSparse() const { |
| 38 return true; |
| 39 } |
| 40 |
| 41 bool BackendTestTraits::DoomedSparseEntriesIOWorks() const { |
| 42 return true; |
| 43 } |
| 44 |
| 45 bool BackendTestTraits::EnumerationsAreLexicographicByKey() const { |
| 46 return true; |
| 47 } |
| 48 |
| 49 DiskCacheTest::DiskCacheTest() : use_current_thread_(false), |
| 50 max_size_(0), |
| 51 traits_(GetParam()), |
| 52 cache_thread_("CacheThread") { |
| 53 CHECK(temp_dir_.CreateUniqueTempDir()); |
| 54 if (!base::MessageLoop::current()) |
| 55 message_loop_.reset(new base::MessageLoopForIO()); |
| 56 } |
| 57 |
| 58 DiskCacheTest::~DiskCacheTest() { |
| 59 } |
| 60 |
| 61 bool DiskCacheTest::CopyTestCache(const std::string& name) { |
| 62 base::FilePath path; |
| 63 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 64 path = path.AppendASCII("net"); |
| 65 path = path.AppendASCII("data"); |
| 66 path = path.AppendASCII("cache_tests"); |
| 67 path = path.AppendASCII(name); |
| 68 |
| 69 if (!CleanupCacheDir()) |
| 70 return false; |
| 71 return base::CopyDirectory(path, cache_path(), false); |
| 72 } |
| 73 |
| 74 bool DiskCacheTest::CleanupCacheDir() { |
| 75 return ::DeleteCache(cache_path()); |
| 76 } |
| 77 |
| 78 void DiskCacheTest::TearDown() { |
| 79 base::RunLoop().RunUntilIdle(); |
| 80 if (traits_) { |
| 81 traits_->PreTearDown(); |
| 82 base::RunLoop().RunUntilIdle(); |
| 83 } |
| 84 cache_.reset(); |
| 85 if (cache_thread_.IsRunning()) |
| 86 cache_thread_.Stop(); |
| 87 |
| 88 base::RunLoop().RunUntilIdle(); |
| 89 if (traits_) |
| 90 traits_->PostTearDown(); |
| 91 } |
| 92 |
| 93 void DiskCacheTest::InitCacheWithExtraData( |
| 94 const BackendTestTraits::CreateBackendExtraData* extra_data) { |
| 95 base::MessageLoopProxy* runner; |
| 96 if (use_current_thread_) { |
| 97 runner = base::MessageLoopProxy::current().get(); |
| 98 } else { |
| 99 if (!cache_thread_.IsRunning()) { |
| 100 ASSERT_TRUE(cache_thread_.StartWithOptions( |
| 101 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| 102 } |
| 103 runner = cache_thread_.message_loop_proxy().get(); |
| 104 } |
| 105 |
| 106 cache_.reset(); |
| 107 cache_.reset(traits_->CreateBackend(extra_data, cache_path(), max_size_, runne
r)); |
| 108 } |
| 109 |
| 110 int DiskCacheTest::OpenEntry(const std::string& key, |
| 111 disk_cache::Entry** entry) { |
| 112 net::TestCompletionCallback cb; |
| 113 int rv = cache_->OpenEntry(key, entry, cb.callback()); |
| 114 return cb.GetResult(rv); |
| 115 } |
| 116 |
| 117 int DiskCacheTest::CreateEntry(const std::string& key, |
| 118 disk_cache::Entry** entry) { |
| 119 net::TestCompletionCallback cb; |
| 120 int rv = cache_->CreateEntry(key, entry, cb.callback()); |
| 121 return cb.GetResult(rv); |
| 122 } |
| 123 |
| 124 int DiskCacheTest::DoomEntry(const std::string& key) { |
| 125 net::TestCompletionCallback cb; |
| 126 int rv = cache_->DoomEntry(key, cb.callback()); |
| 127 return cb.GetResult(rv); |
| 128 } |
| 129 |
| 130 int DiskCacheTest::DoomAllEntries() { |
| 131 net::TestCompletionCallback cb; |
| 132 int rv = cache_->DoomAllEntries(cb.callback()); |
| 133 return cb.GetResult(rv); |
| 134 } |
| 135 |
| 136 int DiskCacheTest::DoomEntriesBetween(const base::Time initial_time, |
| 137 const base::Time end_time) { |
| 138 net::TestCompletionCallback cb; |
| 139 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback()); |
| 140 return cb.GetResult(rv); |
| 141 } |
| 142 |
| 143 int DiskCacheTest::DoomEntriesSince(const base::Time initial_time) { |
| 144 net::TestCompletionCallback cb; |
| 145 int rv = cache_->DoomEntriesSince(initial_time, cb.callback()); |
| 146 return cb.GetResult(rv); |
| 147 } |
| 148 |
| 149 int DiskCacheTest::OpenNextEntry(void** iter, |
| 150 disk_cache::Entry** next_entry) { |
| 151 net::TestCompletionCallback cb; |
| 152 int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback()); |
| 153 return cb.GetResult(rv); |
| 154 } |
| 155 |
| 156 void DiskCacheTest::SetMaxSize(int max_size) { |
| 157 max_size_ = max_size; |
| 158 if (cache_) |
| 159 traits_->SetMaxSize(cache_.get(), max_size); |
| 160 } |
| 161 |
| 162 void DiskCacheTest::FlushQueueForTest() { |
| 163 traits_->FlushQueueForTest(cache_.get()); |
| 164 } |
| 165 |
| 166 int DiskCacheTest::ReadData(disk_cache::Entry* entry, int index, |
| 167 int offset, net::IOBuffer* buf, int len) { |
| 168 net::TestCompletionCallback cb; |
| 169 int rv = entry->ReadData(index, offset, buf, len, cb.callback()); |
| 170 return cb.GetResult(rv); |
| 171 } |
| 172 |
| 173 int DiskCacheTest::WriteData(disk_cache::Entry* entry, int index, |
| 174 int offset, net::IOBuffer* buf, int len, |
| 175 bool truncate) { |
| 176 net::TestCompletionCallback cb; |
| 177 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate); |
| 178 return cb.GetResult(rv); |
| 179 } |
| 180 |
| 181 int DiskCacheTest::ReadSparseData(disk_cache::Entry* entry, |
| 182 int64 offset, net::IOBuffer* buf, |
| 183 int len) { |
| 184 net::TestCompletionCallback cb; |
| 185 int rv = entry->ReadSparseData(offset, buf, len, cb.callback()); |
| 186 return cb.GetResult(rv); |
| 187 } |
| 188 |
| 189 int DiskCacheTest::WriteSparseData(disk_cache::Entry* entry, |
| 190 int64 offset, |
| 191 net::IOBuffer* buf, int len) { |
| 192 net::TestCompletionCallback cb; |
| 193 int rv = entry->WriteSparseData(offset, buf, len, cb.callback()); |
| 194 return cb.GetResult(rv); |
| 195 } |
| 196 |
| 197 base::MessageLoopProxy* DiskCacheTest::GetCacheThread() { |
| 198 if (traits_->UsesCacheThread()) |
| 199 return cache_thread_.message_loop_proxy(); |
| 200 |
| 201 return base::MessageLoopProxy::current(); |
| 202 } |
| 203 |
| 204 void BackendTestTraits::AddDelay() const { |
| 205 base::Time initial = base::Time::Now(); |
| 206 while (base::Time::Now() <= initial) { |
| 207 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
| 208 }; |
| 209 } |
| 210 |
| 211 void DiskCacheTest::AddDelay() { |
| 212 traits()->AddDelay(); |
| 213 } |
| 214 |
| 215 } // namespace disk_cache |
OLD | NEW |