Chromium Code Reviews| Index: net/disk_cache/backend_unittest.cc |
| diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc |
| index 0df770ea963c36550af676e709986ade28446318..2d6d3f01dbf06525846fb721dc8c33567582f956 100644 |
| --- a/net/disk_cache/backend_unittest.cc |
| +++ b/net/disk_cache/backend_unittest.cc |
| @@ -41,26 +41,25 @@ namespace { |
| const char kExistingEntryKey[] = "existing entry key"; |
| -disk_cache::BackendImpl* CreateExistingEntryCache( |
| +scoped_ptr<disk_cache::BackendImpl> CreateExistingEntryCache( |
| const base::Thread& cache_thread, |
| base::FilePath& cache_path) { |
| net::TestCompletionCallback cb; |
| - disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
| - cache_path, cache_thread.message_loop_proxy(), NULL); |
| + scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
| + cache_path, cache_thread.message_loop_proxy(), NULL)); |
| int rv = cache->Init(cb.callback()); |
| if (cb.GetResult(rv) != net::OK) |
| - return NULL; |
| + return scoped_ptr<disk_cache::BackendImpl>(); |
| disk_cache::Entry* entry = NULL; |
| rv = cache->CreateEntry(kExistingEntryKey, &entry, cb.callback()); |
| if (cb.GetResult(rv) != net::OK) { |
| - delete cache; |
| - return NULL; |
| + return scoped_ptr<disk_cache::BackendImpl>(); |
| } |
|
rvargas (doing something else)
2013/07/26 21:05:01
nit: remove {}
qsr
2013/07/29 08:26:28
Done.
|
| entry->Close(); |
| - return cache; |
| + return cache.Pass(); |
| } |
| } // namespace |
| @@ -268,11 +267,10 @@ TEST_F(DiskCacheTest, CreateBackend) { |
| base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| // Test the private factory method(s). |
| - disk_cache::Backend* cache = NULL; |
| + scoped_ptr<disk_cache::Backend> cache; |
| cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL); |
| - ASSERT_TRUE(cache); |
| - delete cache; |
| - cache = NULL; |
| + ASSERT_TRUE(cache.get()); |
| + cache.reset(); |
| // Now test the public API. |
| int rv = |
| @@ -286,9 +284,8 @@ TEST_F(DiskCacheTest, CreateBackend) { |
| &cache, |
| cb.callback()); |
| ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| - ASSERT_TRUE(cache); |
| - delete cache; |
| - cache = NULL; |
| + ASSERT_TRUE(cache.get()); |
| + cache.reset(); |
| rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, |
| net::CACHE_BACKEND_DEFAULT, |
| @@ -296,8 +293,8 @@ TEST_F(DiskCacheTest, CreateBackend) { |
| false, NULL, NULL, &cache, |
| cb.callback()); |
| ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| - ASSERT_TRUE(cache); |
| - delete cache; |
| + ASSERT_TRUE(cache.get()); |
| + cache.reset(); |
| } |
| base::MessageLoop::current()->RunUntilIdle(); |
| @@ -314,13 +311,13 @@ TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) { |
| net::TestCompletionCallback cb; |
| bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
| - disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
| - cache_path_, cache_thread.message_loop_proxy().get(), NULL); |
| + scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
| + cache_path_, cache_thread.message_loop_proxy().get(), NULL)); |
| int rv = cache->Init(cb.callback()); |
| ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); |
| base::ThreadRestrictions::SetIOAllowed(prev); |
| - delete cache; |
| + cache.reset(); |
| DisableIntegrityCheck(); |
| } |
| @@ -390,9 +387,7 @@ void DiskCacheBackendTest::BackendShutdownWithPendingFileIO(bool fast) { |
| entry->Release(); |
| // The cache destructor will see one pending operation here. |
| - delete cache_; |
| - // Prevent the TearDown() to delete the backend again. |
| - cache_ = NULL; |
| + cache_.reset(); |
| if (rv == net::ERR_IO_PENDING) { |
| if (fast) |
| @@ -448,9 +443,7 @@ void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) { |
| entry->Close(); |
| // The cache destructor will see one pending operation here. |
| - delete cache_; |
| - // Prevent the TearDown() to delete the backend again. |
| - cache_ = NULL; |
| + cache_.reset(); |
| } |
| base::MessageLoop::current()->RunUntilIdle(); |
| @@ -486,9 +479,7 @@ void DiskCacheBackendTest::BackendShutdownWithPendingCreate(bool fast) { |
| int rv = cache_->CreateEntry("some key", &entry, cb.callback()); |
| ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| - delete cache_; |
| - // Prevent the TearDown() to delete the backend again. |
| - cache_ = NULL; |
| + cache_.reset(); |
| EXPECT_FALSE(cb.have_result()); |
| } |
| @@ -517,7 +508,7 @@ TEST_F(DiskCacheTest, TruncatedIndex) { |
| base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| net::TestCompletionCallback cb; |
| - disk_cache::Backend* backend = NULL; |
| + scoped_ptr<disk_cache::Backend> backend; |
| int rv = |
| disk_cache::CreateCacheBackend(net::DISK_CACHE, |
| net::CACHE_BACKEND_BLOCKFILE, |
| @@ -530,8 +521,7 @@ TEST_F(DiskCacheTest, TruncatedIndex) { |
| cb.callback()); |
| ASSERT_NE(net::OK, cb.GetResult(rv)); |
| - ASSERT_TRUE(backend == NULL); |
| - delete backend; |
| + ASSERT_TRUE(backend.get() == NULL); |
|
rvargas (doing something else)
2013/07/26 21:05:01
nit: false(get())
qsr
2013/07/29 08:26:28
Done.
|
| } |
| void DiskCacheBackendTest::BackendSetSize() { |
| @@ -1603,8 +1593,7 @@ void DiskCacheBackendTest::BackendTransaction(const std::string& name, |
| ASSERT_EQ(num_entries - 1, actual); |
| } |
| - delete cache_; |
| - cache_ = NULL; |
| + cache_.reset(); |
| cache_impl_ = NULL; |
| ASSERT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask)); |
| @@ -1723,12 +1712,10 @@ TEST_F(DiskCacheTest, WrongVersion) { |
| base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| net::TestCompletionCallback cb; |
| - disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
| - cache_path_, cache_thread.message_loop_proxy().get(), NULL); |
| + scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
| + cache_path_, cache_thread.message_loop_proxy().get(), NULL)); |
| int rv = cache->Init(cb.callback()); |
| ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); |
| - |
| - delete cache; |
| } |
| class BadEntropyProvider : public base::FieldTrial::EntropyProvider { |
| @@ -1748,11 +1735,10 @@ TEST_F(DiskCacheTest, SimpleCacheControlJoin) { |
| ASSERT_TRUE(cache_thread.StartWithOptions( |
| base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| - disk_cache::BackendImpl* cache = CreateExistingEntryCache(cache_thread, |
| - cache_path_); |
| - ASSERT_TRUE(cache); |
| - delete cache; |
| - cache = NULL; |
| + scoped_ptr<disk_cache::BackendImpl> cache = |
| + CreateExistingEntryCache(cache_thread, cache_path_); |
| + ASSERT_TRUE(cache.get()); |
| + cache.reset(); |
| // Instantiate the SimpleCacheTrial, forcing this run into the |
| // ExperimentControl group. |
| @@ -1760,7 +1746,7 @@ TEST_F(DiskCacheTest, SimpleCacheControlJoin) { |
| base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", |
| "ExperimentControl"); |
| net::TestCompletionCallback cb; |
| - disk_cache::Backend* base_cache = NULL; |
| + scoped_ptr<disk_cache::Backend> base_cache; |
| int rv = |
| disk_cache::CreateCacheBackend(net::DISK_CACHE, |
| net::CACHE_BACKEND_BLOCKFILE, |
| @@ -1772,9 +1758,8 @@ TEST_F(DiskCacheTest, SimpleCacheControlJoin) { |
| &base_cache, |
| cb.callback()); |
| ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| - cache = static_cast<disk_cache::BackendImpl*>(base_cache); |
| EXPECT_EQ(0, base_cache->GetEntryCount()); |
| - delete cache; |
| + cache.reset(static_cast<disk_cache::BackendImpl*>(base_cache.release())); |
|
rvargas (doing something else)
2013/07/26 21:05:01
nit: remove this line
qsr
2013/07/29 08:26:28
Done.
|
| } |
| // Tests that the disk cache can restart in the control group preserving |
| @@ -1790,19 +1775,16 @@ TEST_F(DiskCacheTest, SimpleCacheControlRestart) { |
| ASSERT_TRUE(cache_thread.StartWithOptions( |
| base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| - disk_cache::BackendImpl* cache = CreateExistingEntryCache(cache_thread, |
| - cache_path_); |
| - ASSERT_TRUE(cache); |
| - delete cache; |
| - cache = NULL; |
| + scoped_ptr<disk_cache::BackendImpl> cache = |
| + CreateExistingEntryCache(cache_thread, cache_path_); |
| + ASSERT_TRUE(cache.get()); |
| net::TestCompletionCallback cb; |
| const int kRestartCount = 5; |
| for (int i=0; i < kRestartCount; ++i) { |
| - cache = new disk_cache::BackendImpl(cache_path_, |
| - cache_thread.message_loop_proxy(), |
| - NULL); |
| + cache.reset(new disk_cache::BackendImpl( |
| + cache_path_, cache_thread.message_loop_proxy(), NULL)); |
| int rv = cache->Init(cb.callback()); |
| ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| EXPECT_EQ(1, cache->GetEntryCount()); |
| @@ -1812,8 +1794,6 @@ TEST_F(DiskCacheTest, SimpleCacheControlRestart) { |
| EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| EXPECT_TRUE(entry); |
| entry->Close(); |
| - delete cache; |
| - cache = NULL; |
| } |
| } |
| @@ -1831,10 +1811,9 @@ TEST_F(DiskCacheTest, SimpleCacheControlLeave) { |
| base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", |
| "ExperimentControl"); |
| - disk_cache::BackendImpl* cache = CreateExistingEntryCache(cache_thread, |
| - cache_path_); |
| - ASSERT_TRUE(cache); |
| - delete cache; |
| + scoped_ptr<disk_cache::BackendImpl> cache = |
| + CreateExistingEntryCache(cache_thread, cache_path_); |
| + ASSERT_TRUE(cache.get()); |
| } |
| // Instantiate the SimpleCacheTrial, forcing this run into the |
| @@ -1845,8 +1824,8 @@ TEST_F(DiskCacheTest, SimpleCacheControlLeave) { |
| const int kRestartCount = 5; |
| for (int i = 0; i < kRestartCount; ++i) { |
| - disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
| - cache_path_, cache_thread.message_loop_proxy(), NULL); |
| + scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
| + cache_path_, cache_thread.message_loop_proxy(), NULL)); |
| int rv = cache->Init(cb.callback()); |
| ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| EXPECT_EQ(1, cache->GetEntryCount()); |
| @@ -1856,8 +1835,6 @@ TEST_F(DiskCacheTest, SimpleCacheControlLeave) { |
| EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| EXPECT_TRUE(entry); |
| entry->Close(); |
| - delete cache; |
| - cache = NULL; |
| } |
| } |
| @@ -1885,8 +1862,7 @@ TEST_F(DiskCacheBackendTest, DeleteOld) { |
| path.clear(); // Make sure path was captured by the previous call. |
| ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| base::ThreadRestrictions::SetIOAllowed(prev); |
| - delete cache_; |
| - cache_ = NULL; |
| + cache_.reset(); |
| EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
| } |
| @@ -2760,7 +2736,7 @@ TEST_F(DiskCacheTest, MultipleInstances) { |
| net::TestCompletionCallback cb; |
| const int kNumberOfCaches = 2; |
| - disk_cache::Backend* cache[kNumberOfCaches]; |
| + scoped_ptr<disk_cache::Backend> cache[kNumberOfCaches]; |
| int rv = |
| disk_cache::CreateCacheBackend(net::DISK_CACHE, |
| @@ -2784,7 +2760,7 @@ TEST_F(DiskCacheTest, MultipleInstances) { |
| cb.callback()); |
| ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| - ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL); |
| + ASSERT_TRUE(cache[0].get() != NULL && cache[1].get() != NULL); |
| std::string key("the first key"); |
| disk_cache::Entry* entry; |
| @@ -2793,8 +2769,6 @@ TEST_F(DiskCacheTest, MultipleInstances) { |
| ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| entry->Close(); |
| } |
| - delete cache[0]; |
| - delete cache[1]; |
| } |
| // Test the six regions of the curve that determines the max cache size. |
| @@ -3005,7 +2979,7 @@ TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) { |
| void DiskCacheBackendTest::TracingBackendBasics() { |
| InitCache(); |
| - cache_ = new disk_cache::TracingCacheBackend(cache_); |
| + cache_.reset(new disk_cache::TracingCacheBackend(cache_.release())); |
| cache_impl_ = NULL; |
| EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType()); |
| if (!simple_cache_mode_) { |
| @@ -3200,8 +3174,7 @@ TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) { |
| ASSERT_EQ(net::OK, CreateEntry("key", &entry)); |
| ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); |
| entry->Close(); |
| - delete cache_; |
| - cache_ = NULL; |
| + cache_.reset(); |
| // Check that the |SimpleBackendImpl| does not favor this structure. |
| base::Thread cache_thread("CacheThread"); |
| @@ -3233,8 +3206,7 @@ TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) { |
| ASSERT_EQ(net::OK, CreateEntry("key", &entry)); |
| ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); |
| entry->Close(); |
| - delete cache_; |
| - cache_ = NULL; |
| + cache_.reset(); |
| // Check that the |BackendImpl| does not favor this structure. |
| base::Thread cache_thread("CacheThread"); |