| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/engine/browser_thread_model_worker.h" | 5 #include "components/sync/engine/sequenced_model_worker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/task_scheduler/post_task.h" |
| 14 #include "base/test/scoped_async_task_scheduler.h" |
| 13 #include "base/test/test_timeouts.h" | 15 #include "base/test/test_timeouts.h" |
| 14 #include "base/threading/thread.h" | 16 #include "base/threading/sequenced_task_runner_handle.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | |
| 16 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 using base::Thread; | |
| 20 using base::TimeDelta; | |
| 21 | |
| 22 namespace syncer { | 20 namespace syncer { |
| 23 | 21 |
| 24 namespace { | 22 namespace { |
| 25 | 23 |
| 26 class SyncBrowserThreadModelWorkerTest : public testing::Test { | 24 class SequencedModelWorkerTest : public testing::Test { |
| 27 public: | 25 public: |
| 28 SyncBrowserThreadModelWorkerTest() | 26 SequencedModelWorkerTest() : did_do_work_(false), weak_factory_(this) {} |
| 29 : db_thread_("DB_Thread"), did_do_work_(false), weak_factory_(this) {} | |
| 30 | 27 |
| 31 bool did_do_work() { return did_do_work_; } | 28 bool did_do_work() { return did_do_work_; } |
| 32 BrowserThreadModelWorker* worker() { return worker_.get(); } | 29 SequencedModelWorker* worker() { return worker_.get(); } |
| 33 base::OneShotTimer* timer() { return &timer_; } | 30 base::OneShotTimer* timer() { return &timer_; } |
| 34 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest>* factory() { | 31 base::WeakPtrFactory<SequencedModelWorkerTest>* factory() { |
| 35 return &weak_factory_; | 32 return &weak_factory_; |
| 36 } | 33 } |
| 37 | 34 |
| 38 // Schedule DoWork to be executed on the DB thread and have the test fail if | 35 // Schedule DoWork to be executed on the DB sequence and have the test fail if |
| 39 // DoWork hasn't executed within action_timeout(). | 36 // DoWork hasn't executed within action_timeout(). |
| 40 void ScheduleWork() { | 37 void ScheduleWork() { |
| 41 // We wait until the callback is done. So it is safe to use unretained. | 38 // We wait until the callback is done. So it is safe to use unretained. |
| 42 timer()->Start(FROM_HERE, TestTimeouts::action_timeout(), this, | 39 timer()->Start(FROM_HERE, TestTimeouts::action_timeout(), this, |
| 43 &SyncBrowserThreadModelWorkerTest::Timeout); | 40 &SequencedModelWorkerTest::Timeout); |
| 44 worker()->DoWorkAndWaitUntilDone(base::BindOnce( | 41 worker()->DoWorkAndWaitUntilDone(base::BindOnce( |
| 45 &SyncBrowserThreadModelWorkerTest::DoWork, base::Unretained(this))); | 42 &SequencedModelWorkerTest::DoWork, base::Unretained(this))); |
| 46 } | 43 } |
| 47 | 44 |
| 48 // This is the work that will be scheduled to be done on the DB thread. | 45 // This is the work that will be scheduled to be done on the DB sequence. |
| 49 SyncerError DoWork() { | 46 SyncerError DoWork() { |
| 50 EXPECT_TRUE(db_thread_.task_runner()->BelongsToCurrentThread()); | 47 EXPECT_TRUE(task_runner_->RunsTasksInCurrentSequence()); |
| 51 main_message_loop_.task_runner()->PostTask( | 48 main_message_loop_.task_runner()->PostTask( |
| 52 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 49 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 53 did_do_work_ = true; | 50 did_do_work_ = true; |
| 54 return SYNCER_OK; | 51 return SYNCER_OK; |
| 55 } | 52 } |
| 56 | 53 |
| 57 // This will be called by the OneShotTimer and make the test fail unless | 54 // This will be called by the OneShotTimer and make the test fail unless |
| 58 // DoWork is called first. | 55 // DoWork is called first. |
| 59 void Timeout() { | 56 void Timeout() { |
| 60 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; | 57 ADD_FAILURE() |
| 58 << "Timed out waiting for work to be done on the DB sequence."; |
| 61 main_message_loop_.task_runner()->PostTask( | 59 main_message_loop_.task_runner()->PostTask( |
| 62 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 60 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 63 } | 61 } |
| 64 | 62 |
| 65 protected: | 63 protected: |
| 66 void SetUp() override { | 64 void SetUp() override { |
| 67 db_thread_.Start(); | 65 task_runner_ = base::CreateSequencedTaskRunnerWithTraits( |
| 68 worker_ = new BrowserThreadModelWorker(db_thread_.task_runner(), GROUP_DB); | 66 {base::MayBlock(), base::TaskPriority::BACKGROUND}); |
| 69 } | 67 worker_ = new SequencedModelWorker(task_runner_, GROUP_DB); |
| 70 | |
| 71 virtual void Teardown() { | |
| 72 worker_ = nullptr; | |
| 73 db_thread_.Stop(); | |
| 74 } | 68 } |
| 75 | 69 |
| 76 private: | 70 private: |
| 77 base::MessageLoop main_message_loop_; | 71 base::MessageLoop main_message_loop_; |
| 78 Thread db_thread_; | 72 base::test::ScopedAsyncTaskScheduler task_scheduler_; |
| 79 bool did_do_work_; | 73 bool did_do_work_; |
| 80 scoped_refptr<BrowserThreadModelWorker> worker_; | 74 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 75 scoped_refptr<SequencedModelWorker> worker_; |
| 81 base::OneShotTimer timer_; | 76 base::OneShotTimer timer_; |
| 82 | 77 |
| 83 base::WeakPtrFactory<SyncBrowserThreadModelWorkerTest> weak_factory_; | 78 base::WeakPtrFactory<SequencedModelWorkerTest> weak_factory_; |
| 84 }; | 79 }; |
| 85 | 80 |
| 86 TEST_F(SyncBrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { | 81 TEST_F(SequencedModelWorkerTest, DoesWorkOnDatabaseSequence) { |
| 87 base::ThreadTaskRunnerHandle::Get()->PostTask( | 82 base::SequencedTaskRunnerHandle::Get()->PostTask( |
| 88 FROM_HERE, base::Bind(&SyncBrowserThreadModelWorkerTest::ScheduleWork, | 83 FROM_HERE, base::Bind(&SequencedModelWorkerTest::ScheduleWork, |
| 89 factory()->GetWeakPtr())); | 84 factory()->GetWeakPtr())); |
| 90 base::RunLoop().Run(); | 85 base::RunLoop().Run(); |
| 91 EXPECT_TRUE(did_do_work()); | 86 EXPECT_TRUE(did_do_work()); |
| 92 } | 87 } |
| 93 | 88 |
| 94 } // namespace | 89 } // namespace |
| 95 | 90 |
| 96 } // namespace syncer | 91 } // namespace syncer |
| OLD | NEW |