Chromium Code Reviews| 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) |
|
rvargas (doing something else)
2013/07/26 21:05:01
nit: this is kind of ugly :(. The argument suggest
qsr
2013/07/29 08:26:28
Given that this is a pointer to the scoped_ptr, an
| |
| 2029 : cache_(cache) { | 2029 : cache_(cache) { |
| 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 SparseTestCompletionCallback cb(&cache_); |
| 2067 int rv = cache_->DoomEntry(key, cb.callback()); | 2067 int rv = cache_->DoomEntry(key, cb.callback()); |
| 2068 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 2068 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 2069 EXPECT_EQ(net::OK, cb.WaitForResult()); | 2069 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 2070 | |
| 2071 // TearDown will attempt to delete the cache_. | |
| 2072 cache_ = NULL; | |
| 2073 } | 2070 } |
| 2074 | 2071 |
| 2075 void DiskCacheEntryTest::PartialSparseEntry() { | 2072 void DiskCacheEntryTest::PartialSparseEntry() { |
| 2076 std::string key("the first key"); | 2073 std::string key("the first key"); |
| 2077 disk_cache::Entry* entry; | 2074 disk_cache::Entry* entry; |
| 2078 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 2075 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 2079 | 2076 |
| 2080 // We should be able to deal with IO that is not aligned to the block size | 2077 // 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. | 2078 // of a sparse entry, at least to write a big range without leaving holes. |
| 2082 const int kSize = 4 * 1024; | 2079 const int kSize = 4 * 1024; |
| (...skipping 1316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3399 | 3396 |
| 3400 // Check that we are not leaking. | 3397 // Check that we are not leaking. |
| 3401 ASSERT_NE(entry, null); | 3398 ASSERT_NE(entry, null); |
| 3402 EXPECT_TRUE( | 3399 EXPECT_TRUE( |
| 3403 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); | 3400 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); |
| 3404 entry->Close(); | 3401 entry->Close(); |
| 3405 entry = NULL; | 3402 entry = NULL; |
| 3406 } | 3403 } |
| 3407 | 3404 |
| 3408 #endif // defined(OS_POSIX) | 3405 #endif // defined(OS_POSIX) |
| OLD | NEW |