| 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/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 | 12 // Duplicated from base/threading/thread_checker.h so that we can be |
| 13 // good citizens there and undef the macro. | 13 // good citizens there and undef the macro. |
| 14 #define ENABLE_THREAD_CHECKER (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | 14 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 15 #define ENABLE_THREAD_CHECKER 1 |
| 16 #else |
| 17 #define ENABLE_THREAD_CHECKER 0 |
| 18 #endif |
| 15 | 19 |
| 16 namespace base { | 20 namespace base { |
| 17 | 21 |
| 22 namespace { |
| 23 |
| 18 // Simple class to exercise the basics of ThreadChecker. | 24 // Simple class to exercise the basics of ThreadChecker. |
| 19 // Both the destructor and DoStuff should verify that they were | 25 // Both the destructor and DoStuff should verify that they were |
| 20 // called on the same thread as the constructor. | 26 // called on the same thread as the constructor. |
| 21 class ThreadCheckerClass : public ThreadChecker { | 27 class ThreadCheckerClass : public ThreadChecker { |
| 22 public: | 28 public: |
| 23 ThreadCheckerClass() {} | 29 ThreadCheckerClass() {} |
| 24 | 30 |
| 25 // Verifies that it was called on the same thread as the constructor. | 31 // Verifies that it was called on the same thread as the constructor. |
| 26 void DoStuff() { | 32 void DoStuff() { |
| 27 DCHECK(CalledOnValidThread()); | 33 DCHECK(CalledOnValidThread()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 virtual void Run() OVERRIDE { | 73 virtual void Run() OVERRIDE { |
| 68 thread_checker_class_.reset(); | 74 thread_checker_class_.reset(); |
| 69 } | 75 } |
| 70 | 76 |
| 71 private: | 77 private: |
| 72 scoped_ptr<ThreadCheckerClass> thread_checker_class_; | 78 scoped_ptr<ThreadCheckerClass> thread_checker_class_; |
| 73 | 79 |
| 74 DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); | 80 DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); |
| 75 }; | 81 }; |
| 76 | 82 |
| 83 } // namespace |
| 84 |
| 77 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { | 85 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { |
| 78 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 86 scoped_ptr<ThreadCheckerClass> thread_checker_class( |
| 79 new ThreadCheckerClass); | 87 new ThreadCheckerClass); |
| 80 | 88 |
| 81 // Verify that DoStuff doesn't assert. | 89 // Verify that DoStuff doesn't assert. |
| 82 thread_checker_class->DoStuff(); | 90 thread_checker_class->DoStuff(); |
| 83 | 91 |
| 84 // Verify that the destructor doesn't assert. | 92 // Verify that the destructor doesn't assert. |
| 85 thread_checker_class.reset(); | 93 thread_checker_class.reset(); |
| 86 } | 94 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 // DoStuff should assert in debug builds only when called on a | 128 // DoStuff should assert in debug builds only when called on a |
| 121 // different thread. | 129 // different thread. |
| 122 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 130 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 123 | 131 |
| 124 call_on_thread.Start(); | 132 call_on_thread.Start(); |
| 125 call_on_thread.Join(); | 133 call_on_thread.Join(); |
| 126 } | 134 } |
| 127 | 135 |
| 128 #if ENABLE_THREAD_CHECKER | 136 #if ENABLE_THREAD_CHECKER |
| 129 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 137 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
| 130 ASSERT_DEBUG_DEATH({ | 138 ASSERT_DEATH({ |
| 131 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 139 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
| 132 }, ""); | 140 }, ""); |
| 133 } | 141 } |
| 134 #else | 142 #else |
| 135 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { | 143 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { |
| 136 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 144 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
| 137 } | 145 } |
| 138 #endif // ENABLE_THREAD_CHECKER | 146 #endif // ENABLE_THREAD_CHECKER |
| 139 | 147 |
| 140 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { | 148 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { |
| 141 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 149 scoped_ptr<ThreadCheckerClass> thread_checker_class( |
| 142 new ThreadCheckerClass); | 150 new ThreadCheckerClass); |
| 143 | 151 |
| 144 // DoStuff doesn't assert when called on a different thread | 152 // DoStuff doesn't assert when called on a different thread |
| 145 // after a call to DetachFromThread. | 153 // after a call to DetachFromThread. |
| 146 thread_checker_class->DetachFromThread(); | 154 thread_checker_class->DetachFromThread(); |
| 147 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 155 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 148 | 156 |
| 149 call_on_thread.Start(); | 157 call_on_thread.Start(); |
| 150 call_on_thread.Join(); | 158 call_on_thread.Join(); |
| 151 | 159 |
| 152 // DoStuff should assert in debug builds only after moving to | 160 // DoStuff should assert in debug builds only after moving to |
| 153 // another thread. | 161 // another thread. |
| 154 thread_checker_class->DoStuff(); | 162 thread_checker_class->DoStuff(); |
| 155 } | 163 } |
| 156 | 164 |
| 157 #if ENABLE_THREAD_CHECKER | 165 #if ENABLE_THREAD_CHECKER |
| 158 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { | 166 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { |
| 159 ASSERT_DEBUG_DEATH({ | 167 ASSERT_DEATH({ |
| 160 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 168 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
| 161 }, ""); | 169 }, ""); |
| 162 } | 170 } |
| 163 #else | 171 #else |
| 164 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { | 172 TEST(ThreadCheckerTest, DetachFromThreadInRelease) { |
| 165 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 173 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
| 166 } | 174 } |
| 167 #endif // ENABLE_THREAD_CHECKER | 175 #endif // ENABLE_THREAD_CHECKER |
| 168 | 176 |
| 169 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER | 177 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
| 170 | 178 |
| 171 // Just in case we ever get lumped together with other compilation units. | 179 // Just in case we ever get lumped together with other compilation units. |
| 172 #undef ENABLE_THREAD_CHECKER | 180 #undef ENABLE_THREAD_CHECKER |
| 173 | 181 |
| 174 } // namespace base | 182 } // namespace base |
| OLD | NEW |