| 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 2981 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2992 net::TestCompletionCallback cb; | 2992 net::TestCompletionCallback cb; |
| 2993 const int kSize1 = 10; | 2993 const int kSize1 = 10; |
| 2994 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); | 2994 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); |
| 2995 CacheTestFillBuffer(buffer1->data(), kSize1, false); | 2995 CacheTestFillBuffer(buffer1->data(), kSize1, false); |
| 2996 disk_cache::Entry* entry = NULL; | 2996 disk_cache::Entry* entry = NULL; |
| 2997 | 2997 |
| 2998 ASSERT_EQ(net::OK, | 2998 ASSERT_EQ(net::OK, |
| 2999 cache_->CreateEntry(key, &entry, net::CompletionCallback())); | 2999 cache_->CreateEntry(key, &entry, net::CompletionCallback())); |
| 3000 EXPECT_NE(null, entry); | 3000 EXPECT_NE(null, entry); |
| 3001 | 3001 |
| 3002 cache_->DoomEntry(key, cb.callback()); | 3002 EXPECT_EQ(net::ERR_IO_PENDING, cache_->DoomEntry(key, cb.callback())); |
| 3003 EXPECT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING)); | 3003 EXPECT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING)); |
| 3004 | 3004 |
| 3005 EXPECT_EQ( | 3005 EXPECT_EQ( |
| 3006 kSize1, | 3006 kSize1, |
| 3007 entry->WriteData(0, 0, buffer1.get(), kSize1, cb.callback(), false)); | 3007 entry->WriteData(0, 0, buffer1.get(), kSize1, cb.callback(), false)); |
| 3008 | 3008 |
| 3009 entry->Close(); | 3009 entry->Close(); |
| 3010 | 3010 |
| 3011 // Finish running the pending tasks so that we fully complete the close | 3011 // Finish running the pending tasks so that we fully complete the close |
| 3012 // operation and destroy the entry object. | 3012 // operation and destroy the entry object. |
| 3013 base::MessageLoop::current()->RunUntilIdle(); | 3013 base::MessageLoop::current()->RunUntilIdle(); |
| 3014 | 3014 |
| 3015 for (int i = 0; i < disk_cache::kSimpleEntryFileCount; ++i) { | 3015 for (int i = 0; i < disk_cache::kSimpleEntryFileCount; ++i) { |
| 3016 base::FilePath entry_file_path = cache_path_.AppendASCII( | 3016 base::FilePath entry_file_path = cache_path_.AppendASCII( |
| 3017 disk_cache::simple_util::GetFilenameFromKeyAndIndex(key, i)); | 3017 disk_cache::simple_util::GetFilenameFromKeyAndIndex(key, i)); |
| 3018 base::PlatformFileInfo info; | 3018 base::PlatformFileInfo info; |
| 3019 EXPECT_FALSE(file_util::GetFileInfo(entry_file_path, &info)); | 3019 EXPECT_FALSE(file_util::GetFileInfo(entry_file_path, &info)); |
| 3020 } | 3020 } |
| 3021 } | 3021 } |
| 3022 | 3022 |
| 3023 TEST_F(DiskCacheEntryTest, SimpleCacheDoomCreateRace) { |
| 3024 // This test runs as APP_CACHE to make operations more synchronous. Test |
| 3025 // sequence: |
| 3026 // Create, Doom, Create. |
| 3027 SetCacheType(net::APP_CACHE); |
| 3028 SetSimpleCacheMode(); |
| 3029 InitCache(); |
| 3030 disk_cache::Entry* null = NULL; |
| 3031 const char key[] = "the first key"; |
| 3032 |
| 3033 net::TestCompletionCallback create_callback; |
| 3034 |
| 3035 disk_cache::Entry* entry1 = NULL; |
| 3036 ASSERT_EQ(net::OK, |
| 3037 create_callback.GetResult( |
| 3038 cache_->CreateEntry(key, &entry1, create_callback.callback()))); |
| 3039 ScopedEntryPtr entry1_closer(entry1); |
| 3040 EXPECT_NE(null, entry1); |
| 3041 |
| 3042 net::TestCompletionCallback doom_callback; |
| 3043 EXPECT_EQ(net::ERR_IO_PENDING, |
| 3044 cache_->DoomEntry(key, doom_callback.callback())); |
| 3045 |
| 3046 disk_cache::Entry* entry2 = NULL; |
| 3047 ASSERT_EQ(net::OK, |
| 3048 create_callback.GetResult( |
| 3049 cache_->CreateEntry(key, &entry2, create_callback.callback()))); |
| 3050 ScopedEntryPtr entry2_closer(entry2); |
| 3051 EXPECT_EQ(net::OK, doom_callback.GetResult(net::ERR_IO_PENDING)); |
| 3052 } |
| 3053 |
| 3023 // Checks that an optimistic Create would fail later on a racing Open. | 3054 // Checks that an optimistic Create would fail later on a racing Open. |
| 3024 TEST_F(DiskCacheEntryTest, SimpleCacheOptimisticCreateFailsOnOpen) { | 3055 TEST_F(DiskCacheEntryTest, SimpleCacheOptimisticCreateFailsOnOpen) { |
| 3025 SetSimpleCacheMode(); | 3056 SetSimpleCacheMode(); |
| 3026 InitCache(); | 3057 InitCache(); |
| 3027 | 3058 |
| 3028 // Create a corrupt file in place of a future entry. Optimistic create should | 3059 // Create a corrupt file in place of a future entry. Optimistic create should |
| 3029 // initially succeed, but realize later that creation failed. | 3060 // initially succeed, but realize later that creation failed. |
| 3030 const std::string key = "the key"; | 3061 const std::string key = "the key"; |
| 3031 net::TestCompletionCallback cb; | 3062 net::TestCompletionCallback cb; |
| 3032 disk_cache::Entry* entry = NULL; | 3063 disk_cache::Entry* entry = NULL; |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3384 | 3415 |
| 3385 // Check that we are not leaking. | 3416 // Check that we are not leaking. |
| 3386 ASSERT_NE(entry, null); | 3417 ASSERT_NE(entry, null); |
| 3387 EXPECT_TRUE( | 3418 EXPECT_TRUE( |
| 3388 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); | 3419 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); |
| 3389 entry->Close(); | 3420 entry->Close(); |
| 3390 entry = NULL; | 3421 entry = NULL; |
| 3391 } | 3422 } |
| 3392 | 3423 |
| 3393 #endif // defined(OS_POSIX) | 3424 #endif // defined(OS_POSIX) |
| OLD | NEW |