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
245
246
247
248
249
250
251
252
253
254
255
256
257
|
#include "pool.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/stream/output.h>
#include <util/random/fast.h>
#include <util/system/spinlock.h>
#include <util/system/thread.h>
#include <util/system/mutex.h>
#include <util/system/condvar.h>
struct TThreadPoolTest {
TSpinLock Lock;
long R = -1;
struct TTask: public IObjectInQueue {
TThreadPoolTest* Test = nullptr;
long Value = 0;
TTask(TThreadPoolTest* test, int value)
: Test(test)
, Value(value)
{
}
void Process(void*) override {
THolder<TTask> This(this);
TGuard<TSpinLock> guard(Test->Lock);
Test->R ^= Value;
}
};
struct TOwnedTask: public IObjectInQueue {
bool& Processed;
bool& Destructed;
TOwnedTask(bool& processed, bool& destructed)
: Processed(processed)
, Destructed(destructed)
{
}
~TOwnedTask() override {
Destructed = true;
}
void Process(void*) override {
Processed = true;
}
};
inline void TestAnyQueue(IThreadPool* queue, size_t queueSize = 1000) {
TReallyFastRng32 rand(17);
const size_t cnt = 1000;
R = 0;
for (size_t i = 0; i < cnt; ++i) {
R ^= (long)rand.GenRand();
}
queue->Start(10, queueSize);
rand = TReallyFastRng32(17);
for (size_t i = 0; i < cnt; ++i) {
UNIT_ASSERT(queue->Add(new TTask(this, (long)rand.GenRand())));
}
queue->Stop();
UNIT_ASSERT_EQUAL(0, R);
}
};
class TFailAddQueue: public IThreadPool {
public:
bool Add(IObjectInQueue* /*obj*/) override Y_WARN_UNUSED_RESULT {
return false;
}
void Start(size_t, size_t) override {
}
void Stop() noexcept override {
}
size_t Size() const noexcept override {
return 0;
}
};
Y_UNIT_TEST_SUITE(TThreadPoolTest) {
Y_UNIT_TEST(TestTThreadPool) {
TThreadPoolTest t;
TThreadPool q;
t.TestAnyQueue(&q);
}
Y_UNIT_TEST(TestTThreadPoolBlocking) {
TThreadPoolTest t;
TThreadPool q(TThreadPool::TParams().SetBlocking(true));
t.TestAnyQueue(&q, 100);
}
// disabled by pg@ long time ago due to test flaps
// Tried to enable: REVIEW:78772
Y_UNIT_TEST(TestTAdaptiveThreadPool) {
if (false) {
TThreadPoolTest t;
TAdaptiveThreadPool q;
t.TestAnyQueue(&q);
}
}
Y_UNIT_TEST(TestAddAndOwn) {
TThreadPool q;
q.Start(2);
bool processed = false;
bool destructed = false;
q.SafeAddAndOwn(MakeHolder<TThreadPoolTest::TOwnedTask>(processed, destructed));
q.Stop();
UNIT_ASSERT_C(processed, "Not processed");
UNIT_ASSERT_C(destructed, "Not destructed");
}
Y_UNIT_TEST(TestAddFunc) {
TFailAddQueue queue;
bool added = queue.AddFunc(
[]() {} // Lambda, I call him 'Lambda'!
);
UNIT_ASSERT_VALUES_EQUAL(added, false);
}
Y_UNIT_TEST(TestSafeAddFuncThrows) {
TFailAddQueue queue;
UNIT_CHECK_GENERATED_EXCEPTION(queue.SafeAddFunc([] {}), TThreadPoolException);
}
Y_UNIT_TEST(TestFunctionNotCopied) {
struct TFailOnCopy {
TFailOnCopy() {
}
TFailOnCopy(TFailOnCopy&&) {
}
TFailOnCopy(const TFailOnCopy&) {
UNIT_FAIL("Don't copy std::function inside TThreadPool");
}
};
TThreadPool queue(TThreadPool::TParams().SetBlocking(false).SetCatching(true));
queue.Start(2);
queue.SafeAddFunc([data = TFailOnCopy()]() {});
queue.Stop();
}
Y_UNIT_TEST(TestInfoGetters) {
TThreadPool queue;
queue.Start(2, 7);
UNIT_ASSERT_EQUAL(queue.GetThreadCountExpected(), 2);
UNIT_ASSERT_EQUAL(queue.GetThreadCountReal(), 2);
UNIT_ASSERT_EQUAL(queue.GetMaxQueueSize(), 7);
queue.Stop();
queue.Start(4, 1);
UNIT_ASSERT_EQUAL(queue.GetThreadCountExpected(), 4);
UNIT_ASSERT_EQUAL(queue.GetThreadCountReal(), 4);
UNIT_ASSERT_EQUAL(queue.GetMaxQueueSize(), 1);
queue.Stop();
}
void TestFixedThreadName(IThreadPool& pool, const TString& expectedName) {
pool.Start(1);
TString name;
pool.SafeAddFunc([&name]() {
name = TThread::CurrentThreadName();
});
pool.Stop();
if (TThread::CanGetCurrentThreadName()) {
UNIT_ASSERT_EQUAL(name, expectedName);
UNIT_ASSERT_UNEQUAL(TThread::CurrentThreadName(), expectedName);
}
}
Y_UNIT_TEST(TestFixedThreadName) {
const TString expectedName = "HelloWorld";
{
TThreadPool pool(TThreadPool::TParams().SetBlocking(true).SetCatching(false).SetThreadName(expectedName));
TestFixedThreadName(pool, expectedName);
}
{
TAdaptiveThreadPool pool(TThreadPool::TParams().SetThreadName(expectedName));
TestFixedThreadName(pool, expectedName);
}
}
void TestEnumeratedThreadName(IThreadPool& pool, const THashSet<TString>& expectedNames) {
pool.Start(expectedNames.size());
TMutex lock;
TCondVar allReady;
size_t readyCount = 0;
THashSet<TString> names;
for (size_t i = 0; i < expectedNames.size(); ++i) {
pool.SafeAddFunc([&]() {
with_lock (lock) {
if (++readyCount == expectedNames.size()) {
allReady.BroadCast();
} else {
while (readyCount != expectedNames.size()) {
allReady.WaitI(lock);
}
}
names.insert(TThread::CurrentThreadName());
}
});
}
pool.Stop();
if (TThread::CanGetCurrentThreadName()) {
UNIT_ASSERT_EQUAL(names, expectedNames);
}
}
Y_UNIT_TEST(TestEnumeratedThreadName) {
const TString namePrefix = "HelloWorld";
const THashSet<TString> expectedNames = {
"HelloWorld0",
"HelloWorld1",
"HelloWorld2",
"HelloWorld3",
"HelloWorld4",
"HelloWorld5",
"HelloWorld6",
"HelloWorld7",
"HelloWorld8",
"HelloWorld9",
"HelloWorld10",
};
{
TThreadPool pool(TThreadPool::TParams().SetBlocking(true).SetCatching(false).SetThreadNamePrefix(namePrefix));
TestEnumeratedThreadName(pool, expectedNames);
}
{
TAdaptiveThreadPool pool(TThreadPool::TParams().SetThreadNamePrefix(namePrefix));
TestEnumeratedThreadName(pool, expectedNames);
}
}
}
|