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

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

Powered by Google App Engine
This is Rietveld 408576698