| 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" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 // DoStuff should assert in debug builds only when called on a | 118 // DoStuff should assert in debug builds only when called on a |
| 119 // different thread. | 119 // different thread. |
| 120 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 120 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 121 | 121 |
| 122 call_on_thread.Start(); | 122 call_on_thread.Start(); |
| 123 call_on_thread.Join(); | 123 call_on_thread.Join(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 #if DCHECK_IS_ON() | 126 #if DCHECK_IS_ON() |
| 127 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 127 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
| 128 ASSERT_DCHECK_DEATH({ ThreadCheckerClass::MethodOnDifferentThreadImpl(); }, | 128 ASSERT_DCHECK_DEATH({ ThreadCheckerClass::MethodOnDifferentThreadImpl(); }); |
| 129 ""); | |
| 130 } | 129 } |
| 131 #else | 130 #else |
| 132 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { | 131 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { |
| 133 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 132 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
| 134 } | 133 } |
| 135 #endif // DCHECK_IS_ON() | 134 #endif // DCHECK_IS_ON() |
| 136 | 135 |
| 137 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { | 136 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { |
| 138 std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 137 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 139 new ThreadCheckerClass); | 138 new ThreadCheckerClass); |
| 140 | 139 |
| 141 // DoStuff doesn't assert when called on a different thread | 140 // DoStuff doesn't assert when called on a different thread |
| 142 // after a call to DetachFromThread. | 141 // after a call to DetachFromThread. |
| 143 thread_checker_class->DetachFromThread(); | 142 thread_checker_class->DetachFromThread(); |
| 144 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 143 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 145 | 144 |
| 146 call_on_thread.Start(); | 145 call_on_thread.Start(); |
| 147 call_on_thread.Join(); | 146 call_on_thread.Join(); |
| 148 | 147 |
| 149 // DoStuff should assert in debug builds only after moving to | 148 // DoStuff should assert in debug builds only after moving to |
| 150 // another thread. | 149 // another thread. |
| 151 thread_checker_class->DoStuff(); | 150 thread_checker_class->DoStuff(); |
| 152 } | 151 } |
| 153 | 152 |
| 154 #if DCHECK_IS_ON() | 153 #if DCHECK_IS_ON() |
| 155 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { | 154 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { |
| 156 ASSERT_DCHECK_DEATH( | 155 ASSERT_DCHECK_DEATH( |
| 157 { ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); }, ""); | 156 { ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); }); |
| 158 } | 157 } |
| 159 #else | 158 #else |
| 160 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { | 159 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { |
| 161 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 160 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
| 162 } | 161 } |
| 163 #endif // DCHECK_IS_ON() | 162 #endif // DCHECK_IS_ON() |
| 164 | 163 |
| 165 } // namespace base | 164 } // namespace base |
| OLD | NEW |