Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / base / test_completion_callback_unittest.cc
blob454da5dc727312e796aa923e6fc66e104a59983e
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
7 #include "base/bind.h"
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"
18 namespace net {
20 namespace {
22 const int kMagicResult = 8888;
24 void CallClosureAfterCheckingResult(const base::Closure& closure,
25 bool* did_check_result,
26 int result) {
27 DCHECK_EQ(result, kMagicResult);
28 *did_check_result = true;
29 closure.Run();
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 {
36 public:
37 ExampleEmployer();
38 ~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);
45 private:
46 class ExampleWorker;
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> {
55 public:
56 ExampleWorker(ExampleEmployer* employer, const CompletionCallback& callback)
57 : employer_(employer),
58 callback_(callback),
59 origin_loop_(base::MessageLoop::current()) {}
60 void DoWork();
61 void DoCallback();
62 private:
63 friend class base::RefCountedThreadSafe<ExampleWorker>;
65 ~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_);
84 if (origin_loop_)
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
95 // destroyed.
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(
114 FROM_HERE,
115 base::Bind(&ExampleWorker::DoWork, request_.get()),
116 true)) {
117 NOTREACHED();
118 request_ = NULL;
119 return false;
122 return true;
125 } // namespace
127 typedef PlatformTest TestCompletionCallbackTest;
129 TEST_F(TestCompletionCallbackTest, Simple) {
130 ExampleEmployer boss;
131 TestCompletionCallback callback;
132 bool queued = boss.DoSomething(callback.callback());
133 EXPECT_TRUE(queued);
134 int result = callback.WaitForResult();
135 EXPECT_EQ(result, kMagicResult);
138 TEST_F(TestCompletionCallbackTest, Closure) {
139 ExampleEmployer boss;
140 TestClosure closure;
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);
146 EXPECT_TRUE(queued);
148 EXPECT_FALSE(did_check_result);
149 closure.WaitForResult();
150 EXPECT_TRUE(did_check_result);
153 // TODO: test deleting ExampleEmployer while work outstanding
155 } // namespace net