Index: util/thread/worker_thread_test.cc |
diff --git a/util/thread/worker_thread_test.cc b/util/thread/worker_thread_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4d6dd34f0ba11b87f935e2c4bb0c00a24eb6227d |
--- /dev/null |
+++ b/util/thread/worker_thread_test.cc |
@@ -0,0 +1,126 @@ |
+// 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 <time.h> |
Mark Mentovai
2015/12/15 17:56:59
Don’t use time(), use ClockMonotonicNanoseconds()
Robert Sesek
2015/12/23 19:22:32
Done.
|
+ |
+#include "gtest/gtest.h" |
+ |
+namespace crashpad { |
+namespace test { |
+namespace { |
+ |
+class WorkDelegate : public WorkerThread::Delegate { |
+ public: |
+ WorkDelegate() {} |
+ ~WorkDelegate() {} |
+ |
+ void DoWork(const WorkerThread* thread) override { |
+ ++work_count_; |
+ } |
+ |
+ int work_count() const { return work_count_; } |
+ |
+ private: |
+ int work_count_ = 0; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WorkDelegate); |
+}; |
+ |
+TEST(WorkerThread, DoWork) { |
+ WorkDelegate delegate; |
+ WorkerThread thread(0.5, &delegate); |
Mark Mentovai
2015/12/15 17:56:59
Can you speed this up? If you’re not using 1-secon
Robert Sesek
2015/12/23 19:22:32
Done.
|
+ |
+ time_t now = time(nullptr); |
+ thread.Start(0); |
+ EXPECT_TRUE(thread.is_running()); |
+ |
+ while (delegate.work_count() < 2) {} |
Mark Mentovai
2015/12/15 17:56:59
Yuck. Can you do something better than a busy-spin
Robert Sesek
2015/12/23 19:22:32
Pfft, done.
|
+ thread.Stop(); |
+ EXPECT_FALSE(thread.is_running()); |
+ |
+ EXPECT_GE(2, time(nullptr) - now); |
Mark Mentovai
2015/12/15 17:56:59
Everything named “now” in this file should be “sta
Robert Sesek
2015/12/23 19:22:32
Done.
|
+} |
+ |
+TEST(WorkerThread, StopBeforeDoWork) { |
+ WorkDelegate delegate; |
+ WorkerThread thread(1, &delegate); |
+ |
+ thread.Start(15); |
+ thread.Stop(); |
+ |
+ EXPECT_EQ(0, delegate.work_count()); |
+} |
+ |
+TEST(WorkerThread, Restart) { |
+ WorkDelegate delegate; |
+ WorkerThread thread(0.5, &delegate); |
+ |
+ thread.Start(0); |
+ EXPECT_TRUE(thread.is_running()); |
+ |
+ while (delegate.work_count() < 1) {} |
+ thread.Stop(); |
+ ASSERT_FALSE(thread.is_running()); |
+ |
+ thread.Start(0); |
+ while (delegate.work_count() < 2) {} |
+ thread.Stop(); |
+ ASSERT_FALSE(thread.is_running()); |
+} |
+ |
+TEST(WorkerThread, DoWorkNow) { |
+ WorkDelegate delegate; |
+ WorkerThread thread(100, &delegate); |
+ |
+ thread.Start(0); |
+ EXPECT_TRUE(thread.is_running()); |
+ |
+ time_t now = time(nullptr); |
+ |
+ while (delegate.work_count() < 1) {} |
+ EXPECT_EQ(1, delegate.work_count()); |
+ |
+ thread.DoWorkNow(); |
+ while (delegate.work_count() < 2) {} |
+ thread.Stop(); |
+ EXPECT_EQ(2, delegate.work_count()); |
+ |
+ EXPECT_GE(100, now - time(nullptr)); |
+} |
+ |
+TEST(WorkerThread, DoWorkNowAtStart) { |
+ WorkDelegate delegate; |
+ WorkerThread thread(100, &delegate); |
+ |
+ time_t now = time(nullptr); |
+ |
+ thread.Start(100); |
+ EXPECT_TRUE(thread.is_running()); |
+ |
+ thread.DoWorkNow(); |
+ while (delegate.work_count() < 1) {} |
+ EXPECT_EQ(1, delegate.work_count()); |
+ |
+ EXPECT_GE(100, now - time(nullptr)); |
+ |
+ thread.Stop(); |
+ EXPECT_FALSE(thread.is_running()); |
+ |
Mark Mentovai
2015/12/15 17:56:59
Blank line at end of function.
Robert Sesek
2015/12/23 19:22:32
Done.
|
+} |
+ |
+} // namespace |
+} // namespace test |
+} // namespace crashpad |