OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/thread/thread.h" |
| 16 |
| 17 #include "base/basictypes.h" |
| 18 #include "gtest/gtest.h" |
| 19 #include "util/synchronization/semaphore.h" |
| 20 |
| 21 namespace crashpad { |
| 22 namespace test { |
| 23 namespace { |
| 24 |
| 25 class NoopThread : public Thread { |
| 26 public: |
| 27 NoopThread() {} |
| 28 ~NoopThread() override {} |
| 29 |
| 30 private: |
| 31 void ThreadMain() override {} |
| 32 |
| 33 DISALLOW_COPY_AND_ASSIGN(NoopThread); |
| 34 }; |
| 35 |
| 36 class WaitThread : public Thread { |
| 37 public: |
| 38 explicit WaitThread(Semaphore* semaphore) : semaphore_(semaphore) {} |
| 39 ~WaitThread() override {} |
| 40 |
| 41 private: |
| 42 void ThreadMain() override { semaphore_->Wait(); } |
| 43 |
| 44 Semaphore* semaphore_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(WaitThread); |
| 47 }; |
| 48 |
| 49 class JoinAndSignalThread : public Thread { |
| 50 public: |
| 51 JoinAndSignalThread(Thread* thread, Semaphore* semaphore) |
| 52 : thread_(thread), semaphore_(semaphore) {} |
| 53 ~JoinAndSignalThread() override {} |
| 54 |
| 55 private: |
| 56 void ThreadMain() override { |
| 57 thread_->Join(); |
| 58 semaphore_->Signal(); |
| 59 } |
| 60 |
| 61 Thread* thread_; |
| 62 Semaphore* semaphore_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(JoinAndSignalThread); |
| 65 }; |
| 66 |
| 67 TEST(ThreadTest, NoStart) { |
| 68 NoopThread thread; |
| 69 } |
| 70 |
| 71 TEST(ThreadTest, Start) { |
| 72 NoopThread thread; |
| 73 thread.Start(); |
| 74 thread.Join(); |
| 75 } |
| 76 |
| 77 TEST(ThreadTest, JoinBlocks) { |
| 78 Semaphore unblock_wait_thread_semaphore(0); |
| 79 Semaphore join_completed_semaphore(0); |
| 80 WaitThread wait_thread(&unblock_wait_thread_semaphore); |
| 81 wait_thread.Start(); |
| 82 JoinAndSignalThread join_and_signal_thread(&wait_thread, |
| 83 &join_completed_semaphore); |
| 84 join_and_signal_thread.Start(); |
| 85 // join_completed_semaphore will be signaled when wait_thread.Join() returns |
| 86 // (in JoinAndSignalThread::ThreadMain). Since wait_thread is blocking on |
| 87 // unblock_wait_thread_semaphore, we don't expect the Join to return yet. We |
| 88 // wait up to 100ms to give a broken implementation of Thread::Join a chance |
| 89 // to return. |
| 90 ASSERT_FALSE(join_completed_semaphore.TimedWait(.1)); |
| 91 unblock_wait_thread_semaphore.Signal(); |
| 92 join_completed_semaphore.Wait(); |
| 93 join_and_signal_thread.Join(); |
| 94 } |
| 95 |
| 96 } // namespace |
| 97 } // namespace test |
| 98 } // namespace crashpad |
OLD | NEW |