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

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 comments. 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.
eroman 2011/01/05 01:24:52 nit: might consider changing these to 2011
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 TestJob : public WorkerPoolJob {
17 public:
18 explicit TestJob()
eroman 2011/01/05 01:24:52 is explicit needed?
19 : run_on_worker_pool_event_(true /* manual reset */,
20 false /* not initially signalled */),
21 have_run_(false),
22 waiting_for_run_(false) {}
23
24 void SignalOnWorkerPool() {
25 run_on_worker_pool_event_.Signal();
26 }
27
28 void WaitUntilComplete() {
29 DCHECK(!waiting_for_run_);
30 while (!have_run_) {
31 waiting_for_run_ = true;
32 MessageLoop::current()->Run();
33 waiting_for_run_ = false;
34 }
35 have_run_ = false; // auto-reset for next callback
36 }
37
38 using WorkerPoolJob::StartJob;
39 using WorkerPoolJob::CancelJob;
40 using WorkerPoolJob::canceled;
41 using WorkerPoolJob::is_running;
42
43 protected:
44 ~TestJob() {}
45
46 private:
47 virtual void RunJob() {
48 run_on_worker_pool_event_.Wait();
49 }
50
51 virtual void OnJobCompleted() {
52 if (waiting_for_run_)
53 MessageLoop::current()->Quit();
54 have_run_ = true;
55 }
56
57 WaitableEvent run_on_worker_pool_event_;
58 bool have_run_;
59 bool waiting_for_run_;
60 };
61
62 TEST(WorkerPoolJobTest, Simple) {
63 MessageLoop loop;
64 scoped_refptr<TestJob> job(new TestJob);
65 job->StartJob();
66 EXPECT_TRUE(job->is_running());
67 job->SignalOnWorkerPool();
68 job->WaitUntilComplete();
69 EXPECT_FALSE(job->canceled());
70 EXPECT_FALSE(job->is_running());
71 }
72
73 TEST(WorkerPoolJobTest, CancelOnWorker) {
74 MessageLoop loop;
75 scoped_refptr<TestJob> job(new TestJob);
76 job->StartJob();
77 job->CancelJob();
78 job->SignalOnWorkerPool();
79 EXPECT_TRUE(job->canceled());
80
81 // Try to catch any broken behavior.
82 MessageLoop::current()->RunAllPending();
83 }
84
85 TEST(WorkerPoolJobTest, CancelOnOrigin) {
86 MessageLoop loop;
87 scoped_refptr<TestJob> job(new TestJob);
88 job->StartJob();
89 EXPECT_TRUE(job->is_running());
90 job->SignalOnWorkerPool();
91 job->CancelJob();
92 EXPECT_TRUE(job->canceled());
93
94 // Try to catch any broken behavior.
95 MessageLoop::current()->RunAllPending();
96 }
97
98 } // namespace
99
100 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698