OLD | NEW |
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" | 5 #include "base/threading/thread_checker.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/test/gtest_util.h" |
11 #include "base/threading/simple_thread.h" | 12 #include "base/threading/simple_thread.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 // Duplicated from base/threading/thread_checker.h so that we can be | 15 // Duplicated from base/threading/thread_checker.h so that we can be |
15 // good citizens there and undef the macro. | 16 // good citizens there and undef the macro. |
16 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 17 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
17 #define ENABLE_THREAD_CHECKER 1 | 18 #define ENABLE_THREAD_CHECKER 1 |
18 #else | 19 #else |
19 #define ENABLE_THREAD_CHECKER 0 | 20 #define ENABLE_THREAD_CHECKER 0 |
20 #endif | 21 #endif |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 112 |
112 // Verify that DoStuff doesn't assert when called on a different thread after | 113 // Verify that DoStuff doesn't assert when called on a different thread after |
113 // a call to DetachFromThread. | 114 // a call to DetachFromThread. |
114 thread_checker_class->DetachFromThread(); | 115 thread_checker_class->DetachFromThread(); |
115 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 116 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
116 | 117 |
117 call_on_thread.Start(); | 118 call_on_thread.Start(); |
118 call_on_thread.Join(); | 119 call_on_thread.Join(); |
119 } | 120 } |
120 | 121 |
121 #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER | |
122 | |
123 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { | 122 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { |
124 std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 123 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
125 new ThreadCheckerClass); | 124 new ThreadCheckerClass); |
126 | 125 |
127 // DoStuff should assert in debug builds only when called on a | 126 // DoStuff should assert in debug builds only when called on a |
128 // different thread. | 127 // different thread. |
129 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 128 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
130 | 129 |
131 call_on_thread.Start(); | 130 call_on_thread.Start(); |
132 call_on_thread.Join(); | 131 call_on_thread.Join(); |
133 } | 132 } |
134 | 133 |
135 #if ENABLE_THREAD_CHECKER | 134 #if ENABLE_THREAD_CHECKER |
136 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 135 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
137 ASSERT_DEATH({ | 136 ASSERT_DCHECK_DEATH({ ThreadCheckerClass::MethodOnDifferentThreadImpl(); }, |
138 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 137 ""); |
139 }, ""); | |
140 } | 138 } |
141 #else | 139 #else |
142 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { | 140 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { |
143 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 141 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
144 } | 142 } |
145 #endif // ENABLE_THREAD_CHECKER | 143 #endif // ENABLE_THREAD_CHECKER |
146 | 144 |
147 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { | 145 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { |
148 std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 146 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
149 new ThreadCheckerClass); | 147 new ThreadCheckerClass); |
150 | 148 |
151 // DoStuff doesn't assert when called on a different thread | 149 // DoStuff doesn't assert when called on a different thread |
152 // after a call to DetachFromThread. | 150 // after a call to DetachFromThread. |
153 thread_checker_class->DetachFromThread(); | 151 thread_checker_class->DetachFromThread(); |
154 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 152 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
155 | 153 |
156 call_on_thread.Start(); | 154 call_on_thread.Start(); |
157 call_on_thread.Join(); | 155 call_on_thread.Join(); |
158 | 156 |
159 // DoStuff should assert in debug builds only after moving to | 157 // DoStuff should assert in debug builds only after moving to |
160 // another thread. | 158 // another thread. |
161 thread_checker_class->DoStuff(); | 159 thread_checker_class->DoStuff(); |
162 } | 160 } |
163 | 161 |
164 #if ENABLE_THREAD_CHECKER | 162 #if ENABLE_THREAD_CHECKER |
165 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { | 163 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { |
166 ASSERT_DEATH({ | 164 ASSERT_DCHECK_DEATH( |
167 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 165 { ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); }, ""); |
168 }, ""); | |
169 } | 166 } |
170 #else | 167 #else |
171 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { | 168 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { |
172 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 169 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
173 } | 170 } |
174 #endif // ENABLE_THREAD_CHECKER | 171 #endif // ENABLE_THREAD_CHECKER |
175 | 172 |
176 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER | |
177 | |
178 // Just in case we ever get lumped together with other compilation units. | 173 // Just in case we ever get lumped together with other compilation units. |
179 #undef ENABLE_THREAD_CHECKER | 174 #undef ENABLE_THREAD_CHECKER |
180 | 175 |
181 } // namespace base | 176 } // namespace base |
OLD | NEW |