OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 2007 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2018 InitCache(); | 2018 InitCache(); |
2019 DoomSparseEntry(); | 2019 DoomSparseEntry(); |
2020 } | 2020 } |
2021 | 2021 |
2022 // A CompletionCallback wrapper that deletes the cache from within the callback. | 2022 // A CompletionCallback wrapper that deletes the cache from within the callback. |
2023 // The way a CompletionCallback works means that all tasks (even new ones) | 2023 // The way a CompletionCallback works means that all tasks (even new ones) |
2024 // are executed by the message loop before returning to the caller so the only | 2024 // are executed by the message loop before returning to the caller so the only |
2025 // way to simulate a race is to execute what we want on the callback. | 2025 // way to simulate a race is to execute what we want on the callback. |
2026 class SparseTestCompletionCallback: public net::TestCompletionCallback { | 2026 class SparseTestCompletionCallback: public net::TestCompletionCallback { |
2027 public: | 2027 public: |
2028 explicit SparseTestCompletionCallback(disk_cache::Backend* cache) | 2028 explicit SparseTestCompletionCallback(scoped_ptr<disk_cache::Backend> cache) |
2029 : cache_(cache) { | 2029 : cache_(cache.Pass()) { |
2030 } | 2030 } |
2031 | 2031 |
2032 private: | 2032 private: |
2033 virtual void SetResult(int result) OVERRIDE { | 2033 virtual void SetResult(int result) OVERRIDE { |
2034 delete cache_; | 2034 cache_.reset(); |
2035 TestCompletionCallback::SetResult(result); | 2035 TestCompletionCallback::SetResult(result); |
2036 } | 2036 } |
2037 | 2037 |
2038 disk_cache::Backend* cache_; | 2038 scoped_ptr<disk_cache::Backend> cache_; |
2039 DISALLOW_COPY_AND_ASSIGN(SparseTestCompletionCallback); | 2039 DISALLOW_COPY_AND_ASSIGN(SparseTestCompletionCallback); |
2040 }; | 2040 }; |
2041 | 2041 |
2042 // Tests that we don't crash when the backend is deleted while we are working | 2042 // Tests that we don't crash when the backend is deleted while we are working |
2043 // deleting the sub-entries of a sparse entry. | 2043 // deleting the sub-entries of a sparse entry. |
2044 TEST_F(DiskCacheEntryTest, DoomSparseEntry2) { | 2044 TEST_F(DiskCacheEntryTest, DoomSparseEntry2) { |
2045 UseCurrentThread(); | 2045 UseCurrentThread(); |
2046 InitCache(); | 2046 InitCache(); |
2047 std::string key("the key"); | 2047 std::string key("the key"); |
2048 disk_cache::Entry* entry; | 2048 disk_cache::Entry* entry; |
2049 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 2049 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
2050 | 2050 |
2051 const int kSize = 4 * 1024; | 2051 const int kSize = 4 * 1024; |
2052 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize)); | 2052 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize)); |
2053 CacheTestFillBuffer(buf->data(), kSize, false); | 2053 CacheTestFillBuffer(buf->data(), kSize, false); |
2054 | 2054 |
2055 int64 offset = 1024; | 2055 int64 offset = 1024; |
2056 // Write to a bunch of ranges. | 2056 // Write to a bunch of ranges. |
2057 for (int i = 0; i < 12; i++) { | 2057 for (int i = 0; i < 12; i++) { |
2058 EXPECT_EQ(kSize, | 2058 EXPECT_EQ(kSize, |
2059 entry->WriteSparseData( | 2059 entry->WriteSparseData( |
2060 offset, buf.get(), kSize, net::CompletionCallback())); | 2060 offset, buf.get(), kSize, net::CompletionCallback())); |
2061 offset *= 4; | 2061 offset *= 4; |
2062 } | 2062 } |
2063 EXPECT_EQ(9, cache_->GetEntryCount()); | 2063 EXPECT_EQ(9, cache_->GetEntryCount()); |
2064 | 2064 |
2065 entry->Close(); | 2065 entry->Close(); |
2066 SparseTestCompletionCallback cb(cache_); | 2066 disk_cache::Backend* cache = cache_.get(); |
2067 int rv = cache_->DoomEntry(key, cb.callback()); | 2067 SparseTestCompletionCallback cb(cache_.Pass()); |
| 2068 int rv = cache->DoomEntry(key, cb.callback()); |
2068 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 2069 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
2069 EXPECT_EQ(net::OK, cb.WaitForResult()); | 2070 EXPECT_EQ(net::OK, cb.WaitForResult()); |
2070 | |
2071 // TearDown will attempt to delete the cache_. | |
2072 cache_ = NULL; | |
2073 } | 2071 } |
2074 | 2072 |
2075 void DiskCacheEntryTest::PartialSparseEntry() { | 2073 void DiskCacheEntryTest::PartialSparseEntry() { |
2076 std::string key("the first key"); | 2074 std::string key("the first key"); |
2077 disk_cache::Entry* entry; | 2075 disk_cache::Entry* entry; |
2078 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 2076 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
2079 | 2077 |
2080 // We should be able to deal with IO that is not aligned to the block size | 2078 // We should be able to deal with IO that is not aligned to the block size |
2081 // of a sparse entry, at least to write a big range without leaving holes. | 2079 // of a sparse entry, at least to write a big range without leaving holes. |
2082 const int kSize = 4 * 1024; | 2080 const int kSize = 4 * 1024; |
(...skipping 1316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3399 | 3397 |
3400 // Check that we are not leaking. | 3398 // Check that we are not leaking. |
3401 ASSERT_NE(entry, null); | 3399 ASSERT_NE(entry, null); |
3402 EXPECT_TRUE( | 3400 EXPECT_TRUE( |
3403 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); | 3401 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); |
3404 entry->Close(); | 3402 entry->Close(); |
3405 entry = NULL; | 3403 entry = NULL; |
3406 } | 3404 } |
3407 | 3405 |
3408 #endif // defined(OS_POSIX) | 3406 #endif // defined(OS_POSIX) |
OLD | NEW |