2016-01-04 17:10:58 -05:00
|
|
|
// 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/worker_thread.h"
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "util/misc/clock.h"
|
|
|
|
#include "util/synchronization/semaphore.h"
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
namespace test {
|
|
|
|
namespace {
|
|
|
|
|
2017-07-25 14:31:27 -04:00
|
|
|
constexpr uint64_t kNanosecondsPerSecond = static_cast<uint64_t>(1E9);
|
2016-01-04 17:10:58 -05:00
|
|
|
|
|
|
|
class WorkDelegate : public WorkerThread::Delegate {
|
|
|
|
public:
|
|
|
|
WorkDelegate() {}
|
|
|
|
~WorkDelegate() {}
|
|
|
|
|
|
|
|
void DoWork(const WorkerThread* thread) override {
|
2017-03-19 15:30:19 -04:00
|
|
|
if (work_count_ < waiting_for_count_) {
|
|
|
|
if (++work_count_ == waiting_for_count_) {
|
|
|
|
semaphore_.Signal();
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 17:10:58 -05:00
|
|
|
}
|
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
void SetDesiredWorkCount(int times) {
|
2016-01-04 17:10:58 -05:00
|
|
|
waiting_for_count_ = times;
|
2016-05-20 13:05:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Suspends the calling thread until the DoWork() has been called
|
|
|
|
//! the number of times specified by SetDesiredWorkCount().
|
|
|
|
void WaitForWorkCount() {
|
2016-01-04 17:10:58 -05:00
|
|
|
semaphore_.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
int work_count() const { return work_count_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Semaphore semaphore_{0};
|
|
|
|
int work_count_ = 0;
|
|
|
|
int waiting_for_count_ = -1;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(WorkDelegate);
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(WorkerThread, DoWork) {
|
|
|
|
WorkDelegate delegate;
|
|
|
|
WorkerThread thread(0.05, &delegate);
|
|
|
|
|
|
|
|
uint64_t start = ClockMonotonicNanoseconds();
|
2017-03-19 15:30:19 -04:00
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.SetDesiredWorkCount(2);
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.Start(0);
|
|
|
|
EXPECT_TRUE(thread.is_running());
|
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.WaitForWorkCount();
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.Stop();
|
|
|
|
EXPECT_FALSE(thread.is_running());
|
|
|
|
|
|
|
|
EXPECT_GE(1 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WorkerThread, StopBeforeDoWork) {
|
|
|
|
WorkDelegate delegate;
|
|
|
|
WorkerThread thread(1, &delegate);
|
|
|
|
|
|
|
|
thread.Start(15);
|
|
|
|
thread.Stop();
|
|
|
|
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
EXPECT_EQ(delegate.work_count(), 0);
|
2016-01-04 17:10:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WorkerThread, Restart) {
|
|
|
|
WorkDelegate delegate;
|
|
|
|
WorkerThread thread(0.05, &delegate);
|
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.SetDesiredWorkCount(1);
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.Start(0);
|
|
|
|
EXPECT_TRUE(thread.is_running());
|
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.WaitForWorkCount();
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.Stop();
|
|
|
|
ASSERT_FALSE(thread.is_running());
|
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.SetDesiredWorkCount(2);
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.Start(0);
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.WaitForWorkCount();
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.Stop();
|
|
|
|
ASSERT_FALSE(thread.is_running());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WorkerThread, DoWorkNow) {
|
|
|
|
WorkDelegate delegate;
|
|
|
|
WorkerThread thread(100, &delegate);
|
|
|
|
|
2017-03-19 15:30:19 -04:00
|
|
|
uint64_t start = ClockMonotonicNanoseconds();
|
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.SetDesiredWorkCount(1);
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.Start(0);
|
|
|
|
EXPECT_TRUE(thread.is_running());
|
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.WaitForWorkCount();
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
EXPECT_EQ(delegate.work_count(), 1);
|
2016-01-04 17:10:58 -05:00
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.SetDesiredWorkCount(2);
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.DoWorkNow();
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.WaitForWorkCount();
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.Stop();
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
EXPECT_EQ(delegate.work_count(), 2);
|
2016-01-04 17:10:58 -05:00
|
|
|
|
|
|
|
EXPECT_GE(100 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WorkerThread, DoWorkNowAtStart) {
|
|
|
|
WorkDelegate delegate;
|
|
|
|
WorkerThread thread(100, &delegate);
|
|
|
|
|
|
|
|
uint64_t start = ClockMonotonicNanoseconds();
|
|
|
|
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.SetDesiredWorkCount(1);
|
2016-01-04 17:10:58 -05:00
|
|
|
thread.Start(100);
|
|
|
|
EXPECT_TRUE(thread.is_running());
|
|
|
|
|
|
|
|
thread.DoWorkNow();
|
2016-05-20 13:05:06 -07:00
|
|
|
delegate.WaitForWorkCount();
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
EXPECT_EQ(delegate.work_count(), 1);
|
2016-01-04 17:10:58 -05:00
|
|
|
|
|
|
|
EXPECT_GE(100 * kNanosecondsPerSecond, ClockMonotonicNanoseconds() - start);
|
|
|
|
|
|
|
|
thread.Stop();
|
|
|
|
EXPECT_FALSE(thread.is_running());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace test
|
|
|
|
} // namespace crashpad
|