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

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: 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 bool signaled = test_event_.Wait();
52 EXPECT_TRUE(signaled);
53 }
54
55 void OnWorkerThread() {
56 // We're not on the original thread.
57 EXPECT_FALSE(thread_checker_.CalledOnValidThread());
58
59 test_event_.Signal();
60 }
61
62 void OnOriginalThread() {
63 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
64 finished_ = true;
65 }
66
67 bool finished() const {
68 return finished_;
69 }
70
71 private:
72 bool finished_;
73 WaitableEvent test_event_;
74
75 // The Impl version performs its checks even in release builds.
76 ThreadCheckerImpl thread_checker_;
77 };
78
32 } // namespace 79 } // namespace
33 80
34 TEST_F(WorkerPoolTest, PostTask) { 81 TEST_F(WorkerPoolTest, PostTask) {
35 WaitableEvent test_event(false, false); 82 WaitableEvent test_event(false, false);
36 WaitableEvent long_test_event(false, false); 83 WaitableEvent long_test_event(false, false);
37 84
38 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&test_event), false); 85 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&test_event), false);
39 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&long_test_event), true); 86 WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&long_test_event), true);
40 87
41 test_event.Wait(); 88 test_event.Wait();
42 long_test_event.Wait(); 89 long_test_event.Wait();
43 } 90 }
44 91
92 TEST_F(WorkerPoolTest, PostTaskAndReply) {
93 MessageLoop message_loop;
94 scoped_refptr<PostTaskAndReplyTester> tester(new PostTaskAndReplyTester());
95 tester->RunTest();
96
97 const TimeDelta kMaxDuration =
98 TimeDelta::FromMilliseconds(TestTimeouts::tiny_timeout_ms());
99 TimeTicks start = TimeTicks::Now();
100 while (!tester->finished() && TimeTicks::Now() - start < kMaxDuration) {
101 MessageLoop::current()->RunAllPending();
102 }
103 EXPECT_TRUE(tester->finished());
104 }
105
45 } // namespace base 106 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698