| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
| 14 #include "base/threading/thread_checker_impl.h" | 14 #include "base/threading/thread_checker_impl.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "testing/platform_test.h" | 18 #include "testing/platform_test.h" |
| 19 | 19 |
| 20 typedef PlatformTest WorkerPoolTest; | 20 typedef PlatformTest WorkerPoolTest; |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 class PostTaskAndReplyTester | 26 class PostTaskAndReplyTester |
| 27 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> { | 27 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> { |
| 28 public: | 28 public: |
| 29 PostTaskAndReplyTester() : finished_(false), test_event_(false, false) {} | 29 PostTaskAndReplyTester() |
| 30 : finished_(false), |
| 31 test_event_(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 32 WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 30 | 33 |
| 31 void RunTest() { | 34 void RunTest() { |
| 32 ASSERT_TRUE(thread_checker_.CalledOnValidThread()); | 35 ASSERT_TRUE(thread_checker_.CalledOnValidThread()); |
| 33 WorkerPool::PostTaskAndReply( | 36 WorkerPool::PostTaskAndReply( |
| 34 FROM_HERE, | 37 FROM_HERE, |
| 35 base::Bind(&PostTaskAndReplyTester::OnWorkerThread, this), | 38 base::Bind(&PostTaskAndReplyTester::OnWorkerThread, this), |
| 36 base::Bind(&PostTaskAndReplyTester::OnOriginalThread, this), | 39 base::Bind(&PostTaskAndReplyTester::OnOriginalThread, this), |
| 37 false); | 40 false); |
| 38 | 41 |
| 39 test_event_.Wait(); | 42 test_event_.Wait(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 62 bool finished_; | 65 bool finished_; |
| 63 WaitableEvent test_event_; | 66 WaitableEvent test_event_; |
| 64 | 67 |
| 65 // The Impl version performs its checks even in release builds. | 68 // The Impl version performs its checks even in release builds. |
| 66 ThreadCheckerImpl thread_checker_; | 69 ThreadCheckerImpl thread_checker_; |
| 67 }; | 70 }; |
| 68 | 71 |
| 69 } // namespace | 72 } // namespace |
| 70 | 73 |
| 71 TEST_F(WorkerPoolTest, PostTask) { | 74 TEST_F(WorkerPoolTest, PostTask) { |
| 72 WaitableEvent test_event(false, false); | 75 WaitableEvent test_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 73 WaitableEvent long_test_event(false, false); | 76 WaitableEvent::InitialState::NOT_SIGNALED); |
| 77 WaitableEvent long_test_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 78 WaitableEvent::InitialState::NOT_SIGNALED); |
| 74 | 79 |
| 75 WorkerPool::PostTask(FROM_HERE, | 80 WorkerPool::PostTask(FROM_HERE, |
| 76 base::Bind(&WaitableEvent::Signal, | 81 base::Bind(&WaitableEvent::Signal, |
| 77 base::Unretained(&test_event)), | 82 base::Unretained(&test_event)), |
| 78 false); | 83 false); |
| 79 WorkerPool::PostTask(FROM_HERE, | 84 WorkerPool::PostTask(FROM_HERE, |
| 80 base::Bind(&WaitableEvent::Signal, | 85 base::Bind(&WaitableEvent::Signal, |
| 81 base::Unretained(&long_test_event)), | 86 base::Unretained(&long_test_event)), |
| 82 true); | 87 true); |
| 83 | 88 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 104 // Ensure that the other thread has a chance to run even on a single-core | 109 // Ensure that the other thread has a chance to run even on a single-core |
| 105 // device. | 110 // device. |
| 106 pthread_yield_np(); | 111 pthread_yield_np(); |
| 107 #endif | 112 #endif |
| 108 RunLoop().RunUntilIdle(); | 113 RunLoop().RunUntilIdle(); |
| 109 } | 114 } |
| 110 EXPECT_TRUE(tester->finished()); | 115 EXPECT_TRUE(tester->finished()); |
| 111 } | 116 } |
| 112 | 117 |
| 113 } // namespace base | 118 } // namespace base |
| OLD | NEW |