OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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 "net/disk_cache/disk_cache_test_base.h" | 5 #include "net/disk_cache/disk_cache_test_base.h" |
6 | 6 |
| 7 #include "net/base/net_errors.h" |
7 #include "net/base/test_completion_callback.h" | 8 #include "net/base/test_completion_callback.h" |
8 #include "net/disk_cache/backend_impl.h" | 9 #include "net/disk_cache/backend_impl.h" |
9 #include "net/disk_cache/disk_cache_test_util.h" | 10 #include "net/disk_cache/disk_cache_test_util.h" |
10 #include "net/disk_cache/mem_backend_impl.h" | 11 #include "net/disk_cache/mem_backend_impl.h" |
11 | 12 |
12 void DiskCacheTest::TearDown() { | 13 void DiskCacheTest::TearDown() { |
13 MessageLoop::current()->RunAllPending(); | 14 MessageLoop::current()->RunAllPending(); |
14 } | 15 } |
15 | 16 |
16 void DiskCacheTestWithCache::SetMaxSize(int size) { | 17 void DiskCacheTestWithCache::SetMaxSize(int size) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 60 |
60 if (!cache_thread_.IsRunning()) { | 61 if (!cache_thread_.IsRunning()) { |
61 EXPECT_TRUE(cache_thread_.StartWithOptions( | 62 EXPECT_TRUE(cache_thread_.StartWithOptions( |
62 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 63 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
63 } | 64 } |
64 ASSERT_TRUE(cache_thread_.message_loop() != NULL); | 65 ASSERT_TRUE(cache_thread_.message_loop() != NULL); |
65 | 66 |
66 if (implementation_) | 67 if (implementation_) |
67 return InitDiskCacheImpl(path); | 68 return InitDiskCacheImpl(path); |
68 | 69 |
| 70 scoped_refptr<base::MessageLoopProxy> thread = |
| 71 use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() : |
| 72 cache_thread_.message_loop_proxy(); |
| 73 |
69 TestCompletionCallback cb; | 74 TestCompletionCallback cb; |
70 int rv = disk_cache::BackendImpl::CreateBackend( | 75 int rv = disk_cache::BackendImpl::CreateBackend( |
71 path, force_creation_, size_, net::DISK_CACHE, | 76 path, force_creation_, size_, net::DISK_CACHE, |
72 disk_cache::kNoRandom, cache_thread_.message_loop_proxy(), | 77 disk_cache::kNoRandom, thread, &cache_, &cb); |
73 &cache_, &cb); | |
74 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 78 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
75 } | 79 } |
76 | 80 |
77 void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath& path) { | 81 void DiskCacheTestWithCache::InitDiskCacheImpl(const FilePath& path) { |
| 82 scoped_refptr<base::MessageLoopProxy> thread = |
| 83 use_current_thread_ ? base::MessageLoopProxy::CreateForCurrentThread() : |
| 84 cache_thread_.message_loop_proxy(); |
78 if (mask_) | 85 if (mask_) |
79 cache_impl_ = new disk_cache::BackendImpl( | 86 cache_impl_ = new disk_cache::BackendImpl(path, mask_, thread); |
80 path, mask_, cache_thread_.message_loop_proxy()); | |
81 else | 87 else |
82 cache_impl_ = new disk_cache::BackendImpl( | 88 cache_impl_ = new disk_cache::BackendImpl(path, thread); |
83 path, cache_thread_.message_loop_proxy()); | |
84 | 89 |
85 cache_ = cache_impl_; | 90 cache_ = cache_impl_; |
86 ASSERT_TRUE(NULL != cache_); | 91 ASSERT_TRUE(NULL != cache_); |
87 | 92 |
88 if (size_) | 93 if (size_) |
89 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); | 94 EXPECT_TRUE(cache_impl_->SetMaxSize(size_)); |
90 | 95 |
91 if (new_eviction_) | 96 if (new_eviction_) |
92 cache_impl_->SetNewEviction(); | 97 cache_impl_->SetNewEviction(); |
93 | 98 |
94 cache_impl_->SetFlags(disk_cache::kNoRandom); | 99 cache_impl_->SetFlags(disk_cache::kNoRandom); |
95 ASSERT_TRUE(cache_impl_->Init()); | 100 TestCompletionCallback cb; |
| 101 int rv = cache_impl_->Init(&cb); |
| 102 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
96 } | 103 } |
97 | 104 |
98 void DiskCacheTestWithCache::TearDown() { | 105 void DiskCacheTestWithCache::TearDown() { |
99 MessageLoop::current()->RunAllPending(); | 106 MessageLoop::current()->RunAllPending(); |
100 delete cache_; | 107 delete cache_; |
101 if (cache_thread_.IsRunning()) | 108 if (cache_thread_.IsRunning()) |
102 cache_thread_.Stop(); | 109 cache_thread_.Stop(); |
103 | 110 |
104 if (!memory_only_ && integrity_) { | 111 if (!memory_only_ && integrity_) { |
105 FilePath path = GetCacheFilePath(); | 112 FilePath path = GetCacheFilePath(); |
106 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); | 113 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); |
107 } | 114 } |
108 | 115 |
109 PlatformTest::TearDown(); | 116 PlatformTest::TearDown(); |
110 } | 117 } |
111 | 118 |
112 // We are expected to leak memory when simulating crashes. | 119 // We are expected to leak memory when simulating crashes. |
113 void DiskCacheTestWithCache::SimulateCrash() { | 120 void DiskCacheTestWithCache::SimulateCrash() { |
114 ASSERT_TRUE(implementation_ && !memory_only_); | 121 ASSERT_TRUE(implementation_ && !memory_only_); |
| 122 TestCompletionCallback cb; |
| 123 int rv = cache_impl_->FlushQueueForTest(&cb); |
| 124 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
115 cache_impl_->ClearRefCountForTest(); | 125 cache_impl_->ClearRefCountForTest(); |
116 | 126 |
117 delete cache_impl_; | 127 delete cache_impl_; |
118 FilePath path = GetCacheFilePath(); | 128 FilePath path = GetCacheFilePath(); |
119 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); | 129 EXPECT_TRUE(CheckCacheIntegrity(path, new_eviction_)); |
120 | 130 |
121 InitDiskCacheImpl(path); | 131 InitDiskCacheImpl(path); |
122 } | 132 } |
123 | 133 |
124 void DiskCacheTestWithCache::SetTestMode() { | 134 void DiskCacheTestWithCache::SetTestMode() { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 int rv = cache_->DoomEntriesSince(initial_time, &cb); | 174 int rv = cache_->DoomEntriesSince(initial_time, &cb); |
165 return cb.GetResult(rv); | 175 return cb.GetResult(rv); |
166 } | 176 } |
167 | 177 |
168 int DiskCacheTestWithCache::OpenNextEntry(void** iter, | 178 int DiskCacheTestWithCache::OpenNextEntry(void** iter, |
169 disk_cache::Entry** next_entry) { | 179 disk_cache::Entry** next_entry) { |
170 TestCompletionCallback cb; | 180 TestCompletionCallback cb; |
171 int rv = cache_->OpenNextEntry(iter, next_entry, &cb); | 181 int rv = cache_->OpenNextEntry(iter, next_entry, &cb); |
172 return cb.GetResult(rv); | 182 return cb.GetResult(rv); |
173 } | 183 } |
| 184 |
| 185 void DiskCacheTestWithCache::FlushQueueForTest() { |
| 186 if (memory_only_ || !cache_impl_) |
| 187 return; |
| 188 |
| 189 TestCompletionCallback cb; |
| 190 int rv = cache_impl_->FlushQueueForTest(&cb); |
| 191 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 192 } |
OLD | NEW |