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
3 // found in the LICENSE file.
4
5 #include "base/threading/worker_pool.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/test/test_timeouts.h"
14 #include "base/threading/thread_checker_impl.h"
15 #include "base/time/time.h"
16 #include "build/build_config.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h"
19
20 typedef PlatformTest WorkerPoolTest;
21
22 namespace base {
23
24 namespace {
25
26 class PostTaskAndReplyTester
27 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> {
28 public:
PostTaskAndReplyTester()29 PostTaskAndReplyTester()
30 : finished_(false),
31 test_event_(WaitableEvent::ResetPolicy::AUTOMATIC,
32 WaitableEvent::InitialState::NOT_SIGNALED) {}
33
RunTest()34 void RunTest() {
35 ASSERT_TRUE(thread_checker_.CalledOnValidThread());
36 WorkerPool::PostTaskAndReply(
37 FROM_HERE,
38 base::Bind(&PostTaskAndReplyTester::OnWorkerThread, this),
39 base::Bind(&PostTaskAndReplyTester::OnOriginalThread, this),
40 false);
41
42 test_event_.Wait();
43 }
44
OnWorkerThread()45 void OnWorkerThread() {
46 // We're not on the original thread.
47 EXPECT_FALSE(thread_checker_.CalledOnValidThread());
48
49 test_event_.Signal();
50 }
51
OnOriginalThread()52 void OnOriginalThread() {
53 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
54 finished_ = true;
55 }
56
finished() const57 bool finished() const {
58 return finished_;
59 }
60
61 private:
62 friend class base::RefCountedThreadSafe<PostTaskAndReplyTester>;
~PostTaskAndReplyTester()63 ~PostTaskAndReplyTester() {}
64
65 bool finished_;
66 WaitableEvent test_event_;
67
68 // The Impl version performs its checks even in release builds.
69 ThreadCheckerImpl thread_checker_;
70 };
71
72 } // namespace
73
TEST_F(WorkerPoolTest,PostTask)74 TEST_F(WorkerPoolTest, PostTask) {
75 WaitableEvent test_event(WaitableEvent::ResetPolicy::AUTOMATIC,
76 WaitableEvent::InitialState::NOT_SIGNALED);
77 WaitableEvent long_test_event(WaitableEvent::ResetPolicy::AUTOMATIC,
78 WaitableEvent::InitialState::NOT_SIGNALED);
79
80 WorkerPool::PostTask(FROM_HERE,
81 base::Bind(&WaitableEvent::Signal,
82 base::Unretained(&test_event)),
83 false);
84 WorkerPool::PostTask(FROM_HERE,
85 base::Bind(&WaitableEvent::Signal,
86 base::Unretained(&long_test_event)),
87 true);
88
89 test_event.Wait();
90 long_test_event.Wait();
91 }
92
93 #if defined(OS_WIN) || defined(OS_LINUX)
94 // Flaky on Windows and Linux (http://crbug.com/130337)
95 #define MAYBE_PostTaskAndReply DISABLED_PostTaskAndReply
96 #else
97 #define MAYBE_PostTaskAndReply PostTaskAndReply
98 #endif
99
TEST_F(WorkerPoolTest,MAYBE_PostTaskAndReply)100 TEST_F(WorkerPoolTest, MAYBE_PostTaskAndReply) {
101 MessageLoop message_loop;
102 scoped_refptr<PostTaskAndReplyTester> tester(new PostTaskAndReplyTester());
103 tester->RunTest();
104
105 const TimeDelta kMaxDuration = TestTimeouts::tiny_timeout();
106 TimeTicks start = TimeTicks::Now();
107 while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) {
108 #if defined(OS_IOS)
109 // Ensure that the other thread has a chance to run even on a single-core
110 // device.
111 pthread_yield_np();
112 #endif
113 RunLoop().RunUntilIdle();
114 }
115 EXPECT_TRUE(tester->finished());
116 }
117
118 } // namespace base
119