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/location.h" | 8 #include "base/location.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/task.h" | 10 #include "base/task.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 PostTaskTestTask : public Task { | 24 void PostTaskTestHelper(WaitableEvent* event) { |
willchan no longer on Chromium
2011/11/23 18:15:46
Why is this wrapper function necessary?
dcheng
2011/11/23 18:47:49
Done.
| |
25 public: | 25 event->Signal(); |
26 explicit PostTaskTestTask(WaitableEvent* event) : event_(event) { | 26 } |
27 } | |
28 | |
29 void Run() { | |
30 event_->Signal(); | |
31 } | |
32 | |
33 private: | |
34 WaitableEvent* event_; | |
35 }; | |
36 | 27 |
37 class PostTaskAndReplyTester | 28 class PostTaskAndReplyTester |
38 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> { | 29 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> { |
39 public: | 30 public: |
40 PostTaskAndReplyTester() : finished_(false), test_event_(false, false) { | 31 PostTaskAndReplyTester() : finished_(false), test_event_(false, false) { |
41 } | 32 } |
42 | 33 |
43 void RunTest() { | 34 void RunTest() { |
44 ASSERT_TRUE(thread_checker_.CalledOnValidThread()); | 35 ASSERT_TRUE(thread_checker_.CalledOnValidThread()); |
45 WorkerPool::PostTaskAndReply( | 36 WorkerPool::PostTaskAndReply( |
(...skipping 28 matching lines...) Expand all Loading... | |
74 // The Impl version performs its checks even in release builds. | 65 // The Impl version performs its checks even in release builds. |
75 ThreadCheckerImpl thread_checker_; | 66 ThreadCheckerImpl thread_checker_; |
76 }; | 67 }; |
77 | 68 |
78 } // namespace | 69 } // namespace |
79 | 70 |
80 TEST_F(WorkerPoolTest, PostTask) { | 71 TEST_F(WorkerPoolTest, PostTask) { |
81 WaitableEvent test_event(false, false); | 72 WaitableEvent test_event(false, false); |
82 WaitableEvent long_test_event(false, false); | 73 WaitableEvent long_test_event(false, false); |
83 | 74 |
84 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&test_event), false); | 75 WorkerPool::PostTask(FROM_HERE, |
85 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&long_test_event), true); | 76 base::Bind(&PostTaskTestHelper, &test_event), |
77 false); | |
78 WorkerPool::PostTask(FROM_HERE, | |
79 base::Bind(&PostTaskTestHelper, &long_test_event), | |
80 true); | |
86 | 81 |
87 test_event.Wait(); | 82 test_event.Wait(); |
88 long_test_event.Wait(); | 83 long_test_event.Wait(); |
89 } | 84 } |
90 | 85 |
91 TEST_F(WorkerPoolTest, PostTaskAndReply) { | 86 TEST_F(WorkerPoolTest, PostTaskAndReply) { |
92 MessageLoop message_loop; | 87 MessageLoop message_loop; |
93 scoped_refptr<PostTaskAndReplyTester> tester(new PostTaskAndReplyTester()); | 88 scoped_refptr<PostTaskAndReplyTester> tester(new PostTaskAndReplyTester()); |
94 tester->RunTest(); | 89 tester->RunTest(); |
95 | 90 |
96 const TimeDelta kMaxDuration = | 91 const TimeDelta kMaxDuration = |
97 TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms()); | 92 TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms()); |
98 TimeTicks start = TimeTicks::Now(); | 93 TimeTicks start = TimeTicks::Now(); |
99 while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) { | 94 while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) { |
100 MessageLoop::current()->RunAllPending(); | 95 MessageLoop::current()->RunAllPending(); |
101 } | 96 } |
102 EXPECT_TRUE(tester->finished()); | 97 EXPECT_TRUE(tester->finished()); |
103 } | 98 } |
104 | 99 |
105 } // namespace base | 100 } // namespace base |
OLD | NEW |