| 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 5bb9649864d7899fd6a1edb75be7677f54442eab..262f62056f47d9c53b006aa10ff8759490d2cfa0 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
|
| @@ -45,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 {
|
| @@ -69,13 +76,6 @@ class ServiceWorkerDiskCacheMigratorTest : public testing::Test {
|
| base::RunLoop().RunUntilIdle();
|
| }
|
|
|
| - base::FilePath GetOldDiskCachePath() {
|
| - return user_data_directory_.path().AppendASCII("SrcCache");
|
| - }
|
| - base::FilePath GetDiskCachePath() {
|
| - return user_data_directory_.path().AppendASCII("DestCache");
|
| - }
|
| -
|
| scoped_ptr<ServiceWorkerDiskCache> CreateSrcDiskCache() {
|
| #if defined(OS_ANDROID)
|
| // Android has already used the Simple backend.
|
| @@ -88,7 +88,7 @@ class ServiceWorkerDiskCacheMigratorTest : public testing::Test {
|
|
|
| net::TestCompletionCallback cb;
|
| src->InitWithDiskBackend(
|
| - GetOldDiskCachePath(), kMaxDiskCacheSize, false /* force */,
|
| + storage()->GetOldDiskCachePath(), kMaxDiskCacheSize, false /* force */,
|
| base::ThreadTaskRunnerHandle::Get(), cb.callback());
|
| EXPECT_EQ(net::OK, cb.WaitForResult());
|
| return src.Pass();
|
| @@ -99,7 +99,7 @@ class ServiceWorkerDiskCacheMigratorTest : public testing::Test {
|
| ServiceWorkerDiskCache::CreateWithSimpleBackend());
|
| net::TestCompletionCallback cb;
|
| dest->InitWithDiskBackend(
|
| - GetDiskCachePath(), kMaxDiskCacheSize, false /* force */,
|
| + storage()->GetDiskCachePath(), kMaxDiskCacheSize, false /* force */,
|
| base::ThreadTaskRunnerHandle::Get(), cb.callback());
|
| EXPECT_EQ(net::OK, cb.WaitForResult());
|
| return dest.Pass();
|
| @@ -107,8 +107,8 @@ class ServiceWorkerDiskCacheMigratorTest : public testing::Test {
|
|
|
| scoped_ptr<ServiceWorkerDiskCacheMigrator> CreateMigrator() {
|
| return make_scoped_ptr(new ServiceWorkerDiskCacheMigrator(
|
| - GetOldDiskCachePath(), GetDiskCachePath(), kMaxDiskCacheSize,
|
| - base::ThreadTaskRunnerHandle::Get()));
|
| + storage()->GetOldDiskCachePath(), storage()->GetDiskCachePath(),
|
| + kMaxDiskCacheSize, base::ThreadTaskRunnerHandle::Get()));
|
| }
|
|
|
| bool WriteResponse(ServiceWorkerDiskCache* disk_cache,
|
| @@ -331,4 +331,174 @@ TEST_F(ServiceWorkerDiskCacheMigratorTest, ThrottleInflightTasks) {
|
| 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 resources 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();
|
| +
|
| + // Write something in the database to prevent the storage from bypassing the
|
| + // diskcache migration.
|
| + scoped_ptr<ServiceWorkerDatabase> database(
|
| + new ServiceWorkerDatabase(storage()->GetDatabasePath()));
|
| + std::vector<ServiceWorkerDatabase::ResourceRecord> resources;
|
| + resources.push_back(ServiceWorkerDatabase::ResourceRecord(
|
| + 1, GURL("https://example.com/foo"), 10));
|
| + ServiceWorkerDatabase::RegistrationData deleted_version;
|
| + std::vector<int64> newly_purgeable_resources;
|
| + ServiceWorkerDatabase::RegistrationData data;
|
| + data.registration_id = 100;
|
| + data.scope = GURL("https://example.com/");
|
| + data.script = GURL("https://example.com/script.js");
|
| + data.version_id = 200;
|
| + data.resources_total_size_bytes = 10;
|
| + ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
|
| + database->WriteRegistration(data, resources, &deleted_version,
|
| + &newly_purgeable_resources));
|
| + database.reset();
|
| +
|
| + // LazyInitialize() reads initial data and should schedule to migrate.
|
| + ASSERT_EQ(ServiceWorkerDatabase::DISKCACHE_UNKNOWN_STATE,
|
| + storage()->disk_cache_migration_state_);
|
| + base::RunLoop run_loop;
|
| + storage()->LazyInitialize(run_loop.QuitClosure());
|
| + run_loop.Run();
|
| + EXPECT_EQ(ServiceWorkerDatabase::DISKCACHE_NEEDS_TO_MIGRATE,
|
| + storage()->disk_cache_migration_state_);
|
| +
|
| + // DiskCache access should start the 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));
|
| +
|
| + // After the migration, the src diskcache should be deleted.
|
| + EXPECT_FALSE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
|
| +
|
| + // After the migration, the migration state should be updated.
|
| + ServiceWorkerDatabase::DiskCacheMigrationState state =
|
| + ServiceWorkerDatabase::DISKCACHE_UNKNOWN_STATE;
|
| + EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
|
| + storage()->database_->ReadDiskCacheMigrationState(&state));
|
| + EXPECT_EQ(ServiceWorkerDatabase::DISKCACHE_MIGRATED, state);
|
| +}
|
| +
|
| +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 resources 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()));
|
| +
|
| + // Write something in the database to prevent the storage from bypassing the
|
| + // diskcache migration.
|
| + scoped_ptr<ServiceWorkerDatabase> database(
|
| + new ServiceWorkerDatabase(storage()->GetDatabasePath()));
|
| + std::vector<ServiceWorkerDatabase::ResourceRecord> resources;
|
| + resources.push_back(ServiceWorkerDatabase::ResourceRecord(
|
| + 1, GURL("https://example.com/foo"), 10));
|
| + ServiceWorkerDatabase::RegistrationData deleted_version;
|
| + std::vector<int64> newly_purgeable_resources;
|
| + ServiceWorkerDatabase::RegistrationData data;
|
| + data.registration_id = 100;
|
| + data.scope = GURL("https://example.com/");
|
| + data.script = GURL("https://example.com/script.js");
|
| + data.version_id = 200;
|
| + data.resources_total_size_bytes = 10;
|
| + ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
|
| + database->WriteRegistration(data, resources, &deleted_version,
|
| + &newly_purgeable_resources));
|
| + database.reset();
|
| +
|
| + // LazyInitialize() reads initial data and should schedule to migrate.
|
| + ASSERT_EQ(ServiceWorkerDatabase::DISKCACHE_UNKNOWN_STATE,
|
| + storage()->disk_cache_migration_state_);
|
| + base::RunLoop run_loop1;
|
| + storage()->LazyInitialize(run_loop1.QuitClosure());
|
| + run_loop1.Run();
|
| + EXPECT_EQ(ServiceWorkerDatabase::DISKCACHE_NEEDS_TO_MIGRATE,
|
| + storage()->disk_cache_migration_state_);
|
| +
|
| + // Database access should not start the migration.
|
| + base::RunLoop run_loop2;
|
| + storage()->FindRegistrationForDocument(
|
| + GURL("http://example.com/"),
|
| + base::Bind(&OnRegistrationFound, run_loop2.QuitClosure()));
|
| + run_loop2.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()));
|
| +}
|
| +
|
| +TEST_F(ServiceWorkerDiskCacheMigratorTest, NotMigrateForEmptyDatabase) {
|
| + 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 resources 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();
|
| +
|
| + // LazyInitialize() reads initial data and should not schedule to migrate
|
| + // because the database is empty.
|
| + ASSERT_EQ(ServiceWorkerDatabase::DISKCACHE_UNKNOWN_STATE,
|
| + storage()->disk_cache_migration_state_);
|
| + base::RunLoop run_loop;
|
| + storage()->LazyInitialize(run_loop.QuitClosure());
|
| + run_loop.Run();
|
| + EXPECT_EQ(ServiceWorkerDatabase::DISKCACHE_NOT_USED,
|
| + storage()->disk_cache_migration_state_);
|
| +
|
| + // DiskCache access should not start the migration.
|
| + ServiceWorkerDiskCache* dest = storage()->disk_cache();
|
| +
|
| + // Verify that the migration didn't happen.
|
| + src = CreateSrcDiskCache();
|
| + for (const ResponseData& response : responses)
|
| + VerifyResponse(src.get(), response);
|
| + EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
|
| + EXPECT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
|
| + EXPECT_EQ(0, GetEntryCount(dest));
|
| +
|
| + // After the diskcache initialization, the migration state should be updated.
|
| + ServiceWorkerDatabase::DiskCacheMigrationState state =
|
| + ServiceWorkerDatabase::DISKCACHE_UNKNOWN_STATE;
|
| + EXPECT_EQ(ServiceWorkerDatabase::STATUS_OK,
|
| + storage()->database_->ReadDiskCacheMigrationState(&state));
|
| + EXPECT_EQ(ServiceWorkerDatabase::DISKCACHE_MIGRATED, state);
|
| +}
|
| +
|
| } // namespace content
|
|
|