OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/browser/service_worker/service_worker_disk_cache_migrator.h" | 5 #include "content/browser/service_worker/service_worker_disk_cache_migrator.h" |
6 | 6 |
| 7 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
8 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
9 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_storage.h" |
10 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
11 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
12 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
13 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
14 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
16 | 19 |
17 namespace content { | 20 namespace content { |
18 | 21 |
19 namespace { | 22 namespace { |
(...skipping 15 matching lines...) Expand all Loading... |
35 body(body), | 38 body(body), |
36 metadata(metadata) {} | 39 metadata(metadata) {} |
37 }; | 40 }; |
38 | 41 |
39 void OnDiskCacheMigrated(const base::Closure& callback, | 42 void OnDiskCacheMigrated(const base::Closure& callback, |
40 ServiceWorkerStatusCode status) { | 43 ServiceWorkerStatusCode status) { |
41 EXPECT_EQ(SERVICE_WORKER_OK, status); | 44 EXPECT_EQ(SERVICE_WORKER_OK, status); |
42 callback.Run(); | 45 callback.Run(); |
43 } | 46 } |
44 | 47 |
| 48 void OnRegistrationFound( |
| 49 const base::Closure& callback, |
| 50 ServiceWorkerStatusCode status, |
| 51 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 52 callback.Run(); |
| 53 } |
| 54 |
45 } // namespace | 55 } // namespace |
46 | 56 |
47 class ServiceWorkerDiskCacheMigratorTest : public testing::Test { | 57 class ServiceWorkerDiskCacheMigratorTest : public testing::Test { |
48 public: | 58 public: |
49 ServiceWorkerDiskCacheMigratorTest() | 59 ServiceWorkerDiskCacheMigratorTest() |
50 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 60 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
51 | 61 |
52 void SetUp() override { | 62 void SetUp() override { |
53 ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir()); | 63 ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir()); |
54 const base::FilePath kSrcDiskCachePath = | 64 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager( |
55 user_data_directory_.path().AppendASCII("SrcCache"); | 65 new MockServiceWorkerDatabaseTaskManager( |
56 const base::FilePath kDestDiskCachePath = | 66 base::ThreadTaskRunnerHandle::Get())); |
57 user_data_directory_.path().AppendASCII("DestCache"); | |
58 | 67 |
59 // Initialize the src BlockFile diskcache. | 68 context_.reset(new ServiceWorkerContextCore( |
60 src_ = ServiceWorkerDiskCache::CreateWithBlockFileBackend(); | 69 user_data_directory_.path(), database_task_manager.Pass(), |
61 net::TestCompletionCallback cb1; | 70 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr, nullptr, |
62 src_->InitWithDiskBackend( | 71 nullptr)); |
63 kSrcDiskCachePath, kMaxDiskCacheSize, false /* force */, | 72 } |
64 base::ThreadTaskRunnerHandle::Get(), cb1.callback()); | |
65 ASSERT_EQ(net::OK, cb1.WaitForResult()); | |
66 | 73 |
67 // Initialize the dest Simple diskcache. | 74 void TearDown() override { |
68 dest_ = ServiceWorkerDiskCache::CreateWithSimpleBackend(); | 75 context_.reset(); |
69 net::TestCompletionCallback cb2; | 76 base::RunLoop().RunUntilIdle(); |
70 dest_->InitWithDiskBackend( | 77 } |
71 kDestDiskCachePath, kMaxDiskCacheSize, false /* force */, | |
72 base::ThreadTaskRunnerHandle::Get(), cb2.callback()); | |
73 ASSERT_EQ(net::OK, cb2.WaitForResult()); | |
74 | 78 |
75 migrator_.reset( | 79 scoped_ptr<ServiceWorkerDiskCache> CreateSrcDiskCache() { |
76 new ServiceWorkerDiskCacheMigrator(src_.get(), dest_.get())); | 80 scoped_ptr<ServiceWorkerDiskCache> src( |
| 81 ServiceWorkerDiskCache::CreateWithBlockFileBackend()); |
| 82 net::TestCompletionCallback cb; |
| 83 src->InitWithDiskBackend( |
| 84 storage()->GetOldDiskCachePath(), kMaxDiskCacheSize, false /* force */, |
| 85 base::ThreadTaskRunnerHandle::Get(), cb.callback()); |
| 86 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 87 return src.Pass(); |
| 88 } |
| 89 |
| 90 scoped_ptr<ServiceWorkerDiskCache> CreateDestDiskCache() { |
| 91 scoped_ptr<ServiceWorkerDiskCache> dest( |
| 92 ServiceWorkerDiskCache::CreateWithSimpleBackend()); |
| 93 net::TestCompletionCallback cb; |
| 94 dest->InitWithDiskBackend( |
| 95 storage()->GetDiskCachePath(), kMaxDiskCacheSize, false /* force */, |
| 96 base::ThreadTaskRunnerHandle::Get(), cb.callback()); |
| 97 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 98 return dest.Pass(); |
77 } | 99 } |
78 | 100 |
79 bool WriteResponse(ServiceWorkerDiskCache* disk_cache, | 101 bool WriteResponse(ServiceWorkerDiskCache* disk_cache, |
80 const ResponseData& response) { | 102 const ResponseData& response) { |
81 scoped_ptr<ServiceWorkerResponseWriter> writer( | 103 scoped_ptr<ServiceWorkerResponseWriter> writer( |
82 new ServiceWorkerResponseWriter(response.resource_id, disk_cache)); | 104 new ServiceWorkerResponseWriter(response.resource_id, disk_cache)); |
83 | 105 |
84 // Write the response info. | 106 // Write the response info. |
85 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); | 107 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); |
86 info->request_time = base::Time() + base::TimeDelta::FromSeconds(10); | 108 info->request_time = base::Time() + base::TimeDelta::FromSeconds(10); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 // Verify the response body. | 174 // Verify the response body. |
153 const int kBigEnough = 512; | 175 const int kBigEnough = 512; |
154 scoped_refptr<net::IOBuffer> body_buffer = new net::IOBuffer(kBigEnough); | 176 scoped_refptr<net::IOBuffer> body_buffer = new net::IOBuffer(kBigEnough); |
155 net::TestCompletionCallback cb2; | 177 net::TestCompletionCallback cb2; |
156 reader->ReadData(body_buffer.get(), kBigEnough, cb2.callback()); | 178 reader->ReadData(body_buffer.get(), kBigEnough, cb2.callback()); |
157 rv = cb2.WaitForResult(); | 179 rv = cb2.WaitForResult(); |
158 ASSERT_EQ(static_cast<int>(expected.body.length()), rv); | 180 ASSERT_EQ(static_cast<int>(expected.body.length()), rv); |
159 EXPECT_EQ(0, memcmp(expected.body.data(), body_buffer->data(), rv)); | 181 EXPECT_EQ(0, memcmp(expected.body.data(), body_buffer->data(), rv)); |
160 } | 182 } |
161 | 183 |
162 void Migrate() { | |
163 base::RunLoop run_loop; | |
164 migrator_->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure())); | |
165 run_loop.Run(); | |
166 } | |
167 | |
168 int32 GetEntryCount(ServiceWorkerDiskCache* disk_cache) { | 184 int32 GetEntryCount(ServiceWorkerDiskCache* disk_cache) { |
169 return disk_cache->disk_cache()->GetEntryCount(); | 185 return disk_cache->disk_cache()->GetEntryCount(); |
170 } | 186 } |
171 | 187 |
172 void SetMaxNumberOfInflightTasks(size_t max_number) { | 188 ServiceWorkerStorage* storage() { return context_->storage(); } |
173 migrator_->set_max_number_of_inflight_tasks(max_number); | |
174 } | |
175 | 189 |
176 protected: | 190 private: |
177 TestBrowserThreadBundle browser_thread_bundle_; | 191 TestBrowserThreadBundle browser_thread_bundle_; |
178 base::ScopedTempDir user_data_directory_; | 192 base::ScopedTempDir user_data_directory_; |
179 scoped_ptr<ServiceWorkerDiskCache> src_; | 193 |
180 scoped_ptr<ServiceWorkerDiskCache> dest_; | 194 scoped_ptr<ServiceWorkerContextCore> context_; |
181 scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator_; | |
182 }; | 195 }; |
183 | 196 |
184 TEST_F(ServiceWorkerDiskCacheMigratorTest, Basic) { | 197 TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateDiskCache) { |
185 std::vector<ResponseData> responses; | 198 std::vector<ResponseData> responses; |
186 responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", "")); | 199 responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", "")); |
187 responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", "")); | 200 responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", "")); |
188 responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", "")); | 201 responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", "")); |
189 responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta")); | 202 responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta")); |
190 responses.push_back(ResponseData(10, "HTTP/1.1 200 OK\0\0", "", "meta")); | 203 responses.push_back(ResponseData(10, "HTTP/1.1 200 OK\0\0", "", "meta")); |
191 responses.push_back(ResponseData(11, "HTTP/1.1 200 OK\0\0", "body", "")); | 204 responses.push_back(ResponseData(11, "HTTP/1.1 200 OK\0\0", "body", "")); |
192 responses.push_back(ResponseData(12, "HTTP/1.1 200 OK\0\0", "", "")); | 205 responses.push_back(ResponseData(12, "HTTP/1.1 200 OK\0\0", "", "")); |
193 responses.push_back(ResponseData( | 206 responses.push_back(ResponseData( |
194 20, "HTTP/1.1 200 OK\0\0", std::string(256, 'a'), std::string(128, 'b'))); | 207 20, "HTTP/1.1 200 OK\0\0", std::string(256, 'a'), std::string(128, 'b'))); |
195 | 208 |
196 // Populate initial data in the src diskcache. | 209 // Populate initial data in the src diskcache. |
| 210 scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache()); |
197 for (const ResponseData& response : responses) { | 211 for (const ResponseData& response : responses) { |
198 ASSERT_TRUE(WriteResponse(src_.get(), response)); | 212 ASSERT_TRUE(WriteResponse(src.get(), response)); |
199 VerifyResponse(src_.get(), response); | 213 VerifyResponse(src.get(), response); |
200 } | 214 } |
201 ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src_.get())); | 215 ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get())); |
202 | 216 |
203 Migrate(); | 217 scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache()); |
| 218 |
| 219 // Start the migrator. |
| 220 base::RunLoop run_loop; |
| 221 scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator( |
| 222 new ServiceWorkerDiskCacheMigrator(src.get(), dest.get())); |
| 223 migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure())); |
| 224 run_loop.Run(); |
204 | 225 |
205 // Verify the migrated contents in the dest diskcache. | 226 // Verify the migrated contents in the dest diskcache. |
206 for (const ResponseData& response : responses) | 227 for (const ResponseData& response : responses) |
207 VerifyResponse(dest_.get(), response); | 228 VerifyResponse(dest.get(), response); |
208 EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest_.get())); | 229 EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest.get())); |
209 } | 230 } |
210 | 231 |
211 TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateEmptyDiskCache) { | 232 TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateEmptyDiskCache) { |
212 ASSERT_EQ(0, GetEntryCount(src_.get())); | 233 scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache()); |
213 Migrate(); | 234 scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache()); |
214 EXPECT_EQ(0, GetEntryCount(dest_.get())); | 235 |
| 236 ASSERT_EQ(0, GetEntryCount(src.get())); |
| 237 ASSERT_EQ(0, GetEntryCount(dest.get())); |
| 238 |
| 239 // Start the migrator. |
| 240 base::RunLoop run_loop; |
| 241 scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator( |
| 242 new ServiceWorkerDiskCacheMigrator(src.get(), dest.get())); |
| 243 migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure())); |
| 244 run_loop.Run(); |
| 245 |
| 246 ASSERT_EQ(0, GetEntryCount(src.get())); |
| 247 ASSERT_EQ(0, GetEntryCount(dest.get())); |
215 } | 248 } |
216 | 249 |
217 TEST_F(ServiceWorkerDiskCacheMigratorTest, ThrottleInflightTasks) { | 250 TEST_F(ServiceWorkerDiskCacheMigratorTest, ThrottleInflightTasks) { |
218 std::vector<ResponseData> responses; | 251 std::vector<ResponseData> responses; |
219 for (int i = 0; i < 10; ++i) | 252 for (int i = 0; i < 10; ++i) |
220 responses.push_back(ResponseData(i, "HTTP/1.1 200 OK\0\0", "foo", "bar")); | 253 responses.push_back(ResponseData(i, "HTTP/1.1 200 OK\0\0", "foo", "bar")); |
221 | 254 |
222 // Populate initial data in the src diskcache. | 255 // Populate initial data in the src diskcache. |
| 256 scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache()); |
223 for (const ResponseData& response : responses) { | 257 for (const ResponseData& response : responses) { |
224 ASSERT_TRUE(WriteResponse(src_.get(), response)); | 258 ASSERT_TRUE(WriteResponse(src.get(), response)); |
225 VerifyResponse(src_.get(), response); | 259 VerifyResponse(src.get(), response); |
226 } | 260 } |
227 ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src_.get())); | 261 ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get())); |
| 262 |
| 263 scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache()); |
| 264 scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator( |
| 265 new ServiceWorkerDiskCacheMigrator(src.get(), dest.get())); |
228 | 266 |
229 // Tighten the max number of inflight tasks. | 267 // Tighten the max number of inflight tasks. |
230 SetMaxNumberOfInflightTasks(2); | 268 migrator->set_max_number_of_inflight_tasks(2); |
231 | 269 |
232 // Migration should hit the limit, but should successfully complete. | 270 // Migration should hit the limit, but should successfully complete. |
233 Migrate(); | 271 base::RunLoop run_loop; |
| 272 migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure())); |
| 273 run_loop.Run(); |
234 | 274 |
235 // Verify the migrated contents in the dest diskcache. | 275 // Verify the migrated contents in the dest diskcache. |
236 for (const ResponseData& response : responses) | 276 for (const ResponseData& response : responses) |
237 VerifyResponse(dest_.get(), response); | 277 VerifyResponse(dest.get(), response); |
238 EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest_.get())); | 278 EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest.get())); |
| 279 } |
| 280 |
| 281 TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateOnDiskCacheAccess) { |
| 282 std::vector<ResponseData> responses; |
| 283 responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", "")); |
| 284 responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", "")); |
| 285 responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", "")); |
| 286 responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta")); |
| 287 |
| 288 // Populate initial data in the src diskcache. |
| 289 scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache()); |
| 290 for (const ResponseData& response : responses) { |
| 291 ASSERT_TRUE(WriteResponse(src.get(), response)); |
| 292 VerifyResponse(src.get(), response); |
| 293 } |
| 294 ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get())); |
| 295 ASSERT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath())); |
| 296 src.reset(); |
| 297 |
| 298 base::RunLoop run_loop; |
| 299 storage()->LazyInitialize(run_loop.QuitClosure()); |
| 300 run_loop.Run(); |
| 301 |
| 302 // DiskCache access should start the migration. |
| 303 ASSERT_TRUE(storage()->needs_disk_cache_migration_); |
| 304 ServiceWorkerDiskCache* dest = storage()->disk_cache(); |
| 305 |
| 306 // Verify the migrated contents in the dest diskcache. |
| 307 for (const ResponseData& response : responses) |
| 308 VerifyResponse(dest, response); |
| 309 EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest)); |
| 310 |
| 311 // The src DiskCache should be deleted after the migration. |
| 312 EXPECT_FALSE(base::DirectoryExists(storage()->GetOldDiskCachePath())); |
| 313 } |
| 314 |
| 315 TEST_F(ServiceWorkerDiskCacheMigratorTest, NotMigrateOnDatabaseAccess) { |
| 316 std::vector<ResponseData> responses; |
| 317 responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", "")); |
| 318 responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", "")); |
| 319 responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", "")); |
| 320 responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta")); |
| 321 |
| 322 // Populate initial data in the src diskcache. |
| 323 scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache()); |
| 324 for (const ResponseData& response : responses) { |
| 325 ASSERT_TRUE(WriteResponse(src.get(), response)); |
| 326 VerifyResponse(src.get(), response); |
| 327 } |
| 328 ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get())); |
| 329 ASSERT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath())); |
| 330 |
| 331 // Database access should not start the migration. |
| 332 base::RunLoop run_loop; |
| 333 storage()->FindRegistrationForDocument( |
| 334 GURL("http://example.com/"), |
| 335 base::Bind(&OnRegistrationFound, run_loop.QuitClosure())); |
| 336 run_loop.Run(); |
| 337 |
| 338 // Verify that the migration didn't happen. |
| 339 scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache()); |
| 340 EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get())); |
| 341 EXPECT_EQ(0, GetEntryCount(dest.get())); |
| 342 EXPECT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath())); |
239 } | 343 } |
240 | 344 |
241 } // namespace content | 345 } // namespace content |
OLD | NEW |