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/location.h" | 9 #include "base/location.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/task.h" | 11 #include "base/task.h" |
11 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
12 #include "base/time.h" | 13 #include "base/time.h" |
13 #include "base/threading/thread_checker_impl.h" | 14 #include "base/threading/thread_checker_impl.h" |
14 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
17 | 18 |
18 typedef PlatformTest WorkerPoolTest; | 19 typedef PlatformTest WorkerPoolTest; |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 class PostTaskTestTask : public Task { | |
25 public: | |
26 explicit PostTaskTestTask(WaitableEvent* event) : event_(event) { | |
27 } | |
28 | |
29 void Run() { | |
30 event_->Signal(); | |
31 } | |
32 | |
33 private: | |
34 WaitableEvent* event_; | |
35 }; | |
36 | |
37 class PostTaskAndReplyTester | 25 class PostTaskAndReplyTester |
38 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> { | 26 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> { |
39 public: | 27 public: |
40 PostTaskAndReplyTester() : finished_(false), test_event_(false, false) { | 28 PostTaskAndReplyTester() : finished_(false), test_event_(false, false) { |
41 } | 29 } |
42 | 30 |
43 void RunTest() { | 31 void RunTest() { |
44 ASSERT_TRUE(thread_checker_.CalledOnValidThread()); | 32 ASSERT_TRUE(thread_checker_.CalledOnValidThread()); |
45 WorkerPool::PostTaskAndReply( | 33 WorkerPool::PostTaskAndReply( |
46 FROM_HERE, | 34 FROM_HERE, |
(...skipping 27 matching lines...) Expand all Loading... |
74 // The Impl version performs its checks even in release builds. | 62 // The Impl version performs its checks even in release builds. |
75 ThreadCheckerImpl thread_checker_; | 63 ThreadCheckerImpl thread_checker_; |
76 }; | 64 }; |
77 | 65 |
78 } // namespace | 66 } // namespace |
79 | 67 |
80 TEST_F(WorkerPoolTest, PostTask) { | 68 TEST_F(WorkerPoolTest, PostTask) { |
81 WaitableEvent test_event(false, false); | 69 WaitableEvent test_event(false, false); |
82 WaitableEvent long_test_event(false, false); | 70 WaitableEvent long_test_event(false, false); |
83 | 71 |
84 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&test_event), false); | 72 WorkerPool::PostTask(FROM_HERE, |
85 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&long_test_event), true); | 73 base::Bind(&WaitableEvent::Signal, |
| 74 base::Unretained(&test_event)), |
| 75 false); |
| 76 WorkerPool::PostTask(FROM_HERE, |
| 77 base::Bind(&WaitableEvent::Signal, |
| 78 base::Unretained(&long_test_event)), |
| 79 true); |
86 | 80 |
87 test_event.Wait(); | 81 test_event.Wait(); |
88 long_test_event.Wait(); | 82 long_test_event.Wait(); |
89 } | 83 } |
90 | 84 |
91 TEST_F(WorkerPoolTest, PostTaskAndReply) { | 85 TEST_F(WorkerPoolTest, PostTaskAndReply) { |
92 MessageLoop message_loop; | 86 MessageLoop message_loop; |
93 scoped_refptr<PostTaskAndReplyTester> tester(new PostTaskAndReplyTester()); | 87 scoped_refptr<PostTaskAndReplyTester> tester(new PostTaskAndReplyTester()); |
94 tester->RunTest(); | 88 tester->RunTest(); |
95 | 89 |
96 const TimeDelta kMaxDuration = | 90 const TimeDelta kMaxDuration = |
97 TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms()); | 91 TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms()); |
98 TimeTicks start = TimeTicks::Now(); | 92 TimeTicks start = TimeTicks::Now(); |
99 while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) { | 93 while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) { |
100 MessageLoop::current()->RunAllPending(); | 94 MessageLoop::current()->RunAllPending(); |
101 } | 95 } |
102 EXPECT_TRUE(tester->finished()); | 96 EXPECT_TRUE(tester->finished()); |
103 } | 97 } |
104 | 98 |
105 } // namespace base | 99 } // namespace base |
OLD | NEW |