| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // Borrowed from Chromium's src/base/threading/thread_checker_unittest.cc. | 11 // Borrowed from Chromium's src/base/threading/thread_checker_unittest.cc. |
| 12 | 12 |
| 13 #include <memory> |
| 14 |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/thread.h" | 17 #include "webrtc/base/thread.h" |
| 16 #include "webrtc/base/thread_checker.h" | 18 #include "webrtc/base/thread_checker.h" |
| 17 #include "webrtc/base/scoped_ptr.h" | |
| 18 | 19 |
| 19 // Duplicated from base/threading/thread_checker.h so that we can be | 20 // Duplicated from base/threading/thread_checker.h so that we can be |
| 20 // good citizens there and undef the macro. | 21 // good citizens there and undef the macro. |
| 21 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 22 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 22 #define ENABLE_THREAD_CHECKER 1 | 23 #define ENABLE_THREAD_CHECKER 1 |
| 23 #else | 24 #else |
| 24 #define ENABLE_THREAD_CHECKER 0 | 25 #define ENABLE_THREAD_CHECKER 0 |
| 25 #endif | 26 #endif |
| 26 | 27 |
| 27 namespace rtc { | 28 namespace rtc { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 85 |
| 85 void Run() override { thread_checker_class_.reset(); } | 86 void Run() override { thread_checker_class_.reset(); } |
| 86 | 87 |
| 87 // New method. Needed since Thread::Join is protected, and it is called by | 88 // New method. Needed since Thread::Join is protected, and it is called by |
| 88 // the TEST. | 89 // the TEST. |
| 89 void Join() { | 90 void Join() { |
| 90 Thread::Join(); | 91 Thread::Join(); |
| 91 } | 92 } |
| 92 | 93 |
| 93 private: | 94 private: |
| 94 scoped_ptr<ThreadCheckerClass> thread_checker_class_; | 95 std::unique_ptr<ThreadCheckerClass> thread_checker_class_; |
| 95 | 96 |
| 96 RTC_DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); | 97 RTC_DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); |
| 97 }; | 98 }; |
| 98 | 99 |
| 99 } // namespace | 100 } // namespace |
| 100 | 101 |
| 101 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { | 102 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { |
| 102 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 103 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 103 new ThreadCheckerClass); | 104 new ThreadCheckerClass); |
| 104 | 105 |
| 105 // Verify that DoStuff doesn't assert. | 106 // Verify that DoStuff doesn't assert. |
| 106 thread_checker_class->DoStuff(); | 107 thread_checker_class->DoStuff(); |
| 107 | 108 |
| 108 // Verify that the destructor doesn't assert. | 109 // Verify that the destructor doesn't assert. |
| 109 thread_checker_class.reset(); | 110 thread_checker_class.reset(); |
| 110 } | 111 } |
| 111 | 112 |
| 112 TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) { | 113 TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) { |
| 113 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 114 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 114 new ThreadCheckerClass); | 115 new ThreadCheckerClass); |
| 115 | 116 |
| 116 // Verify that the destructor doesn't assert | 117 // Verify that the destructor doesn't assert |
| 117 // when called on a different thread. | 118 // when called on a different thread. |
| 118 DeleteThreadCheckerClassOnThread delete_on_thread( | 119 DeleteThreadCheckerClassOnThread delete_on_thread( |
| 119 thread_checker_class.release()); | 120 thread_checker_class.release()); |
| 120 | 121 |
| 121 delete_on_thread.Start(); | 122 delete_on_thread.Start(); |
| 122 delete_on_thread.Join(); | 123 delete_on_thread.Join(); |
| 123 } | 124 } |
| 124 | 125 |
| 125 TEST(ThreadCheckerTest, DetachFromThread) { | 126 TEST(ThreadCheckerTest, DetachFromThread) { |
| 126 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 127 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 127 new ThreadCheckerClass); | 128 new ThreadCheckerClass); |
| 128 | 129 |
| 129 // Verify that DoStuff doesn't assert when called on a different thread after | 130 // Verify that DoStuff doesn't assert when called on a different thread after |
| 130 // a call to DetachFromThread. | 131 // a call to DetachFromThread. |
| 131 thread_checker_class->DetachFromThread(); | 132 thread_checker_class->DetachFromThread(); |
| 132 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 133 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 133 | 134 |
| 134 call_on_thread.Start(); | 135 call_on_thread.Start(); |
| 135 call_on_thread.Join(); | 136 call_on_thread.Join(); |
| 136 } | 137 } |
| 137 | 138 |
| 138 #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER | 139 #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
| 139 | 140 |
| 140 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { | 141 void ThreadCheckerClass::MethodOnDifferentThreadImpl() { |
| 141 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 142 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 142 new ThreadCheckerClass); | 143 new ThreadCheckerClass); |
| 143 | 144 |
| 144 // DoStuff should assert in debug builds only when called on a | 145 // DoStuff should assert in debug builds only when called on a |
| 145 // different thread. | 146 // different thread. |
| 146 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 147 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 147 | 148 |
| 148 call_on_thread.Start(); | 149 call_on_thread.Start(); |
| 149 call_on_thread.Join(); | 150 call_on_thread.Join(); |
| 150 } | 151 } |
| 151 | 152 |
| 152 #if ENABLE_THREAD_CHECKER | 153 #if ENABLE_THREAD_CHECKER |
| 153 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { | 154 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
| 154 ASSERT_DEATH({ | 155 ASSERT_DEATH({ |
| 155 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 156 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
| 156 }, ""); | 157 }, ""); |
| 157 } | 158 } |
| 158 #else | 159 #else |
| 159 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { | 160 TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { |
| 160 ThreadCheckerClass::MethodOnDifferentThreadImpl(); | 161 ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
| 161 } | 162 } |
| 162 #endif // ENABLE_THREAD_CHECKER | 163 #endif // ENABLE_THREAD_CHECKER |
| 163 | 164 |
| 164 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { | 165 void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { |
| 165 scoped_ptr<ThreadCheckerClass> thread_checker_class( | 166 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 166 new ThreadCheckerClass); | 167 new ThreadCheckerClass); |
| 167 | 168 |
| 168 // DoStuff doesn't assert when called on a different thread | 169 // DoStuff doesn't assert when called on a different thread |
| 169 // after a call to DetachFromThread. | 170 // after a call to DetachFromThread. |
| 170 thread_checker_class->DetachFromThread(); | 171 thread_checker_class->DetachFromThread(); |
| 171 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 172 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 172 | 173 |
| 173 call_on_thread.Start(); | 174 call_on_thread.Start(); |
| 174 call_on_thread.Join(); | 175 call_on_thread.Join(); |
| 175 | 176 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 189 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); | 190 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
| 190 } | 191 } |
| 191 #endif // ENABLE_THREAD_CHECKER | 192 #endif // ENABLE_THREAD_CHECKER |
| 192 | 193 |
| 193 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER | 194 #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
| 194 | 195 |
| 195 // Just in case we ever get lumped together with other compilation units. | 196 // Just in case we ever get lumped together with other compilation units. |
| 196 #undef ENABLE_THREAD_CHECKER | 197 #undef ENABLE_THREAD_CHECKER |
| 197 | 198 |
| 198 } // namespace rtc | 199 } // namespace rtc |
| OLD | NEW |