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/non_thread_safe.h" | 5 #include "base/threading/non_thread_safe.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/threading/simple_thread.h" | 12 #include "base/threading/simple_thread.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 // Duplicated from base/threading/non_thread_safe.h so that we can be | 15 // Duplicated from base/threading/non_thread_safe.h so that we can be |
15 // good citizens there and undef the macro. | 16 // good citizens there and undef the macro. |
16 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | 17 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
17 #define ENABLE_NON_THREAD_SAFE 1 | 18 #define ENABLE_NON_THREAD_SAFE 1 |
18 #else | 19 #else |
19 #define ENABLE_NON_THREAD_SAFE 0 | 20 #define ENABLE_NON_THREAD_SAFE 0 |
20 #endif | 21 #endif |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // Verify that the destructor doesn't assert when called on a different thread | 100 // Verify that the destructor doesn't assert when called on a different thread |
100 // after a detach. | 101 // after a detach. |
101 non_thread_safe_class->DetachFromThread(); | 102 non_thread_safe_class->DetachFromThread(); |
102 DeleteNonThreadSafeClassOnThread delete_on_thread( | 103 DeleteNonThreadSafeClassOnThread delete_on_thread( |
103 non_thread_safe_class.release()); | 104 non_thread_safe_class.release()); |
104 | 105 |
105 delete_on_thread.Start(); | 106 delete_on_thread.Start(); |
106 delete_on_thread.Join(); | 107 delete_on_thread.Join(); |
107 } | 108 } |
108 | 109 |
109 #if GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE | |
110 | |
111 void NonThreadSafeClass::MethodOnDifferentThreadImpl() { | 110 void NonThreadSafeClass::MethodOnDifferentThreadImpl() { |
112 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( | 111 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( |
113 new NonThreadSafeClass); | 112 new NonThreadSafeClass); |
114 | 113 |
115 // Verify that DoStuff asserts in debug builds only when called | 114 // Verify that DoStuff asserts in debug builds only when called |
116 // on a different thread. | 115 // on a different thread. |
117 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); | 116 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); |
118 | 117 |
119 call_on_thread.Start(); | 118 call_on_thread.Start(); |
120 call_on_thread.Join(); | 119 call_on_thread.Join(); |
121 } | 120 } |
122 | 121 |
123 #if ENABLE_NON_THREAD_SAFE | 122 #if ENABLE_NON_THREAD_SAFE |
124 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 123 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
125 ASSERT_DEATH({ | 124 ASSERT_DCHECK_DEATH({ NonThreadSafeClass::MethodOnDifferentThreadImpl(); }, |
126 NonThreadSafeClass::MethodOnDifferentThreadImpl(); | 125 ""); |
127 }, ""); | |
128 } | 126 } |
129 #else | 127 #else |
130 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { | 128 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { |
131 NonThreadSafeClass::MethodOnDifferentThreadImpl(); | 129 NonThreadSafeClass::MethodOnDifferentThreadImpl(); |
132 } | 130 } |
133 #endif // ENABLE_NON_THREAD_SAFE | 131 #endif // ENABLE_NON_THREAD_SAFE |
134 | 132 |
135 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { | 133 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { |
136 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( | 134 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( |
137 new NonThreadSafeClass); | 135 new NonThreadSafeClass); |
138 | 136 |
139 // Verify that the destructor asserts in debug builds only | 137 // Verify that the destructor asserts in debug builds only |
140 // when called on a different thread. | 138 // when called on a different thread. |
141 DeleteNonThreadSafeClassOnThread delete_on_thread( | 139 DeleteNonThreadSafeClassOnThread delete_on_thread( |
142 non_thread_safe_class.release()); | 140 non_thread_safe_class.release()); |
143 | 141 |
144 delete_on_thread.Start(); | 142 delete_on_thread.Start(); |
145 delete_on_thread.Join(); | 143 delete_on_thread.Join(); |
146 } | 144 } |
147 | 145 |
148 #if ENABLE_NON_THREAD_SAFE | 146 #if ENABLE_NON_THREAD_SAFE |
149 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { | 147 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { |
150 ASSERT_DEATH({ | 148 ASSERT_DCHECK_DEATH( |
151 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); | 149 { NonThreadSafeClass::DestructorOnDifferentThreadImpl(); }, ""); |
152 }, ""); | |
153 } | 150 } |
154 #else | 151 #else |
155 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { | 152 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { |
156 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); | 153 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); |
157 } | 154 } |
158 #endif // ENABLE_NON_THREAD_SAFE | 155 #endif // ENABLE_NON_THREAD_SAFE |
159 | 156 |
160 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE | |
161 | |
162 // Just in case we ever get lumped together with other compilation units. | 157 // Just in case we ever get lumped together with other compilation units. |
163 #undef ENABLE_NON_THREAD_SAFE | 158 #undef ENABLE_NON_THREAD_SAFE |
164 | 159 |
165 } // namespace base | 160 } // namespace base |
OLD | NEW |