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

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

Issue 6599004: Modify ThreadChecker and NonThreadSafe so that their functionality is (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update copyright year for new/moved files Created 9 years, 9 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/thread_checker_impl.cc ('k') | net/url_request/url_request_throttler_manager.h » ('j') | 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) 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/thread_checker.h" 8 #include "base/threading/thread_checker.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 ThreadChecker. 14 // Simple class to exercise the basics of ThreadChecker.
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 ThreadCheckerClass : public ThreadChecker { 17 class ThreadCheckerClass : public ThreadChecker {
20 public: 18 public:
21 ThreadCheckerClass() {} 19 ThreadCheckerClass() {}
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 ThreadChecker::DetachFromThread(); 27 ThreadChecker::DetachFromThread();
30 } 28 }
31 29
30 static void MethodOnDifferentThreadImpl();
31 static void DetachThenCallFromDifferentThreadImpl();
32
32 private: 33 private:
33 DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass); 34 DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass);
34 }; 35 };
35 36
36 // Calls ThreadCheckerClass::DoStuff on another thread. 37 // Calls ThreadCheckerClass::DoStuff on another thread.
37 class CallDoStuffOnThread : public base::SimpleThread { 38 class CallDoStuffOnThread : public base::SimpleThread {
38 public: 39 public:
39 CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class) 40 CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class)
40 : SimpleThread("call_do_stuff_on_thread"), 41 : SimpleThread("call_do_stuff_on_thread"),
41 thread_checker_class_(thread_checker_class) { 42 thread_checker_class_(thread_checker_class) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 100
100 // Verify that DoStuff doesn't assert when called on a different thread after 101 // Verify that DoStuff doesn't assert when called on a different thread after
101 // a call to DetachFromThread. 102 // a call to DetachFromThread.
102 thread_checker_class->DetachFromThread(); 103 thread_checker_class->DetachFromThread();
103 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); 104 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
104 105
105 call_on_thread.Start(); 106 call_on_thread.Start();
106 call_on_thread.Join(); 107 call_on_thread.Join();
107 } 108 }
108 109
109 #if GTEST_HAS_DEATH_TEST 110 #if GTEST_HAS_DEATH_TEST || NDEBUG
110 111
111 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThread) { 112 void ThreadCheckerClass::MethodOnDifferentThreadImpl() {
113 scoped_ptr<ThreadCheckerClass> thread_checker_class(
114 new ThreadCheckerClass);
115
116 // DoStuff should assert in debug builds only when called on a
117 // different thread.
118 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
119
120 call_on_thread.Start();
121 call_on_thread.Join();
122 }
123
124 #ifndef NDEBUG
125 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
112 ASSERT_DEBUG_DEATH({ 126 ASSERT_DEBUG_DEATH({
113 scoped_ptr<ThreadCheckerClass> thread_checker_class( 127 ThreadCheckerClass::MethodOnDifferentThreadImpl();
114 new ThreadCheckerClass);
115
116 // Verify that DoStuff asserts when called on a different thread.
117 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
118
119 call_on_thread.Start();
120 call_on_thread.Join();
121 }, ""); 128 }, "");
122 } 129 }
130 #else
131 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) {
132 ThreadCheckerClass::MethodOnDifferentThreadImpl();
133 }
134 #endif // NDEBUG
123 135
124 TEST(ThreadCheckerDeathTest, DetachFromThread) { 136 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() {
137 scoped_ptr<ThreadCheckerClass> thread_checker_class(
138 new ThreadCheckerClass);
139
140 // DoStuff doesn't assert when called on a different thread
141 // after a call to DetachFromThread.
142 thread_checker_class->DetachFromThread();
143 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
144
145 call_on_thread.Start();
146 call_on_thread.Join();
147
148 // DoStuff should assert in debug builds only after moving to
149 // another thread.
150 thread_checker_class->DoStuff();
151 }
152
153 #ifndef NDEBUG
154 TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) {
125 ASSERT_DEBUG_DEATH({ 155 ASSERT_DEBUG_DEATH({
126 scoped_ptr<ThreadCheckerClass> thread_checker_class( 156 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
127 new ThreadCheckerClass);
128
129 // Verify that DoStuff doesn't assert when called on a different thread
130 // after a call to DetachFromThread.
131 thread_checker_class->DetachFromThread();
132 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
133
134 call_on_thread.Start();
135 call_on_thread.Join();
136
137 // Verify that DoStuff asserts after moving to another thread.
138 thread_checker_class->DoStuff();
139 }, ""); 157 }, "");
140 } 158 }
159 #else
160 TEST(ThreadCheckerTest, DetachFromThreadInRelease) {
161 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
162 }
163 #endif // NDEBUG
141 164
142 #endif // GTEST_HAS_DEATH_TEST 165 #endif // GTEST_HAS_DEATH_TEST || NDEBUG
143 166
144 } // namespace base 167 } // namespace base
145
146 #endif // NDEBUG
OLDNEW
« no previous file with comments | « base/threading/thread_checker_impl.cc ('k') | net/url_request/url_request_throttler_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698