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

Side by Side Diff: base/worker_pool_job_unittest.cc

Issue 5710002: Create base::WorkerPoolJob. Use it for HostResolverImpl and DirectoryLister. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address cancellation. Created 10 years 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
(Empty)
1 // Copyright (c) 2010 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/worker_pool_job.h"
6
7 #include "base/callback.h"
8 #include "base/message_loop.h"
9 #include "base/waitable_event.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
13
14 namespace {
15
16 class TestCallback : public CallbackRunner<Tuple0> {
eroman 2010/12/22 23:25:08 Can you just fold this logic into TestJob by havin
willchan no longer on Chromium 2010/12/24 00:47:34 Done.
17 public:
18 TestCallback()
19 : have_run_(false),
20 waiting_for_run_(false) {
21 }
22
23 void Wait() {
24 DCHECK(!waiting_for_run_);
25 while (!have_run_) {
26 waiting_for_run_ = true;
27 MessageLoop::current()->Run();
28 waiting_for_run_ = false;
29 }
30 have_run_ = false; // auto-reset for next callback
31 }
32
33 virtual void RunWithParams(const Tuple0& params) {
34 if (waiting_for_run_)
35 MessageLoop::current()->Quit();
36 have_run_ = true;
37 }
38
39 private:
40 bool have_run_;
41 bool waiting_for_run_;
42 };
43
44 class TestJob : public WorkerPoolJob {
45 public:
46 explicit TestJob(Callback0::Type* callback)
47 : callback_(callback),
48 run_on_worker_pool_event_(true /* manual reset */,
49 false /* not initially signalled */) {}
50
51 void SignalOnWorkerPool() {
52 run_on_worker_pool_event_.Signal();
53 }
54
55 using WorkerPoolJob::StartJob;
56 using WorkerPoolJob::CancelJob;
57 using WorkerPoolJob::canceled;
58 using WorkerPoolJob::is_running;
59
60 protected:
61 ~TestJob() {}
62
63 private:
64 virtual void RunJob() {
65 run_on_worker_pool_event_.Wait();
66 }
67
68 virtual void CompleteJob() {
69 callback_->Run();
70 }
71
72 Callback0::Type* const callback_;
73 WaitableEvent run_on_worker_pool_event_;
74 };
75
76 TEST(WorkerPoolJobTest, Simple) {
77 MessageLoop loop;
78 TestCallback callback;
79 scoped_refptr<TestJob> job(new TestJob(&callback));
80 job->StartJob();
81 EXPECT_TRUE(job->is_running());
82 job->SignalOnWorkerPool();
83 callback.Wait();
84 EXPECT_FALSE(job->canceled());
85 EXPECT_FALSE(job->is_running());
86 }
87
88 TEST(WorkerPoolJobTest, CancelOnWorker) {
eroman 2010/12/22 23:25:08 I believe this test may result in a leak report.
willchan no longer on Chromium 2010/12/24 00:47:34 I do not believe that's how valgrind works. I thi
eroman 2010/12/24 00:54:42 Outstanding worker threads at shutdown causes leak
89 MessageLoop loop;
90
91 {
92 TestCallback callback;
93 scoped_refptr<TestJob> job(new TestJob(&callback));
94 job->StartJob();
95 job->CancelJob();
96 job->SignalOnWorkerPool();
97 EXPECT_TRUE(job->canceled());
98 }
99
100 // Try to catch any broken behavior.
101 MessageLoop::current()->RunAllPending();
102 }
103
104 TEST(WorkerPoolJobTest, CancelOnOrigin) {
105 MessageLoop loop;
106 {
107 TestCallback callback;
108 scoped_refptr<TestJob> job(new TestJob(&callback));
109 job->StartJob();
110 EXPECT_TRUE(job->is_running());
111 job->SignalOnWorkerPool();
112 job->CancelJob();
113 EXPECT_TRUE(job->canceled());
114 }
115
116 // Try to catch any broken behavior.
117 MessageLoop::current()->RunAllPending();
eroman 2010/12/22 23:25:08 The timings are hard to reason about here as well.
willchan no longer on Chromium 2010/12/24 00:47:34 What would be wrong with that? Under valgrind, th
118 }
119
120 } // namespace
121
122 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698