Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / threading / thread_checker_unittest.cc
blobae96923e5caab9cc2eb0b76bc4f5314892f231d3
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
16 #else
17 #define ENABLE_THREAD_CHECKER 0
18 #endif
20 namespace base {
22 namespace {
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 {
28 public:
29 ThreadCheckerClass() {}
31 // Verifies that it was called on the same thread as the constructor.
32 void DoStuff() {
33 DCHECK(CalledOnValidThread());
36 void DetachFromThread() {
37 ThreadChecker::DetachFromThread();
40 static void MethodOnDifferentThreadImpl();
41 static void DetachThenCallFromDifferentThreadImpl();
43 private:
44 DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass);
47 // Calls ThreadCheckerClass::DoStuff on another thread.
48 class CallDoStuffOnThread : public base::SimpleThread {
49 public:
50 explicit CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class)
51 : SimpleThread("call_do_stuff_on_thread"),
52 thread_checker_class_(thread_checker_class) {
55 virtual void Run() OVERRIDE {
56 thread_checker_class_->DoStuff();
59 private:
60 ThreadCheckerClass* thread_checker_class_;
62 DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread);
65 // Deletes ThreadCheckerClass on a different thread.
66 class DeleteThreadCheckerClassOnThread : public base::SimpleThread {
67 public:
68 explicit DeleteThreadCheckerClassOnThread(
69 ThreadCheckerClass* thread_checker_class)
70 : SimpleThread("delete_thread_checker_class_on_thread"),
71 thread_checker_class_(thread_checker_class) {
74 virtual void Run() OVERRIDE {
75 thread_checker_class_.reset();
78 private:
79 scoped_ptr<ThreadCheckerClass> thread_checker_class_;
81 DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread);
84 } // namespace
86 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) {
87 scoped_ptr<ThreadCheckerClass> thread_checker_class(
88 new ThreadCheckerClass);
90 // Verify that DoStuff doesn't assert.
91 thread_checker_class->DoStuff();
93 // Verify that the destructor doesn't assert.
94 thread_checker_class.reset();
97 TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) {
98 scoped_ptr<ThreadCheckerClass> thread_checker_class(
99 new ThreadCheckerClass);
101 // Verify that the destructor doesn't assert
102 // when called on a different thread.
103 DeleteThreadCheckerClassOnThread delete_on_thread(
104 thread_checker_class.release());
106 delete_on_thread.Start();
107 delete_on_thread.Join();
110 TEST(ThreadCheckerTest, DetachFromThread) {
111 scoped_ptr<ThreadCheckerClass> thread_checker_class(
112 new ThreadCheckerClass);
114 // Verify that DoStuff doesn't assert when called on a different thread after
115 // a call to DetachFromThread.
116 thread_checker_class->DetachFromThread();
117 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
119 call_on_thread.Start();
120 call_on_thread.Join();
123 #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
125 void ThreadCheckerClass::MethodOnDifferentThreadImpl() {
126 scoped_ptr<ThreadCheckerClass> thread_checker_class(
127 new ThreadCheckerClass);
129 // DoStuff should assert in debug builds only when called on a
130 // different thread.
131 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
133 call_on_thread.Start();
134 call_on_thread.Join();
137 #if ENABLE_THREAD_CHECKER
138 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
139 ASSERT_DEATH({
140 ThreadCheckerClass::MethodOnDifferentThreadImpl();
141 }, "");
143 #else
144 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) {
145 ThreadCheckerClass::MethodOnDifferentThreadImpl();
147 #endif // ENABLE_THREAD_CHECKER
149 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() {
150 scoped_ptr<ThreadCheckerClass> thread_checker_class(
151 new ThreadCheckerClass);
153 // DoStuff doesn't assert when called on a different thread
154 // after a call to DetachFromThread.
155 thread_checker_class->DetachFromThread();
156 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
158 call_on_thread.Start();
159 call_on_thread.Join();
161 // DoStuff should assert in debug builds only after moving to
162 // another thread.
163 thread_checker_class->DoStuff();
166 #if ENABLE_THREAD_CHECKER
167 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) {
168 ASSERT_DEATH({
169 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
170 }, "");
172 #else
173 TEST(ThreadCheckerTest, DetachFromThreadInRelease) {
174 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
176 #endif // ENABLE_THREAD_CHECKER
178 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
180 // Just in case we ever get lumped together with other compilation units.
181 #undef ENABLE_THREAD_CHECKER
183 } // namespace base