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/sessions/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 WorkCallback c = base::Bind(&BrowserThreadModelWorkerTest::DoWork, |
akalin
2011/10/26 00:25:05
comment about unretained
lipalani1
2011/10/26 01:39:11
Done.
| |
42 &BrowserThreadModelWorkerTest::DoWork)); | 44 base::Unretained(this)); |
43 timer()->Start( | 45 timer()->Start( |
44 FROM_HERE, | 46 FROM_HERE, |
45 TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()), | 47 TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()), |
46 this, | 48 this, |
47 &BrowserThreadModelWorkerTest::Timeout); | 49 &BrowserThreadModelWorkerTest::Timeout); |
48 worker()->DoWorkAndWaitUntilDone(c.get()); | 50 worker()->DoWorkAndWaitUntilDone(c); |
49 } | 51 } |
50 | 52 |
51 // This is the work that will be scheduled to be done on the DB thread. | 53 // This is the work that will be scheduled to be done on the DB thread. |
52 void DoWork() { | 54 sessions::UnrecoverableErrorInfo DoWork() { |
53 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 55 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
54 timer_.Stop(); // Stop the failure timer so the test succeeds. | 56 timer_.Stop(); // Stop the failure timer so the test succeeds. |
55 BrowserThread::PostTask( | 57 BrowserThread::PostTask( |
56 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); | 58 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
57 did_do_work_ = true; | 59 did_do_work_ = true; |
60 return sessions::UnrecoverableErrorInfo(); | |
58 } | 61 } |
59 | 62 |
60 // This will be called by the OneShotTimer and make the test fail unless | 63 // This will be called by the OneShotTimer and make the test fail unless |
61 // DoWork is called first. | 64 // DoWork is called first. |
62 void Timeout() { | 65 void Timeout() { |
63 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; | 66 ADD_FAILURE() << "Timed out waiting for work to be done on the DB thread."; |
64 BrowserThread::PostTask( | 67 BrowserThread::PostTask( |
65 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); | 68 BrowserThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
66 } | 69 } |
67 | 70 |
(...skipping 23 matching lines...) Expand all Loading... | |
91 TEST_F(BrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { | 94 TEST_F(BrowserThreadModelWorkerTest, DoesWorkOnDatabaseThread) { |
92 MessageLoop::current()->PostTask(FROM_HERE, factory()->NewRunnableMethod( | 95 MessageLoop::current()->PostTask(FROM_HERE, factory()->NewRunnableMethod( |
93 &BrowserThreadModelWorkerTest::ScheduleWork)); | 96 &BrowserThreadModelWorkerTest::ScheduleWork)); |
94 MessageLoop::current()->Run(); | 97 MessageLoop::current()->Run(); |
95 EXPECT_TRUE(did_do_work()); | 98 EXPECT_TRUE(did_do_work()); |
96 } | 99 } |
97 | 100 |
98 } // namespace | 101 } // namespace |
99 | 102 |
100 } // namespace browser_sync | 103 } // namespace browser_sync |
OLD | NEW |