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. | |
joth
2011/12/21 14:17:16
yeah the duplication is unfortunate but I can't se
| |
14 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | |
15 #define ENABLE_THREAD_CHECKER 1 | |
16 #else | |
17 #define ENABLE_THREAD_CHECKER 0 | |
18 #endif | |
19 | |
12 namespace base { | 20 namespace base { |
13 | 21 |
14 // Simple class to exercise the basics of ThreadChecker. | 22 // Simple class to exercise the basics of ThreadChecker. |
15 // Both the destructor and DoStuff should verify that they were | 23 // Both the destructor and DoStuff should verify that they were |
16 // called on the same thread as the constructor. | 24 // called on the same thread as the constructor. |
17 class ThreadCheckerClass : public ThreadChecker { | 25 class ThreadCheckerClass : public ThreadChecker { |
18 public: | 26 public: |
19 ThreadCheckerClass() {} | 27 ThreadCheckerClass() {} |
20 | 28 |
21 // Verifies that it was called on the same thread as the constructor. | 29 // 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 | 108 |
101 // Verify that DoStuff doesn't assert when called on a different thread after | 109 // Verify that DoStuff doesn't assert when called on a different thread after |
102 // a call to DetachFromThread. | 110 // a call to DetachFromThread. |
103 thread_checker_class->DetachFromThread(); | 111 thread_checker_class->DetachFromThread(); |
104 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 112 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
105 | 113 |
106 call_on_thread.Start(); | 114 call_on_thread.Start(); |
107 call_on_thread.Join(); | 115 call_on_thread.Join(); |
108 } | 116 } |
109 | 117 |
110 #if GTEST_HAS_DEATH_TEST || NDEBUG | 118 #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
111 | 119 |
112 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { | 120 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { |
113 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 121 scoped_ptr<ThreadCheckerClass> thread_checker_class( |
114 new ThreadCheckerClass); | 122 new ThreadCheckerClass); |
115 | 123 |
116 // DoStuff should assert in debug builds only when called on a | 124 // DoStuff should assert in debug builds only when called on a |
117 // different thread. | 125 // different thread. |
118 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 126 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
119 | 127 |
120 call_on_thread.Start(); | 128 call_on_thread.Start(); |
121 call_on_thread.Join(); | 129 call_on_thread.Join(); |
122 } | 130 } |
123 | 131 |
124 #ifndef NDEBUG | 132 #if ENABLE_THREAD_CHECKER |
125 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 133 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
126 ASSERT_DEBUG_DEATH({ | 134 ASSERT_DEBUG_DEATH({ |
127 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 135 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
128 }, ""); | 136 }, ""); |
129 } | 137 } |
130 #else | 138 #else |
131 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { | 139 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { |
132 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 140 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
133 } | 141 } |
134 #endif // NDEBUG | 142 #endif // ENABLE_THREAD_CHECKER |
135 | 143 |
136 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { | 144 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { |
137 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 145 scoped_ptr<ThreadCheckerClass> thread_checker_class( |
138 new ThreadCheckerClass); | 146 new ThreadCheckerClass); |
139 | 147 |
140 // DoStuff doesn't assert when called on a different thread | 148 // DoStuff doesn't assert when called on a different thread |
141 // after a call to DetachFromThread. | 149 // after a call to DetachFromThread. |
142 thread_checker_class->DetachFromThread(); | 150 thread_checker_class->DetachFromThread(); |
143 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 151 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
144 | 152 |
145 call_on_thread.Start(); | 153 call_on_thread.Start(); |
146 call_on_thread.Join(); | 154 call_on_thread.Join(); |
147 | 155 |
148 // DoStuff should assert in debug builds only after moving to | 156 // DoStuff should assert in debug builds only after moving to |
149 // another thread. | 157 // another thread. |
150 thread_checker_class->DoStuff(); | 158 thread_checker_class->DoStuff(); |
151 } | 159 } |
152 | 160 |
153 #ifndef NDEBUG | 161 #if ENABLE_THREAD_CHECKER |
154 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { | 162 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { |
155 ASSERT_DEBUG_DEATH({ | 163 ASSERT_DEBUG_DEATH({ |
156 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 164 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
157 }, ""); | 165 }, ""); |
158 } | 166 } |
159 #else | 167 #else |
160 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { | 168 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { |
161 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 169 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
162 } | 170 } |
163 #endif // NDEBUG | 171 #endif // ENABLE_THREAD_CHECKER |
164 | 172 |
165 #endif // GTEST_HAS_DEATH_TEST || NDEBUG | 173 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
174 | |
175 // Just in case we ever get lumped together with other compilation units. | |
176 #undef ENABLE_THREAD_CHECKER | |
166 | 177 |
167 } // namespace base | 178 } // namespace base |
OLD | NEW |