OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 static const int k10MBytes = 10 * 1024 * 1024; | 51 static const int k10MBytes = 10 * 1024 * 1024; |
52 }; | 52 }; |
53 | 53 |
54 TEST_F(AppCacheDiskCacheTest, DisablePriorToInitCompletion) { | 54 TEST_F(AppCacheDiskCacheTest, DisablePriorToInitCompletion) { |
55 AppCacheDiskCache::Entry* entry = NULL; | 55 AppCacheDiskCache::Entry* entry = NULL; |
56 | 56 |
57 // Create an instance and start it initializing, queue up | 57 // Create an instance and start it initializing, queue up |
58 // one of each kind of "entry" function. | 58 // one of each kind of "entry" function. |
59 std::unique_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache); | 59 std::unique_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache); |
60 EXPECT_FALSE(disk_cache->is_disabled()); | 60 EXPECT_FALSE(disk_cache->is_disabled()); |
61 disk_cache->InitWithDiskBackend( | 61 disk_cache->InitWithDiskBackend(directory_.GetPath(), k10MBytes, false, |
62 directory_.path(), k10MBytes, false, cache_thread_, completion_callback_); | 62 cache_thread_, completion_callback_); |
63 disk_cache->CreateEntry(1, &entry, completion_callback_); | 63 disk_cache->CreateEntry(1, &entry, completion_callback_); |
64 disk_cache->OpenEntry(2, &entry, completion_callback_); | 64 disk_cache->OpenEntry(2, &entry, completion_callback_); |
65 disk_cache->DoomEntry(3, completion_callback_); | 65 disk_cache->DoomEntry(3, completion_callback_); |
66 | 66 |
67 // Pull the plug on all that. | 67 // Pull the plug on all that. |
68 EXPECT_FALSE(disk_cache->is_disabled()); | 68 EXPECT_FALSE(disk_cache->is_disabled()); |
69 disk_cache->Disable(); | 69 disk_cache->Disable(); |
70 EXPECT_TRUE(disk_cache->is_disabled()); | 70 EXPECT_TRUE(disk_cache->is_disabled()); |
71 | 71 |
72 FlushCacheTasks(); | 72 FlushCacheTasks(); |
73 | 73 |
74 EXPECT_EQ(NULL, entry); | 74 EXPECT_EQ(NULL, entry); |
75 EXPECT_EQ(4u, completion_results_.size()); | 75 EXPECT_EQ(4u, completion_results_.size()); |
76 for (std::vector<int>::const_iterator iter = completion_results_.begin(); | 76 for (std::vector<int>::const_iterator iter = completion_results_.begin(); |
77 iter < completion_results_.end(); ++iter) { | 77 iter < completion_results_.end(); ++iter) { |
78 EXPECT_EQ(net::ERR_ABORTED, *iter); | 78 EXPECT_EQ(net::ERR_ABORTED, *iter); |
79 } | 79 } |
80 | 80 |
81 // Ensure the directory can be deleted at this point. | 81 // Ensure the directory can be deleted at this point. |
82 EXPECT_TRUE(base::DirectoryExists(directory_.path())); | 82 EXPECT_TRUE(base::DirectoryExists(directory_.GetPath())); |
83 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path())); | 83 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.GetPath())); |
84 EXPECT_TRUE(base::DeleteFile(directory_.path(), true)); | 84 EXPECT_TRUE(base::DeleteFile(directory_.GetPath(), true)); |
85 EXPECT_FALSE(base::DirectoryExists(directory_.path())); | 85 EXPECT_FALSE(base::DirectoryExists(directory_.GetPath())); |
86 } | 86 } |
87 | 87 |
88 TEST_F(AppCacheDiskCacheTest, DisableAfterInitted) { | 88 TEST_F(AppCacheDiskCacheTest, DisableAfterInitted) { |
89 // Create an instance and let it fully init. | 89 // Create an instance and let it fully init. |
90 std::unique_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache); | 90 std::unique_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache); |
91 EXPECT_FALSE(disk_cache->is_disabled()); | 91 EXPECT_FALSE(disk_cache->is_disabled()); |
92 disk_cache->InitWithDiskBackend( | 92 disk_cache->InitWithDiskBackend(directory_.GetPath(), k10MBytes, false, |
93 directory_.path(), k10MBytes, false, cache_thread_, completion_callback_); | 93 cache_thread_, completion_callback_); |
94 FlushCacheTasks(); | 94 FlushCacheTasks(); |
95 EXPECT_EQ(1u, completion_results_.size()); | 95 EXPECT_EQ(1u, completion_results_.size()); |
96 EXPECT_EQ(net::OK, completion_results_[0]); | 96 EXPECT_EQ(net::OK, completion_results_[0]); |
97 | 97 |
98 // Pull the plug | 98 // Pull the plug |
99 disk_cache->Disable(); | 99 disk_cache->Disable(); |
100 FlushCacheTasks(); | 100 FlushCacheTasks(); |
101 | 101 |
102 // Ensure the directory can be deleted at this point. | 102 // Ensure the directory can be deleted at this point. |
103 EXPECT_TRUE(base::DirectoryExists(directory_.path())); | 103 EXPECT_TRUE(base::DirectoryExists(directory_.GetPath())); |
104 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path())); | 104 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.GetPath())); |
105 EXPECT_TRUE(base::DeleteFile(directory_.path(), true)); | 105 EXPECT_TRUE(base::DeleteFile(directory_.GetPath(), true)); |
106 EXPECT_FALSE(base::DirectoryExists(directory_.path())); | 106 EXPECT_FALSE(base::DirectoryExists(directory_.GetPath())); |
107 | 107 |
108 // Methods should return immediately when disabled and not invoke | 108 // Methods should return immediately when disabled and not invoke |
109 // the callback at all. | 109 // the callback at all. |
110 AppCacheDiskCache::Entry* entry = NULL; | 110 AppCacheDiskCache::Entry* entry = NULL; |
111 completion_results_.clear(); | 111 completion_results_.clear(); |
112 EXPECT_EQ(net::ERR_ABORTED, | 112 EXPECT_EQ(net::ERR_ABORTED, |
113 disk_cache->CreateEntry(1, &entry, completion_callback_)); | 113 disk_cache->CreateEntry(1, &entry, completion_callback_)); |
114 EXPECT_EQ(net::ERR_ABORTED, | 114 EXPECT_EQ(net::ERR_ABORTED, |
115 disk_cache->OpenEntry(2, &entry, completion_callback_)); | 115 disk_cache->OpenEntry(2, &entry, completion_callback_)); |
116 EXPECT_EQ(net::ERR_ABORTED, | 116 EXPECT_EQ(net::ERR_ABORTED, |
117 disk_cache->DoomEntry(3, completion_callback_)); | 117 disk_cache->DoomEntry(3, completion_callback_)); |
118 FlushCacheTasks(); | 118 FlushCacheTasks(); |
119 EXPECT_TRUE(completion_results_.empty()); | 119 EXPECT_TRUE(completion_results_.empty()); |
120 } | 120 } |
121 | 121 |
122 // Flaky on Android: http://crbug.com/339534 | 122 // Flaky on Android: http://crbug.com/339534 |
123 TEST_F(AppCacheDiskCacheTest, DISABLED_DisableWithEntriesOpen) { | 123 TEST_F(AppCacheDiskCacheTest, DISABLED_DisableWithEntriesOpen) { |
124 // Create an instance and let it fully init. | 124 // Create an instance and let it fully init. |
125 std::unique_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache); | 125 std::unique_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache); |
126 EXPECT_FALSE(disk_cache->is_disabled()); | 126 EXPECT_FALSE(disk_cache->is_disabled()); |
127 disk_cache->InitWithDiskBackend( | 127 disk_cache->InitWithDiskBackend(directory_.GetPath(), k10MBytes, false, |
128 directory_.path(), k10MBytes, false, cache_thread_, completion_callback_); | 128 cache_thread_, completion_callback_); |
129 FlushCacheTasks(); | 129 FlushCacheTasks(); |
130 EXPECT_EQ(1u, completion_results_.size()); | 130 EXPECT_EQ(1u, completion_results_.size()); |
131 EXPECT_EQ(net::OK, completion_results_[0]); | 131 EXPECT_EQ(net::OK, completion_results_[0]); |
132 | 132 |
133 // Note: We don't have detailed expectations of the DiskCache | 133 // Note: We don't have detailed expectations of the DiskCache |
134 // operations because on android it's really SimpleCache which | 134 // operations because on android it's really SimpleCache which |
135 // does behave differently. | 135 // does behave differently. |
136 // | 136 // |
137 // What matters for the corruption handling and service reinitiazation | 137 // What matters for the corruption handling and service reinitiazation |
138 // is that the directory can be deleted after the calling Disable() method, | 138 // is that the directory can be deleted after the calling Disable() method, |
(...skipping 18 matching lines...) Expand all Loading... |
157 // Queue up a read and a write. | 157 // Queue up a read and a write. |
158 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kDataLen); | 158 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kDataLen); |
159 entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_); | 159 entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_); |
160 entry2->Write(0, 0, write_buf.get(), kDataLen, completion_callback_); | 160 entry2->Write(0, 0, write_buf.get(), kDataLen, completion_callback_); |
161 | 161 |
162 // Pull the plug | 162 // Pull the plug |
163 disk_cache->Disable(); | 163 disk_cache->Disable(); |
164 FlushCacheTasks(); | 164 FlushCacheTasks(); |
165 | 165 |
166 // Ensure the directory can be deleted at this point. | 166 // Ensure the directory can be deleted at this point. |
167 EXPECT_TRUE(base::DirectoryExists(directory_.path())); | 167 EXPECT_TRUE(base::DirectoryExists(directory_.GetPath())); |
168 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path())); | 168 EXPECT_FALSE(base::IsDirectoryEmpty(directory_.GetPath())); |
169 EXPECT_TRUE(base::DeleteFile(directory_.path(), true)); | 169 EXPECT_TRUE(base::DeleteFile(directory_.GetPath(), true)); |
170 EXPECT_FALSE(base::DirectoryExists(directory_.path())); | 170 EXPECT_FALSE(base::DirectoryExists(directory_.GetPath())); |
171 | 171 |
172 disk_cache.reset(NULL); | 172 disk_cache.reset(NULL); |
173 | 173 |
174 // Also, new IO operations should fail immediately. | 174 // Also, new IO operations should fail immediately. |
175 EXPECT_EQ( | 175 EXPECT_EQ( |
176 net::ERR_ABORTED, | 176 net::ERR_ABORTED, |
177 entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_)); | 177 entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_)); |
178 entry1->Close(); | 178 entry1->Close(); |
179 entry2->Close(); | 179 entry2->Close(); |
180 | 180 |
181 FlushCacheTasks(); | 181 FlushCacheTasks(); |
182 } | 182 } |
183 | 183 |
184 } // namespace content | 184 } // namespace content |
OLD | NEW |