Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: net/base/test_completion_callback_unittest.cc

Issue 693943003: Update from https://crrev.com/302630 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/test_completion_callback.cc ('k') | net/disk_cache/simple/simple_index_file.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
OLDNEW
« no previous file with comments | « net/base/test_completion_callback.cc ('k') | net/disk_cache/simple/simple_index_file.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698