| 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/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 using base::Time; | 29 using base::Time; |
| 30 | 30 |
| 31 static const int kDiskDelayMs = 20; | 31 static const int kDiskDelayMs = 20; |
| 32 | 32 |
| 33 // Tests that can run with different types of caches. | 33 // Tests that can run with different types of caches. |
| 34 class DiskCacheBackendTest : public DiskCacheTestWithCache { | 34 class DiskCacheBackendTest : public DiskCacheTestWithCache { |
| 35 protected: | 35 protected: |
| 36 void BackendBasics(); | 36 void BackendBasics(); |
| 37 void BackendKeying(); | 37 void BackendKeying(); |
| 38 void BackendShutdownWithPendingFileIO(bool fast); |
| 39 void BackendShutdownWithPendingIO(bool fast); |
| 40 void BackendShutdownWithPendingCreate(bool fast); |
| 38 void BackendSetSize(); | 41 void BackendSetSize(); |
| 39 void BackendLoad(); | 42 void BackendLoad(); |
| 40 void BackendChain(); | 43 void BackendChain(); |
| 41 void BackendValidEntry(); | 44 void BackendValidEntry(); |
| 42 void BackendInvalidEntry(); | 45 void BackendInvalidEntry(); |
| 43 void BackendInvalidEntryRead(); | 46 void BackendInvalidEntryRead(); |
| 44 void BackendInvalidEntryWithLoad(); | 47 void BackendInvalidEntryWithLoad(); |
| 45 void BackendTrimInvalidEntry(); | 48 void BackendTrimInvalidEntry(); |
| 46 void BackendTrimInvalidEntry2(); | 49 void BackendTrimInvalidEntry2(); |
| 47 void BackendEnumerations(); | 50 void BackendEnumerations(); |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 ASSERT_EQ(0, WriteData(entry, 0, 20000, buffer1, 0, false)); | 271 ASSERT_EQ(0, WriteData(entry, 0, 20000, buffer1, 0, false)); |
| 269 entry->Close(); | 272 entry->Close(); |
| 270 | 273 |
| 271 // And verify that the first file is still there. | 274 // And verify that the first file is still there. |
| 272 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); | 275 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); |
| 273 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize)); | 276 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize)); |
| 274 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize)); | 277 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize)); |
| 275 } | 278 } |
| 276 | 279 |
| 277 // Tests that we deal with file-level pending operations at destruction time. | 280 // Tests that we deal with file-level pending operations at destruction time. |
| 278 TEST_F(DiskCacheTest, ShutdownWithPendingIO) { | 281 void DiskCacheBackendTest::BackendShutdownWithPendingFileIO(bool fast) { |
| 279 net::TestCompletionCallback cb; | 282 net::TestCompletionCallback cb; |
| 280 | 283 |
| 281 { | 284 { |
| 282 ASSERT_TRUE(CleanupCacheDir()); | 285 ASSERT_TRUE(CleanupCacheDir()); |
| 283 base::Thread cache_thread("CacheThread"); | 286 base::Thread cache_thread("CacheThread"); |
| 284 ASSERT_TRUE(cache_thread.StartWithOptions( | 287 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 285 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 288 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 286 | 289 |
| 287 disk_cache::Backend* cache; | 290 disk_cache::Backend* cache; |
| 291 uint32 flags = disk_cache::kNoBuffering; |
| 292 if (!fast) |
| 293 flags |= disk_cache::kNoRandom; |
| 288 int rv = disk_cache::BackendImpl::CreateBackend( | 294 int rv = disk_cache::BackendImpl::CreateBackend( |
| 289 cache_path_, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, | 295 cache_path_, false, 0, net::DISK_CACHE, flags, |
| 290 base::MessageLoopProxy::current(), NULL, | 296 base::MessageLoopProxy::current(), NULL, |
| 291 &cache, cb.callback()); | 297 &cache, cb.callback()); |
| 292 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 298 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 293 | 299 |
| 294 disk_cache::EntryImpl* entry; | 300 disk_cache::EntryImpl* entry; |
| 295 rv = cache->CreateEntry( | 301 rv = cache->CreateEntry( |
| 296 "some key", reinterpret_cast<disk_cache::Entry**>(&entry), | 302 "some key", reinterpret_cast<disk_cache::Entry**>(&entry), |
| 297 cb.callback()); | 303 cb.callback()); |
| 298 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 304 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 299 | 305 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 312 } | 318 } |
| 313 | 319 |
| 314 // Don't call Close() to avoid going through the queue or we'll deadlock | 320 // Don't call Close() to avoid going through the queue or we'll deadlock |
| 315 // waiting for the operation to finish. | 321 // waiting for the operation to finish. |
| 316 entry->Release(); | 322 entry->Release(); |
| 317 | 323 |
| 318 // The cache destructor will see one pending operation here. | 324 // The cache destructor will see one pending operation here. |
| 319 delete cache; | 325 delete cache; |
| 320 | 326 |
| 321 if (rv == net::ERR_IO_PENDING) { | 327 if (rv == net::ERR_IO_PENDING) { |
| 322 EXPECT_TRUE(cb.have_result()); | 328 if (fast) |
| 329 EXPECT_FALSE(cb.have_result()); |
| 330 else |
| 331 EXPECT_TRUE(cb.have_result()); |
| 323 } | 332 } |
| 324 } | 333 } |
| 325 | 334 |
| 326 MessageLoop::current()->RunAllPending(); | 335 MessageLoop::current()->RunAllPending(); |
| 327 } | 336 } |
| 328 | 337 |
| 338 TEST_F(DiskCacheBackendTest, ShutdownWithPendingFileIO) { |
| 339 BackendShutdownWithPendingFileIO(false); |
| 340 } |
| 341 |
| 342 TEST_F(DiskCacheBackendTest, ShutdownWithPendingFileIO_Fast) { |
| 343 // The integrity test sets kNoRandom so there's a version mismatch if we don't |
| 344 // force new eviction. |
| 345 SetNewEviction(); |
| 346 BackendShutdownWithPendingFileIO(true); |
| 347 } |
| 348 |
| 329 // Tests that we deal with background-thread pending operations. | 349 // Tests that we deal with background-thread pending operations. |
| 330 TEST_F(DiskCacheTest, ShutdownWithPendingIO2) { | 350 void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) { |
| 331 net::TestCompletionCallback cb; | 351 net::TestCompletionCallback cb; |
| 332 | 352 |
| 333 { | 353 { |
| 334 ASSERT_TRUE(CleanupCacheDir()); | 354 ASSERT_TRUE(CleanupCacheDir()); |
| 335 base::Thread cache_thread("CacheThread"); | 355 base::Thread cache_thread("CacheThread"); |
| 336 ASSERT_TRUE(cache_thread.StartWithOptions( | 356 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 337 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 357 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 338 | 358 |
| 339 disk_cache::Backend* cache; | 359 disk_cache::Backend* cache; |
| 360 uint32 flags = disk_cache::kNoBuffering; |
| 361 if (!fast) |
| 362 flags |= disk_cache::kNoRandom; |
| 340 int rv = disk_cache::BackendImpl::CreateBackend( | 363 int rv = disk_cache::BackendImpl::CreateBackend( |
| 341 cache_path_, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, | 364 cache_path_, false, 0, net::DISK_CACHE, flags, |
| 342 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); | 365 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); |
| 343 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 366 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 344 | 367 |
| 345 disk_cache::Entry* entry; | 368 disk_cache::Entry* entry; |
| 346 rv = cache->CreateEntry("some key", &entry, cb.callback()); | 369 rv = cache->CreateEntry("some key", &entry, cb.callback()); |
| 347 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 370 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 348 | 371 |
| 349 const int kSize = 25000; | 372 const int kSize = 25000; |
| 350 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 373 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
| 351 CacheTestFillBuffer(buffer->data(), kSize, false); | 374 CacheTestFillBuffer(buffer->data(), kSize, false); |
| 352 | 375 |
| 353 rv = entry->WriteData(0, 0, buffer, kSize, cb.callback(), false); | 376 rv = entry->WriteData(0, 0, buffer, kSize, cb.callback(), false); |
| 354 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 377 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 355 | 378 |
| 356 entry->Close(); | 379 entry->Close(); |
| 357 | 380 |
| 358 // The cache destructor will see two pending operations here. | 381 // The cache destructor will see two pending operations here. |
| 359 delete cache; | 382 delete cache; |
| 360 } | 383 } |
| 361 | 384 |
| 362 MessageLoop::current()->RunAllPending(); | 385 MessageLoop::current()->RunAllPending(); |
| 363 } | 386 } |
| 364 | 387 |
| 388 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO) { |
| 389 BackendShutdownWithPendingIO(false); |
| 390 } |
| 391 |
| 392 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO_Fast) { |
| 393 // The integrity test sets kNoRandom so there's a version mismatch if we don't |
| 394 // force new eviction. |
| 395 SetNewEviction(); |
| 396 BackendShutdownWithPendingIO(true); |
| 397 } |
| 398 |
| 399 // Tests that we deal with create-type pending operations. |
| 400 void DiskCacheBackendTest::BackendShutdownWithPendingCreate(bool fast) { |
| 401 net::TestCompletionCallback cb; |
| 402 |
| 403 { |
| 404 ASSERT_TRUE(CleanupCacheDir()); |
| 405 base::Thread cache_thread("CacheThread"); |
| 406 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 407 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 408 |
| 409 disk_cache::Backend* cache; |
| 410 disk_cache::BackendFlags flags = |
| 411 fast ? disk_cache::kNone : disk_cache::kNoRandom; |
| 412 int rv = disk_cache::BackendImpl::CreateBackend( |
| 413 cache_path_, false, 0, net::DISK_CACHE, flags, |
| 414 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); |
| 415 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 416 |
| 417 disk_cache::Entry* entry; |
| 418 rv = cache->CreateEntry("some key", &entry, cb.callback()); |
| 419 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 420 |
| 421 delete cache; |
| 422 EXPECT_FALSE(cb.have_result()); |
| 423 } |
| 424 |
| 425 MessageLoop::current()->RunAllPending(); |
| 426 } |
| 427 |
| 428 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate) { |
| 429 BackendShutdownWithPendingCreate(false); |
| 430 } |
| 431 |
| 432 // We'll be leaking an entry from this test. |
| 433 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate_Fast) { |
| 434 // The integrity test sets kNoRandom so there's a version mismatch if we don't |
| 435 // force new eviction. |
| 436 SetNewEviction(); |
| 437 BackendShutdownWithPendingCreate(true); |
| 438 } |
| 439 |
| 365 TEST_F(DiskCacheTest, TruncatedIndex) { | 440 TEST_F(DiskCacheTest, TruncatedIndex) { |
| 366 ASSERT_TRUE(CleanupCacheDir()); | 441 ASSERT_TRUE(CleanupCacheDir()); |
| 367 FilePath index = cache_path_.AppendASCII("index"); | 442 FilePath index = cache_path_.AppendASCII("index"); |
| 368 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); | 443 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); |
| 369 | 444 |
| 370 base::Thread cache_thread("CacheThread"); | 445 base::Thread cache_thread("CacheThread"); |
| 371 ASSERT_TRUE(cache_thread.StartWithOptions( | 446 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 372 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 447 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 373 net::TestCompletionCallback cb; | 448 net::TestCompletionCallback cb; |
| 374 | 449 |
| (...skipping 2115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2490 // Ping the oldest entry. | 2565 // Ping the oldest entry. |
| 2491 cache_->OnExternalCacheHit("key0"); | 2566 cache_->OnExternalCacheHit("key0"); |
| 2492 | 2567 |
| 2493 TrimForTest(false); | 2568 TrimForTest(false); |
| 2494 | 2569 |
| 2495 // Make sure the older key remains. | 2570 // Make sure the older key remains. |
| 2496 EXPECT_EQ(1, cache_->GetEntryCount()); | 2571 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2497 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); | 2572 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); |
| 2498 entry->Close(); | 2573 entry->Close(); |
| 2499 } | 2574 } |
| OLD | NEW |