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