Chromium Code Reviews| Index: util/thread/thread_test.cc |
| diff --git a/util/thread/thread_test.cc b/util/thread/thread_test.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dbebe1a07ce7348a8943c1ea692acdc8a5441290 |
| --- /dev/null |
| +++ b/util/thread/thread_test.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2015 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#include "util/thread/thread.h" |
| + |
| +#include "base/basictypes.h" |
| +#include "gtest/gtest.h" |
| +#include "util/synchronization/semaphore.h" |
| + |
| +namespace crashpad { |
| +namespace test { |
| +namespace { |
| + |
| +class NoopThread : public Thread { |
| + public: |
| + NoopThread() {} |
| + ~NoopThread() override {} |
| + |
| + private: |
| + void ThreadMain() override {} |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NoopThread); |
| +}; |
| + |
| +class WaitThread : public Thread { |
| + public: |
| + explicit WaitThread(Semaphore* semaphore) : semaphore_(semaphore) {} |
| + ~WaitThread() override {} |
| + |
| + private: |
| + void ThreadMain() override { semaphore_->Wait(); } |
| + |
| + Semaphore* semaphore_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WaitThread); |
| +}; |
| + |
| +class JoinAndSignalThread : public Thread { |
| + public: |
| + JoinAndSignalThread(Thread* thread, Semaphore* semaphore) |
| + : thread_(thread), semaphore_(semaphore) {} |
| + ~JoinAndSignalThread() override {} |
| + |
| + private: |
| + void ThreadMain() override { |
| + thread_->Join(); |
| + semaphore_->Signal(); |
| + } |
| + |
| + Thread* thread_; |
| + Semaphore* semaphore_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(JoinAndSignalThread); |
| +}; |
| + |
| +TEST(ThreadTest, NoStart) { |
| + NoopThread thread; |
| +} |
| + |
| +TEST(ThreadTest, Start) { |
| + NoopThread thread; |
| + thread.Start(); |
| + thread.Join(); |
| +} |
| + |
| +TEST(ThreadTest, JoinBlocks) { |
| + Semaphore unblock_wait_thread(0); |
|
scottmg
2015/05/11 21:52:00
`unblock_wait_thread` makes it sound like a thread
erikwright (departed)
2015/05/13 15:27:49
Done.
|
| + Semaphore join_completed(0); |
| + WaitThread wait_thread(&unblock_wait_thread); |
| + wait_thread.Start(); |
| + JoinAndSignalThread join_and_signal_thread(&wait_thread, &join_completed); |
| + join_and_signal_thread.Start(); |
| + // Wait 100ms to give a broken Join implementation a chance to complete. |
| + ASSERT_FALSE(join_completed.TimedWait(.1)); |
|
scottmg
2015/05/11 21:52:00
I don't like this timeout too much. I think just w
Robert Sesek
2015/05/11 22:10:51
+1. This will be flaky.
erikwright (departed)
2015/05/13 15:27:49
I think you misunderstood the purpose of this time
scottmg
2015/05/13 17:31:49
I guess we would need a WFMO version to avoid the
|
| + unblock_wait_thread.Signal(); |
| + join_completed.Wait(); |
| + join_and_signal_thread.Join(); |
| +} |
| + |
| +} // namespace |
| +} // namespace test |
| +} // namespace crashpad |