Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: base/threading/worker_pool_unittest.cc

Issue 8139028: Add WorkerPool::PostTaskAndReply and use in DHCP code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments. Fix compile issue after merge to lkgr. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/location.h" 8 #include "base/location.h"
9 #include "base/message_loop.h"
8 #include "base/task.h" 10 #include "base/task.h"
11 #include "base/test/test_timeouts.h"
12 #include "base/time.h"
13 #include "base/threading/thread_checker_impl.h"
9 #include "base/synchronization/waitable_event.h" 14 #include "base/synchronization/waitable_event.h"
10 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h" 16 #include "testing/platform_test.h"
12 17
13 typedef PlatformTest WorkerPoolTest; 18 typedef PlatformTest WorkerPoolTest;
14 19
15 namespace base { 20 namespace base {
16 21
17 namespace { 22 namespace {
18 23
19 class PostTaskTestTask : public Task { 24 class PostTaskTestTask : public Task {
20 public: 25 public:
21 explicit PostTaskTestTask(WaitableEvent* event) : event_(event) { 26 explicit PostTaskTestTask(WaitableEvent* event) : event_(event) {
22 } 27 }
23 28
24 void Run() { 29 void Run() {
25 event_->Signal(); 30 event_->Signal();
26 } 31 }
27 32
28 private: 33 private:
29 WaitableEvent* event_; 34 WaitableEvent* event_;
30 }; 35 };
31 36
37 class PostTaskAndReplyTester
38 : public base::RefCountedThreadSafe<PostTaskAndReplyTester> {
39 public:
40 PostTaskAndReplyTester() : finished_(false), test_event_(false, false) {
41 }
42
43 void RunTest() {
44 ASSERT_TRUE(thread_checker_.CalledOnValidThread());
45 WorkerPool::PostTaskAndReply(
46 FROM_HERE,
47 base::Bind(&PostTaskAndReplyTester::OnWorkerThread, this),
48 base::Bind(&PostTaskAndReplyTester::OnOriginalThread, this),
49 false);
50
51 test_event_.Wait();
52 }
53
54 void OnWorkerThread() {
55 // We're not on the original thread.
56 EXPECT_FALSE(thread_checker_.CalledOnValidThread());
57
58 test_event_.Signal();
59 }
60
61 void OnOriginalThread() {
62 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
63 finished_ = true;
64 }
65
66 bool finished() const {
67 return finished_;
68 }
69
70 private:
71 bool finished_;
72 WaitableEvent test_event_;
73
74 // The Impl version performs its checks even in release builds.
75 ThreadCheckerImpl thread_checker_;
76 };
77
32 } // namespace 78 } // namespace
33 79
34 TEST_F(WorkerPoolTest, PostTask) { 80 TEST_F(WorkerPoolTest, PostTask) {
35 WaitableEvent test_event(false, false); 81 WaitableEvent test_event(false, false);
36 WaitableEvent long_test_event(false, false); 82 WaitableEvent long_test_event(false, false);
37 83
38 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&test_event), false); 84 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&test_event), false);
39 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&long_test_event), true); 85 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&long_test_event), true);
40 86
41 test_event.Wait(); 87 test_event.Wait();
42 long_test_event.Wait(); 88 long_test_event.Wait();
43 } 89 }
44 90
91 TEST_F(WorkerPoolTest, PostTaskAndReply) {
92 MessageLoop message_loop;
93 scoped_refptr<PostTaskAndReplyTester> tester(new PostTaskAndReplyTester());
94 tester->RunTest();
95
96 const TimeDelta kMaxDuration =
97 TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms());
98 TimeTicks start = TimeTicks::Now();
99 while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) {
100 MessageLoop::current()->RunAllPending();
101 }
102 EXPECT_TRUE(tester->finished());
103 }
104
45 } // namespace base 105 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698