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/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
11 #include "net/base/completion_callback.h" | 11 #include "net/base/completion_callback.h" |
12 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
15 | 15 |
16 typedef PlatformTest TestCompletionCallbackTest; | 16 namespace { |
17 | 17 |
18 const int kMagicResult = 8888; | 18 const int kMagicResult = 8888; |
19 | 19 |
| 20 void CallClosureAfterCheckingResult(const base::Closure& closure, |
| 21 bool* did_check_result, |
| 22 int result) { |
| 23 DCHECK_EQ(result, kMagicResult); |
| 24 *did_check_result = true; |
| 25 closure.Run(); |
| 26 } |
| 27 |
20 // ExampleEmployer is a toy version of HostResolver | 28 // ExampleEmployer is a toy version of HostResolver |
21 // TODO: restore damage done in extracting example from real code | 29 // TODO: restore damage done in extracting example from real code |
22 // (e.g. bring back real destructor, bring back comments) | 30 // (e.g. bring back real destructor, bring back comments) |
23 class ExampleEmployer { | 31 class ExampleEmployer { |
24 public: | 32 public: |
25 ExampleEmployer(); | 33 ExampleEmployer(); |
26 ~ExampleEmployer(); | 34 ~ExampleEmployer(); |
27 | 35 |
28 // Do some imaginary work on a worker thread; | 36 // Do some imaginary work on a worker thread; |
29 // when done, worker posts callback on the original thread. | 37 // when done, worker posts callback on the original thread. |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 base::Bind(&ExampleWorker::DoWork, request_.get()), | 112 base::Bind(&ExampleWorker::DoWork, request_.get()), |
105 true)) { | 113 true)) { |
106 NOTREACHED(); | 114 NOTREACHED(); |
107 request_ = NULL; | 115 request_ = NULL; |
108 return false; | 116 return false; |
109 } | 117 } |
110 | 118 |
111 return true; | 119 return true; |
112 } | 120 } |
113 | 121 |
| 122 } // namespace |
| 123 |
| 124 typedef PlatformTest TestCompletionCallbackTest; |
| 125 |
114 TEST_F(TestCompletionCallbackTest, Simple) { | 126 TEST_F(TestCompletionCallbackTest, Simple) { |
115 ExampleEmployer boss; | 127 ExampleEmployer boss; |
116 net::TestCompletionCallback callback; | 128 net::TestCompletionCallback callback; |
117 bool queued = boss.DoSomething(callback.callback()); | 129 bool queued = boss.DoSomething(callback.callback()); |
118 EXPECT_EQ(queued, true); | 130 EXPECT_TRUE(queued); |
119 int result = callback.WaitForResult(); | 131 int result = callback.WaitForResult(); |
120 EXPECT_EQ(result, kMagicResult); | 132 EXPECT_EQ(result, kMagicResult); |
121 } | 133 } |
122 | 134 |
| 135 TEST_F(TestCompletionCallbackTest, Closure) { |
| 136 ExampleEmployer boss; |
| 137 net::TestClosure closure; |
| 138 bool did_check_result = false; |
| 139 net::CompletionCallback completion_callback = |
| 140 base::Bind(&CallClosureAfterCheckingResult, |
| 141 closure.closure(), base::Unretained(&did_check_result)); |
| 142 bool queued = boss.DoSomething(completion_callback); |
| 143 EXPECT_TRUE(queued); |
| 144 |
| 145 EXPECT_FALSE(did_check_result); |
| 146 closure.WaitForResult(); |
| 147 EXPECT_TRUE(did_check_result); |
| 148 } |
| 149 |
123 // TODO: test deleting ExampleEmployer while work outstanding | 150 // TODO: test deleting ExampleEmployer while work outstanding |
OLD | NEW |