| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "testing/gtest/include/gtest/gtest.h" | 7 #include "base/threading/worker_pool.h" |
| 8 #include "net/base/completion_callback.h" | 8 #include "net/base/completion_callback.h" |
| 9 #include "net/base/test_completion_callback.h" | 9 #include "net/base/test_completion_callback.h" |
| 10 #include "base/worker_pool.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
| 12 | 12 |
| 13 typedef PlatformTest TestCompletionCallbackTest; | 13 typedef PlatformTest TestCompletionCallbackTest; |
| 14 | 14 |
| 15 using net::CompletionCallback; | 15 using net::CompletionCallback; |
| 16 | 16 |
| 17 const int kMagicResult = 8888; | 17 const int kMagicResult = 8888; |
| 18 | 18 |
| 19 // ExampleEmployer is a toy version of HostResolver | 19 // ExampleEmployer is a toy version of HostResolver |
| 20 // TODO: restore damage done in extracting example from real code | 20 // TODO: restore damage done in extracting example from real code |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 95 |
| 96 ExampleEmployer::~ExampleEmployer() { | 96 ExampleEmployer::~ExampleEmployer() { |
| 97 } | 97 } |
| 98 | 98 |
| 99 bool ExampleEmployer::DoSomething(CompletionCallback* callback) { | 99 bool ExampleEmployer::DoSomething(CompletionCallback* callback) { |
| 100 DCHECK(!request_) << "already in use"; | 100 DCHECK(!request_) << "already in use"; |
| 101 | 101 |
| 102 request_ = new ExampleWorker(this, callback); | 102 request_ = new ExampleWorker(this, callback); |
| 103 | 103 |
| 104 // Dispatch to worker thread... | 104 // Dispatch to worker thread... |
| 105 if (!WorkerPool::PostTask(FROM_HERE, | 105 if (!base::WorkerPool::PostTask(FROM_HERE, |
| 106 NewRunnableMethod(request_.get(), &ExampleWorker::DoWork), true)) { | 106 NewRunnableMethod(request_.get(), &ExampleWorker::DoWork), true)) { |
| 107 NOTREACHED(); | 107 NOTREACHED(); |
| 108 request_ = NULL; | 108 request_ = NULL; |
| 109 return false; | 109 return false; |
| 110 } | 110 } |
| 111 | 111 |
| 112 return true; | 112 return true; |
| 113 } | 113 } |
| 114 | 114 |
| 115 TEST_F(TestCompletionCallbackTest, Simple) { | 115 TEST_F(TestCompletionCallbackTest, Simple) { |
| 116 ExampleEmployer boss; | 116 ExampleEmployer boss; |
| 117 TestCompletionCallback callback; | 117 TestCompletionCallback callback; |
| 118 bool queued = boss.DoSomething(&callback); | 118 bool queued = boss.DoSomething(&callback); |
| 119 EXPECT_EQ(queued, true); | 119 EXPECT_EQ(queued, true); |
| 120 int result = callback.WaitForResult(); | 120 int result = callback.WaitForResult(); |
| 121 EXPECT_EQ(result, kMagicResult); | 121 EXPECT_EQ(result, kMagicResult); |
| 122 } | 122 } |
| 123 | 123 |
| 124 // TODO: test deleting ExampleEmployer while work outstanding | 124 // TODO: test deleting ExampleEmployer while work outstanding |
| OLD | NEW |