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
3 // found in the LICENSE file.
5 // Illustrates how to use worker threads that issue completion callbacks
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/worker_pool.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/test_completion_callback.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
22 const int kMagicResult
= 8888;
24 void CallClosureAfterCheckingResult(const base::Closure
& closure
,
25 bool* did_check_result
,
27 DCHECK_EQ(result
, kMagicResult
);
28 *did_check_result
= true;
32 // ExampleEmployer is a toy version of HostResolver
33 // TODO: restore damage done in extracting example from real code
34 // (e.g. bring back real destructor, bring back comments)
35 class ExampleEmployer
{
40 // Do some imaginary work on a worker thread;
41 // when done, worker posts callback on the original thread.
42 // Returns true on success
43 bool DoSomething(const CompletionCallback
& callback
);
47 friend class ExampleWorker
;
48 scoped_refptr
<ExampleWorker
> request_
;
49 DISALLOW_COPY_AND_ASSIGN(ExampleEmployer
);
52 // Helper class; this is how ExampleEmployer puts work on a different thread
53 class ExampleEmployer::ExampleWorker
54 : public base::RefCountedThreadSafe
<ExampleWorker
> {
56 ExampleWorker(ExampleEmployer
* employer
, const CompletionCallback
& callback
)
57 : employer_(employer
),
59 origin_loop_(base::MessageLoop::current()) {}
63 friend class base::RefCountedThreadSafe
<ExampleWorker
>;
67 // Only used on the origin thread (where DoSomething was called).
68 ExampleEmployer
* employer_
;
69 CompletionCallback callback_
;
70 // Used to post ourselves onto the origin thread.
71 base::Lock origin_loop_lock_
;
72 base::MessageLoop
* origin_loop_
;
75 void ExampleEmployer::ExampleWorker::DoWork() {
76 // Running on the worker thread
77 // In a real worker thread, some work would be done here.
78 // Pretend it is, and send the completion callback.
80 // The origin loop could go away while we are trying to post to it, so we
81 // need to call its PostTask method inside a lock. See ~ExampleEmployer.
83 base::AutoLock
locked(origin_loop_lock_
);
85 origin_loop_
->task_runner()->PostTask(
86 FROM_HERE
, base::Bind(&ExampleWorker::DoCallback
, this));
90 void ExampleEmployer::ExampleWorker::DoCallback() {
91 // Running on the origin thread.
93 // Drop the employer_'s reference to us. Do this before running the
94 // callback since the callback might result in the employer being
96 employer_
->request_
= NULL
;
98 callback_
.Run(kMagicResult
);
101 ExampleEmployer::ExampleEmployer() {
104 ExampleEmployer::~ExampleEmployer() {
107 bool ExampleEmployer::DoSomething(const CompletionCallback
& callback
) {
108 DCHECK(!request_
.get()) << "already in use";
110 request_
= new ExampleWorker(this, callback
);
112 // Dispatch to worker thread...
113 if (!base::WorkerPool::PostTask(
115 base::Bind(&ExampleWorker::DoWork
, request_
.get()),
127 typedef PlatformTest TestCompletionCallbackTest
;
129 TEST_F(TestCompletionCallbackTest
, Simple
) {
130 ExampleEmployer boss
;
131 TestCompletionCallback callback
;
132 bool queued
= boss
.DoSomething(callback
.callback());
134 int result
= callback
.WaitForResult();
135 EXPECT_EQ(result
, kMagicResult
);
138 TEST_F(TestCompletionCallbackTest
, Closure
) {
139 ExampleEmployer boss
;
141 bool did_check_result
= false;
142 CompletionCallback completion_callback
=
143 base::Bind(&CallClosureAfterCheckingResult
, closure
.closure(),
144 base::Unretained(&did_check_result
));
145 bool queued
= boss
.DoSomething(completion_callback
);
148 EXPECT_FALSE(did_check_result
);
149 closure
.WaitForResult();
150 EXPECT_TRUE(did_check_result
);
153 // TODO: test deleting ExampleEmployer while work outstanding