Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: base/threading/non_thread_safe_unittest.cc

Issue 10636044: Enable base::NonThreadSafe when DCHECK_ALWAYS_ON is set (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix ODR violation Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/threading/non_thread_safe.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/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 // Duplicated from base/threading/non_thread_safe.h so that we can be
13 // good citizens there and undef the macro.
14 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
15 #define ENABLE_NON_THREAD_SAFE 1
16 #else
17 #define ENABLE_NON_THREAD_SAFE 0
18 #endif
19
12 namespace base { 20 namespace base {
13 21
22 namespace {
23
14 // Simple class to exersice the basics of NonThreadSafe. 24 // Simple class to exersice the basics of NonThreadSafe.
15 // Both the destructor and DoStuff should verify that they were 25 // Both the destructor and DoStuff should verify that they were
16 // called on the same thread as the constructor. 26 // called on the same thread as the constructor.
17 class NonThreadSafeClass : public NonThreadSafe { 27 class NonThreadSafeClass : public NonThreadSafe {
18 public: 28 public:
19 NonThreadSafeClass() {} 29 NonThreadSafeClass() {}
20 30
21 // 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.
22 void DoStuff() { 32 void DoStuff() {
23 DCHECK(CalledOnValidThread()); 33 DCHECK(CalledOnValidThread());
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 virtual void Run() OVERRIDE { 73 virtual void Run() OVERRIDE {
64 non_thread_safe_class_.reset(); 74 non_thread_safe_class_.reset();
65 } 75 }
66 76
67 private: 77 private:
68 scoped_ptr<NonThreadSafeClass> non_thread_safe_class_; 78 scoped_ptr<NonThreadSafeClass> non_thread_safe_class_;
69 79
70 DISALLOW_COPY_AND_ASSIGN(DeleteNonThreadSafeClassOnThread); 80 DISALLOW_COPY_AND_ASSIGN(DeleteNonThreadSafeClassOnThread);
71 }; 81 };
72 82
83 } // namespace
84
73 TEST(NonThreadSafeTest, CallsAllowedOnSameThread) { 85 TEST(NonThreadSafeTest, CallsAllowedOnSameThread) {
74 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( 86 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
75 new NonThreadSafeClass); 87 new NonThreadSafeClass);
76 88
77 // Verify that DoStuff doesn't assert. 89 // Verify that DoStuff doesn't assert.
78 non_thread_safe_class->DoStuff(); 90 non_thread_safe_class->DoStuff();
79 91
80 // Verify that the destructor doesn't assert. 92 // Verify that the destructor doesn't assert.
81 non_thread_safe_class.reset(); 93 non_thread_safe_class.reset();
82 } 94 }
83 95
84 TEST(NonThreadSafeTest, DetachThenDestructOnDifferentThread) { 96 TEST(NonThreadSafeTest, DetachThenDestructOnDifferentThread) {
85 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( 97 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
86 new NonThreadSafeClass); 98 new NonThreadSafeClass);
87 99
88 // 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
89 // after a detach. 101 // after a detach.
90 non_thread_safe_class->DetachFromThread(); 102 non_thread_safe_class->DetachFromThread();
91 DeleteNonThreadSafeClassOnThread delete_on_thread( 103 DeleteNonThreadSafeClassOnThread delete_on_thread(
92 non_thread_safe_class.release()); 104 non_thread_safe_class.release());
93 105
94 delete_on_thread.Start(); 106 delete_on_thread.Start();
95 delete_on_thread.Join(); 107 delete_on_thread.Join();
96 } 108 }
97 109
98 #if GTEST_HAS_DEATH_TEST || NDEBUG 110 #if GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE
99 111
100 void NonThreadSafeClass::MethodOnDifferentThreadImpl() { 112 void NonThreadSafeClass::MethodOnDifferentThreadImpl() {
101 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( 113 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
102 new NonThreadSafeClass); 114 new NonThreadSafeClass);
103 115
104 // Verify that DoStuff asserts in debug builds only when called 116 // Verify that DoStuff asserts in debug builds only when called
105 // on a different thread. 117 // on a different thread.
106 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get()); 118 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get());
107 119
108 call_on_thread.Start(); 120 call_on_thread.Start();
109 call_on_thread.Join(); 121 call_on_thread.Join();
110 } 122 }
111 123
112 #ifndef NDEBUG 124 #if ENABLE_NON_THREAD_SAFE
113 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { 125 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
114 ASSERT_DEBUG_DEATH({ 126 ASSERT_DEATH({
115 NonThreadSafeClass::MethodOnDifferentThreadImpl(); 127 NonThreadSafeClass::MethodOnDifferentThreadImpl();
116 }, ""); 128 }, "");
117 } 129 }
118 #else 130 #else
119 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) { 131 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) {
120 NonThreadSafeClass::MethodOnDifferentThreadImpl(); 132 NonThreadSafeClass::MethodOnDifferentThreadImpl();
121 } 133 }
122 #endif // NDEBUG 134 #endif // ENABLE_NON_THREAD_SAFE
123 135
124 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() { 136 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() {
125 scoped_ptr<NonThreadSafeClass> non_thread_safe_class( 137 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
126 new NonThreadSafeClass); 138 new NonThreadSafeClass);
127 139
128 // Verify that the destructor asserts in debug builds only 140 // Verify that the destructor asserts in debug builds only
129 // when called on a different thread. 141 // when called on a different thread.
130 DeleteNonThreadSafeClassOnThread delete_on_thread( 142 DeleteNonThreadSafeClassOnThread delete_on_thread(
131 non_thread_safe_class.release()); 143 non_thread_safe_class.release());
132 144
133 delete_on_thread.Start(); 145 delete_on_thread.Start();
134 delete_on_thread.Join(); 146 delete_on_thread.Join();
135 } 147 }
136 148
137 #ifndef NDEBUG 149 #if ENABLE_NON_THREAD_SAFE
138 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) { 150 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) {
139 ASSERT_DEBUG_DEATH({ 151 ASSERT_DEATH({
140 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); 152 NonThreadSafeClass::DestructorOnDifferentThreadImpl();
141 }, ""); 153 }, "");
142 } 154 }
143 #else 155 #else
144 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) { 156 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) {
145 NonThreadSafeClass::DestructorOnDifferentThreadImpl(); 157 NonThreadSafeClass::DestructorOnDifferentThreadImpl();
146 } 158 }
147 #endif // NDEBUG 159 #endif // ENABLE_NON_THREAD_SAFE
148 160
149 #endif // GTEST_HAS_DEATH_TEST || NDEBUG 161 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE
162
163 // Just in case we ever get lumped together with other compilation units.
164 #undef ENABLE_NON_THREAD_SAFE
150 165
151 } // namespace base 166 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/non_thread_safe.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698