| 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/test/gtest_util.h" |
| 12 #include "base/threading/simple_thread.h" | 12 #include "base/threading/simple_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 // Duplicated from base/threading/thread_checker.h so that we can be | |
| 16 // good citizens there and undef the macro. | |
| 17 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | |
| 18 #define ENABLE_THREAD_CHECKER 1 | |
| 19 #else | |
| 20 #define ENABLE_THREAD_CHECKER 0 | |
| 21 #endif | |
| 22 | |
| 23 namespace base { | 15 namespace base { |
| 24 | 16 |
| 25 namespace { | 17 namespace { |
| 26 | 18 |
| 27 // Simple class to exercise the basics of ThreadChecker. | 19 // Simple class to exercise the basics of ThreadChecker. |
| 28 // Both the destructor and DoStuff should verify that they were | 20 // Both the destructor and DoStuff should verify that they were |
| 29 // called on the same thread as the constructor. | 21 // called on the same thread as the constructor. |
| 30 class ThreadCheckerClass : public ThreadChecker { | 22 class ThreadCheckerClass : public ThreadChecker { |
| 31 public: | 23 public: |
| 32 ThreadCheckerClass() {} | 24 ThreadCheckerClass() {} |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 new ThreadCheckerClass); | 116 new ThreadCheckerClass); |
| 125 | 117 |
| 126 // DoStuff should assert in debug builds only when called on a | 118 // DoStuff should assert in debug builds only when called on a |
| 127 // different thread. | 119 // different thread. |
| 128 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 120 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 129 | 121 |
| 130 call_on_thread.Start(); | 122 call_on_thread.Start(); |
| 131 call_on_thread.Join(); | 123 call_on_thread.Join(); |
| 132 } | 124 } |
| 133 | 125 |
| 134 #if ENABLE_THREAD_CHECKER | 126 #if DCHECK_IS_ON() |
| 135 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 127 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
| 136 ASSERT_DCHECK_DEATH({ ThreadCheckerClass::MethodOnDifferentThreadImpl(); }, | 128 ASSERT_DCHECK_DEATH({ ThreadCheckerClass::MethodOnDifferentThreadImpl(); }, |
| 137 ""); | 129 ""); |
| 138 } | 130 } |
| 139 #else | 131 #else |
| 140 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { | 132 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { |
| 141 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 133 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
| 142 } | 134 } |
| 143 #endif // ENABLE_THREAD_CHECKER | 135 #endif // DCHECK_IS_ON() |
| 144 | 136 |
| 145 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { | 137 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { |
| 146 std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 138 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 147 new ThreadCheckerClass); | 139 new ThreadCheckerClass); |
| 148 | 140 |
| 149 // DoStuff doesn't assert when called on a different thread | 141 // DoStuff doesn't assert when called on a different thread |
| 150 // after a call to DetachFromThread. | 142 // after a call to DetachFromThread. |
| 151 thread_checker_class->DetachFromThread(); | 143 thread_checker_class->DetachFromThread(); |
| 152 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 144 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 153 | 145 |
| 154 call_on_thread.Start(); | 146 call_on_thread.Start(); |
| 155 call_on_thread.Join(); | 147 call_on_thread.Join(); |
| 156 | 148 |
| 157 // DoStuff should assert in debug builds only after moving to | 149 // DoStuff should assert in debug builds only after moving to |
| 158 // another thread. | 150 // another thread. |
| 159 thread_checker_class->DoStuff(); | 151 thread_checker_class->DoStuff(); |
| 160 } | 152 } |
| 161 | 153 |
| 162 #if ENABLE_THREAD_CHECKER | 154 #if DCHECK_IS_ON() |
| 163 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { | 155 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { |
| 164 ASSERT_DCHECK_DEATH( | 156 ASSERT_DCHECK_DEATH( |
| 165 { ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); }, ""); | 157 { ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); }, ""); |
| 166 } | 158 } |
| 167 #else | 159 #else |
| 168 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { | 160 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { |
| 169 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 161 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
| 170 } | 162 } |
| 171 #endif // ENABLE_THREAD_CHECKER | 163 #endif // DCHECK_IS_ON() |
| 172 | |
| 173 // Just in case we ever get lumped together with other compilation units. | |
| 174 #undef ENABLE_THREAD_CHECKER | |
| 175 | 164 |
| 176 } // namespace base | 165 } // namespace base |
| OLD | NEW |