[email protected] | 8c3881ab | 2012-01-04 19:02:38 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 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" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 7 | #include "base/memory/scoped_ptr.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 8 | #include "base/threading/thread_checker.h" |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 9 | #include "base/threading/simple_thread.h" |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
[email protected] | 8c3881ab | 2012-01-04 19:02:38 | [diff] [blame] | 12 | // Duplicated from base/threading/thread_checker.h so that we can be |
| 13 | // good citizens there and undef the macro. |
[email protected] | 8558841 | 2012-06-27 01:39:52 | [diff] [blame^] | 14 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 15 | #define ENABLE_THREAD_CHECKER 1 |
| 16 | #else |
| 17 | #define ENABLE_THREAD_CHECKER 0 |
| 18 | #endif |
[email protected] | 8c3881ab | 2012-01-04 19:02:38 | [diff] [blame] | 19 | |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 20 | namespace base { |
| 21 | |
[email protected] | 8558841 | 2012-06-27 01:39:52 | [diff] [blame^] | 22 | namespace { |
| 23 | |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 24 | // Simple class to exercise the basics of ThreadChecker. |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 25 | // Both the destructor and DoStuff should verify that they were |
| 26 | // called on the same thread as the constructor. |
| 27 | class ThreadCheckerClass : public ThreadChecker { |
| 28 | public: |
| 29 | ThreadCheckerClass() {} |
| 30 | |
| 31 | // Verifies that it was called on the same thread as the constructor. |
| 32 | void DoStuff() { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 33 | DCHECK(CalledOnValidThread()); |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 34 | } |
| 35 | |
[email protected] | 8ff67251 | 2010-10-07 20:17:33 | [diff] [blame] | 36 | void DetachFromThread() { |
| 37 | ThreadChecker::DetachFromThread(); |
| 38 | } |
| 39 | |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 40 | static void MethodOnDifferentThreadImpl(); |
| 41 | static void DetachThenCallFromDifferentThreadImpl(); |
| 42 | |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 43 | private: |
| 44 | DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass); |
| 45 | }; |
| 46 | |
| 47 | // Calls ThreadCheckerClass::DoStuff on another thread. |
| 48 | class CallDoStuffOnThread : public base::SimpleThread { |
| 49 | public: |
| 50 | CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class) |
| 51 | : SimpleThread("call_do_stuff_on_thread"), |
| 52 | thread_checker_class_(thread_checker_class) { |
| 53 | } |
| 54 | |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 55 | virtual void Run() OVERRIDE { |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 56 | thread_checker_class_->DoStuff(); |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | ThreadCheckerClass* thread_checker_class_; |
| 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread); |
| 63 | }; |
| 64 | |
| 65 | // Deletes ThreadCheckerClass on a different thread. |
| 66 | class DeleteThreadCheckerClassOnThread : public base::SimpleThread { |
| 67 | public: |
| 68 | DeleteThreadCheckerClassOnThread(ThreadCheckerClass* thread_checker_class) |
| 69 | : SimpleThread("delete_thread_checker_class_on_thread"), |
| 70 | thread_checker_class_(thread_checker_class) { |
| 71 | } |
| 72 | |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 73 | virtual void Run() OVERRIDE { |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 74 | thread_checker_class_.reset(); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | scoped_ptr<ThreadCheckerClass> thread_checker_class_; |
| 79 | |
| 80 | DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); |
| 81 | }; |
| 82 | |
[email protected] | 8558841 | 2012-06-27 01:39:52 | [diff] [blame^] | 83 | } // namespace |
| 84 | |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 85 | TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { |
| 86 | scoped_ptr<ThreadCheckerClass> thread_checker_class( |
| 87 | new ThreadCheckerClass); |
| 88 | |
| 89 | // Verify that DoStuff doesn't assert. |
| 90 | thread_checker_class->DoStuff(); |
| 91 | |
| 92 | // Verify that the destructor doesn't assert. |
| 93 | thread_checker_class.reset(); |
| 94 | } |
| 95 | |
| 96 | TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) { |
| 97 | scoped_ptr<ThreadCheckerClass> thread_checker_class( |
| 98 | new ThreadCheckerClass); |
| 99 | |
| 100 | // Verify that the destructor doesn't assert |
| 101 | // when called on a different thread. |
| 102 | DeleteThreadCheckerClassOnThread delete_on_thread( |
| 103 | thread_checker_class.release()); |
| 104 | |
| 105 | delete_on_thread.Start(); |
| 106 | delete_on_thread.Join(); |
| 107 | } |
| 108 | |
[email protected] | 8ff67251 | 2010-10-07 20:17:33 | [diff] [blame] | 109 | TEST(ThreadCheckerTest, DetachFromThread) { |
| 110 | scoped_ptr<ThreadCheckerClass> thread_checker_class( |
| 111 | new ThreadCheckerClass); |
| 112 | |
| 113 | // Verify that DoStuff doesn't assert when called on a different thread after |
| 114 | // a call to DetachFromThread. |
| 115 | thread_checker_class->DetachFromThread(); |
| 116 | CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 117 | |
| 118 | call_on_thread.Start(); |
| 119 | call_on_thread.Join(); |
| 120 | } |
| 121 | |
[email protected] | 8c3881ab | 2012-01-04 19:02:38 | [diff] [blame] | 122 | #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 123 | |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 124 | void ThreadCheckerClass::MethodOnDifferentThreadImpl() { |
| 125 | scoped_ptr<ThreadCheckerClass> thread_checker_class( |
| 126 | new ThreadCheckerClass); |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 127 | |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 128 | // DoStuff should assert in debug builds only when called on a |
| 129 | // different thread. |
| 130 | CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 131 | |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 132 | call_on_thread.Start(); |
| 133 | call_on_thread.Join(); |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | 8c3881ab | 2012-01-04 19:02:38 | [diff] [blame] | 136 | #if ENABLE_THREAD_CHECKER |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 137 | TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
[email protected] | 8558841 | 2012-06-27 01:39:52 | [diff] [blame^] | 138 | ASSERT_DEATH({ |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 139 | ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
[email protected] | 8ff67251 | 2010-10-07 20:17:33 | [diff] [blame] | 140 | }, ""); |
| 141 | } |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 142 | #else |
| 143 | TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { |
| 144 | ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
| 145 | } |
[email protected] | 8c3881ab | 2012-01-04 19:02:38 | [diff] [blame] | 146 | #endif // ENABLE_THREAD_CHECKER |
[email protected] | 8ff67251 | 2010-10-07 20:17:33 | [diff] [blame] | 147 | |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 148 | void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { |
| 149 | scoped_ptr<ThreadCheckerClass> thread_checker_class( |
| 150 | new ThreadCheckerClass); |
| 151 | |
| 152 | // DoStuff doesn't assert when called on a different thread |
| 153 | // after a call to DetachFromThread. |
| 154 | thread_checker_class->DetachFromThread(); |
| 155 | CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 156 | |
| 157 | call_on_thread.Start(); |
| 158 | call_on_thread.Join(); |
| 159 | |
| 160 | // DoStuff should assert in debug builds only after moving to |
| 161 | // another thread. |
| 162 | thread_checker_class->DoStuff(); |
| 163 | } |
| 164 | |
[email protected] | 8c3881ab | 2012-01-04 19:02:38 | [diff] [blame] | 165 | #if ENABLE_THREAD_CHECKER |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 166 | TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { |
[email protected] | 8558841 | 2012-06-27 01:39:52 | [diff] [blame^] | 167 | ASSERT_DEATH({ |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 168 | ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
| 169 | }, ""); |
| 170 | } |
| 171 | #else |
| 172 | TEST(ThreadCheckerTest, DetachFromThreadInRelease) { |
| 173 | ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
| 174 | } |
[email protected] | 8c3881ab | 2012-01-04 19:02:38 | [diff] [blame] | 175 | #endif // ENABLE_THREAD_CHECKER |
[email protected] | 4006448 | 2011-03-03 23:38:51 | [diff] [blame] | 176 | |
[email protected] | 8c3881ab | 2012-01-04 19:02:38 | [diff] [blame] | 177 | #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
| 178 | |
| 179 | // Just in case we ever get lumped together with other compilation units. |
| 180 | #undef ENABLE_THREAD_CHECKER |
[email protected] | d9ddc96 | 2010-08-24 04:29:56 | [diff] [blame] | 181 | |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 182 | } // namespace base |