| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/bind.h" |
| 5 #include "base/callback.h" | 6 #include "base/callback.h" |
| 6 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 8 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
| 9 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
| 10 #include "base/timer.h" | 11 #include "base/timer.h" |
| 11 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" | 12 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" |
| 13 #include "chrome/browser/sync/util/unrecoverable_error_info.h" |
| 12 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 16 |
| 15 using base::OneShotTimer; | 17 using base::OneShotTimer; |
| 16 using base::Thread; | 18 using base::Thread; |
| 17 using base::TimeDelta; | 19 using base::TimeDelta; |
| 18 | 20 |
| 19 namespace browser_sync { | 21 namespace browser_sync { |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 class BrowserThreadModelWorkerTest : public testing::Test { | 25 class BrowserThreadModelWorkerTest : public testing::Test { |
| 24 public: | 26 public: |
| 25 BrowserThreadModelWorkerTest() : | 27 BrowserThreadModelWorkerTest() : |
| 26 did_do_work_(false), | 28 did_do_work_(false), |
| 27 db_thread_(BrowserThread::DB), | 29 db_thread_(BrowserThread::DB), |
| 28 io_thread_(BrowserThread::IO, &io_loop_), | 30 io_thread_(BrowserThread::IO, &io_loop_), |
| 29 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} | 31 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} |
| 30 | 32 |
| 31 bool did_do_work() { return did_do_work_; } | 33 bool did_do_work() { return did_do_work_; } |
| 32 BrowserThreadModelWorker* worker() { return worker_.get(); } | 34 BrowserThreadModelWorker* worker() { return worker_.get(); } |
| 33 OneShotTimer<BrowserThreadModelWorkerTest>* timer() { return &timer_; } | 35 OneShotTimer<BrowserThreadModelWorkerTest>* timer() { return &timer_; } |
| 34 ScopedRunnableMethodFactory<BrowserThreadModelWorkerTest>* factory() { | 36 ScopedRunnableMethodFactory<BrowserThreadModelWorkerTest>* factory() { |
| 35 return &method_factory_; | 37 return &method_factory_; |
| 36 } | 38 } |
| 37 | 39 |
| 38 // Schedule DoWork to be executed on the DB thread and have the test fail if | 40 // Schedule DoWork to be executed on the DB thread and have the test fail if |
| 39 // DoWork hasn't executed within action_timeout_ms() ms. | 41 // DoWork hasn't executed within action_timeout_ms() ms. |
| 40 void ScheduleWork() { | 42 void ScheduleWork() { |
| 41 scoped_ptr<Callback0::Type> c(NewCallback(this, | 43 // We wait until the callback is done. So it is safe to use unretained. |
| 42 &BrowserThreadModelWorkerTest::DoWork)); | 44 WorkCallback c = base::Bind(&BrowserThreadModelWorkerTest::DoWork, |
| 45 base::Unretained(this)); |
| 43 timer()->Start( | 46 timer()->Start( |
| 44 FROM_HERE, | 47 FROM_HERE, |
| 45 TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()), | 48 TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()), |
| 46 this, | 49 this, |
| 47 &BrowserThreadModelWorkerTest::Timeout); | 50 &BrowserThreadModelWorkerTest::Timeout); |
| 48 worker()->DoWorkAndWaitUntilDone(c.get()); | 51 worker()->DoWorkAndWaitUntilDone(c); |
| 49 } | 52 } |
| 50 | 53 |
| 51 // This is the work that will be scheduled to be done on the DB thread. | 54 // This is the work that will be scheduled to be done on the DB thread. |
| 52 void DoWork() { | 55 UnrecoverableErrorInfo DoWork() { |
| 53 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 56 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 54 timer_.Stop(); // Stop the failure timer so the test succeeds. | 57 timer_.Stop(); // Stop the failure timer so the test succeeds. |
| 55 BrowserThread::PostTask( | 58 BrowserThread::PostTask( |
| 56 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); | 59 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
| 57 did_do_work_ = true; | 60 did_do_work_ = true; |
| 61 return UnrecoverableErrorInfo(); |
| 58 } | 62 } |
| 59 | 63 |
| 60 // This will be called by the OneShotTimer and make the test fail unless | 64 // This will be called by the OneShotTimer and make the test fail unless |
| 61 // DoWork is called first. | 65 // DoWork is called first. |
| 62 void Timeout() { | 66 void Timeout() { |
| 63 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; | 67 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; |
| 64 BrowserThread::PostTask( | 68 BrowserThread::PostTask( |
| 65 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); | 69 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
| 66 } | 70 } |
| 67 | 71 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 91 TEST_F(BrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { | 95 TEST_F(BrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { |
| 92 MessageLoop::current()->PostTask(FROM_HERE, factory()->NewRunnableMethod( | 96 MessageLoop::current()->PostTask(FROM_HERE, factory()->NewRunnableMethod( |
| 93 &BrowserThreadModelWorkerTest::ScheduleWork)); | 97 &BrowserThreadModelWorkerTest::ScheduleWork)); |
| 94 MessageLoop::current()->Run(); | 98 MessageLoop::current()->Run(); |
| 95 EXPECT_TRUE(did_do_work()); | 99 EXPECT_TRUE(did_do_work()); |
| 96 } | 100 } |
| 97 | 101 |
| 98 } // namespace | 102 } // namespace |
| 99 | 103 |
| 100 } // namespace browser_sync | 104 } // namespace browser_sync |
| OLD | NEW |