Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: webkit/browser/appcache/appcache_disk_cache_unittest.cc

Issue 137493003: Appcache::OnCorruptionDetected handling (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014 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 "base/bind.h"
6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "base/run_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "webkit/browser/appcache/appcache_disk_cache.h"
14
15 namespace appcache {
16
17 class AppCacheDiskCacheTest : public testing::Test {
18 public:
19 AppCacheDiskCacheTest() {
20 }
jsbell 2014/01/24 21:13:44 Nit: put on previous line
michaeln 2014/01/28 22:12:16 Done.
21
22 virtual void SetUp() OVERRIDE {
23 // Use the current thread for the DiskCache's cache_thread.
24 message_loop_.reset(new base::MessageLoopForIO());
25 cache_thread_ = base::MessageLoopProxy::current();
26 ASSERT_TRUE(directory_.CreateUniqueTempDir());
27 completion_callback_ = base::Bind(
28 &AppCacheDiskCacheTest::OnComplete,
29 base::Unretained(this));
30 }
31
32 virtual void TearDown() OVERRIDE {
33 base::RunLoop().RunUntilIdle();
34 message_loop_.reset(NULL);
35 }
36
37 void FlushCacheTasks() {
38 base::RunLoop().RunUntilIdle();
39 }
40
41 void OnComplete(int err) {
42 completion_results_.push_back(err);
43 }
44
45 base::ScopedTempDir directory_;
46 scoped_ptr<base::MessageLoop> message_loop_;
47 scoped_refptr<base::MessageLoopProxy> cache_thread_;
48 net::CompletionCallback completion_callback_;
49 std::vector<int> completion_results_;
50
51 static const int k10MBytes = 10 * 1024 * 1024;
52 };
53
54 TEST_F(AppCacheDiskCacheTest, DisablePriorToInitCompletion) {
55 AppCacheDiskCache::Entry* entry = NULL;
56
57 // Create an instance and start it initializing, queue up
58 // one of each kind of "entry" function.
59 scoped_ptr<AppCacheDiskCache> disk_cache (new AppCacheDiskCache);
60 EXPECT_FALSE(disk_cache->is_disabled());
61 disk_cache->InitWithDiskBackend(
62 directory_.path(), k10MBytes, false, cache_thread_,
63 completion_callback_);
64 disk_cache->CreateEntry(1, &entry, completion_callback_);
65 disk_cache->OpenEntry(2, &entry, completion_callback_);
66 disk_cache->DoomEntry(3, completion_callback_);
67
68 // Pull the plug on all that.
69 EXPECT_FALSE(disk_cache->is_disabled());
70 disk_cache->Disable();
71 EXPECT_TRUE(disk_cache->is_disabled());
72
73 FlushCacheTasks();
74
75 EXPECT_EQ(NULL, entry);
76 EXPECT_EQ(4u, completion_results_.size());
77 for (std::vector<int>::const_iterator iter = completion_results_.begin();
78 iter < completion_results_.end(); ++iter) {
79 EXPECT_EQ(net::ERR_ABORTED, *iter);
80 }
81
82 // Ensure the directory can be deleted at this point.
83 EXPECT_TRUE(base::DirectoryExists(directory_.path()));
84 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path()));
85 EXPECT_TRUE(base::DeleteFile(directory_.path(), true));
86 EXPECT_FALSE(base::DirectoryExists(directory_.path()));
87 }
88
89 TEST_F(AppCacheDiskCacheTest, DisableAfterInitted) {
90 // Create an instance and let it fully init.
91 scoped_ptr<AppCacheDiskCache> disk_cache (new AppCacheDiskCache);
92 EXPECT_FALSE(disk_cache->is_disabled());
93 disk_cache->InitWithDiskBackend(
94 directory_.path(), k10MBytes, false, cache_thread_,
95 completion_callback_);
96 FlushCacheTasks();
97 EXPECT_EQ(1u, completion_results_.size());
98 EXPECT_EQ(net::OK, completion_results_[0]);
99
100 // Pull the plug
101 disk_cache->Disable();
102 FlushCacheTasks();
103
104 // Ensure the directory can be deleted at this point.
105 EXPECT_TRUE(base::DirectoryExists(directory_.path()));
106 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path()));
107 EXPECT_TRUE(base::DeleteFile(directory_.path(), true));
108 EXPECT_FALSE(base::DirectoryExists(directory_.path()));
109
110 // Methods should return immediately when disabled and not invoke
111 // the callback at all.
112 AppCacheDiskCache::Entry* entry = NULL;
113 completion_results_.clear();
114 EXPECT_EQ(net::ERR_ABORTED,
115 disk_cache->CreateEntry(1, &entry, completion_callback_));
116 EXPECT_EQ(net::ERR_ABORTED,
117 disk_cache->OpenEntry(2, &entry, completion_callback_));
118 EXPECT_EQ(net::ERR_ABORTED,
119 disk_cache->DoomEntry(3, completion_callback_));
120 FlushCacheTasks();
121 EXPECT_TRUE(completion_results_.empty());
122 }
123
124 TEST_F(AppCacheDiskCacheTest, DisableWithEntriesOpen) {
125 // Create an instance and let it fully init.
126 scoped_ptr<AppCacheDiskCache> disk_cache (new AppCacheDiskCache);
127 EXPECT_FALSE(disk_cache->is_disabled());
128 disk_cache->InitWithDiskBackend(
129 directory_.path(), k10MBytes, false, cache_thread_,
130 completion_callback_);
131 FlushCacheTasks();
132 EXPECT_EQ(1u, completion_results_.size());
133 EXPECT_EQ(net::OK, completion_results_[0]);
134
135 // Create/open some entries.
136 AppCacheDiskCache::Entry* entry1 = NULL;
137 AppCacheDiskCache::Entry* entry2 = NULL;
138 completion_results_.clear();
139 EXPECT_EQ(net::ERR_IO_PENDING,
140 disk_cache->CreateEntry(1, &entry1, completion_callback_));
141 EXPECT_EQ(net::ERR_IO_PENDING,
142 disk_cache->CreateEntry(2, &entry2, completion_callback_));
143 FlushCacheTasks();
144 EXPECT_EQ(2u, completion_results_.size());
145 EXPECT_EQ(net::OK, completion_results_[0]);
146 EXPECT_EQ(net::OK, completion_results_[1]);
147 EXPECT_TRUE(entry1);
148 EXPECT_TRUE(entry2);
149 completion_results_.clear();
150
151 // Write something to one of the entries and flush it.
152 const char* kData = "Hello";
153 const int kDataLen = strlen(kData) + 1;
154 scoped_refptr<net::IOBuffer> write_buf(new net::WrappedIOBuffer(kData));
155 EXPECT_EQ(net::ERR_IO_PENDING,
156 entry1->Write(0, 0, write_buf, kDataLen, completion_callback_));
157 FlushCacheTasks();
158 EXPECT_EQ(1u, completion_results_.size());
159 EXPECT_EQ(kDataLen, completion_results_[0]);
160 completion_results_.clear();
161
162 // Queue up a read and a write.
163 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kDataLen);
164 EXPECT_EQ(
165 net::ERR_IO_PENDING,
166 entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_));
167 EXPECT_EQ(
168 net::ERR_IO_PENDING,
169 entry2->Write(0, 0, write_buf.get(), kDataLen, completion_callback_));
170
171 // Pull the plug
172 disk_cache->Disable();
173 FlushCacheTasks();
174
175 // The entries are abandoned and the read/write callbacks
176 // should not be called.
177 EXPECT_TRUE(completion_results_.empty());
178
179 // Ensure the directory can be deleted at this point.
180 EXPECT_TRUE(base::DirectoryExists(directory_.path()));
181 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path()));
182 EXPECT_TRUE(base::DeleteFile(directory_.path(), true));
183 EXPECT_FALSE(base::DirectoryExists(directory_.path()));
184
185 disk_cache.reset(NULL);
186
187 // New IO operations should fail immediately.
188 EXPECT_EQ(
189 net::ERR_ABORTED,
190 entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_));
191 entry1->Close();
192 entry2->Close();
193
194 FlushCacheTasks();
195 EXPECT_TRUE(completion_results_.empty());
196 }
197
198 } // namespace appcache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698