Disable OutOfProcessPPAPITest FlashDRM as it flakes locally all the time and on swarm...
[chromium-blink-merge.git] / base / threading / non_thread_safe_unittest.cc
blob8a82a637ab781f9e694ef5b23fd4c640b759d60e
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 explicit 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 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) {
74 virtual void Run() OVERRIDE {
75 non_thread_safe_class_.reset();
78 private:
79 scoped_ptr<NonThreadSafeClass> non_thread_safe_class_;
81 DISALLOW_COPY_AND_ASSIGN(DeleteNonThreadSafeClassOnThread);
84 } // namespace
86 TEST(NonThreadSafeTest, CallsAllowedOnSameThread) {
87 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
88 new NonThreadSafeClass);
90 // Verify that DoStuff doesn't assert.
91 non_thread_safe_class->DoStuff();
93 // Verify that the destructor doesn't assert.
94 non_thread_safe_class.reset();
97 TEST(NonThreadSafeTest, DetachThenDestructOnDifferentThread) {
98 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
99 new NonThreadSafeClass);
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());
107 delete_on_thread.Start();
108 delete_on_thread.Join();
111 #if GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE
113 void NonThreadSafeClass::MethodOnDifferentThreadImpl() {
114 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
115 new NonThreadSafeClass);
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());
121 call_on_thread.Start();
122 call_on_thread.Join();
125 #if ENABLE_NON_THREAD_SAFE
126 TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
127 ASSERT_DEATH({
128 NonThreadSafeClass::MethodOnDifferentThreadImpl();
129 }, "");
131 #else
132 TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) {
133 NonThreadSafeClass::MethodOnDifferentThreadImpl();
135 #endif // ENABLE_NON_THREAD_SAFE
137 void NonThreadSafeClass::DestructorOnDifferentThreadImpl() {
138 scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
139 new NonThreadSafeClass);
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());
146 delete_on_thread.Start();
147 delete_on_thread.Join();
150 #if ENABLE_NON_THREAD_SAFE
151 TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) {
152 ASSERT_DEATH({
153 NonThreadSafeClass::DestructorOnDifferentThreadImpl();
154 }, "");
156 #else
157 TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) {
158 NonThreadSafeClass::DestructorOnDifferentThreadImpl();
160 #endif // ENABLE_NON_THREAD_SAFE
162 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE
164 // Just in case we ever get lumped together with other compilation units.
165 #undef ENABLE_NON_THREAD_SAFE
167 } // namespace base