| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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. | |
| 4 | |
| 5 #include "base/basictypes.h" | |
| 6 #include "base/logging.h" | |
| 7 #include "base/thread_checker.h" | |
| 8 #include "base/scoped_ptr.h" | |
| 9 #include "base/threading/simple_thread.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 #ifndef NDEBUG | |
| 13 | |
| 14 // Simple class to exersice the basics of ThreadChecker. | |
| 15 // Both the destructor and DoStuff should verify that they were | |
| 16 // called on the same thread as the constructor. | |
| 17 class ThreadCheckerClass : public ThreadChecker { | |
| 18 public: | |
| 19 ThreadCheckerClass() {} | |
| 20 | |
| 21 // Verifies that it was called on the same thread as the constructor. | |
| 22 void DoStuff() { | |
| 23 DCHECK(CalledOnValidThread()); | |
| 24 } | |
| 25 | |
| 26 void DetachFromThread() { | |
| 27 ThreadChecker::DetachFromThread(); | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass); | |
| 32 }; | |
| 33 | |
| 34 // Calls ThreadCheckerClass::DoStuff on another thread. | |
| 35 class CallDoStuffOnThread : public base::SimpleThread { | |
| 36 public: | |
| 37 CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class) | |
| 38 : SimpleThread("call_do_stuff_on_thread"), | |
| 39 thread_checker_class_(thread_checker_class) { | |
| 40 } | |
| 41 | |
| 42 virtual void Run() { | |
| 43 thread_checker_class_->DoStuff(); | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 ThreadCheckerClass* thread_checker_class_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread); | |
| 50 }; | |
| 51 | |
| 52 // Deletes ThreadCheckerClass on a different thread. | |
| 53 class DeleteThreadCheckerClassOnThread : public base::SimpleThread { | |
| 54 public: | |
| 55 DeleteThreadCheckerClassOnThread(ThreadCheckerClass* thread_checker_class) | |
| 56 : SimpleThread("delete_thread_checker_class_on_thread"), | |
| 57 thread_checker_class_(thread_checker_class) { | |
| 58 } | |
| 59 | |
| 60 virtual void Run() { | |
| 61 thread_checker_class_.reset(); | |
| 62 } | |
| 63 | |
| 64 private: | |
| 65 scoped_ptr<ThreadCheckerClass> thread_checker_class_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); | |
| 68 }; | |
| 69 | |
| 70 TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { | |
| 71 scoped_ptr<ThreadCheckerClass> thread_checker_class( | |
| 72 new ThreadCheckerClass); | |
| 73 | |
| 74 // Verify that DoStuff doesn't assert. | |
| 75 thread_checker_class->DoStuff(); | |
| 76 | |
| 77 // Verify that the destructor doesn't assert. | |
| 78 thread_checker_class.reset(); | |
| 79 } | |
| 80 | |
| 81 TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) { | |
| 82 scoped_ptr<ThreadCheckerClass> thread_checker_class( | |
| 83 new ThreadCheckerClass); | |
| 84 | |
| 85 // Verify that the destructor doesn't assert | |
| 86 // when called on a different thread. | |
| 87 DeleteThreadCheckerClassOnThread delete_on_thread( | |
| 88 thread_checker_class.release()); | |
| 89 | |
| 90 delete_on_thread.Start(); | |
| 91 delete_on_thread.Join(); | |
| 92 } | |
| 93 | |
| 94 TEST(ThreadCheckerTest, DetachFromThread) { | |
| 95 scoped_ptr<ThreadCheckerClass> thread_checker_class( | |
| 96 new ThreadCheckerClass); | |
| 97 | |
| 98 // Verify that DoStuff doesn't assert when called on a different thread after | |
| 99 // a call to DetachFromThread. | |
| 100 thread_checker_class->DetachFromThread(); | |
| 101 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | |
| 102 | |
| 103 call_on_thread.Start(); | |
| 104 call_on_thread.Join(); | |
| 105 } | |
| 106 | |
| 107 #if GTEST_HAS_DEATH_TEST | |
| 108 | |
| 109 TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThread) { | |
| 110 ASSERT_DEBUG_DEATH({ | |
| 111 scoped_ptr<ThreadCheckerClass> thread_checker_class( | |
| 112 new ThreadCheckerClass); | |
| 113 | |
| 114 // Verify that DoStuff asserts when called on a different thread. | |
| 115 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | |
| 116 | |
| 117 call_on_thread.Start(); | |
| 118 call_on_thread.Join(); | |
| 119 }, ""); | |
| 120 } | |
| 121 | |
| 122 TEST(ThreadCheckerDeathTest, DetachFromThread) { | |
| 123 ASSERT_DEBUG_DEATH({ | |
| 124 scoped_ptr<ThreadCheckerClass> thread_checker_class( | |
| 125 new ThreadCheckerClass); | |
| 126 | |
| 127 // Verify that DoStuff doesn't assert when called on a different thread | |
| 128 // after a call to DetachFromThread. | |
| 129 thread_checker_class->DetachFromThread(); | |
| 130 CallDoStuffOnThread call_on_thread(thread_checker_class.get()); | |
| 131 | |
| 132 call_on_thread.Start(); | |
| 133 call_on_thread.Join(); | |
| 134 | |
| 135 // Verify that DoStuff asserts after moving to another thread. | |
| 136 thread_checker_class->DoStuff(); | |
| 137 }, ""); | |
| 138 } | |
| 139 | |
| 140 #endif // GTEST_HAS_DEATH_TEST | |
| 141 | |
| 142 #endif // NDEBUG | |
| OLD | NEW |