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 24 matching lines...) Expand all Loading... | |
44 | 47 |
45 } // namespace | 48 } // namespace |
46 | 49 |
47 class ServiceWorkerDiskCacheMigratorTest : public testing::Test { | 50 class ServiceWorkerDiskCacheMigratorTest : public testing::Test { |
48 public: | 51 public: |
49 ServiceWorkerDiskCacheMigratorTest() | 52 ServiceWorkerDiskCacheMigratorTest() |
50 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 53 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
51 | 54 |
52 void SetUp() override { | 55 void SetUp() override { |
53 ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir()); | 56 ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir()); |
54 const base::FilePath kSrcDiskCachePath = | 57 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager( |
55 user_data_directory_.path().AppendASCII("SrcCache"); | 58 new MockServiceWorkerDatabaseTaskManager( |
56 const base::FilePath kDestDiskCachePath = | 59 base::ThreadTaskRunnerHandle::Get())); |
57 user_data_directory_.path().AppendASCII("DestCache"); | 60 |
61 context_.reset(new ServiceWorkerContextCore( | |
62 user_data_directory_.path(), database_task_manager.Pass(), | |
63 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr, nullptr, | |
64 nullptr)); | |
65 ServiceWorkerStorage* storage = context_->storage(); | |
58 | 66 |
59 // Initialize the src BlockFile diskcache. | 67 // Initialize the src BlockFile diskcache. |
60 src_ = ServiceWorkerDiskCache::CreateWithBlockFileBackend(); | 68 src_ = ServiceWorkerDiskCache::CreateWithBlockFileBackend(); |
61 net::TestCompletionCallback cb1; | 69 net::TestCompletionCallback cb1; |
62 src_->InitWithDiskBackend( | 70 src_->InitWithDiskBackend( |
63 kSrcDiskCachePath, kMaxDiskCacheSize, false /* force */, | 71 storage->GetOldDiskCachePath(), kMaxDiskCacheSize, false /* force */, |
64 base::ThreadTaskRunnerHandle::Get(), cb1.callback()); | 72 base::ThreadTaskRunnerHandle::Get(), cb1.callback()); |
65 ASSERT_EQ(net::OK, cb1.WaitForResult()); | 73 ASSERT_EQ(net::OK, cb1.WaitForResult()); |
66 | 74 |
67 // Initialize the dest Simple diskcache. | 75 // Initialize the dest Simple diskcache. |
68 dest_ = ServiceWorkerDiskCache::CreateWithSimpleBackend(); | 76 dest_ = ServiceWorkerDiskCache::CreateWithSimpleBackend(); |
69 net::TestCompletionCallback cb2; | 77 net::TestCompletionCallback cb2; |
70 dest_->InitWithDiskBackend( | 78 dest_->InitWithDiskBackend( |
71 kDestDiskCachePath, kMaxDiskCacheSize, false /* force */, | 79 storage->GetDiskCachePath(), kMaxDiskCacheSize, false /* force */, |
72 base::ThreadTaskRunnerHandle::Get(), cb2.callback()); | 80 base::ThreadTaskRunnerHandle::Get(), cb2.callback()); |
73 ASSERT_EQ(net::OK, cb2.WaitForResult()); | 81 ASSERT_EQ(net::OK, cb2.WaitForResult()); |
74 | 82 |
75 migrator_.reset( | 83 migrator_.reset( |
76 new ServiceWorkerDiskCacheMigrator(src_.get(), dest_.get())); | 84 new ServiceWorkerDiskCacheMigrator(src_.get(), dest_.get())); |
77 } | 85 } |
78 | 86 |
79 bool WriteResponse(ServiceWorkerDiskCache* disk_cache, | 87 bool WriteResponse(ServiceWorkerDiskCache* disk_cache, |
80 const ResponseData& response) { | 88 const ResponseData& response) { |
81 scoped_ptr<ServiceWorkerResponseWriter> writer( | 89 scoped_ptr<ServiceWorkerResponseWriter> writer( |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 } | 174 } |
167 | 175 |
168 int32 GetEntryCount(ServiceWorkerDiskCache* disk_cache) { | 176 int32 GetEntryCount(ServiceWorkerDiskCache* disk_cache) { |
169 return disk_cache->disk_cache()->GetEntryCount(); | 177 return disk_cache->disk_cache()->GetEntryCount(); |
170 } | 178 } |
171 | 179 |
172 void SetMaxNumberOfInflightTasks(size_t max_number) { | 180 void SetMaxNumberOfInflightTasks(size_t max_number) { |
173 migrator_->set_max_number_of_inflight_tasks(max_number); | 181 migrator_->set_max_number_of_inflight_tasks(max_number); |
174 } | 182 } |
175 | 183 |
184 void LazyInitialize() { | |
185 base::RunLoop run_loop; | |
186 context_->storage()->LazyInitialize(run_loop.QuitClosure()); | |
187 run_loop.Run(); | |
188 } | |
189 | |
190 base::FilePath GetSrcDiskCachePath() { | |
191 return context_->storage()->GetOldDiskCachePath(); | |
192 } | |
193 | |
194 ServiceWorkerDiskCache* disk_cache() { | |
195 return context_->storage()->disk_cache(); | |
196 } | |
197 | |
176 protected: | 198 protected: |
177 TestBrowserThreadBundle browser_thread_bundle_; | 199 TestBrowserThreadBundle browser_thread_bundle_; |
178 base::ScopedTempDir user_data_directory_; | 200 base::ScopedTempDir user_data_directory_; |
201 | |
202 scoped_ptr<ServiceWorkerContextCore> context_; | |
203 | |
179 scoped_ptr<ServiceWorkerDiskCache> src_; | 204 scoped_ptr<ServiceWorkerDiskCache> src_; |
180 scoped_ptr<ServiceWorkerDiskCache> dest_; | 205 scoped_ptr<ServiceWorkerDiskCache> dest_; |
181 scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator_; | 206 scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator_; |
182 }; | 207 }; |
183 | 208 |
184 TEST_F(ServiceWorkerDiskCacheMigratorTest, Basic) { | 209 TEST_F(ServiceWorkerDiskCacheMigratorTest, Basic) { |
185 std::vector<ResponseData> responses; | 210 std::vector<ResponseData> responses; |
186 responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", "")); | 211 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", "")); | 212 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", "")); | 213 responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", "")); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 | 252 |
228 // Migration should hit the limit, but not crash. | 253 // Migration should hit the limit, but not crash. |
229 StartMigration(); | 254 StartMigration(); |
230 | 255 |
231 // Verify the migrated contents in the dest diskcache. | 256 // Verify the migrated contents in the dest diskcache. |
232 for (const ResponseData& response : responses) | 257 for (const ResponseData& response : responses) |
233 VerifyResponse(dest_.get(), response); | 258 VerifyResponse(dest_.get(), response); |
234 EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest_.get())); | 259 EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest_.get())); |
235 } | 260 } |
236 | 261 |
262 TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateDuringLazyInitialize) { | |
263 std::vector<ResponseData> responses; | |
264 responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", "")); | |
265 responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", "")); | |
266 responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", "")); | |
267 responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta")); | |
268 | |
269 // Populate initial data in the src diskcache. | |
270 for (const ResponseData& response : responses) { | |
271 ASSERT_TRUE(WriteResponse(src_.get(), response)); | |
272 VerifyResponse(src_.get(), response); | |
273 } | |
274 ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src_.get())); | |
275 ASSERT_TRUE(base::DirectoryExists(GetSrcDiskCachePath())); | |
276 | |
277 // LazyInitialize ignites the migrator. | |
278 LazyInitialize(); | |
kinuko
2015/06/02 06:18:52
Is it possible to also test that concurrent reques
kinuko
2015/06/02 06:18:52
Did you check the local histogram to see how long
nhiroki
2015/06/02 09:18:32
Done.
nhiroki
2015/06/02 09:18:32
I tried the migration 3 times (Linux, SSD, Release
| |
279 | |
280 // Verify the migrated contents in the dest diskcache. | |
281 for (const ResponseData& response : responses) | |
282 VerifyResponse(disk_cache(), response); | |
283 EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(disk_cache())); | |
284 | |
285 // The src DiskCache should be deleted after migration. | |
286 EXPECT_FALSE(base::DirectoryExists(GetSrcDiskCachePath())); | |
287 } | |
288 | |
falken
2015/06/01 04:31:49
Should we also do a test for if the migration fail
nhiroki
2015/06/02 09:18:32
I haven't found a handy way to fail the migration
| |
237 } // namespace content | 289 } // namespace content |
OLD | NEW |