1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
base / task / thread_pool / worker_thread_set_unittest.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task/thread_pool/worker_thread_set.h"
#include "base/check_op.h"
#include "base/memory/ref_counted.h"
#include "base/task/thread_pool/task_source.h"
#include "base/task/thread_pool/task_tracker.h"
#include "base/task/thread_pool/worker_thread.h"
#include "base/test/gtest_util.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base::internal {
namespace {
class MockWorkerThreadDelegate : public WorkerThread::Delegate {
public:
WorkerThread::ThreadLabel GetThreadLabel() const override {
return WorkerThread::ThreadLabel::DEDICATED;
}
void OnMainEntry(WorkerThread* worker) override {}
RegisteredTaskSource GetWork(WorkerThread* worker) override {
return nullptr;
}
RegisteredTaskSource SwapProcessedTask(RegisteredTaskSource task_source,
WorkerThread* worker) override {
ADD_FAILURE() << "Unexpected call to SwapProcessedTask()";
return nullptr;
}
TimeDelta GetSleepTimeout() override { return TimeDelta::Max(); }
};
class ThreadPoolWorkerSetTest : public testing::Test {
protected:
void SetUp() override {
worker_a_ = MakeRefCounted<WorkerThread>(
ThreadType::kDefault, std::make_unique<MockWorkerThreadDelegate>(),
task_tracker_.GetTrackedRef(), 0);
ASSERT_TRUE(worker_a_);
worker_b_ = MakeRefCounted<WorkerThread>(
ThreadType::kDefault, std::make_unique<MockWorkerThreadDelegate>(),
task_tracker_.GetTrackedRef(), 1);
ASSERT_TRUE(worker_b_);
worker_c_ = MakeRefCounted<WorkerThread>(
ThreadType::kDefault, std::make_unique<MockWorkerThreadDelegate>(),
task_tracker_.GetTrackedRef(), 2);
ASSERT_TRUE(worker_c_);
}
private:
TaskTracker task_tracker_;
protected:
scoped_refptr<WorkerThread> worker_a_;
scoped_refptr<WorkerThread> worker_b_;
scoped_refptr<WorkerThread> worker_c_;
};
} // namespace
// Verify that Insert() and Take() add/remove values in FIFO order.
TEST_F(ThreadPoolWorkerSetTest, InsertTake) {
WorkerThreadSet set;
EXPECT_EQ(nullptr, set.Take());
EXPECT_TRUE(set.IsEmpty());
EXPECT_EQ(0U, set.Size());
set.Insert(worker_a_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(1U, set.Size());
set.Insert(worker_b_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(2U, set.Size());
set.Insert(worker_c_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(3U, set.Size());
WorkerThread* idle_worker = set.Take();
EXPECT_EQ(idle_worker, worker_a_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(2U, set.Size());
set.Insert(idle_worker);
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(3U, set.Size());
EXPECT_EQ(idle_worker, set.Take());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(2U, set.Size());
EXPECT_TRUE(set.Take());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(1U, set.Size());
EXPECT_TRUE(set.Take());
EXPECT_TRUE(set.IsEmpty());
EXPECT_EQ(0U, set.Size());
EXPECT_EQ(nullptr, set.Take());
}
// Verify that Peek() returns the correct values in FIFO order.
TEST_F(ThreadPoolWorkerSetTest, PeekPop) {
WorkerThreadSet set;
EXPECT_EQ(nullptr, set.Peek());
EXPECT_TRUE(set.IsEmpty());
EXPECT_EQ(0U, set.Size());
set.Insert(worker_a_.get());
EXPECT_EQ(worker_a_.get(), set.Peek());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(1U, set.Size());
set.Insert(worker_b_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(2U, set.Size());
set.Insert(worker_c_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(3U, set.Size());
WorkerThread* idle_worker = set.Take();
EXPECT_EQ(worker_a_.get(), idle_worker);
EXPECT_EQ(worker_b_.get(), set.Peek());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(2U, set.Size());
EXPECT_EQ(worker_b_.get(), set.Take());
EXPECT_EQ(worker_c_.get(), set.Peek());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(1U, set.Size());
EXPECT_EQ(worker_c_.get(), set.Take());
EXPECT_TRUE(set.IsEmpty());
EXPECT_EQ(0U, set.Size());
EXPECT_EQ(nullptr, set.Peek());
}
// Verify that Contains() returns true for workers on the set.
TEST_F(ThreadPoolWorkerSetTest, Contains) {
WorkerThreadSet set;
EXPECT_FALSE(set.Contains(worker_a_.get()));
EXPECT_FALSE(set.Contains(worker_b_.get()));
EXPECT_FALSE(set.Contains(worker_c_.get()));
set.Insert(worker_a_.get());
EXPECT_TRUE(set.Contains(worker_a_.get()));
EXPECT_FALSE(set.Contains(worker_b_.get()));
EXPECT_FALSE(set.Contains(worker_c_.get()));
set.Insert(worker_b_.get());
EXPECT_TRUE(set.Contains(worker_a_.get()));
EXPECT_TRUE(set.Contains(worker_b_.get()));
EXPECT_FALSE(set.Contains(worker_c_.get()));
set.Insert(worker_c_.get());
EXPECT_TRUE(set.Contains(worker_a_.get()));
EXPECT_TRUE(set.Contains(worker_b_.get()));
EXPECT_TRUE(set.Contains(worker_c_.get()));
WorkerThread* idle_worker = set.Take();
EXPECT_EQ(idle_worker, worker_a_.get());
EXPECT_FALSE(set.Contains(worker_a_.get()));
EXPECT_TRUE(set.Contains(worker_b_.get()));
EXPECT_TRUE(set.Contains(worker_c_.get()));
set.Take();
set.Take();
EXPECT_FALSE(set.Contains(worker_a_.get()));
EXPECT_FALSE(set.Contains(worker_b_.get()));
EXPECT_FALSE(set.Contains(worker_c_.get()));
}
// Verify that a value can be removed by Remove().
TEST_F(ThreadPoolWorkerSetTest, Remove) {
WorkerThreadSet set;
EXPECT_TRUE(set.IsEmpty());
EXPECT_EQ(0U, set.Size());
set.Insert(worker_a_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(1U, set.Size());
set.Insert(worker_b_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(2U, set.Size());
set.Insert(worker_c_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(3U, set.Size());
set.Remove(worker_b_.get());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(2U, set.Size());
EXPECT_EQ(worker_a_.get(), set.Take());
EXPECT_FALSE(set.IsEmpty());
EXPECT_EQ(1U, set.Size());
EXPECT_EQ(worker_c_.get(), set.Take());
EXPECT_TRUE(set.IsEmpty());
EXPECT_EQ(0U, set.Size());
}
// Verify that a value can be pushed again after it has been removed.
TEST_F(ThreadPoolWorkerSetTest, PushAfterRemove) {
WorkerThreadSet set;
EXPECT_EQ(0U, set.Size());
set.Insert(worker_a_.get());
EXPECT_EQ(1U, set.Size());
// Need to also push worker B for this test as it's illegal to Remove() the
// front of the set.
set.Insert(worker_b_.get());
EXPECT_EQ(2U, set.Size());
set.Remove(worker_b_.get());
worker_b_->EndUnusedPeriod();
EXPECT_EQ(1U, set.Size());
set.Insert(worker_b_.get());
EXPECT_EQ(2U, set.Size());
}
// Verify that Insert() DCHECKs when a value is inserted twice.
TEST_F(ThreadPoolWorkerSetTest, PushTwice) {
WorkerThreadSet set;
set.Insert(worker_a_.get());
EXPECT_DCHECK_DEATH({ set.Insert(worker_a_.get()); });
}
} // namespace base::internal