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.
4
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"
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
20 namespace base {
21
22 namespace {
23
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:
NonThreadSafeClass()29 NonThreadSafeClass() {}
30
31 // Verifies that it was called on the same thread as the constructor.
DoStuff()32 void DoStuff() {
33 DCHECK(CalledOnValidThread());
34 }
35
DetachFromThread()36 void DetachFromThread() {
37 NonThreadSafe::DetachFromThread();
38 }
39
40 static void MethodOnDifferentThreadImpl();
41 static void DestructorOnDifferentThreadImpl();
42
43 private:
44 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeClass);
45 };
46
47 // Calls NonThreadSafeClass::DoStuff on another thread.
48 class CallDoStuffOnThread : public SimpleThread {
49 public:
CallDoStuffOnThread(NonThreadSafeClass * non_thread_safe_class)50 explicit CallDoStuffOnThread(NonThreadSafeClass* non_thread_safe_class)
51 : SimpleThread("call_do_stuff_on_thread"),
52 non_thread_safe_class_(non_thread_safe_class) {
53 }
54
Run()55 virtual void Run() OVERRIDE {
56 non_thread_safe_class_->DoStuff();
57 }
58
59 private:
60 NonThreadSafeClass* non_thread_safe_class_;
61
62 DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread);
63 };
64
65 // Deletes NonThreadSafeClass on a different thread.
66 class DeleteNonThreadSafeClassOnThread : public SimpleThread {
67 public:
DeleteNonThreadSafeClassOnThread(NonThreadSafeClass * non_thread_safe_class)68 explicit DeleteNonThreadSafeClassOnThread(
69 NonThreadSafeClass* non_thread_safe_class)
70 : SimpleThread("delete_non_thread_safe_class_on_thread"),
71 non_thread_safe_class_(non_thread_safe_class) {
72 }
73
Run()74 virtual void Run() OVERRIDE {
75 non_thread_safe_class_.reset();
76 }
77
78 private:
79 scoped_ptr<NonThreadSafeClass> non_thread_safe_class_;
80
81 DISALLOW_COPY_AND_ASSIGN(DeleteNonThreadSafeClassOnThread);
82 };
83
84 } // namespace
85
TEST(NonThreadSafeTest,CallsAllowedOnSameThread)86 TEST(NonThreadSafeTest, CallsAllowedOnSameThread) {
87 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
88 new NonThreadSafeClass);
89
90 // Verify that DoStuff doesn't assert.
91 non_thread_safe_class->DoStuff();
92
93 // Verify that the destructor doesn't assert.
94 non_thread_safe_class.reset();
95 }
96
TEST(NonThreadSafeTest,DetachThenDestructOnDifferentThread)97 TEST(NonThreadSafeTest, DetachThenDestructOnDifferentThread) {
98 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
99 new NonThreadSafeClass);
100
101 // Verify that the destructor doesn't assert when called on a different thread
102 // after a detach.
103 non_thread_safe_class->DetachFromThread();
104 DeleteNonThreadSafeClassOnThread delete_on_thread(
105 non_thread_safe_class.release());
106
107 delete_on_thread.Start();
108 delete_on_thread.Join();
109 }
110
111 #if GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE
112
MethodOnDifferentThreadImpl()113 void NonThreadSafeClass::MethodOnDifferentThreadImpl() {
114 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
115 new NonThreadSafeClass);
116
117 // Verify that DoStuff asserts in debug builds only when called
118 // on a different thread.
119 CallDoStuffOnThread call_on_thread(non_thread_safe_class.get());
120
121 call_on_thread.Start();
122 call_on_thread.Join();
123 }
124
125 #if ENABLE_NON_THREAD_SAFE
TEST(NonThreadSafeDeathTest,MethodNotAllowedOnDifferentThreadInDebug)126 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
127 ASSERT_DEATH({
128 NonThreadSafeClass::MethodOnDifferentThreadImpl();
129 }, "");
130 }
131 #else
TEST(NonThreadSafeTest,MethodAllowedOnDifferentThreadInRelease)132 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) {
133 NonThreadSafeClass::MethodOnDifferentThreadImpl();
134 }
135 #endif // ENABLE_NON_THREAD_SAFE
136
DestructorOnDifferentThreadImpl()137 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() {
138 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
139 new NonThreadSafeClass);
140
141 // Verify that the destructor asserts in debug builds only
142 // when called on a different thread.
143 DeleteNonThreadSafeClassOnThread delete_on_thread(
144 non_thread_safe_class.release());
145
146 delete_on_thread.Start();
147 delete_on_thread.Join();
148 }
149
150 #if ENABLE_NON_THREAD_SAFE
TEST(NonThreadSafeDeathTest,DestructorNotAllowedOnDifferentThreadInDebug)151 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) {
152 ASSERT_DEATH({
153 NonThreadSafeClass::DestructorOnDifferentThreadImpl();
154 }, "");
155 }
156 #else
TEST(NonThreadSafeTest,DestructorAllowedOnDifferentThreadInRelease)157 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) {
158 NonThreadSafeClass::DestructorOnDifferentThreadImpl();
159 }
160 #endif // ENABLE_NON_THREAD_SAFE
161
162 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE
163
164 // Just in case we ever get lumped together with other compilation units.
165 #undef ENABLE_NON_THREAD_SAFE
166
167 } // namespace base
168