| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
| 8 #include "base/threading/non_thread_safe.h" | 8 #include "base/threading/non_thread_safe.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 #ifndef NDEBUG | |
| 13 | |
| 14 namespace base { | 12 namespace base { |
| 15 | 13 |
| 16 // Simple class to exersice the basics of NonThreadSafe. | 14 // Simple class to exersice the basics of NonThreadSafe. |
| 17 // Both the destructor and DoStuff should verify that they were | 15 // Both the destructor and DoStuff should verify that they were |
| 18 // called on the same thread as the constructor. | 16 // called on the same thread as the constructor. |
| 19 class NonThreadSafeClass : public NonThreadSafe { | 17 class NonThreadSafeClass : public NonThreadSafe { |
| 20 public: | 18 public: |
| 21 NonThreadSafeClass() {} | 19 NonThreadSafeClass() {} |
| 22 | 20 |
| 23 // Verifies that it was called on the same thread as the constructor. | 21 // Verifies that it was called on the same thread as the constructor. |
| 24 void DoStuff() { | 22 void DoStuff() { |
| 25 DCHECK(CalledOnValidThread()); | 23 CHECK(CalledOnValidThread()); |
| 26 } | 24 } |
| 27 | 25 |
| 28 void DetachFromThread() { | 26 void DetachFromThread() { |
| 29 NonThreadSafe::DetachFromThread(); | 27 NonThreadSafe::DetachFromThread(); |
| 30 } | 28 } |
| 31 | 29 |
| 30 static void MethodOnDifferentThreadImpl(); |
| 31 static void DestructorOnDifferentThreadImpl(); |
| 32 |
| 32 private: | 33 private: |
| 33 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeClass); | 34 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeClass); |
| 34 }; | 35 }; |
| 35 | 36 |
| 36 // Calls NonThreadSafeClass::DoStuff on another thread. | 37 // Calls NonThreadSafeClass::DoStuff on another thread. |
| 37 class CallDoStuffOnThread : public SimpleThread { | 38 class CallDoStuffOnThread : public SimpleThread { |
| 38 public: | 39 public: |
| 39 CallDoStuffOnThread(NonThreadSafeClass* non_thread_safe_class) | 40 CallDoStuffOnThread(NonThreadSafeClass* non_thread_safe_class) |
| 40 : SimpleThread("call_do_stuff_on_thread"), | 41 : SimpleThread("call_do_stuff_on_thread"), |
| 41 non_thread_safe_class_(non_thread_safe_class) { | 42 non_thread_safe_class_(non_thread_safe_class) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // Verify that the destructor doesn't assert when called on a different thread | 88 // Verify that the destructor doesn't assert when called on a different thread |
| 88 // after a detach. | 89 // after a detach. |
| 89 non_thread_safe_class->DetachFromThread(); | 90 non_thread_safe_class->DetachFromThread(); |
| 90 DeleteNonThreadSafeClassOnThread delete_on_thread( | 91 DeleteNonThreadSafeClassOnThread delete_on_thread( |
| 91 non_thread_safe_class.release()); | 92 non_thread_safe_class.release()); |
| 92 | 93 |
| 93 delete_on_thread.Start(); | 94 delete_on_thread.Start(); |
| 94 delete_on_thread.Join(); | 95 delete_on_thread.Join(); |
| 95 } | 96 } |
| 96 | 97 |
| 97 #if GTEST_HAS_DEATH_TEST | 98 #if GTEST_HAS_DEATH_TEST || NDEBUG |
| 98 | 99 |
| 99 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThread) { | 100 void NonThreadSafeClass::MethodOnDifferentThreadImpl() { |
| 101 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( |
| 102 new NonThreadSafeClass); |
| 103 |
| 104 // Verify that DoStuff asserts in debug builds only when called |
| 105 // on a different thread. |
| 106 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); |
| 107 |
| 108 call_on_thread.Start(); |
| 109 call_on_thread.Join(); |
| 110 } |
| 111 |
| 112 #ifndef NDEBUG |
| 113 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
| 100 ASSERT_DEBUG_DEATH({ | 114 ASSERT_DEBUG_DEATH({ |
| 101 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( | 115 NonThreadSafeClass::MethodOnDifferentThreadImpl(); |
| 102 new NonThreadSafeClass); | |
| 103 | |
| 104 // Verify that DoStuff asserts when called on a different thread. | |
| 105 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); | |
| 106 | |
| 107 call_on_thread.Start(); | |
| 108 call_on_thread.Join(); | |
| 109 }, ""); | 116 }, ""); |
| 110 } | 117 } |
| 118 #else |
| 119 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { |
| 120 NonThreadSafeClass::MethodOnDifferentThreadImpl(); |
| 121 } |
| 122 #endif // NDEBUG |
| 111 | 123 |
| 112 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThread) { | 124 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { |
| 125 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( |
| 126 new NonThreadSafeClass); |
| 127 |
| 128 // Verify that the destructor asserts in debug builds only |
| 129 // when called on a different thread. |
| 130 DeleteNonThreadSafeClassOnThread delete_on_thread( |
| 131 non_thread_safe_class.release()); |
| 132 |
| 133 delete_on_thread.Start(); |
| 134 delete_on_thread.Join(); |
| 135 } |
| 136 |
| 137 #ifndef NDEBUG |
| 138 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { |
| 113 ASSERT_DEBUG_DEATH({ | 139 ASSERT_DEBUG_DEATH({ |
| 114 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( | 140 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); |
| 115 new NonThreadSafeClass); | |
| 116 | |
| 117 // Verify that the destructor asserts when called on a different thread. | |
| 118 DeleteNonThreadSafeClassOnThread delete_on_thread( | |
| 119 non_thread_safe_class.release()); | |
| 120 | |
| 121 delete_on_thread.Start(); | |
| 122 delete_on_thread.Join(); | |
| 123 }, ""); | 141 }, ""); |
| 124 } | 142 } |
| 143 #else |
| 144 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { |
| 145 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); |
| 146 } |
| 147 #endif // NDEBUG |
| 125 | 148 |
| 126 #endif // GTEST_HAS_DEATH_TEST | 149 #endif // GTEST_HAS_DEATH_TEST || NDEBUG |
| 127 | 150 |
| 128 } // namespace base | 151 } // namespace base |
| 129 | |
| 130 #endif // NDEBUG | |
| OLD | NEW |