OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Illustrates how to use worker threads that issue completion callbacks | 5 // Illustrates how to use worker threads that issue completion callbacks |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 callback_.Run(kMagicResult); | 87 callback_.Run(kMagicResult); |
88 } | 88 } |
89 | 89 |
90 ExampleEmployer::ExampleEmployer() { | 90 ExampleEmployer::ExampleEmployer() { |
91 } | 91 } |
92 | 92 |
93 ExampleEmployer::~ExampleEmployer() { | 93 ExampleEmployer::~ExampleEmployer() { |
94 } | 94 } |
95 | 95 |
96 bool ExampleEmployer::DoSomething(const net::CompletionCallback& callback) { | 96 bool ExampleEmployer::DoSomething(const net::CompletionCallback& callback) { |
97 DCHECK(!request_) << "already in use"; | 97 DCHECK(!request_.get()) << "already in use"; |
98 | 98 |
99 request_ = new ExampleWorker(this, callback); | 99 request_ = new ExampleWorker(this, callback); |
100 | 100 |
101 // Dispatch to worker thread... | 101 // Dispatch to worker thread... |
102 if (!base::WorkerPool::PostTask( | 102 if (!base::WorkerPool::PostTask( |
103 FROM_HERE, | 103 FROM_HERE, |
104 base::Bind(&ExampleWorker::DoWork, request_.get()), | 104 base::Bind(&ExampleWorker::DoWork, request_.get()), |
105 true)) { | 105 true)) { |
106 NOTREACHED(); | 106 NOTREACHED(); |
107 request_ = NULL; | 107 request_ = NULL; |
108 return false; | 108 return false; |
109 } | 109 } |
110 | 110 |
111 return true; | 111 return true; |
112 } | 112 } |
113 | 113 |
114 TEST_F(TestCompletionCallbackTest, Simple) { | 114 TEST_F(TestCompletionCallbackTest, Simple) { |
115 ExampleEmployer boss; | 115 ExampleEmployer boss; |
116 net::TestCompletionCallback callback; | 116 net::TestCompletionCallback callback; |
117 bool queued = boss.DoSomething(callback.callback()); | 117 bool queued = boss.DoSomething(callback.callback()); |
118 EXPECT_EQ(queued, true); | 118 EXPECT_EQ(queued, true); |
119 int result = callback.WaitForResult(); | 119 int result = callback.WaitForResult(); |
120 EXPECT_EQ(result, kMagicResult); | 120 EXPECT_EQ(result, kMagicResult); |
121 } | 121 } |
122 | 122 |
123 // TODO: test deleting ExampleEmployer while work outstanding | 123 // TODO: test deleting ExampleEmployer while work outstanding |
OLD | NEW |