| 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 2847 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2858 ASSERT_TRUE(entry); | 2858 ASSERT_TRUE(entry); |
| 2859 const int kWriteSize = 512; | 2859 const int kWriteSize = 512; |
| 2860 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kWriteSize)); | 2860 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kWriteSize)); |
| 2861 EXPECT_TRUE(buffer1->HasOneRef()); | 2861 EXPECT_TRUE(buffer1->HasOneRef()); |
| 2862 CacheTestFillBuffer(buffer1->data(), kWriteSize, false); | 2862 CacheTestFillBuffer(buffer1->data(), kWriteSize, false); |
| 2863 | 2863 |
| 2864 // An optimistic write happens only when there is an empty queue of pending | 2864 // An optimistic write happens only when there is an empty queue of pending |
| 2865 // operations. To ensure the queue is empty, we issue a write and wait until | 2865 // operations. To ensure the queue is empty, we issue a write and wait until |
| 2866 // it completes. | 2866 // it completes. |
| 2867 EXPECT_EQ(kWriteSize, | 2867 EXPECT_EQ(kWriteSize, |
| 2868 WriteData(entry, 0, 0, buffer1, kWriteSize, false)); | 2868 WriteData(entry, 0, 0, buffer1.get(), kWriteSize, false)); |
| 2869 EXPECT_TRUE(buffer1->HasOneRef()); | 2869 EXPECT_TRUE(buffer1->HasOneRef()); |
| 2870 | 2870 |
| 2871 // Finally, we should perform an optimistic write and confirm that all | 2871 // Finally, we should perform an optimistic write and confirm that all |
| 2872 // references to the IO buffer have been released. | 2872 // references to the IO buffer have been released. |
| 2873 EXPECT_EQ(kWriteSize, entry->WriteData( | 2873 EXPECT_EQ( |
| 2874 1, 0, buffer1, kWriteSize, net::CompletionCallback(), false)); | 2874 kWriteSize, |
| 2875 entry->WriteData( |
| 2876 1, 0, buffer1.get(), kWriteSize, net::CompletionCallback(), false)); |
| 2875 EXPECT_TRUE(buffer1->HasOneRef()); | 2877 EXPECT_TRUE(buffer1->HasOneRef()); |
| 2876 entry->Close(); | 2878 entry->Close(); |
| 2877 } | 2879 } |
| 2878 | 2880 |
| 2879 TEST_F(DiskCacheEntryTest, DISABLED_SimpleCacheCreateDoomRace) { | 2881 TEST_F(DiskCacheEntryTest, DISABLED_SimpleCacheCreateDoomRace) { |
| 2880 // Test sequence: | 2882 // Test sequence: |
| 2881 // Create, Doom, Write, Close, Check files are not on disk anymore. | 2883 // Create, Doom, Write, Close, Check files are not on disk anymore. |
| 2882 SetSimpleCacheMode(); | 2884 SetSimpleCacheMode(); |
| 2883 InitCache(); | 2885 InitCache(); |
| 2884 disk_cache::Entry* null = NULL; | 2886 disk_cache::Entry* null = NULL; |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3211 | 3213 |
| 3212 const int kHalfSize = 200; | 3214 const int kHalfSize = 200; |
| 3213 const int kSize = 2 * kHalfSize; | 3215 const int kSize = 2 * kHalfSize; |
| 3214 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); | 3216 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); |
| 3215 CacheTestFillBuffer(buffer1->data(), kSize, false); | 3217 CacheTestFillBuffer(buffer1->data(), kSize, false); |
| 3216 disk_cache::Entry* entry = NULL; | 3218 disk_cache::Entry* entry = NULL; |
| 3217 | 3219 |
| 3218 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3220 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 3219 EXPECT_NE(null, entry); | 3221 EXPECT_NE(null, entry); |
| 3220 | 3222 |
| 3221 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1, kSize, false)); | 3223 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1.get(), kSize, false)); |
| 3222 entry->Close(); | 3224 entry->Close(); |
| 3223 | 3225 |
| 3224 disk_cache::Entry* entry2 = NULL; | 3226 disk_cache::Entry* entry2 = NULL; |
| 3225 ASSERT_EQ(net::OK, OpenEntry(key, &entry2)); | 3227 ASSERT_EQ(net::OK, OpenEntry(key, &entry2)); |
| 3226 EXPECT_EQ(entry, entry2); | 3228 EXPECT_EQ(entry, entry2); |
| 3227 | 3229 |
| 3228 // Read the first half of the data. | 3230 // Read the first half of the data. |
| 3229 int offset = 0; | 3231 int offset = 0; |
| 3230 int buf_len = kHalfSize; | 3232 int buf_len = kHalfSize; |
| 3231 scoped_refptr<net::IOBuffer> buffer1_read1(new net::IOBuffer(buf_len)); | 3233 scoped_refptr<net::IOBuffer> buffer1_read1(new net::IOBuffer(buf_len)); |
| 3232 EXPECT_EQ(buf_len, ReadData(entry2, 0, offset, buffer1_read1, buf_len)); | 3234 EXPECT_EQ(buf_len, ReadData(entry2, 0, offset, buffer1_read1.get(), buf_len)); |
| 3233 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), buf_len)); | 3235 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), buf_len)); |
| 3234 | 3236 |
| 3235 // Read the second half of the data. | 3237 // Read the second half of the data. |
| 3236 offset = buf_len; | 3238 offset = buf_len; |
| 3237 buf_len = kHalfSize; | 3239 buf_len = kHalfSize; |
| 3238 scoped_refptr<net::IOBuffer> buffer1_read2(new net::IOBuffer(buf_len)); | 3240 scoped_refptr<net::IOBuffer> buffer1_read2(new net::IOBuffer(buf_len)); |
| 3239 EXPECT_EQ(buf_len, ReadData(entry2, 0, offset, buffer1_read2, buf_len)); | 3241 EXPECT_EQ(buf_len, ReadData(entry2, 0, offset, buffer1_read2.get(), buf_len)); |
| 3240 char* buffer1_data = buffer1->data() + offset; | 3242 char* buffer1_data = buffer1->data() + offset; |
| 3241 EXPECT_EQ(0, memcmp(buffer1_data, buffer1_read2->data(), buf_len)); | 3243 EXPECT_EQ(0, memcmp(buffer1_data, buffer1_read2->data(), buf_len)); |
| 3242 | 3244 |
| 3243 // Check that we are not leaking. | 3245 // Check that we are not leaking. |
| 3244 EXPECT_NE(entry, null); | 3246 EXPECT_NE(entry, null); |
| 3245 EXPECT_TRUE( | 3247 EXPECT_TRUE( |
| 3246 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); | 3248 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); |
| 3247 entry->Close(); | 3249 entry->Close(); |
| 3248 entry = NULL; | 3250 entry = NULL; |
| 3249 } | 3251 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 3267 char* buffer1_data = buffer1->data() + kHalfSize; | 3269 char* buffer1_data = buffer1->data() + kHalfSize; |
| 3268 memcpy(buffer2->data(), buffer1_data, kHalfSize); | 3270 memcpy(buffer2->data(), buffer1_data, kHalfSize); |
| 3269 disk_cache::Entry* entry = NULL; | 3271 disk_cache::Entry* entry = NULL; |
| 3270 | 3272 |
| 3271 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3273 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 3272 EXPECT_NE(null, entry); | 3274 EXPECT_NE(null, entry); |
| 3273 | 3275 |
| 3274 int offset = kHalfSize; | 3276 int offset = kHalfSize; |
| 3275 int buf_len = kHalfSize; | 3277 int buf_len = kHalfSize; |
| 3276 | 3278 |
| 3277 EXPECT_EQ(buf_len, WriteData(entry, 0, offset, buffer2, buf_len, false)); | 3279 EXPECT_EQ(buf_len, |
| 3280 WriteData(entry, 0, offset, buffer2.get(), buf_len, false)); |
| 3278 offset = 0; | 3281 offset = 0; |
| 3279 buf_len = kHalfSize; | 3282 buf_len = kHalfSize; |
| 3280 EXPECT_EQ(buf_len, WriteData(entry, 0, offset, buffer1, buf_len, false)); | 3283 EXPECT_EQ(buf_len, |
| 3284 WriteData(entry, 0, offset, buffer1.get(), buf_len, false)); |
| 3281 entry->Close(); | 3285 entry->Close(); |
| 3282 | 3286 |
| 3283 disk_cache::Entry* entry2 = NULL; | 3287 disk_cache::Entry* entry2 = NULL; |
| 3284 ASSERT_EQ(net::OK, OpenEntry(key, &entry2)); | 3288 ASSERT_EQ(net::OK, OpenEntry(key, &entry2)); |
| 3285 EXPECT_EQ(entry, entry2); | 3289 EXPECT_EQ(entry, entry2); |
| 3286 | 3290 |
| 3287 scoped_refptr<net::IOBuffer> buffer1_read1(new net::IOBuffer(kSize)); | 3291 scoped_refptr<net::IOBuffer> buffer1_read1(new net::IOBuffer(kSize)); |
| 3288 EXPECT_EQ(kSize, ReadData(entry2, 0, 0, buffer1_read1, kSize)); | 3292 EXPECT_EQ(kSize, ReadData(entry2, 0, 0, buffer1_read1.get(), kSize)); |
| 3289 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), kSize)); | 3293 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), kSize)); |
| 3290 | 3294 |
| 3291 // Check that we are not leaking. | 3295 // Check that we are not leaking. |
| 3292 ASSERT_NE(entry, null); | 3296 ASSERT_NE(entry, null); |
| 3293 EXPECT_TRUE( | 3297 EXPECT_TRUE( |
| 3294 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); | 3298 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); |
| 3295 entry->Close(); | 3299 entry->Close(); |
| 3296 entry = NULL; | 3300 entry = NULL; |
| 3297 } | 3301 } |
| 3298 | 3302 |
| 3299 #endif // defined(OS_POSIX) | 3303 #endif // defined(OS_POSIX) |
| OLD | NEW |