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" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 // Verify that DoStuff asserts in debug builds only when called | 106 // Verify that DoStuff asserts in debug builds only when called |
107 // on a different thread. | 107 // on a different thread. |
108 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); | 108 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); |
109 | 109 |
110 call_on_thread.Start(); | 110 call_on_thread.Start(); |
111 call_on_thread.Join(); | 111 call_on_thread.Join(); |
112 } | 112 } |
113 | 113 |
114 #if DCHECK_IS_ON() | 114 #if DCHECK_IS_ON() |
115 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 115 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
| 116 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
116 ASSERT_DCHECK_DEATH({ NonThreadSafeClass::MethodOnDifferentThreadImpl(); }); | 117 ASSERT_DCHECK_DEATH({ NonThreadSafeClass::MethodOnDifferentThreadImpl(); }); |
117 } | 118 } |
118 #else | 119 #else |
119 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { | 120 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { |
120 NonThreadSafeClass::MethodOnDifferentThreadImpl(); | 121 NonThreadSafeClass::MethodOnDifferentThreadImpl(); |
121 } | 122 } |
122 #endif // DCHECK_IS_ON() | 123 #endif // DCHECK_IS_ON() |
123 | 124 |
124 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { | 125 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { |
125 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( | 126 std::unique_ptr<NonThreadSafeClass> non_thread_safe_class( |
126 new NonThreadSafeClass); | 127 new NonThreadSafeClass); |
127 | 128 |
128 // Verify that the destructor asserts in debug builds only | 129 // Verify that the destructor asserts in debug builds only |
129 // when called on a different thread. | 130 // when called on a different thread. |
130 DeleteNonThreadSafeClassOnThread delete_on_thread( | 131 DeleteNonThreadSafeClassOnThread delete_on_thread( |
131 non_thread_safe_class.release()); | 132 non_thread_safe_class.release()); |
132 | 133 |
133 delete_on_thread.Start(); | 134 delete_on_thread.Start(); |
134 delete_on_thread.Join(); | 135 delete_on_thread.Join(); |
135 } | 136 } |
136 | 137 |
137 #if DCHECK_IS_ON() | 138 #if DCHECK_IS_ON() |
138 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { | 139 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { |
| 140 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
139 ASSERT_DCHECK_DEATH( | 141 ASSERT_DCHECK_DEATH( |
140 { NonThreadSafeClass::DestructorOnDifferentThreadImpl(); }); | 142 { NonThreadSafeClass::DestructorOnDifferentThreadImpl(); }); |
141 } | 143 } |
142 #else | 144 #else |
143 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { | 145 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { |
144 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); | 146 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); |
145 } | 147 } |
146 #endif // DCHECK_IS_ON() | 148 #endif // DCHECK_IS_ON() |
147 | 149 |
148 } // namespace base | 150 } // namespace base |
OLD | NEW |