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/threading/worker_pool.h" | 5 #include "base/threading/worker_pool.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
12 #include "base/time.h" | 12 #include "base/time.h" |
13 #include "base/threading/thread_checker_impl.h" | 13 #include "base/threading/thread_checker_impl.h" |
14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
17 | 17 |
18 typedef PlatformTest WorkerPoolTest; | 18 typedef PlatformTest WorkerPoolTest; |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 class PostTaskAndReplyTester | 24 class PostTaskAndReplyTester |
25 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> { | 25 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> { |
26 public: | 26 public: |
27 PostTaskAndReplyTester() : finished_(false), test_event_(false, false) { | 27 PostTaskAndReplyTester() : finished_(false), test_event_(false, false) {} |
28 } | |
29 | 28 |
30 void RunTest() { | 29 void RunTest() { |
31 ASSERT_TRUE(thread_checker_.CalledOnValidThread()); | 30 ASSERT_TRUE(thread_checker_.CalledOnValidThread()); |
32 WorkerPool::PostTaskAndReply( | 31 WorkerPool::PostTaskAndReply( |
33 FROM_HERE, | 32 FROM_HERE, |
34 base::Bind(&PostTaskAndReplyTester::OnWorkerThread, this), | 33 base::Bind(&PostTaskAndReplyTester::OnWorkerThread, this), |
35 base::Bind(&PostTaskAndReplyTester::OnOriginalThread, this), | 34 base::Bind(&PostTaskAndReplyTester::OnOriginalThread, this), |
36 false); | 35 false); |
37 | 36 |
38 test_event_.Wait(); | 37 test_event_.Wait(); |
39 } | 38 } |
40 | 39 |
41 void OnWorkerThread() { | 40 void OnWorkerThread() { |
42 // We're not on the original thread. | 41 // We're not on the original thread. |
43 EXPECT_FALSE(thread_checker_.CalledOnValidThread()); | 42 EXPECT_FALSE(thread_checker_.CalledOnValidThread()); |
44 | 43 |
45 test_event_.Signal(); | 44 test_event_.Signal(); |
46 } | 45 } |
47 | 46 |
48 void OnOriginalThread() { | 47 void OnOriginalThread() { |
49 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | 48 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
50 finished_ = true; | 49 finished_ = true; |
51 } | 50 } |
52 | 51 |
53 bool finished() const { | 52 bool finished() const { |
54 return finished_; | 53 return finished_; |
55 } | 54 } |
56 | 55 |
57 private: | 56 private: |
| 57 friend class base::RefCountedThreadSafe<PostTaskAndReplyTester>; |
| 58 ~PostTaskAndReplyTester() {} |
| 59 |
58 bool finished_; | 60 bool finished_; |
59 WaitableEvent test_event_; | 61 WaitableEvent test_event_; |
60 | 62 |
61 // The Impl version performs its checks even in release builds. | 63 // The Impl version performs its checks even in release builds. |
62 ThreadCheckerImpl thread_checker_; | 64 ThreadCheckerImpl thread_checker_; |
63 }; | 65 }; |
64 | 66 |
65 } // namespace | 67 } // namespace |
66 | 68 |
67 TEST_F(WorkerPoolTest, PostTask) { | 69 TEST_F(WorkerPoolTest, PostTask) { |
(...skipping 21 matching lines...) Expand all Loading... |
89 const TimeDelta kMaxDuration = | 91 const TimeDelta kMaxDuration = |
90 TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms()); | 92 TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms()); |
91 TimeTicks start = TimeTicks::Now(); | 93 TimeTicks start = TimeTicks::Now(); |
92 while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) { | 94 while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) { |
93 MessageLoop::current()->RunAllPending(); | 95 MessageLoop::current()->RunAllPending(); |
94 } | 96 } |
95 EXPECT_TRUE(tester->finished()); | 97 EXPECT_TRUE(tester->finished()); |
96 } | 98 } |
97 | 99 |
98 } // namespace base | 100 } // namespace base |
OLD | NEW |