| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/disk_cache/disk_cache_test_util.h" | |
| 6 | |
| 7 #include "base/files/file.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/thread_task_runner_handle.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 #include "net/disk_cache/blockfile/backend_impl.h" | |
| 13 #include "net/disk_cache/blockfile/file.h" | |
| 14 #include "net/disk_cache/cache_util.h" | |
| 15 | |
| 16 using base::Time; | |
| 17 using base::TimeDelta; | |
| 18 | |
| 19 std::string GenerateKey(bool same_length) { | |
| 20 char key[200]; | |
| 21 CacheTestFillBuffer(key, sizeof(key), same_length); | |
| 22 | |
| 23 key[199] = '\0'; | |
| 24 return std::string(key); | |
| 25 } | |
| 26 | |
| 27 void CacheTestFillBuffer(char* buffer, size_t len, bool no_nulls) { | |
| 28 static bool called = false; | |
| 29 if (!called) { | |
| 30 called = true; | |
| 31 int seed = static_cast<int>(Time::Now().ToInternalValue()); | |
| 32 srand(seed); | |
| 33 } | |
| 34 | |
| 35 for (size_t i = 0; i < len; i++) { | |
| 36 buffer[i] = static_cast<char>(rand()); | |
| 37 if (!buffer[i] && no_nulls) | |
| 38 buffer[i] = 'g'; | |
| 39 } | |
| 40 if (len && !buffer[0]) | |
| 41 buffer[0] = 'g'; | |
| 42 } | |
| 43 | |
| 44 bool CreateCacheTestFile(const base::FilePath& name) { | |
| 45 int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ | | |
| 46 base::File::FLAG_WRITE; | |
| 47 | |
| 48 base::File file(name, flags); | |
| 49 if (!file.IsValid()) | |
| 50 return false; | |
| 51 | |
| 52 file.SetLength(4 * 1024 * 1024); | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 bool DeleteCache(const base::FilePath& path) { | |
| 57 disk_cache::DeleteCache(path, false); | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 bool CheckCacheIntegrity(const base::FilePath& path, bool new_eviction, | |
| 62 uint32 mask) { | |
| 63 scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( | |
| 64 path, mask, base::ThreadTaskRunnerHandle::Get(), NULL)); | |
| 65 if (!cache.get()) | |
| 66 return false; | |
| 67 if (new_eviction) | |
| 68 cache->SetNewEviction(); | |
| 69 cache->SetFlags(disk_cache::kNoRandom); | |
| 70 if (cache->SyncInit() != net::OK) | |
| 71 return false; | |
| 72 return cache->SelfCheck() >= 0; | |
| 73 } | |
| 74 | |
| 75 // ----------------------------------------------------------------------- | |
| 76 | |
| 77 MessageLoopHelper::MessageLoopHelper() | |
| 78 : num_callbacks_(0), | |
| 79 num_iterations_(0), | |
| 80 last_(0), | |
| 81 completed_(false), | |
| 82 callback_reused_error_(false), | |
| 83 callbacks_called_(0) { | |
| 84 } | |
| 85 | |
| 86 MessageLoopHelper::~MessageLoopHelper() { | |
| 87 } | |
| 88 | |
| 89 bool MessageLoopHelper::WaitUntilCacheIoFinished(int num_callbacks) { | |
| 90 if (num_callbacks == callbacks_called_) | |
| 91 return true; | |
| 92 | |
| 93 ExpectCallbacks(num_callbacks); | |
| 94 // Create a recurrent timer of 50 mS. | |
| 95 if (!timer_.IsRunning()) | |
| 96 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(50), this, | |
| 97 &MessageLoopHelper::TimerExpired); | |
| 98 base::MessageLoop::current()->Run(); | |
| 99 return completed_; | |
| 100 } | |
| 101 | |
| 102 // Quits the message loop when all callbacks are called or we've been waiting | |
| 103 // too long for them (2 secs without a callback). | |
| 104 void MessageLoopHelper::TimerExpired() { | |
| 105 CHECK_LE(callbacks_called_, num_callbacks_); | |
| 106 if (callbacks_called_ == num_callbacks_) { | |
| 107 completed_ = true; | |
| 108 base::MessageLoop::current()->Quit(); | |
| 109 } else { | |
| 110 // Not finished yet. See if we have to abort. | |
| 111 if (last_ == callbacks_called_) | |
| 112 num_iterations_++; | |
| 113 else | |
| 114 last_ = callbacks_called_; | |
| 115 if (40 == num_iterations_) | |
| 116 base::MessageLoop::current()->Quit(); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 // ----------------------------------------------------------------------- | |
| 121 | |
| 122 CallbackTest::CallbackTest(MessageLoopHelper* helper, | |
| 123 bool reuse) | |
| 124 : helper_(helper), | |
| 125 reuse_(reuse ? 0 : 1) { | |
| 126 } | |
| 127 | |
| 128 CallbackTest::~CallbackTest() { | |
| 129 } | |
| 130 | |
| 131 // On the actual callback, increase the number of tests received and check for | |
| 132 // errors (an unexpected test received) | |
| 133 void CallbackTest::Run(int result) { | |
| 134 last_result_ = result; | |
| 135 | |
| 136 if (reuse_) { | |
| 137 DCHECK_EQ(1, reuse_); | |
| 138 if (2 == reuse_) | |
| 139 helper_->set_callback_reused_error(true); | |
| 140 reuse_++; | |
| 141 } | |
| 142 | |
| 143 helper_->CallbackWasCalled(); | |
| 144 } | |
| OLD | NEW |