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

Unified Diff: content/browser/service_worker/service_worker_disk_cache_migrator_unittest.cc

Issue 1152543002: ServiceWorker: Migrate the script cache backend from BlockFile to Simple (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: incorporate falken@'s comments Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_disk_cache_migrator_unittest.cc
diff --git a/content/browser/service_worker/service_worker_disk_cache_migrator_unittest.cc b/content/browser/service_worker/service_worker_disk_cache_migrator_unittest.cc
index 7f3f030a3430331453efd75734da7ff18e4ee052..009e1cbc1b2a55212ab84fc9fa2a8b3c43adfb06 100644
--- a/content/browser/service_worker/service_worker_disk_cache_migrator_unittest.cc
+++ b/content/browser/service_worker/service_worker_disk_cache_migrator_unittest.cc
@@ -4,9 +4,12 @@
#include "content/browser/service_worker/service_worker_disk_cache_migrator.h"
+#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/thread_task_runner_handle.h"
+#include "content/browser/service_worker/service_worker_context_core.h"
+#include "content/browser/service_worker/service_worker_storage.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
@@ -42,6 +45,13 @@ void OnDiskCacheMigrated(const base::Closure& callback,
callback.Run();
}
+void OnRegistrationFound(
+ const base::Closure& callback,
+ ServiceWorkerStatusCode status,
+ const scoped_refptr<ServiceWorkerRegistration>& registration) {
+ callback.Run();
+}
+
} // namespace
class ServiceWorkerDiskCacheMigratorTest : public testing::Test {
@@ -51,29 +61,41 @@ class ServiceWorkerDiskCacheMigratorTest : public testing::Test {
void SetUp() override {
ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir());
- const base::FilePath kSrcDiskCachePath =
- user_data_directory_.path().AppendASCII("SrcCache");
- const base::FilePath kDestDiskCachePath =
- user_data_directory_.path().AppendASCII("DestCache");
+ scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager(
+ new MockServiceWorkerDatabaseTaskManager(
+ base::ThreadTaskRunnerHandle::Get()));
+
+ context_.reset(new ServiceWorkerContextCore(
+ user_data_directory_.path(), database_task_manager.Pass(),
+ base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr, nullptr,
+ nullptr));
+ }
- // Initialize the src BlockFile diskcache.
- src_ = ServiceWorkerDiskCache::CreateWithBlockFileBackend();
- net::TestCompletionCallback cb1;
- src_->InitWithDiskBackend(
- kSrcDiskCachePath, kMaxDiskCacheSize, false /* force */,
- base::ThreadTaskRunnerHandle::Get(), cb1.callback());
- ASSERT_EQ(net::OK, cb1.WaitForResult());
+ void TearDown() override {
+ context_.reset();
+ base::RunLoop().RunUntilIdle();
+ }
- // Initialize the dest Simple diskcache.
- dest_ = ServiceWorkerDiskCache::CreateWithSimpleBackend();
- net::TestCompletionCallback cb2;
- dest_->InitWithDiskBackend(
- kDestDiskCachePath, kMaxDiskCacheSize, false /* force */,
- base::ThreadTaskRunnerHandle::Get(), cb2.callback());
- ASSERT_EQ(net::OK, cb2.WaitForResult());
+ scoped_ptr<ServiceWorkerDiskCache> CreateSrcDiskCache() {
+ scoped_ptr<ServiceWorkerDiskCache> src(
+ ServiceWorkerDiskCache::CreateWithBlockFileBackend());
+ net::TestCompletionCallback cb;
+ src->InitWithDiskBackend(
+ storage()->GetOldDiskCachePath(), kMaxDiskCacheSize, false /* force */,
+ base::ThreadTaskRunnerHandle::Get(), cb.callback());
+ EXPECT_EQ(net::OK, cb.WaitForResult());
+ return src.Pass();
+ }
- migrator_.reset(
- new ServiceWorkerDiskCacheMigrator(src_.get(), dest_.get()));
+ scoped_ptr<ServiceWorkerDiskCache> CreateDestDiskCache() {
+ scoped_ptr<ServiceWorkerDiskCache> dest(
+ ServiceWorkerDiskCache::CreateWithSimpleBackend());
+ net::TestCompletionCallback cb;
+ dest->InitWithDiskBackend(
+ storage()->GetDiskCachePath(), kMaxDiskCacheSize, false /* force */,
+ base::ThreadTaskRunnerHandle::Get(), cb.callback());
+ EXPECT_EQ(net::OK, cb.WaitForResult());
+ return dest.Pass();
}
bool WriteResponse(ServiceWorkerDiskCache* disk_cache,
@@ -159,29 +181,20 @@ class ServiceWorkerDiskCacheMigratorTest : public testing::Test {
EXPECT_EQ(0, memcmp(expected.body.data(), body_buffer->data(), rv));
}
- void Migrate() {
- base::RunLoop run_loop;
- migrator_->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure()));
- run_loop.Run();
- }
-
int32 GetEntryCount(ServiceWorkerDiskCache* disk_cache) {
return disk_cache->disk_cache()->GetEntryCount();
}
- void SetMaxNumberOfInflightTasks(size_t max_number) {
- migrator_->set_max_number_of_inflight_tasks(max_number);
- }
+ ServiceWorkerStorage* storage() { return context_->storage(); }
- protected:
+ private:
TestBrowserThreadBundle browser_thread_bundle_;
base::ScopedTempDir user_data_directory_;
- scoped_ptr<ServiceWorkerDiskCache> src_;
- scoped_ptr<ServiceWorkerDiskCache> dest_;
- scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator_;
+
+ scoped_ptr<ServiceWorkerContextCore> context_;
};
-TEST_F(ServiceWorkerDiskCacheMigratorTest, Basic) {
+TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateDiskCache) {
std::vector<ResponseData> responses;
responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", ""));
responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", ""));
@@ -194,24 +207,44 @@ TEST_F(ServiceWorkerDiskCacheMigratorTest, Basic) {
20, "HTTP/1.1 200 OK\0\0", std::string(256, 'a'), std::string(128, 'b')));
// Populate initial data in the src diskcache.
+ scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
for (const ResponseData& response : responses) {
- ASSERT_TRUE(WriteResponse(src_.get(), response));
- VerifyResponse(src_.get(), response);
+ ASSERT_TRUE(WriteResponse(src.get(), response));
+ VerifyResponse(src.get(), response);
}
- ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src_.get()));
+ ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
- Migrate();
+ scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache());
+
+ // Start the migrator.
+ base::RunLoop run_loop;
+ scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator(
+ new ServiceWorkerDiskCacheMigrator(src.get(), dest.get()));
+ migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure()));
+ run_loop.Run();
// Verify the migrated contents in the dest diskcache.
for (const ResponseData& response : responses)
- VerifyResponse(dest_.get(), response);
- EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest_.get()));
+ VerifyResponse(dest.get(), response);
+ EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest.get()));
}
TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateEmptyDiskCache) {
- ASSERT_EQ(0, GetEntryCount(src_.get()));
- Migrate();
- EXPECT_EQ(0, GetEntryCount(dest_.get()));
+ scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
+ scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache());
+
+ ASSERT_EQ(0, GetEntryCount(src.get()));
+ ASSERT_EQ(0, GetEntryCount(dest.get()));
+
+ // Start the migrator.
+ base::RunLoop run_loop;
+ scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator(
+ new ServiceWorkerDiskCacheMigrator(src.get(), dest.get()));
+ migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure()));
+ run_loop.Run();
+
+ ASSERT_EQ(0, GetEntryCount(src.get()));
+ ASSERT_EQ(0, GetEntryCount(dest.get()));
}
TEST_F(ServiceWorkerDiskCacheMigratorTest, ThrottleInflightTasks) {
@@ -220,22 +253,93 @@ TEST_F(ServiceWorkerDiskCacheMigratorTest, ThrottleInflightTasks) {
responses.push_back(ResponseData(i, "HTTP/1.1 200 OK\0\0", "foo", "bar"));
// Populate initial data in the src diskcache.
+ scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
for (const ResponseData& response : responses) {
- ASSERT_TRUE(WriteResponse(src_.get(), response));
- VerifyResponse(src_.get(), response);
+ ASSERT_TRUE(WriteResponse(src.get(), response));
+ VerifyResponse(src.get(), response);
}
- ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src_.get()));
+ ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
+
+ scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache());
+ scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator(
+ new ServiceWorkerDiskCacheMigrator(src.get(), dest.get()));
// Tighten the max number of inflight tasks.
- SetMaxNumberOfInflightTasks(2);
+ migrator->set_max_number_of_inflight_tasks(2);
// Migration should hit the limit, but should successfully complete.
- Migrate();
+ base::RunLoop run_loop;
+ migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure()));
+ run_loop.Run();
// Verify the migrated contents in the dest diskcache.
for (const ResponseData& response : responses)
- VerifyResponse(dest_.get(), response);
- EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest_.get()));
+ VerifyResponse(dest.get(), response);
+ EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest.get()));
+}
+
+TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateOnDiskCacheAccess) {
+ std::vector<ResponseData> responses;
+ responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", ""));
+ responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", ""));
+ responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", ""));
+ responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta"));
+
+ // Populate initial data in the src diskcache.
+ scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
+ for (const ResponseData& response : responses) {
+ ASSERT_TRUE(WriteResponse(src.get(), response));
+ VerifyResponse(src.get(), response);
+ }
+ ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
+ ASSERT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
+ src.reset();
+
+ base::RunLoop run_loop;
+ storage()->LazyInitialize(run_loop.QuitClosure());
+ run_loop.Run();
+
+ // DiskCache access should start the migration.
+ ASSERT_TRUE(storage()->needs_disk_cache_migration_);
+ ServiceWorkerDiskCache* dest = storage()->disk_cache();
+
+ // Verify the migrated contents in the dest diskcache.
+ for (const ResponseData& response : responses)
+ VerifyResponse(dest, response);
+ EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest));
+
+ // The src DiskCache should be deleted after the migration.
+ EXPECT_FALSE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
+}
+
+TEST_F(ServiceWorkerDiskCacheMigratorTest, NotMigrateOnDatabaseAccess) {
+ std::vector<ResponseData> responses;
+ responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", ""));
+ responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", ""));
+ responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", ""));
+ responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta"));
+
+ // Populate initial data in the src diskcache.
+ scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
+ for (const ResponseData& response : responses) {
+ ASSERT_TRUE(WriteResponse(src.get(), response));
+ VerifyResponse(src.get(), response);
+ }
+ ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
+ ASSERT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
+
+ // Database access should not start the migration.
+ base::RunLoop run_loop;
+ storage()->FindRegistrationForDocument(
+ GURL("http://example.com/"),
+ base::Bind(&OnRegistrationFound, run_loop.QuitClosure()));
+ run_loop.Run();
+
+ // Verify that the migration didn't happen.
+ scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache());
+ EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
+ EXPECT_EQ(0, GetEntryCount(dest.get()));
+ EXPECT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698