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/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/worker_pool.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/test_completion_callback.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
20 const int kMagicResult
= 8888;
22 void CallClosureAfterCheckingResult(const base::Closure
& closure
,
23 bool* did_check_result
,
25 DCHECK_EQ(result
, kMagicResult
);
26 *did_check_result
= true;
30 // ExampleEmployer is a toy version of HostResolver
31 // TODO: restore damage done in extracting example from real code
32 // (e.g. bring back real destructor, bring back comments)
33 class ExampleEmployer
{
38 // Do some imaginary work on a worker thread;
39 // when done, worker posts callback on the original thread.
40 // Returns true on success
41 bool DoSomething(const CompletionCallback
& callback
);
45 friend class ExampleWorker
;
46 scoped_refptr
<ExampleWorker
> request_
;
47 DISALLOW_COPY_AND_ASSIGN(ExampleEmployer
);
50 // Helper class; this is how ExampleEmployer puts work on a different thread
51 class ExampleEmployer::ExampleWorker
52 : public base::RefCountedThreadSafe
<ExampleWorker
> {
54 ExampleWorker(ExampleEmployer
* employer
, const CompletionCallback
& callback
)
55 : employer_(employer
),
57 origin_loop_(base::MessageLoop::current()) {}
61 friend class base::RefCountedThreadSafe
<ExampleWorker
>;
65 // Only used on the origin thread (where DoSomething was called).
66 ExampleEmployer
* employer_
;
67 CompletionCallback callback_
;
68 // Used to post ourselves onto the origin thread.
69 base::Lock origin_loop_lock_
;
70 base::MessageLoop
* origin_loop_
;
73 void ExampleEmployer::ExampleWorker::DoWork() {
74 // Running on the worker thread
75 // In a real worker thread, some work would be done here.
76 // Pretend it is, and send the completion callback.
78 // The origin loop could go away while we are trying to post to it, so we
79 // need to call its PostTask method inside a lock. See ~ExampleEmployer.
81 base::AutoLock
locked(origin_loop_lock_
);
83 origin_loop_
->PostTask(FROM_HERE
,
84 base::Bind(&ExampleWorker::DoCallback
, this));
88 void ExampleEmployer::ExampleWorker::DoCallback() {
89 // Running on the origin thread.
91 // Drop the employer_'s reference to us. Do this before running the
92 // callback since the callback might result in the employer being
94 employer_
->request_
= NULL
;
96 callback_
.Run(kMagicResult
);
99 ExampleEmployer::ExampleEmployer() {
102 ExampleEmployer::~ExampleEmployer() {
105 bool ExampleEmployer::DoSomething(const CompletionCallback
& callback
) {
106 DCHECK(!request_
.get()) << "already in use";
108 request_
= new ExampleWorker(this, callback
);
110 // Dispatch to worker thread...
111 if (!base::WorkerPool::PostTask(
113 base::Bind(&ExampleWorker::DoWork
, request_
.get()),
125 typedef PlatformTest TestCompletionCallbackTest
;
127 TEST_F(TestCompletionCallbackTest
, Simple
) {
128 ExampleEmployer boss
;
129 TestCompletionCallback callback
;
130 bool queued
= boss
.DoSomething(callback
.callback());
132 int result
= callback
.WaitForResult();
133 EXPECT_EQ(result
, kMagicResult
);
136 TEST_F(TestCompletionCallbackTest
, Closure
) {
137 ExampleEmployer boss
;
139 bool did_check_result
= false;
140 CompletionCallback completion_callback
=
141 base::Bind(&CallClosureAfterCheckingResult
, closure
.closure(),
142 base::Unretained(&did_check_result
));
143 bool queued
= boss
.DoSomething(completion_callback
);
146 EXPECT_FALSE(did_check_result
);
147 closure
.WaitForResult();
148 EXPECT_TRUE(did_check_result
);
151 // TODO: test deleting ExampleEmployer while work outstanding