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/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 BackendShutdownWithPendingIO(bool wait); | |
| 39 void BackendShutdownWithPendingIO2(bool wait); | |
| 40 void BackendShutdownWithPendingIO3(bool wait); | |
| 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::BackendShutdownWithPendingIO(bool wait) { |
|
gavinp
2012/03/18 00:35:26
A single bool argument at a call site can be confu
gavinp
2012/03/18 00:35:26
How about calling this BackendShutdownWithPendingC
| |
| 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 = wait ? disk_cache::kNoRandom : disk_cache::kNone; | |
|
gavinp
2012/03/18 00:35:26
uint32 flags = disk_cache::kNoBuffering;
if (pendi
| |
| 292 flags |= disk_cache::kNoBuffering; | |
| 288 int rv = disk_cache::BackendImpl::CreateBackend( | 293 int rv = disk_cache::BackendImpl::CreateBackend( |
| 289 cache_path_, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, | 294 cache_path_, false, 0, net::DISK_CACHE, flags, |
| 290 base::MessageLoopProxy::current(), NULL, | 295 base::MessageLoopProxy::current(), NULL, |
| 291 &cache, cb.callback()); | 296 &cache, cb.callback()); |
| 292 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 297 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 293 | 298 |
| 294 disk_cache::EntryImpl* entry; | 299 disk_cache::EntryImpl* entry; |
| 295 rv = cache->CreateEntry( | 300 rv = cache->CreateEntry( |
| 296 "some key", reinterpret_cast<disk_cache::Entry**>(&entry), | 301 "some key", reinterpret_cast<disk_cache::Entry**>(&entry), |
| 297 cb.callback()); | 302 cb.callback()); |
| 298 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 303 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 299 | 304 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 312 } | 317 } |
| 313 | 318 |
| 314 // Don't call Close() to avoid going through the queue or we'll deadlock | 319 // Don't call Close() to avoid going through the queue or we'll deadlock |
| 315 // waiting for the operation to finish. | 320 // waiting for the operation to finish. |
| 316 entry->Release(); | 321 entry->Release(); |
| 317 | 322 |
| 318 // The cache destructor will see one pending operation here. | 323 // The cache destructor will see one pending operation here. |
| 319 delete cache; | 324 delete cache; |
| 320 | 325 |
| 321 if (rv == net::ERR_IO_PENDING) { | 326 if (rv == net::ERR_IO_PENDING) { |
| 322 EXPECT_TRUE(cb.have_result()); | 327 if (wait) { |
|
gavinp
2012/03/18 00:35:26
Take it or leave it nit: I favour no braces in thi
| |
| 328 EXPECT_TRUE(cb.have_result()); | |
| 329 } else { | |
| 330 EXPECT_FALSE(cb.have_result()); | |
| 331 } | |
| 323 } | 332 } |
| 324 } | 333 } |
| 325 | 334 |
| 326 MessageLoop::current()->RunAllPending(); | 335 MessageLoop::current()->RunAllPending(); |
| 327 } | 336 } |
| 328 | 337 |
| 338 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO) { | |
| 339 // Do full synchronization at destruction. | |
| 340 BackendShutdownWithPendingIO(true); | |
| 341 } | |
| 342 | |
| 343 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO_Fast) { | |
| 344 // The integrity test sets kNoRandom so there's a version mismatch if we don't | |
| 345 // force new eviction. | |
| 346 SetNewEviction(); | |
| 347 // Do minimal synchronization at destruction. | |
| 348 BackendShutdownWithPendingIO(false); | |
| 349 } | |
| 350 | |
| 329 // Tests that we deal with background-thread pending operations. | 351 // Tests that we deal with background-thread pending operations. |
| 330 TEST_F(DiskCacheTest, ShutdownWithPendingIO2) { | 352 void DiskCacheBackendTest::BackendShutdownWithPendingIO2(bool wait) { |
|
gavinp
2012/03/18 00:35:26
How about BackendShutdownWithPendingWrites() ???
| |
| 331 net::TestCompletionCallback cb; | 353 net::TestCompletionCallback cb; |
| 332 | 354 |
| 333 { | 355 { |
| 334 ASSERT_TRUE(CleanupCacheDir()); | 356 ASSERT_TRUE(CleanupCacheDir()); |
| 335 base::Thread cache_thread("CacheThread"); | 357 base::Thread cache_thread("CacheThread"); |
| 336 ASSERT_TRUE(cache_thread.StartWithOptions( | 358 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 337 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 359 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 338 | 360 |
| 339 disk_cache::Backend* cache; | 361 disk_cache::Backend* cache; |
| 362 uint32 flags = wait ? disk_cache::kNoRandom : disk_cache::kNone; | |
| 363 flags |= disk_cache::kNoBuffering; | |
| 340 int rv = disk_cache::BackendImpl::CreateBackend( | 364 int rv = disk_cache::BackendImpl::CreateBackend( |
| 341 cache_path_, false, 0, net::DISK_CACHE, disk_cache::kNoRandom, | 365 cache_path_, false, 0, net::DISK_CACHE, flags, |
| 342 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); | 366 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); |
| 343 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 367 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 344 | 368 |
| 345 disk_cache::Entry* entry; | 369 disk_cache::Entry* entry; |
| 346 rv = cache->CreateEntry("some key", &entry, cb.callback()); | 370 rv = cache->CreateEntry("some key", &entry, cb.callback()); |
| 347 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 371 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 348 | 372 |
| 349 const int kSize = 25000; | 373 const int kSize = 25000; |
| 350 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 374 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
| 351 CacheTestFillBuffer(buffer->data(), kSize, false); | 375 CacheTestFillBuffer(buffer->data(), kSize, false); |
| 352 | 376 |
| 353 rv = entry->WriteData(0, 0, buffer, kSize, cb.callback(), false); | 377 rv = entry->WriteData(0, 0, buffer, kSize, cb.callback(), false); |
| 354 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 378 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 355 | 379 |
| 356 entry->Close(); | 380 entry->Close(); |
| 357 | 381 |
| 358 // The cache destructor will see two pending operations here. | 382 // The cache destructor will see two pending operations here. |
| 359 delete cache; | 383 delete cache; |
| 360 } | 384 } |
| 361 | 385 |
| 362 MessageLoop::current()->RunAllPending(); | 386 MessageLoop::current()->RunAllPending(); |
| 363 } | 387 } |
| 364 | 388 |
| 389 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO2) { | |
| 390 // Do full synchronization at destruction. | |
| 391 BackendShutdownWithPendingIO2(true); | |
| 392 } | |
| 393 | |
| 394 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO2_Fast) { | |
| 395 // The integrity test sets kNoRandom so there's a version mismatch if we don't | |
| 396 // force new eviction. | |
| 397 SetNewEviction(); | |
| 398 // Do minimal synchronization at destruction. | |
| 399 BackendShutdownWithPendingIO2(false); | |
| 400 } | |
| 401 | |
| 402 // Tests that we deal with create-type pending operations. | |
| 403 void DiskCacheBackendTest::BackendShutdownWithPendingIO3(bool wait) { | |
|
gavinp
2012/03/18 00:35:26
BackendShutdownWithPendingBufferedCreates ?
| |
| 404 net::TestCompletionCallback cb; | |
| 405 | |
| 406 { | |
| 407 ASSERT_TRUE(CleanupCacheDir()); | |
| 408 base::Thread cache_thread("CacheThread"); | |
| 409 ASSERT_TRUE(cache_thread.StartWithOptions( | |
| 410 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | |
| 411 | |
| 412 disk_cache::Backend* cache; | |
| 413 disk_cache::BackendFlags flags = | |
| 414 wait ? disk_cache::kNoRandom : disk_cache::kNone; | |
| 415 int rv = disk_cache::BackendImpl::CreateBackend( | |
| 416 cache_path_, false, 0, net::DISK_CACHE, flags, | |
| 417 cache_thread.message_loop_proxy(), NULL, &cache, cb.callback()); | |
| 418 ASSERT_EQ(net::OK, cb.GetResult(rv)); | |
| 419 | |
| 420 disk_cache::Entry* entry; | |
| 421 rv = cache->CreateEntry("some key", &entry, cb.callback()); | |
| 422 ASSERT_EQ(net::ERR_IO_PENDING, rv); | |
| 423 | |
| 424 delete cache; | |
| 425 EXPECT_FALSE(cb.have_result()); | |
| 426 } | |
| 427 | |
| 428 MessageLoop::current()->RunAllPending(); | |
| 429 } | |
| 430 | |
| 431 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO3) { | |
| 432 // Do full synchronization at destruction. | |
| 433 BackendShutdownWithPendingIO3(true); | |
| 434 } | |
| 435 | |
| 436 // We'll be leaking an entry from this test. | |
| 437 TEST_F(DiskCacheBackendTest, ShutdownWithPendingIO3_Fast) { | |
| 438 // The integrity test sets kNoRandom so there's a version mismatch if we don't | |
| 439 // force new eviction. | |
| 440 SetNewEviction(); | |
| 441 // Do minimal synchronization at destruction. | |
| 442 BackendShutdownWithPendingIO3(false); | |
| 443 } | |
| 444 | |
| 365 TEST_F(DiskCacheTest, TruncatedIndex) { | 445 TEST_F(DiskCacheTest, TruncatedIndex) { |
| 366 ASSERT_TRUE(CleanupCacheDir()); | 446 ASSERT_TRUE(CleanupCacheDir()); |
| 367 FilePath index = cache_path_.AppendASCII("index"); | 447 FilePath index = cache_path_.AppendASCII("index"); |
| 368 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); | 448 ASSERT_EQ(5, file_util::WriteFile(index, "hello", 5)); |
| 369 | 449 |
| 370 base::Thread cache_thread("CacheThread"); | 450 base::Thread cache_thread("CacheThread"); |
| 371 ASSERT_TRUE(cache_thread.StartWithOptions( | 451 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 372 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 452 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 373 net::TestCompletionCallback cb; | 453 net::TestCompletionCallback cb; |
| 374 | 454 |
| (...skipping 2115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2490 // Ping the oldest entry. | 2570 // Ping the oldest entry. |
| 2491 cache_->OnExternalCacheHit("key0"); | 2571 cache_->OnExternalCacheHit("key0"); |
| 2492 | 2572 |
| 2493 TrimForTest(false); | 2573 TrimForTest(false); |
| 2494 | 2574 |
| 2495 // Make sure the older key remains. | 2575 // Make sure the older key remains. |
| 2496 EXPECT_EQ(1, cache_->GetEntryCount()); | 2576 EXPECT_EQ(1, cache_->GetEntryCount()); |
| 2497 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); | 2577 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); |
| 2498 entry->Close(); | 2578 entry->Close(); |
| 2499 } | 2579 } |
| OLD | NEW |