Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: util/thread/worker_thread_test.cc

Issue 1526563003: Create WorkerThread, an abstraction to perform some work on an interval. (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <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.
18
19 #include "gtest/gtest.h"
20
21 namespace crashpad {
22 namespace test {
23 namespace {
24
25 class WorkDelegate : public WorkerThread::Delegate {
26 public:
27 WorkDelegate() {}
28 ~WorkDelegate() {}
29
30 void DoWork(const WorkerThread* thread) override {
31 ++work_count_;
32 }
33
34 int work_count() const { return work_count_; }
35
36 private:
37 int work_count_ = 0;
38
39 DISALLOW_COPY_AND_ASSIGN(WorkDelegate);
40 };
41
42 TEST(WorkerThread, DoWork) {
43 WorkDelegate delegate;
44 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.
45
46 time_t now = time(nullptr);
47 thread.Start(0);
48 EXPECT_TRUE(thread.is_running());
49
50 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.
51 thread.Stop();
52 EXPECT_FALSE(thread.is_running());
53
54 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.
55 }
56
57 TEST(WorkerThread, StopBeforeDoWork) {
58 WorkDelegate delegate;
59 WorkerThread thread(1, &delegate);
60
61 thread.Start(15);
62 thread.Stop();
63
64 EXPECT_EQ(0, delegate.work_count());
65 }
66
67 TEST(WorkerThread, Restart) {
68 WorkDelegate delegate;
69 WorkerThread thread(0.5, &delegate);
70
71 thread.Start(0);
72 EXPECT_TRUE(thread.is_running());
73
74 while (delegate.work_count() < 1) {}
75 thread.Stop();
76 ASSERT_FALSE(thread.is_running());
77
78 thread.Start(0);
79 while (delegate.work_count() < 2) {}
80 thread.Stop();
81 ASSERT_FALSE(thread.is_running());
82 }
83
84 TEST(WorkerThread, DoWorkNow) {
85 WorkDelegate delegate;
86 WorkerThread thread(100, &delegate);
87
88 thread.Start(0);
89 EXPECT_TRUE(thread.is_running());
90
91 time_t now = time(nullptr);
92
93 while (delegate.work_count() < 1) {}
94 EXPECT_EQ(1, delegate.work_count());
95
96 thread.DoWorkNow();
97 while (delegate.work_count() < 2) {}
98 thread.Stop();
99 EXPECT_EQ(2, delegate.work_count());
100
101 EXPECT_GE(100, now - time(nullptr));
102 }
103
104 TEST(WorkerThread, DoWorkNowAtStart) {
105 WorkDelegate delegate;
106 WorkerThread thread(100, &delegate);
107
108 time_t now = time(nullptr);
109
110 thread.Start(100);
111 EXPECT_TRUE(thread.is_running());
112
113 thread.DoWorkNow();
114 while (delegate.work_count() < 1) {}
115 EXPECT_EQ(1, delegate.work_count());
116
117 EXPECT_GE(100, now - time(nullptr));
118
119 thread.Stop();
120 EXPECT_FALSE(thread.is_running());
121
Mark Mentovai 2015/12/15 17:56:59 Blank line at end of function.
Robert Sesek 2015/12/23 19:22:32 Done.
122 }
123
124 } // namespace
125 } // namespace test
126 } // namespace crashpad
OLDNEW
« util/thread/worker_thread.cc ('K') | « util/thread/worker_thread.cc ('k') | util/util.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698