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

Side by Side Diff: chrome/browser/sync_file_system/drive_backend/drive_backend_sync_unittest.cc

Issue 306813002: [SyncFS] Make routines using metadata database async (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/sync_file_system/drive_backend/sync_engine.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm> 5 #include <algorithm>
6 #include <stack> 6 #include <stack>
7 7
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "chrome/browser/drive/drive_uploader.h" 11 #include "chrome/browser/drive/drive_uploader.h"
12 #include "chrome/browser/drive/fake_drive_service.h" 12 #include "chrome/browser/drive/fake_drive_service.h"
13 #include "chrome/browser/drive/test_util.h" 13 #include "chrome/browser/drive/test_util.h"
14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants. h" 14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants. h"
15 #include "chrome/browser/sync_file_system/drive_backend/fake_drive_service_helpe r.h" 15 #include "chrome/browser/sync_file_system/drive_backend/fake_drive_service_helpe r.h"
16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h" 16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" 17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
18 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h" 18 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h"
19 #include "chrome/browser/sync_file_system/drive_backend/sync_worker.h"
19 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h" 20 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
20 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" 21 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
21 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h" 22 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h"
22 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" 23 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
23 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" 24 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
24 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" 25 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
25 #include "chrome/test/base/testing_profile.h" 26 #include "chrome/test/base/testing_profile.h"
26 #include "content/public/test/test_browser_thread.h" 27 #include "content/public/test/test_browser_thread.h"
27 #include "content/public/test/test_browser_thread_bundle.h" 28 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "extensions/common/extension.h" 29 #include "extensions/common/extension.h"
29 #include "google_apis/drive/drive_api_parser.h" 30 #include "google_apis/drive/drive_api_parser.h"
30 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
31 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" 32 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
32 #include "third_party/leveldatabase/src/include/leveldb/env.h" 33 #include "third_party/leveldatabase/src/include/leveldb/env.h"
33 #include "webkit/browser/fileapi/file_system_context.h" 34 #include "webkit/browser/fileapi/file_system_context.h"
34 35
35 #define FPL(a) FILE_PATH_LITERAL(a) 36 #define FPL(a) FILE_PATH_LITERAL(a)
36 37
37 namespace sync_file_system { 38 namespace sync_file_system {
38 namespace drive_backend { 39 namespace drive_backend {
39 40
40 typedef fileapi::FileSystemOperation::FileEntryList FileEntryList; 41 typedef fileapi::FileSystemOperation::FileEntryList FileEntryList;
41 42
42 namespace { 43 namespace {
43 44
44 void SetSyncStatus(const base::Closure& closure, 45 template <typename T>
45 SyncStatusCode* status_out, 46 void SetValueAndCallClosure(const base::Closure& closure,
46 SyncStatusCode status) { 47 T* arg_out,
47 *status_out = status; 48 T arg) {
49 *arg_out = base::internal::CallbackForward(arg);
48 closure.Run(); 50 closure.Run();
49 } 51 }
50 52
51 void SetSyncStatusAndUrl(const base::Closure& closure, 53 void SetSyncStatusAndUrl(const base::Closure& closure,
52 SyncStatusCode* status_out, 54 SyncStatusCode* status_out,
53 fileapi::FileSystemURL* url_out, 55 fileapi::FileSystemURL* url_out,
54 SyncStatusCode status, 56 SyncStatusCode status,
55 const fileapi::FileSystemURL& url) { 57 const fileapi::FileSystemURL& url) {
56 *status_out = status; 58 *status_out = status;
57 *url_out = url; 59 *url_out = url;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 } 153 }
152 154
153 fileapi::FileSystemURL CreateURL(const std::string& app_id, 155 fileapi::FileSystemURL CreateURL(const std::string& app_id,
154 const base::FilePath& path) { 156 const base::FilePath& path) {
155 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id); 157 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
156 return CreateSyncableFileSystemURL(origin, path); 158 return CreateSyncableFileSystemURL(origin, path);
157 } 159 }
158 160
159 bool GetAppRootFolderID(const std::string& app_id, 161 bool GetAppRootFolderID(const std::string& app_id,
160 std::string* folder_id) { 162 std::string* folder_id) {
163 base::RunLoop run_loop;
164 bool success = false;
161 FileTracker tracker; 165 FileTracker tracker;
162 if (!metadata_database()->FindAppRootTracker(app_id, &tracker)) 166 PostTaskAndReplyWithResult(
167 worker_task_runner_,
168 FROM_HERE,
169 base::Bind(&MetadataDatabase::FindAppRootTracker,
170 base::Unretained(metadata_database()),
171 app_id,
172 &tracker),
173 base::Bind(&SetValueAndCallClosure<bool>,
174 run_loop.QuitClosure(),
175 &success));
176 run_loop.Run();
177 if (!success)
163 return false; 178 return false;
164 *folder_id = tracker.file_id(); 179 *folder_id = tracker.file_id();
165 return true; 180 return true;
166 } 181 }
167 182
168 std::string GetFileIDByPath(const std::string& app_id, 183 std::string GetFileIDByPath(const std::string& app_id,
169 const base::FilePath::StringType& path) { 184 const base::FilePath::StringType& path) {
170 return GetFileIDByPath(app_id, base::FilePath(path)); 185 return GetFileIDByPath(app_id, base::FilePath(path));
171 } 186 }
172 187
173 std::string GetFileIDByPath(const std::string& app_id, 188 std::string GetFileIDByPath(const std::string& app_id,
174 const base::FilePath& path) { 189 const base::FilePath& path) {
190 base::RunLoop run_loop;
191 bool success = false;
175 FileTracker tracker; 192 FileTracker tracker;
176 base::FilePath result_path; 193 base::FilePath result_path;
177 base::FilePath normalized_path = path.NormalizePathSeparators(); 194 base::FilePath normalized_path = path.NormalizePathSeparators();
178 EXPECT_TRUE(metadata_database()->FindNearestActiveAncestor( 195 PostTaskAndReplyWithResult(
179 app_id, normalized_path, &tracker, &result_path)); 196 worker_task_runner_,
197 FROM_HERE,
198 base::Bind(&MetadataDatabase::FindNearestActiveAncestor,
199 base::Unretained(metadata_database()),
200 app_id,
201 normalized_path,
202 &tracker,
203 &result_path),
204 base::Bind(&SetValueAndCallClosure<bool>,
205 run_loop.QuitClosure(),
206 &success));
207 run_loop.Run();
208 EXPECT_TRUE(success);
180 EXPECT_EQ(normalized_path, result_path); 209 EXPECT_EQ(normalized_path, result_path);
181 return tracker.file_id(); 210 return tracker.file_id();
182 } 211 }
183 212
184 SyncStatusCode RegisterApp(const std::string& app_id) { 213 SyncStatusCode RegisterApp(const std::string& app_id) {
185 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id); 214 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
186 if (!ContainsKey(file_systems_, app_id)) { 215 if (!ContainsKey(file_systems_, app_id)) {
187 CannedSyncableFileSystem* file_system = new CannedSyncableFileSystem( 216 CannedSyncableFileSystem* file_system = new CannedSyncableFileSystem(
188 origin, in_memory_env_.get(), 217 origin, in_memory_env_.get(),
189 io_task_runner_.get(), file_task_runner_.get()); 218 io_task_runner_.get(), file_task_runner_.get());
190 file_system->SetUp(CannedSyncableFileSystem::QUOTA_DISABLED); 219 file_system->SetUp(CannedSyncableFileSystem::QUOTA_DISABLED);
191 220
192 SyncStatusCode status = SYNC_STATUS_UNKNOWN; 221 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
193 base::RunLoop run_loop; 222 base::RunLoop run_loop;
194 local_sync_service_->MaybeInitializeFileSystemContext( 223 local_sync_service_->MaybeInitializeFileSystemContext(
195 origin, file_system->file_system_context(), 224 origin, file_system->file_system_context(),
196 base::Bind(&SetSyncStatus, run_loop.QuitClosure(), &status)); 225 base::Bind(&SetValueAndCallClosure<SyncStatusCode>,
226 run_loop.QuitClosure(), &status));
197 run_loop.Run(); 227 run_loop.Run();
198 EXPECT_EQ(SYNC_STATUS_OK, status); 228 EXPECT_EQ(SYNC_STATUS_OK, status);
199 229
200 file_system->backend()->sync_context()-> 230 file_system->backend()->sync_context()->
201 set_mock_notify_changes_duration_in_sec(0); 231 set_mock_notify_changes_duration_in_sec(0);
202 232
203 EXPECT_EQ(base::File::FILE_OK, file_system->OpenFileSystem()); 233 EXPECT_EQ(base::File::FILE_OK, file_system->OpenFileSystem());
204 file_systems_[app_id] = file_system; 234 file_systems_[app_id] = file_system;
205 } 235 }
206 236
207 SyncStatusCode status = SYNC_STATUS_UNKNOWN; 237 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
208 base::RunLoop run_loop; 238 base::RunLoop run_loop;
209 remote_sync_service_->RegisterOrigin( 239 remote_sync_service_->RegisterOrigin(
210 origin, 240 origin,
211 base::Bind(&SetSyncStatus, run_loop.QuitClosure(), &status)); 241 base::Bind(&SetValueAndCallClosure<SyncStatusCode>,
242 run_loop.QuitClosure(), &status));
212 run_loop.Run(); 243 run_loop.Run();
213 return status; 244 return status;
214 } 245 }
215 246
216 void AddLocalFolder(const std::string& app_id, 247 void AddLocalFolder(const std::string& app_id,
217 const base::FilePath::StringType& path) { 248 const base::FilePath::StringType& path) {
218 ASSERT_TRUE(ContainsKey(file_systems_, app_id)); 249 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
219 EXPECT_EQ(base::File::FILE_OK, 250 EXPECT_EQ(base::File::FILE_OK,
220 file_systems_[app_id]->CreateDirectory( 251 file_systems_[app_id]->CreateDirectory(
221 CreateURL(app_id, path))); 252 CreateURL(app_id, path)));
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 return remote_sync_status; 334 return remote_sync_status;
304 335
305 if (local_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC && 336 if (local_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC &&
306 remote_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC) { 337 remote_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC) {
307 remote_sync_service_->PromoteDemotedChanges(); 338 remote_sync_service_->PromoteDemotedChanges();
308 local_sync_service_->PromoteDemotedChanges(); 339 local_sync_service_->PromoteDemotedChanges();
309 340
310 if (pending_remote_changes_ || pending_local_changes_) 341 if (pending_remote_changes_ || pending_local_changes_)
311 continue; 342 continue;
312 343
313 int64 largest_fetched_change_id = 344 base::RunLoop run_loop;
314 metadata_database()->GetLargestFetchedChangeID(); 345 int64 largest_fetched_change_id = -1;
346 PostTaskAndReplyWithResult(
347 worker_task_runner_,
348 FROM_HERE,
349 base::Bind(&MetadataDatabase::GetLargestFetchedChangeID,
350 base::Unretained(metadata_database())),
351 base::Bind(&SetValueAndCallClosure<int64>,
352 run_loop.QuitClosure(),
353 &largest_fetched_change_id));
354 run_loop.Run();
315 if (largest_fetched_change_id != GetLargestChangeID()) { 355 if (largest_fetched_change_id != GetLargestChangeID()) {
316 FetchRemoteChanges(); 356 FetchRemoteChanges();
317 continue; 357 continue;
318 } 358 }
319 break; 359 break;
320 } 360 }
321 } 361 }
322 return SYNC_STATUS_OK; 362 return SYNC_STATUS_OK;
323 } 363 }
324 364
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 const base::FilePath::StringType& path) { 520 const base::FilePath::StringType& path) {
481 SCOPED_TRACE(testing::Message() << "Verifying local file: " 521 SCOPED_TRACE(testing::Message() << "Verifying local file: "
482 << "app_id = " << app_id 522 << "app_id = " << app_id
483 << ", path = " << path); 523 << ", path = " << path);
484 ASSERT_TRUE(ContainsKey(file_systems_, app_id)); 524 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
485 EXPECT_EQ(base::File::FILE_OK, 525 EXPECT_EQ(base::File::FILE_OK,
486 file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path))); 526 file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path)));
487 } 527 }
488 528
489 size_t CountMetadata() { 529 size_t CountMetadata() {
490 return metadata_database()->CountFileMetadata(); 530 size_t count = 0;
531 base::RunLoop run_loop;
532 PostTaskAndReplyWithResult(
533 worker_task_runner_,
534 FROM_HERE,
535 base::Bind(&MetadataDatabase::CountFileMetadata,
536 base::Unretained(metadata_database())),
537 base::Bind(&SetValueAndCallClosure<size_t>,
538 run_loop.QuitClosure(),
539 &count));
540 run_loop.Run();
541 return count;
491 } 542 }
492 543
493 size_t CountTracker() { 544 size_t CountTracker() {
494 return metadata_database()->CountFileTracker(); 545 size_t count = 0;
546 base::RunLoop run_loop;
547 PostTaskAndReplyWithResult(
548 worker_task_runner_,
549 FROM_HERE,
550 base::Bind(&MetadataDatabase::CountFileTracker,
551 base::Unretained(metadata_database())),
552 base::Bind(&SetValueAndCallClosure<size_t>,
553 run_loop.QuitClosure(), &count));
554 run_loop.Run();
555 return count;
495 } 556 }
496 557
497 drive::FakeDriveService* fake_drive_service() { 558 drive::FakeDriveService* fake_drive_service() {
498 return static_cast<drive::FakeDriveService*>( 559 return static_cast<drive::FakeDriveService*>(
499 remote_sync_service_->GetDriveService()); 560 remote_sync_service_->GetDriveService());
500 } 561 }
501 562
502 FakeDriveServiceHelper* fake_drive_service_helper() { 563 FakeDriveServiceHelper* fake_drive_service_helper() {
503 return fake_drive_service_helper_.get(); 564 return fake_drive_service_helper_.get();
504 } 565 }
505 566
567 private:
568 // NOTE: Member functions of MetadataDatabase class must not be called
569 // directly through this method. Call them via PostTask.
506 MetadataDatabase* metadata_database() { 570 MetadataDatabase* metadata_database() {
507 return remote_sync_service_->GetMetadataDatabase(); 571 return remote_sync_service_->sync_worker_->GetMetadataDatabase();
508 } 572 }
509 573
510 private:
511 content::TestBrowserThreadBundle thread_bundle_; 574 content::TestBrowserThreadBundle thread_bundle_;
512 575
513 base::ScopedTempDir base_dir_; 576 base::ScopedTempDir base_dir_;
514 scoped_ptr<leveldb::Env> in_memory_env_; 577 scoped_ptr<leveldb::Env> in_memory_env_;
515 TestingProfile profile_; 578 TestingProfile profile_;
516 579
517 scoped_ptr<SyncEngine> remote_sync_service_; 580 scoped_ptr<SyncEngine> remote_sync_service_;
518 scoped_ptr<LocalFileSyncService> local_sync_service_; 581 scoped_ptr<LocalFileSyncService> local_sync_service_;
519 582
520 int64 pending_remote_changes_; 583 int64 pending_remote_changes_;
(...skipping 1089 matching lines...) Expand 10 before | Expand all | Expand 10 after
1610 1673
1611 EXPECT_EQ(1u, CountApp()); 1674 EXPECT_EQ(1u, CountApp());
1612 EXPECT_EQ(1u, CountLocalFile(app_id)); 1675 EXPECT_EQ(1u, CountLocalFile(app_id));
1613 1676
1614 EXPECT_EQ(2u, CountMetadata()); 1677 EXPECT_EQ(2u, CountMetadata());
1615 EXPECT_EQ(2u, CountTracker()); 1678 EXPECT_EQ(2u, CountTracker());
1616 } 1679 }
1617 1680
1618 } // namespace drive_backend 1681 } // namespace drive_backend
1619 } // namespace sync_file_system 1682 } // namespace sync_file_system
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/sync_file_system/drive_backend/sync_engine.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698