OLD | NEW |
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 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/threading/thread_checker.h" | 8 #include "base/threading/thread_checker.h" |
9 #include "base/threading/simple_thread.h" | 9 #include "base/threading/simple_thread.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
| 12 // Duplicated from base/threading/thread_checker.h so that we can be |
| 13 // good citizens there and undef the macro. |
| 14 #define ENABLE_THREAD_CHECKER (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
| 15 |
12 namespace base { | 16 namespace base { |
13 | 17 |
14 // Simple class to exercise the basics of ThreadChecker. | 18 // Simple class to exercise the basics of ThreadChecker. |
15 // Both the destructor and DoStuff should verify that they were | 19 // Both the destructor and DoStuff should verify that they were |
16 // called on the same thread as the constructor. | 20 // called on the same thread as the constructor. |
17 class ThreadCheckerClass : public ThreadChecker { | 21 class ThreadCheckerClass : public ThreadChecker { |
18 public: | 22 public: |
19 ThreadCheckerClass() {} | 23 ThreadCheckerClass() {} |
20 | 24 |
21 // Verifies that it was called on the same thread as the constructor. | 25 // Verifies that it was called on the same thread as the constructor. |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 104 |
101 // Verify that DoStuff doesn't assert when called on a different thread after | 105 // Verify that DoStuff doesn't assert when called on a different thread after |
102 // a call to DetachFromThread. | 106 // a call to DetachFromThread. |
103 thread_checker_class->DetachFromThread(); | 107 thread_checker_class->DetachFromThread(); |
104 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 108 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
105 | 109 |
106 call_on_thread.Start(); | 110 call_on_thread.Start(); |
107 call_on_thread.Join(); | 111 call_on_thread.Join(); |
108 } | 112 } |
109 | 113 |
110 #if GTEST_HAS_DEATH_TEST || NDEBUG | 114 #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
111 | 115 |
112 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { | 116 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { |
113 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 117 scoped_ptr<ThreadCheckerClass> thread_checker_class( |
114 new ThreadCheckerClass); | 118 new ThreadCheckerClass); |
115 | 119 |
116 // DoStuff should assert in debug builds only when called on a | 120 // DoStuff should assert in debug builds only when called on a |
117 // different thread. | 121 // different thread. |
118 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 122 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
119 | 123 |
120 call_on_thread.Start(); | 124 call_on_thread.Start(); |
121 call_on_thread.Join(); | 125 call_on_thread.Join(); |
122 } | 126 } |
123 | 127 |
124 #ifndef NDEBUG | 128 #if ENABLE_THREAD_CHECKER |
125 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 129 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
126 ASSERT_DEBUG_DEATH({ | 130 ASSERT_DEBUG_DEATH({ |
127 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 131 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
128 }, ""); | 132 }, ""); |
129 } | 133 } |
130 #else | 134 #else |
131 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { | 135 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { |
132 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 136 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
133 } | 137 } |
134 #endif // NDEBUG | 138 #endif // ENABLE_THREAD_CHECKER |
135 | 139 |
136 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { | 140 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { |
137 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 141 scoped_ptr<ThreadCheckerClass> thread_checker_class( |
138 new ThreadCheckerClass); | 142 new ThreadCheckerClass); |
139 | 143 |
140 // DoStuff doesn't assert when called on a different thread | 144 // DoStuff doesn't assert when called on a different thread |
141 // after a call to DetachFromThread. | 145 // after a call to DetachFromThread. |
142 thread_checker_class->DetachFromThread(); | 146 thread_checker_class->DetachFromThread(); |
143 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 147 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
144 | 148 |
145 call_on_thread.Start(); | 149 call_on_thread.Start(); |
146 call_on_thread.Join(); | 150 call_on_thread.Join(); |
147 | 151 |
148 // DoStuff should assert in debug builds only after moving to | 152 // DoStuff should assert in debug builds only after moving to |
149 // another thread. | 153 // another thread. |
150 thread_checker_class->DoStuff(); | 154 thread_checker_class->DoStuff(); |
151 } | 155 } |
152 | 156 |
153 #ifndef NDEBUG | 157 #if ENABLE_THREAD_CHECKER |
154 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { | 158 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { |
155 ASSERT_DEBUG_DEATH({ | 159 ASSERT_DEBUG_DEATH({ |
156 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 160 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
157 }, ""); | 161 }, ""); |
158 } | 162 } |
159 #else | 163 #else |
160 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { | 164 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { |
161 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 165 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
162 } | 166 } |
163 #endif // NDEBUG | 167 #endif // ENABLE_THREAD_CHECKER |
164 | 168 |
165 #endif // GTEST_HAS_DEATH_TEST || NDEBUG | 169 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
| 170 |
| 171 // Just in case we ever get lumped together with other compilation units. |
| 172 #undef ENABLE_THREAD_CHECKER |
166 | 173 |
167 } // namespace base | 174 } // namespace base |
OLD | NEW |