| 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> | 13 #include <memory> |
| 14 | 14 |
| 15 #include "webrtc/rtc_base/checks.h" | 15 #include "webrtc/rtc_base/checks.h" |
| 16 #include "webrtc/rtc_base/constructormagic.h" | 16 #include "webrtc/rtc_base/constructormagic.h" |
| 17 #include "webrtc/rtc_base/nullsocketserver.h" |
| 17 #include "webrtc/rtc_base/task_queue.h" | 18 #include "webrtc/rtc_base/task_queue.h" |
| 18 #include "webrtc/rtc_base/thread.h" | 19 #include "webrtc/rtc_base/thread.h" |
| 19 #include "webrtc/rtc_base/thread_checker.h" | 20 #include "webrtc/rtc_base/thread_checker.h" |
| 20 #include "webrtc/test/gtest.h" | 21 #include "webrtc/test/gtest.h" |
| 21 | 22 |
| 22 // Duplicated from base/threading/thread_checker.h so that we can be | 23 // Duplicated from base/threading/thread_checker.h so that we can be |
| 23 // good citizens there and undef the macro. | 24 // good citizens there and undef the macro. |
| 24 #define ENABLE_THREAD_CHECKER RTC_DCHECK_IS_ON | 25 #define ENABLE_THREAD_CHECKER RTC_DCHECK_IS_ON |
| 25 | 26 |
| 26 namespace rtc { | 27 namespace rtc { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 45 static void DetachThenCallFromDifferentThreadImpl(); | 46 static void DetachThenCallFromDifferentThreadImpl(); |
| 46 | 47 |
| 47 private: | 48 private: |
| 48 RTC_DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass); | 49 RTC_DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass); |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 // Calls ThreadCheckerClass::DoStuff on another thread. | 52 // Calls ThreadCheckerClass::DoStuff on another thread. |
| 52 class CallDoStuffOnThread : public Thread { | 53 class CallDoStuffOnThread : public Thread { |
| 53 public: | 54 public: |
| 54 explicit CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class) | 55 explicit CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class) |
| 55 : Thread(), | 56 : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())), |
| 56 thread_checker_class_(thread_checker_class) { | 57 thread_checker_class_(thread_checker_class) { |
| 57 SetName("call_do_stuff_on_thread", nullptr); | 58 SetName("call_do_stuff_on_thread", nullptr); |
| 58 } | 59 } |
| 59 | 60 |
| 60 void Run() override { thread_checker_class_->DoStuff(); } | 61 void Run() override { thread_checker_class_->DoStuff(); } |
| 61 | 62 |
| 62 // New method. Needed since Thread::Join is protected, and it is called by | 63 // New method. Needed since Thread::Join is protected, and it is called by |
| 63 // the TEST. | 64 // the TEST. |
| 64 void Join() { | 65 void Join() { |
| 65 Thread::Join(); | 66 Thread::Join(); |
| 66 } | 67 } |
| 67 | 68 |
| 68 private: | 69 private: |
| 69 ThreadCheckerClass* thread_checker_class_; | 70 ThreadCheckerClass* thread_checker_class_; |
| 70 | 71 |
| 71 RTC_DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread); | 72 RTC_DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread); |
| 72 }; | 73 }; |
| 73 | 74 |
| 74 // Deletes ThreadCheckerClass on a different thread. | 75 // Deletes ThreadCheckerClass on a different thread. |
| 75 class DeleteThreadCheckerClassOnThread : public Thread { | 76 class DeleteThreadCheckerClassOnThread : public Thread { |
| 76 public: | 77 public: |
| 77 explicit DeleteThreadCheckerClassOnThread( | 78 explicit DeleteThreadCheckerClassOnThread( |
| 78 ThreadCheckerClass* thread_checker_class) | 79 std::unique_ptr<ThreadCheckerClass> thread_checker_class) |
| 79 : Thread(), | 80 : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())), |
| 80 thread_checker_class_(thread_checker_class) { | 81 thread_checker_class_(std::move(thread_checker_class)) { |
| 81 SetName("delete_thread_checker_class_on_thread", nullptr); | 82 SetName("delete_thread_checker_class_on_thread", nullptr); |
| 82 } | 83 } |
| 83 | 84 |
| 84 void Run() override { thread_checker_class_.reset(); } | 85 void Run() override { thread_checker_class_.reset(); } |
| 85 | 86 |
| 86 // New method. Needed since Thread::Join is protected, and it is called by | 87 // New method. Needed since Thread::Join is protected, and it is called by |
| 87 // the TEST. | 88 // the TEST. |
| 88 void Join() { | 89 void Join() { |
| 89 Thread::Join(); | 90 Thread::Join(); |
| 90 } | 91 } |
| 91 | 92 |
| 93 bool has_been_deleted() const { return !thread_checker_class_; } |
| 94 |
| 92 private: | 95 private: |
| 93 std::unique_ptr<ThreadCheckerClass> thread_checker_class_; | 96 std::unique_ptr<ThreadCheckerClass> thread_checker_class_; |
| 94 | 97 |
| 95 RTC_DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); | 98 RTC_DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); |
| 96 }; | 99 }; |
| 97 | 100 |
| 98 } // namespace | 101 } // namespace |
| 99 | 102 |
| 100 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { | 103 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { |
| 101 std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 104 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 102 new ThreadCheckerClass); | 105 new ThreadCheckerClass); |
| 103 | 106 |
| 104 // Verify that DoStuff doesn't assert. | 107 // Verify that DoStuff doesn't assert. |
| 105 thread_checker_class->DoStuff(); | 108 thread_checker_class->DoStuff(); |
| 106 | 109 |
| 107 // Verify that the destructor doesn't assert. | 110 // Verify that the destructor doesn't assert. |
| 108 thread_checker_class.reset(); | 111 thread_checker_class.reset(); |
| 109 } | 112 } |
| 110 | 113 |
| 111 TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) { | 114 TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) { |
| 112 std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 115 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 113 new ThreadCheckerClass); | 116 new ThreadCheckerClass); |
| 114 | 117 |
| 115 // Verify that the destructor doesn't assert | 118 // Verify that the destructor doesn't assert |
| 116 // when called on a different thread. | 119 // when called on a different thread. |
| 117 DeleteThreadCheckerClassOnThread delete_on_thread( | 120 DeleteThreadCheckerClassOnThread delete_on_thread( |
| 118 thread_checker_class.release()); | 121 std::move(thread_checker_class)); |
| 122 |
| 123 EXPECT_FALSE(delete_on_thread.has_been_deleted()); |
| 119 | 124 |
| 120 delete_on_thread.Start(); | 125 delete_on_thread.Start(); |
| 121 delete_on_thread.Join(); | 126 delete_on_thread.Join(); |
| 127 |
| 128 EXPECT_TRUE(delete_on_thread.has_been_deleted()); |
| 122 } | 129 } |
| 123 | 130 |
| 124 TEST(ThreadCheckerTest, DetachFromThread) { | 131 TEST(ThreadCheckerTest, DetachFromThread) { |
| 125 std::unique_ptr<ThreadCheckerClass> thread_checker_class( | 132 std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
| 126 new ThreadCheckerClass); | 133 new ThreadCheckerClass); |
| 127 | 134 |
| 128 // Verify that DoStuff doesn't assert when called on a different thread after | 135 // Verify that DoStuff doesn't assert when called on a different thread after |
| 129 // a call to DetachFromThread. | 136 // a call to DetachFromThread. |
| 130 thread_checker_class->DetachFromThread(); | 137 thread_checker_class->DetachFromThread(); |
| 131 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | 138 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 250 |
| 244 int var_thread_ ACCESS_ON(thread_); | 251 int var_thread_ ACCESS_ON(thread_); |
| 245 int var_checker_ GUARDED_BY(checker_); | 252 int var_checker_ GUARDED_BY(checker_); |
| 246 int var_queue_ ACCESS_ON(queue_); | 253 int var_queue_ ACCESS_ON(queue_); |
| 247 }; | 254 }; |
| 248 | 255 |
| 249 // Just in case we ever get lumped together with other compilation units. | 256 // Just in case we ever get lumped together with other compilation units. |
| 250 #undef ENABLE_THREAD_CHECKER | 257 #undef ENABLE_THREAD_CHECKER |
| 251 | 258 |
| 252 } // namespace rtc | 259 } // namespace rtc |
| OLD | NEW |