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() : finished_(false), test_event_(false, false) {}
30 
RunTest()31   void RunTest() {
32     ASSERT_TRUE(thread_checker_.CalledOnValidThread());
33     WorkerPool::PostTaskAndReply(
34       FROM_HERE,
35       base::Bind(&PostTaskAndReplyTester::OnWorkerThread, this),
36       base::Bind(&PostTaskAndReplyTester::OnOriginalThread, this),
37       false);
38 
39     test_event_.Wait();
40   }
41 
OnWorkerThread()42   void OnWorkerThread() {
43     // We're not on the original thread.
44     EXPECT_FALSE(thread_checker_.CalledOnValidThread());
45 
46     test_event_.Signal();
47   }
48 
OnOriginalThread()49   void OnOriginalThread() {
50     EXPECT_TRUE(thread_checker_.CalledOnValidThread());
51     finished_ = true;
52   }
53 
finished() const54   bool finished() const {
55     return finished_;
56   }
57 
58  private:
59   friend class base::RefCountedThreadSafe<PostTaskAndReplyTester>;
~PostTaskAndReplyTester()60   ~PostTaskAndReplyTester() {}
61 
62   bool finished_;
63   WaitableEvent test_event_;
64 
65   // The Impl version performs its checks even in release builds.
66   ThreadCheckerImpl thread_checker_;
67 };
68 
69 }  // namespace
70 
TEST_F(WorkerPoolTest,PostTask)71 TEST_F(WorkerPoolTest, PostTask) {
72   WaitableEvent test_event(false, false);
73   WaitableEvent long_test_event(false, false);
74 
75   WorkerPool::PostTask(FROM_HERE,
76                        base::Bind(&WaitableEvent::Signal,
77                                   base::Unretained(&test_event)),
78                        false);
79   WorkerPool::PostTask(FROM_HERE,
80                        base::Bind(&WaitableEvent::Signal,
81                                   base::Unretained(&long_test_event)),
82                        true);
83 
84   test_event.Wait();
85   long_test_event.Wait();
86 }
87 
88 #if defined(OS_WIN) || defined(OS_LINUX)
89 // Flaky on Windows and Linux (http://crbug.com/130337)
90 #define MAYBE_PostTaskAndReply DISABLED_PostTaskAndReply
91 #else
92 #define MAYBE_PostTaskAndReply PostTaskAndReply
93 #endif
94 
TEST_F(WorkerPoolTest,MAYBE_PostTaskAndReply)95 TEST_F(WorkerPoolTest, MAYBE_PostTaskAndReply) {
96   MessageLoop message_loop;
97   scoped_refptr<PostTaskAndReplyTester> tester(new PostTaskAndReplyTester());
98   tester->RunTest();
99 
100   const TimeDelta kMaxDuration = TestTimeouts::tiny_timeout();
101   TimeTicks start = TimeTicks::Now();
102   while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) {
103 #if defined(OS_IOS)
104     // Ensure that the other thread has a chance to run even on a single-core
105     // device.
106     pthread_yield_np();
107 #endif
108     RunLoop().RunUntilIdle();
109   }
110   EXPECT_TRUE(tester->finished());
111 }
112 
113 }  // namespace base
114