| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "components/sync_driver/glue/sync_backend_registrar.h" | 5 #include "components/sync_driver/glue/sync_backend_registrar.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "components/sync_driver/change_processor_mock.h" | 10 #include "components/sync_driver/change_processor_mock.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 | 101 |
| 102 ~SyncBackendRegistrarTest() override {} | 102 ~SyncBackendRegistrarTest() override {} |
| 103 | 103 |
| 104 void SetUp() override { | 104 void SetUp() override { |
| 105 db_thread_.StartAndWaitForTesting(); | 105 db_thread_.StartAndWaitForTesting(); |
| 106 file_thread_.StartAndWaitForTesting(); | 106 file_thread_.StartAndWaitForTesting(); |
| 107 test_user_share_.SetUp(); | 107 test_user_share_.SetUp(); |
| 108 sync_client_.reset(new RegistrarSyncClient( | 108 sync_client_.reset(new RegistrarSyncClient( |
| 109 ui_task_runner(), db_task_runner(), file_task_runner())); | 109 ui_task_runner(), db_task_runner(), file_task_runner())); |
| 110 registrar_.reset(new SyncBackendRegistrar( | 110 registrar_.reset(new SyncBackendRegistrar( |
| 111 "test", sync_client_.get(), scoped_ptr<base::Thread>(), | 111 "test", sync_client_.get(), std::unique_ptr<base::Thread>(), |
| 112 ui_task_runner(), db_task_runner(), file_task_runner())); | 112 ui_task_runner(), db_task_runner(), file_task_runner())); |
| 113 sync_thread_ = registrar_->sync_thread(); | 113 sync_thread_ = registrar_->sync_thread(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void TearDown() override { | 116 void TearDown() override { |
| 117 registrar_->RequestWorkerStopOnUIThread(); | 117 registrar_->RequestWorkerStopOnUIThread(); |
| 118 test_user_share_.TearDown(); | 118 test_user_share_.TearDown(); |
| 119 sync_thread_->task_runner()->PostTask( | 119 sync_thread_->task_runner()->PostTask( |
| 120 FROM_HERE, base::Bind(&SyncBackendRegistrar::Shutdown, | 120 FROM_HERE, base::Bind(&SyncBackendRegistrar::Shutdown, |
| 121 base::Unretained(registrar_.release()))); | 121 base::Unretained(registrar_.release()))); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 150 | 150 |
| 151 const scoped_refptr<base::SingleThreadTaskRunner> file_task_runner() { | 151 const scoped_refptr<base::SingleThreadTaskRunner> file_task_runner() { |
| 152 return db_thread_.task_runner(); | 152 return db_thread_.task_runner(); |
| 153 } | 153 } |
| 154 | 154 |
| 155 base::MessageLoop message_loop_; | 155 base::MessageLoop message_loop_; |
| 156 base::Thread db_thread_; | 156 base::Thread db_thread_; |
| 157 base::Thread file_thread_; | 157 base::Thread file_thread_; |
| 158 | 158 |
| 159 syncer::TestUserShare test_user_share_; | 159 syncer::TestUserShare test_user_share_; |
| 160 scoped_ptr<RegistrarSyncClient> sync_client_; | 160 std::unique_ptr<RegistrarSyncClient> sync_client_; |
| 161 scoped_ptr<SyncBackendRegistrar> registrar_; | 161 std::unique_ptr<SyncBackendRegistrar> registrar_; |
| 162 | 162 |
| 163 base::Thread* sync_thread_; | 163 base::Thread* sync_thread_; |
| 164 }; | 164 }; |
| 165 | 165 |
| 166 TEST_F(SyncBackendRegistrarTest, ConstructorEmpty) { | 166 TEST_F(SyncBackendRegistrarTest, ConstructorEmpty) { |
| 167 registrar_->SetInitialTypes(ModelTypeSet()); | 167 registrar_->SetInitialTypes(ModelTypeSet()); |
| 168 EXPECT_FALSE(registrar_->IsNigoriEnabled()); | 168 EXPECT_FALSE(registrar_->IsNigoriEnabled()); |
| 169 { | 169 { |
| 170 std::vector<scoped_refptr<syncer::ModelSafeWorker> > workers; | 170 std::vector<scoped_refptr<syncer::ModelSafeWorker> > workers; |
| 171 registrar_->GetWorkers(&workers); | 171 registrar_->GetWorkers(&workers); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 } | 351 } |
| 352 | 352 |
| 353 const scoped_refptr<base::SingleThreadTaskRunner> file_task_runner() { | 353 const scoped_refptr<base::SingleThreadTaskRunner> file_task_runner() { |
| 354 return file_thread_.task_runner(); | 354 return file_thread_.task_runner(); |
| 355 } | 355 } |
| 356 | 356 |
| 357 base::MessageLoop message_loop_; | 357 base::MessageLoop message_loop_; |
| 358 base::Thread db_thread_; | 358 base::Thread db_thread_; |
| 359 base::Thread file_thread_; | 359 base::Thread file_thread_; |
| 360 | 360 |
| 361 scoped_ptr<RegistrarSyncClient> sync_client_; | 361 std::unique_ptr<RegistrarSyncClient> sync_client_; |
| 362 base::WaitableEvent db_thread_blocked_; | 362 base::WaitableEvent db_thread_blocked_; |
| 363 | 363 |
| 364 base::Lock db_thread_lock_; | 364 base::Lock db_thread_lock_; |
| 365 base::RunLoop run_loop_; | 365 base::RunLoop run_loop_; |
| 366 base::Closure quit_closure_; | 366 base::Closure quit_closure_; |
| 367 }; | 367 }; |
| 368 | 368 |
| 369 // Wrap SyncBackendRegistrar so that we can monitor its lifetime. | 369 // Wrap SyncBackendRegistrar so that we can monitor its lifetime. |
| 370 class TestRegistrar : public SyncBackendRegistrar { | 370 class TestRegistrar : public SyncBackendRegistrar { |
| 371 public: | 371 public: |
| 372 explicit TestRegistrar( | 372 explicit TestRegistrar( |
| 373 sync_driver::SyncClient* sync_client, | 373 sync_driver::SyncClient* sync_client, |
| 374 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, | 374 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, |
| 375 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread, | 375 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread, |
| 376 const scoped_refptr<base::SingleThreadTaskRunner>& file_thread, | 376 const scoped_refptr<base::SingleThreadTaskRunner>& file_thread, |
| 377 SyncBackendRegistrarShutdownTest* test) | 377 SyncBackendRegistrarShutdownTest* test) |
| 378 : SyncBackendRegistrar("test", | 378 : SyncBackendRegistrar("test", |
| 379 sync_client, | 379 sync_client, |
| 380 scoped_ptr<base::Thread>(), | 380 std::unique_ptr<base::Thread>(), |
| 381 ui_thread, | 381 ui_thread, |
| 382 db_thread, | 382 db_thread, |
| 383 file_thread), | 383 file_thread), |
| 384 test_(test) {} | 384 test_(test) {} |
| 385 | 385 |
| 386 ~TestRegistrar() override { test_->PostQuitOnUIMessageLoop(); } | 386 ~TestRegistrar() override { test_->PostQuitOnUIMessageLoop(); } |
| 387 | 387 |
| 388 private: | 388 private: |
| 389 SyncBackendRegistrarShutdownTest* test_; | 389 SyncBackendRegistrarShutdownTest* test_; |
| 390 }; | 390 }; |
| 391 | 391 |
| 392 TEST_F(SyncBackendRegistrarShutdownTest, BlockingShutdown) { | 392 TEST_F(SyncBackendRegistrarShutdownTest, BlockingShutdown) { |
| 393 // Take ownership of |db_thread_lock_| so that the DB thread can't acquire it. | 393 // Take ownership of |db_thread_lock_| so that the DB thread can't acquire it. |
| 394 db_thread_lock_.Acquire(); | 394 db_thread_lock_.Acquire(); |
| 395 | 395 |
| 396 // This will block the DB thread by waiting on |db_thread_lock_|. | 396 // This will block the DB thread by waiting on |db_thread_lock_|. |
| 397 db_task_runner()->PostTask( | 397 db_task_runner()->PostTask( |
| 398 FROM_HERE, base::Bind(&SyncBackendRegistrarShutdownTest::BlockDBThread, | 398 FROM_HERE, base::Bind(&SyncBackendRegistrarShutdownTest::BlockDBThread, |
| 399 base::Unretained(this))); | 399 base::Unretained(this))); |
| 400 | 400 |
| 401 scoped_ptr<TestRegistrar> registrar( | 401 std::unique_ptr<TestRegistrar> registrar( |
| 402 new TestRegistrar(sync_client_.get(), ui_task_runner(), db_task_runner(), | 402 new TestRegistrar(sync_client_.get(), ui_task_runner(), db_task_runner(), |
| 403 file_task_runner(), this)); | 403 file_task_runner(), this)); |
| 404 base::Thread* sync_thread = registrar->sync_thread(); | 404 base::Thread* sync_thread = registrar->sync_thread(); |
| 405 | 405 |
| 406 // Stop here until the DB thread gets a chance to run and block on the lock. | 406 // Stop here until the DB thread gets a chance to run and block on the lock. |
| 407 // Please note that since the task above didn't finish, the task to | 407 // Please note that since the task above didn't finish, the task to |
| 408 // initialize the worker on the DB thread hasn't had a chance to run yet too. | 408 // initialize the worker on the DB thread hasn't had a chance to run yet too. |
| 409 // Which means ModelSafeWorker::SetWorkingLoopToCurrent hasn't been called | 409 // Which means ModelSafeWorker::SetWorkingLoopToCurrent hasn't been called |
| 410 // for the DB worker. | 410 // for the DB worker. |
| 411 db_thread_blocked_.Wait(); | 411 db_thread_blocked_.Wait(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 429 db_thread_lock_.Release(); | 429 db_thread_lock_.Release(); |
| 430 | 430 |
| 431 // Run the main thread loop until all workers have been removed and the | 431 // Run the main thread loop until all workers have been removed and the |
| 432 // registrar destroyed. | 432 // registrar destroyed. |
| 433 run_loop_.Run(); | 433 run_loop_.Run(); |
| 434 } | 434 } |
| 435 | 435 |
| 436 } // namespace | 436 } // namespace |
| 437 | 437 |
| 438 } // namespace browser_sync | 438 } // namespace browser_sync |
| OLD | NEW |