| 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" | 
|  | 6 | 
|  | 7 #include <memory> | 
|  | 8 | 
| 5 #include "base/logging.h" | 9 #include "base/logging.h" | 
| 6 #include "base/macros.h" | 10 #include "base/macros.h" | 
| 7 #include "base/memory/scoped_ptr.h" |  | 
| 8 #include "base/threading/simple_thread.h" | 11 #include "base/threading/simple_thread.h" | 
| 9 #include "base/threading/thread_checker.h" |  | 
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" | 
| 11 | 13 | 
| 12 // Duplicated from base/threading/thread_checker.h so that we can be | 14 // Duplicated from base/threading/thread_checker.h so that we can be | 
| 13 // good citizens there and undef the macro. | 15 // good citizens there and undef the macro. | 
| 14 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 16 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 
| 15 #define ENABLE_THREAD_CHECKER 1 | 17 #define ENABLE_THREAD_CHECKER 1 | 
| 16 #else | 18 #else | 
| 17 #define ENABLE_THREAD_CHECKER 0 | 19 #define ENABLE_THREAD_CHECKER 0 | 
| 18 #endif | 20 #endif | 
| 19 | 21 | 
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 65  public: | 67  public: | 
| 66   explicit DeleteThreadCheckerClassOnThread( | 68   explicit DeleteThreadCheckerClassOnThread( | 
| 67       ThreadCheckerClass* thread_checker_class) | 69       ThreadCheckerClass* thread_checker_class) | 
| 68       : SimpleThread("delete_thread_checker_class_on_thread"), | 70       : SimpleThread("delete_thread_checker_class_on_thread"), | 
| 69         thread_checker_class_(thread_checker_class) { | 71         thread_checker_class_(thread_checker_class) { | 
| 70   } | 72   } | 
| 71 | 73 | 
| 72   void Run() override { thread_checker_class_.reset(); } | 74   void Run() override { thread_checker_class_.reset(); } | 
| 73 | 75 | 
| 74  private: | 76  private: | 
| 75   scoped_ptr<ThreadCheckerClass> thread_checker_class_; | 77   std::unique_ptr<ThreadCheckerClass> thread_checker_class_; | 
| 76 | 78 | 
| 77   DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); | 79   DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); | 
| 78 }; | 80 }; | 
| 79 | 81 | 
| 80 }  // namespace | 82 }  // namespace | 
| 81 | 83 | 
| 82 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { | 84 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { | 
| 83   scoped_ptr<ThreadCheckerClass> thread_checker_class( | 85   std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 
| 84       new ThreadCheckerClass); | 86       new ThreadCheckerClass); | 
| 85 | 87 | 
| 86   // Verify that DoStuff doesn't assert. | 88   // Verify that DoStuff doesn't assert. | 
| 87   thread_checker_class->DoStuff(); | 89   thread_checker_class->DoStuff(); | 
| 88 | 90 | 
| 89   // Verify that the destructor doesn't assert. | 91   // Verify that the destructor doesn't assert. | 
| 90   thread_checker_class.reset(); | 92   thread_checker_class.reset(); | 
| 91 } | 93 } | 
| 92 | 94 | 
| 93 TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) { | 95 TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) { | 
| 94   scoped_ptr<ThreadCheckerClass> thread_checker_class( | 96   std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 
| 95       new ThreadCheckerClass); | 97       new ThreadCheckerClass); | 
| 96 | 98 | 
| 97   // Verify that the destructor doesn't assert | 99   // Verify that the destructor doesn't assert | 
| 98   // when called on a different thread. | 100   // when called on a different thread. | 
| 99   DeleteThreadCheckerClassOnThread delete_on_thread( | 101   DeleteThreadCheckerClassOnThread delete_on_thread( | 
| 100       thread_checker_class.release()); | 102       thread_checker_class.release()); | 
| 101 | 103 | 
| 102   delete_on_thread.Start(); | 104   delete_on_thread.Start(); | 
| 103   delete_on_thread.Join(); | 105   delete_on_thread.Join(); | 
| 104 } | 106 } | 
| 105 | 107 | 
| 106 TEST(ThreadCheckerTest, DetachFromThread) { | 108 TEST(ThreadCheckerTest, DetachFromThread) { | 
| 107   scoped_ptr<ThreadCheckerClass> thread_checker_class( | 109   std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 
| 108       new ThreadCheckerClass); | 110       new ThreadCheckerClass); | 
| 109 | 111 | 
| 110   // Verify that DoStuff doesn't assert when called on a different thread after | 112   // Verify that DoStuff doesn't assert when called on a different thread after | 
| 111   // a call to DetachFromThread. | 113   // a call to DetachFromThread. | 
| 112   thread_checker_class->DetachFromThread(); | 114   thread_checker_class->DetachFromThread(); | 
| 113   CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 115   CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 
| 114 | 116 | 
| 115   call_on_thread.Start(); | 117   call_on_thread.Start(); | 
| 116   call_on_thread.Join(); | 118   call_on_thread.Join(); | 
| 117 } | 119 } | 
| 118 | 120 | 
| 119 #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER | 121 #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER | 
| 120 | 122 | 
| 121 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { | 123 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { | 
| 122   scoped_ptr<ThreadCheckerClass> thread_checker_class( | 124   std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 
| 123       new ThreadCheckerClass); | 125       new ThreadCheckerClass); | 
| 124 | 126 | 
| 125   // DoStuff should assert in debug builds only when called on a | 127   // DoStuff should assert in debug builds only when called on a | 
| 126   // different thread. | 128   // different thread. | 
| 127   CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 129   CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 
| 128 | 130 | 
| 129   call_on_thread.Start(); | 131   call_on_thread.Start(); | 
| 130   call_on_thread.Join(); | 132   call_on_thread.Join(); | 
| 131 } | 133 } | 
| 132 | 134 | 
| 133 #if ENABLE_THREAD_CHECKER | 135 #if ENABLE_THREAD_CHECKER | 
| 134 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 136 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 
| 135   ASSERT_DEATH({ | 137   ASSERT_DEATH({ | 
| 136       ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 138       ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 
| 137     }, ""); | 139     }, ""); | 
| 138 } | 140 } | 
| 139 #else | 141 #else | 
| 140 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { | 142 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { | 
| 141   ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 143   ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 
| 142 } | 144 } | 
| 143 #endif  // ENABLE_THREAD_CHECKER | 145 #endif  // ENABLE_THREAD_CHECKER | 
| 144 | 146 | 
| 145 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { | 147 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { | 
| 146   scoped_ptr<ThreadCheckerClass> thread_checker_class( | 148   std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 
| 147       new ThreadCheckerClass); | 149       new ThreadCheckerClass); | 
| 148 | 150 | 
| 149   // DoStuff doesn't assert when called on a different thread | 151   // DoStuff doesn't assert when called on a different thread | 
| 150   // after a call to DetachFromThread. | 152   // after a call to DetachFromThread. | 
| 151   thread_checker_class->DetachFromThread(); | 153   thread_checker_class->DetachFromThread(); | 
| 152   CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 154   CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 
| 153 | 155 | 
| 154   call_on_thread.Start(); | 156   call_on_thread.Start(); | 
| 155   call_on_thread.Join(); | 157   call_on_thread.Join(); | 
| 156 | 158 | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 170   ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 172   ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 
| 171 } | 173 } | 
| 172 #endif  // ENABLE_THREAD_CHECKER | 174 #endif  // ENABLE_THREAD_CHECKER | 
| 173 | 175 | 
| 174 #endif  // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER | 176 #endif  // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER | 
| 175 | 177 | 
| 176 // Just in case we ever get lumped together with other compilation units. | 178 // Just in case we ever get lumped together with other compilation units. | 
| 177 #undef ENABLE_THREAD_CHECKER | 179 #undef ENABLE_THREAD_CHECKER | 
| 178 | 180 | 
| 179 }  // namespace base | 181 }  // namespace base | 
| OLD | NEW | 
|---|