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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/threading/thread_checker.h"
9 #include "base/threading/simple_thread.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 // Duplicated from base/threading/thread_checker.h so that we can be
13 // good citizens there and undef the macro.
14 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
15 #define ENABLE_THREAD_CHECKER 1
17 #define ENABLE_THREAD_CHECKER 0
24 // Simple class to exercise the basics of ThreadChecker.
25 // Both the destructor and DoStuff should verify that they were
26 // called on the same thread as the constructor.
27 class ThreadCheckerClass
: public ThreadChecker
{
29 ThreadCheckerClass() {}
31 // Verifies that it was called on the same thread as the constructor.
33 DCHECK(CalledOnValidThread());
36 void DetachFromThread() {
37 ThreadChecker::DetachFromThread();
40 static void MethodOnDifferentThreadImpl();
41 static void DetachThenCallFromDifferentThreadImpl();
44 DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass
);
47 // Calls ThreadCheckerClass::DoStuff on another thread.
48 class CallDoStuffOnThread
: public base::SimpleThread
{
50 explicit CallDoStuffOnThread(ThreadCheckerClass
* thread_checker_class
)
51 : SimpleThread("call_do_stuff_on_thread"),
52 thread_checker_class_(thread_checker_class
) {
55 void Run() override
{ thread_checker_class_
->DoStuff(); }
58 ThreadCheckerClass
* thread_checker_class_
;
60 DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread
);
63 // Deletes ThreadCheckerClass on a different thread.
64 class DeleteThreadCheckerClassOnThread
: public base::SimpleThread
{
66 explicit DeleteThreadCheckerClassOnThread(
67 ThreadCheckerClass
* thread_checker_class
)
68 : SimpleThread("delete_thread_checker_class_on_thread"),
69 thread_checker_class_(thread_checker_class
) {
72 void Run() override
{ thread_checker_class_
.reset(); }
75 scoped_ptr
<ThreadCheckerClass
> thread_checker_class_
;
77 DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread
);
82 TEST(ThreadCheckerTest
, CallsAllowedOnSameThread
) {
83 scoped_ptr
<ThreadCheckerClass
> thread_checker_class(
84 new ThreadCheckerClass
);
86 // Verify that DoStuff doesn't assert.
87 thread_checker_class
->DoStuff();
89 // Verify that the destructor doesn't assert.
90 thread_checker_class
.reset();
93 TEST(ThreadCheckerTest
, DestructorAllowedOnDifferentThread
) {
94 scoped_ptr
<ThreadCheckerClass
> thread_checker_class(
95 new ThreadCheckerClass
);
97 // Verify that the destructor doesn't assert
98 // when called on a different thread.
99 DeleteThreadCheckerClassOnThread
delete_on_thread(
100 thread_checker_class
.release());
102 delete_on_thread
.Start();
103 delete_on_thread
.Join();
106 TEST(ThreadCheckerTest
, DetachFromThread
) {
107 scoped_ptr
<ThreadCheckerClass
> thread_checker_class(
108 new ThreadCheckerClass
);
110 // Verify that DoStuff doesn't assert when called on a different thread after
111 // a call to DetachFromThread.
112 thread_checker_class
->DetachFromThread();
113 CallDoStuffOnThread
call_on_thread(thread_checker_class
.get());
115 call_on_thread
.Start();
116 call_on_thread
.Join();
119 #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
121 void ThreadCheckerClass::MethodOnDifferentThreadImpl() {
122 scoped_ptr
<ThreadCheckerClass
> thread_checker_class(
123 new ThreadCheckerClass
);
125 // DoStuff should assert in debug builds only when called on a
127 CallDoStuffOnThread
call_on_thread(thread_checker_class
.get());
129 call_on_thread
.Start();
130 call_on_thread
.Join();
133 #if ENABLE_THREAD_CHECKER
134 TEST(ThreadCheckerDeathTest
, MethodNotAllowedOnDifferentThreadInDebug
) {
136 ThreadCheckerClass::MethodOnDifferentThreadImpl();
140 TEST(ThreadCheckerTest
, MethodAllowedOnDifferentThreadInRelease
) {
141 ThreadCheckerClass::MethodOnDifferentThreadImpl();
143 #endif // ENABLE_THREAD_CHECKER
145 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() {
146 scoped_ptr
<ThreadCheckerClass
> thread_checker_class(
147 new ThreadCheckerClass
);
149 // DoStuff doesn't assert when called on a different thread
150 // after a call to DetachFromThread.
151 thread_checker_class
->DetachFromThread();
152 CallDoStuffOnThread
call_on_thread(thread_checker_class
.get());
154 call_on_thread
.Start();
155 call_on_thread
.Join();
157 // DoStuff should assert in debug builds only after moving to
159 thread_checker_class
->DoStuff();
162 #if ENABLE_THREAD_CHECKER
163 TEST(ThreadCheckerDeathTest
, DetachFromThreadInDebug
) {
165 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
169 TEST(ThreadCheckerTest
, DetachFromThreadInRelease
) {
170 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
172 #endif // ENABLE_THREAD_CHECKER
174 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
176 // Just in case we ever get lumped together with other compilation units.
177 #undef ENABLE_THREAD_CHECKER