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/worker_thread.h" |
| 16 |
| 17 #include "gtest/gtest.h" |
| 18 #include "util/misc/clock.h" |
| 19 #include "util/synchronization/semaphore.h" |
| 20 |
| 21 namespace crashpad { |
| 22 namespace test { |
| 23 namespace { |
| 24 |
| 25 const uint64_t kNanosecondsPerSecond = 1E9; |
| 26 |
| 27 class WorkDelegate : public WorkerThread::Delegate { |
| 28 public: |
| 29 WorkDelegate() {} |
| 30 ~WorkDelegate() {} |
| 31 |
| 32 void DoWork(const WorkerThread* thread) override { |
| 33 if (++work_count_ == waiting_for_count_) |
| 34 semaphore_.Signal(); |
| 35 } |
| 36 |
| 37 //! \brief Suspends the calling thread until the DoWork() has been called |
| 38 //! the specified number of times. |
| 39 void WaitForWorkCount(int times) { |
| 40 waiting_for_count_ = times; |
| 41 semaphore_.Wait(); |
| 42 } |
| 43 |
| 44 int work_count() const { return work_count_; } |
| 45 |
| 46 private: |
| 47 Semaphore semaphore_{0}; |
| 48 int work_count_ = 0; |
| 49 int waiting_for_count_ = -1; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(WorkDelegate); |
| 52 }; |
| 53 |
| 54 TEST(WorkerThread, DoWork) { |
| 55 WorkDelegate delegate; |
| 56 WorkerThread thread(0.05, &delegate); |
| 57 |
| 58 uint64_t start = ClockMonotonicNanoseconds(); |
| 59 thread.Start(0); |
| 60 EXPECT_TRUE(thread.is_running()); |
| 61 |
| 62 delegate.WaitForWorkCount(2); |
| 63 thread.Stop(); |
| 64 EXPECT_FALSE(thread.is_running()); |
| 65 |
| 66 EXPECT_GE(1 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start); |
| 67 } |
| 68 |
| 69 TEST(WorkerThread, StopBeforeDoWork) { |
| 70 WorkDelegate delegate; |
| 71 WorkerThread thread(1, &delegate); |
| 72 |
| 73 thread.Start(15); |
| 74 thread.Stop(); |
| 75 |
| 76 EXPECT_EQ(0, delegate.work_count()); |
| 77 } |
| 78 |
| 79 TEST(WorkerThread, Restart) { |
| 80 WorkDelegate delegate; |
| 81 WorkerThread thread(0.05, &delegate); |
| 82 |
| 83 thread.Start(0); |
| 84 EXPECT_TRUE(thread.is_running()); |
| 85 |
| 86 delegate.WaitForWorkCount(1); |
| 87 thread.Stop(); |
| 88 ASSERT_FALSE(thread.is_running()); |
| 89 |
| 90 thread.Start(0); |
| 91 delegate.WaitForWorkCount(2); |
| 92 thread.Stop(); |
| 93 ASSERT_FALSE(thread.is_running()); |
| 94 } |
| 95 |
| 96 TEST(WorkerThread, DoWorkNow) { |
| 97 WorkDelegate delegate; |
| 98 WorkerThread thread(100, &delegate); |
| 99 |
| 100 thread.Start(0); |
| 101 EXPECT_TRUE(thread.is_running()); |
| 102 |
| 103 uint64_t start = ClockMonotonicNanoseconds(); |
| 104 |
| 105 delegate.WaitForWorkCount(1); |
| 106 EXPECT_EQ(1, delegate.work_count()); |
| 107 |
| 108 thread.DoWorkNow(); |
| 109 delegate.WaitForWorkCount(2); |
| 110 thread.Stop(); |
| 111 EXPECT_EQ(2, delegate.work_count()); |
| 112 |
| 113 EXPECT_GE(100 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start); |
| 114 } |
| 115 |
| 116 TEST(WorkerThread, DoWorkNowAtStart) { |
| 117 WorkDelegate delegate; |
| 118 WorkerThread thread(100, &delegate); |
| 119 |
| 120 uint64_t start = ClockMonotonicNanoseconds(); |
| 121 |
| 122 thread.Start(100); |
| 123 EXPECT_TRUE(thread.is_running()); |
| 124 |
| 125 thread.DoWorkNow(); |
| 126 delegate.WaitForWorkCount(1); |
| 127 EXPECT_EQ(1, delegate.work_count()); |
| 128 |
| 129 EXPECT_GE(100 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start); |
| 130 |
| 131 thread.Stop(); |
| 132 EXPECT_FALSE(thread.is_running()); |
| 133 } |
| 134 |
| 135 } // namespace |
| 136 } // namespace test |
| 137 } // namespace crashpad |
OLD | NEW |