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

Side by Side Diff: base/threading/thread_checker_unittest.cc

Issue 2869893003: New Sequence/Thread checking API. (Closed)
Patch Set: fix tests in non-dcheck builds, need fixture can't use lambdas Created 3 years, 7 months 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 | « base/threading/thread_checker.h ('k') | no next file » | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "base/threading/thread_checker.h"
6
5 #include <memory> 7 #include <memory>
6 8
7 #include "base/bind.h" 9 #include "base/bind.h"
8 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
9 #include "base/macros.h" 11 #include "base/macros.h"
10 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
11 #include "base/sequence_token.h" 13 #include "base/sequence_token.h"
14 #include "base/test/gtest_util.h"
12 #include "base/test/test_simple_task_runner.h" 15 #include "base/test/test_simple_task_runner.h"
13 #include "base/threading/simple_thread.h" 16 #include "base/threading/simple_thread.h"
14 #include "base/threading/thread_checker_impl.h"
15 #include "base/threading/thread_task_runner_handle.h" 17 #include "base/threading/thread_task_runner_handle.h"
16 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
17 19
18 namespace base { 20 namespace base {
19 namespace { 21 namespace {
20 22
21 // A thread that runs a callback. 23 // A thread that runs a callback.
22 class RunCallbackThread : public SimpleThread { 24 class RunCallbackThread : public SimpleThread {
23 public: 25 public:
24 explicit RunCallbackThread(const Closure& callback) 26 explicit RunCallbackThread(const Closure& callback)
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 thread_checker.DetachFromThread(); 187 thread_checker.DetachFromThread();
186 188
187 // Verify that CalledOnValidThread() returns true when called on a different 189 // Verify that CalledOnValidThread() returns true when called on a different
188 // thread after a call to DetachFromThread(). 190 // thread after a call to DetachFromThread().
189 RunCallbackOnNewThreadSynchronously( 191 RunCallbackOnNewThreadSynchronously(
190 Bind(&ExpectCalledOnValidThread, Unretained(&thread_checker))); 192 Bind(&ExpectCalledOnValidThread, Unretained(&thread_checker)));
191 193
192 EXPECT_FALSE(thread_checker.CalledOnValidThread()); 194 EXPECT_FALSE(thread_checker.CalledOnValidThread());
193 } 195 }
194 196
197 namespace {
198
199 // This fixture is a helper for unit testing the thread checker macros as it is
200 // not possible to inline ExpectDeathOnOtherThread() and
201 // ExpectNoDeathOnOtherThreadAfterDetach() as lambdas since binding
202 // |Unretained(&my_sequence_checker)| wouldn't compile on non-dcheck builds
203 // where it won't be defined.
204 class ThreadCheckerMacroTest : public testing::Test {
205 public:
206 ThreadCheckerMacroTest() = default;
207
208 void ExpectDeathOnOtherThread() {
209 #if DCHECK_IS_ON()
210 EXPECT_DCHECK_DEATH({ DCHECK_CALLED_ON_VALID_THREAD(my_thread_checker_); });
211 #else
212 // Happily no-ops on non-dcheck builds.
213 DCHECK_CALLED_ON_VALID_THREAD(my_thread_checker_);
214 #endif
215 }
216
217 void ExpectNoDeathOnOtherThreadAfterDetach() {
218 DCHECK_CALLED_ON_VALID_THREAD(my_thread_checker_);
219 }
220
221 protected:
222 THREAD_CHECKER(my_thread_checker_);
223
224 private:
225 DISALLOW_COPY_AND_ASSIGN(ThreadCheckerMacroTest);
226 };
227
228 } // namespace
229
230 TEST_F(ThreadCheckerMacroTest, Macros) {
231 THREAD_CHECKER(my_thread_checker);
232
233 RunCallbackOnNewThreadSynchronously(Bind(
234 &ThreadCheckerMacroTest::ExpectDeathOnOtherThread, Unretained(this)));
235
236 DETACH_FROM_THREAD(my_thread_checker_);
237
238 RunCallbackOnNewThreadSynchronously(
239 Bind(&ThreadCheckerMacroTest::ExpectNoDeathOnOtherThreadAfterDetach,
240 Unretained(this)));
241 }
242
195 } // namespace base 243 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/thread_checker.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698