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