Update .DEPS.git
[chromium-blink-merge.git] / base / threading / non_thread_safe_unittest.cc
blobee31701dc65ec6067d3c84702591040dc46acddb
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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/threading/non_thread_safe.h"
9 #include "base/threading/simple_thread.h"
10 #include "testing/gtest/include/gtest/gtest.h"
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
20 namespace base {
22 namespace {
24 // Simple class to exersice the basics of NonThreadSafe.
25 // Both the destructor and DoStuff should verify that they were
26 // called on the same thread as the constructor.
27 class NonThreadSafeClass : public NonThreadSafe {
28 public:
29 NonThreadSafeClass() {}
31 // Verifies that it was called on the same thread as the constructor.
32 void DoStuff() {
33 DCHECK(CalledOnValidThread());
36 void DetachFromThread() {
37 NonThreadSafe::DetachFromThread();
40 static void MethodOnDifferentThreadImpl();
41 static void DestructorOnDifferentThreadImpl();
43 private:
44 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeClass);
47 // Calls NonThreadSafeClass::DoStuff on another thread.
48 class CallDoStuffOnThread : public SimpleThread {
49 public:
50 CallDoStuffOnThread(NonThreadSafeClass* non_thread_safe_class)
51 : SimpleThread("call_do_stuff_on_thread"),
52 non_thread_safe_class_(non_thread_safe_class) {
55 virtual void Run() OVERRIDE {
56 non_thread_safe_class_->DoStuff();
59 private:
60 NonThreadSafeClass* non_thread_safe_class_;
62 DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread);
65 // Deletes NonThreadSafeClass on a different thread.
66 class DeleteNonThreadSafeClassOnThread : public SimpleThread {
67 public:
68 DeleteNonThreadSafeClassOnThread(NonThreadSafeClass* non_thread_safe_class)
69 : SimpleThread("delete_non_thread_safe_class_on_thread"),
70 non_thread_safe_class_(non_thread_safe_class) {
73 virtual void Run() OVERRIDE {
74 non_thread_safe_class_.reset();
77 private:
78 scoped_ptr<NonThreadSafeClass> non_thread_safe_class_;
80 DISALLOW_COPY_AND_ASSIGN(DeleteNonThreadSafeClassOnThread);
83 } // namespace
85 TEST(NonThreadSafeTest, CallsAllowedOnSameThread) {
86 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
87 new NonThreadSafeClass);
89 // Verify that DoStuff doesn't assert.
90 non_thread_safe_class->DoStuff();
92 // Verify that the destructor doesn't assert.
93 non_thread_safe_class.reset();
96 TEST(NonThreadSafeTest, DetachThenDestructOnDifferentThread) {
97 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
98 new NonThreadSafeClass);
100 // Verify that the destructor doesn't assert when called on a different thread
101 // after a detach.
102 non_thread_safe_class->DetachFromThread();
103 DeleteNonThreadSafeClassOnThread delete_on_thread(
104 non_thread_safe_class.release());
106 delete_on_thread.Start();
107 delete_on_thread.Join();
110 #if GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE
112 void NonThreadSafeClass::MethodOnDifferentThreadImpl() {
113 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
114 new NonThreadSafeClass);
116 // Verify that DoStuff asserts in debug builds only when called
117 // on a different thread.
118 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get());
120 call_on_thread.Start();
121 call_on_thread.Join();
124 #if ENABLE_NON_THREAD_SAFE
125 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
126 ASSERT_DEATH({
127 NonThreadSafeClass::MethodOnDifferentThreadImpl();
128 }, "");
130 #else
131 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) {
132 NonThreadSafeClass::MethodOnDifferentThreadImpl();
134 #endif // ENABLE_NON_THREAD_SAFE
136 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() {
137 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
138 new NonThreadSafeClass);
140 // Verify that the destructor asserts in debug builds only
141 // when called on a different thread.
142 DeleteNonThreadSafeClassOnThread delete_on_thread(
143 non_thread_safe_class.release());
145 delete_on_thread.Start();
146 delete_on_thread.Join();
149 #if ENABLE_NON_THREAD_SAFE
150 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) {
151 ASSERT_DEATH({
152 NonThreadSafeClass::DestructorOnDifferentThreadImpl();
153 }, "");
155 #else
156 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) {
157 NonThreadSafeClass::DestructorOnDifferentThreadImpl();
159 #endif // ENABLE_NON_THREAD_SAFE
161 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE
163 // Just in case we ever get lumped together with other compilation units.
164 #undef ENABLE_NON_THREAD_SAFE
166 } // namespace base