forked from afborchert/tpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_suite.cpp
417 lines (391 loc) · 12 KB
/
test_suite.cpp
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
Copyright (c) 2015, 2016, 2017 Andreas F. Borchert
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
small test suite for thread_pool.h with 100% C0 coverage
to test the coverage using gcov,
* uncomment the -fprofile-arcs and -ftest-coverage flags in
the Makefile
* make clean && make
* ./test_suite
* gcov -m test_suite.cpp
* examine thread_pool.hpp.gcov
gcov -m -b test_suite.cpp delivers following results for
thread_pool.hpp:
Lines executed:100.00% of 63
Branches executed:97.63% of 422
Taken at least once:51.90% of 422
Calls executed:73.00% of 626
*/
#include <atomic>
#include <chrono>
#include <exception>
#include <iostream>
#include <memory>
#include <vector>
#include <thread_pool.hpp>
/* trivial test case with two simple tasks that do not overload
the thread pool */
bool t1() {
mt::thread_pool tpool(2);
auto r1 = tpool.submit([]() { return 20; });
auto r2 = tpool.submit([]() { return 22; });
return r1.get() + r2.get() == 42;
}
/* trivial test case where we explicitly join
with two simple tasks that do not overload the thread pool */
bool t2() {
mt::thread_pool tpool(2);
auto r1 = tpool.submit([]() { return 20; });
auto r2 = tpool.submit([]() { return 22; });
tpool.join();
return r1.get() + r2.get() == 42;
}
/* trivial test case with two simple tasks where the second
task has to be queued */
bool t3() {
mt::thread_pool tpool(1);
auto r1 = tpool.submit([]() { return 20; });
auto r2 = tpool.submit([]() { return 22; });
return r1.get() + r2.get() == 42;
}
/* somewhat larger test case where some tasks are to be queued */
bool t4() {
constexpr unsigned int size = 2;
constexpr unsigned int extra = 4;
mt::thread_pool tpool(size);
std::future<unsigned int> results[size + extra];
for (unsigned int i = 0; i < size + extra; ++i) {
results[i] = std::move(tpool.submit([i]() -> unsigned int {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return i + 1;
}));
}
unsigned int sum = 0;
for (auto& result: results) {
sum += result.get();
}
return sum == (size + extra) * (size + extra + 1) / 2;
}
/* this test checks if terminate() is done correctly,
i.e. all tasks shall either complete or deliver broken promises;
we do not overload the queue here, this is done in t6() */
bool t5() {
try {
constexpr unsigned int size = 2;
std::future<unsigned int> results[size];
{
mt::thread_pool tpool(size);
for (unsigned int i = 0; i < size; ++i) {
results[i] = tpool.submit([i]() -> unsigned int {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return i + 1;
});
}
tpool.terminate();
}
unsigned int sum = 0;
for (auto& result: results) {
sum += result.get();
}
return sum == size * (size + 1) / 2;
} catch (std::future_error& e) {
return e.code() == std::future_errc::broken_promise;
}
}
/* this tests checks that unprocessed tasks in the queue
deliver broken promises when terminate() is invoked;
as we overload the queue broken promises are very likely */
bool t6() {
try {
constexpr unsigned int size = 2;
constexpr unsigned int extra = 4;
mt::thread_pool tpool(size);
std::future<unsigned int> results[size + extra];
for (unsigned int i = 0; i < size + extra; ++i) {
results[i] = std::move(tpool.submit([i]() -> unsigned int {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return i + 1;
}));
}
tpool.terminate();
unsigned int sum = 0;
for (auto& result: results) {
sum += result.get();
}
return sum == (size + extra) * (size + extra + 1) / 2;
} catch (std::future_error& e) {
return e.code() == std::future_errc::broken_promise;
}
}
/* this test checks if it is ok
when join() is invoked after terminate() */
bool t7() {
try {
constexpr unsigned int size = 2;
constexpr unsigned int extra = 2;
mt::thread_pool tpool(size);
std::future<unsigned int> results[size + extra];
for (unsigned int i = 0; i < size + extra; ++i) {
results[i] = std::move(tpool.submit([i]() -> unsigned int {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return i + 1;
}));
}
tpool.terminate(); tpool.join();
unsigned int sum = 0;
for (auto& result: results) {
sum += result.get();
}
return sum == (size + extra) * (size + extra + 1) / 2;
} catch (std::future_error& e) {
return e.code() == std::future_errc::broken_promise;
}
}
/* this test checks if it is ok
when terminate() is invoked after join() */
bool t8() {
constexpr unsigned int size = 2;
constexpr unsigned int extra = 2;
mt::thread_pool tpool(size);
std::future<unsigned int> results[size + extra];
for (unsigned int i = 0; i < size + extra; ++i) {
results[i] = std::move(tpool.submit([i]() -> unsigned int {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return i + 1;
}));
}
tpool.join(); tpool.terminate();
unsigned int sum = 0;
for (auto& result: results) {
sum += result.get();
}
return sum == (size + extra) * (size + extra + 1) / 2;
}
/* this tests verifies that the destructor
waits until all tasks are done even if
they are submitted long after the destructor
has been invoked */
bool t9() {
constexpr unsigned int size = 2;
constexpr unsigned int maxlevel = 4;
std::atomic<unsigned int> count(0);
std::function<void(unsigned int)> f;
{
mt::thread_pool tpool(size);
f = [&count, &tpool, &f](unsigned int level) {
tpool.submit([&f, level, &count]() {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (level > 0) {
f(level-1);
f(level-1);
}
++count;
});
};
f(maxlevel);
}
return count.load() == (2<<maxlevel) - 1;
}
/* this test checks if tasks that are submitted
after a completed join() operation return
future objects whose promise has been broken */
bool t10() {
constexpr unsigned int size = 2;
{
mt::thread_pool tpool(size);
tpool.join();
auto f = tpool.submit([]() { return 42; });
try {
f.get();
return false;
} catch (std::future_error& e) {
return e.code() == std::future_errc::broken_promise;
}
}
}
/* test submissions with parameters */
bool t11() {
constexpr unsigned int size = 2;
constexpr unsigned int extra = 4;
constexpr unsigned int noftasks = size + extra;
auto f = [](int a, int b) { return a + b; };
std::future<int> results[noftasks];
{
mt::thread_pool tpool(size);
for (unsigned int i = 0; i < noftasks; ++i) {
results[i] = tpool.submit(f, i, 2*i);
}
}
int sum = 0;
for (auto& result: results) {
sum += result.get();
}
return sum == 3 * noftasks * (noftasks - 1) / 2;
}
/* test default constructor and size method */
bool t12() {
mt::thread_pool tpool;
unsigned int size = tpool.size();
unsigned int extra = size * 2;
std::vector<std::future<unsigned int>> results(size + extra);
for (unsigned int i = 0; i < size + extra; ++i) {
results[i] = std::move(tpool.submit([i]() -> unsigned int {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return i + 1;
}));
}
unsigned int sum = 0;
for (auto& result: results) {
sum += result.get();
}
return sum == (size + extra) * (size + extra + 1) / 2;
}
/* test concurrent invocations of the join() method */
bool t13() {
constexpr unsigned int size = 2;
constexpr unsigned int maxlevel = 4;
std::atomic<unsigned int> count(0);
std::function<void(unsigned int)> f;
mt::thread_pool tpool(size);
f = [&count, &tpool, &f](unsigned int level) {
tpool.submit([&f, level, &count]() {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (level > 0) {
f(level-1);
f(level-1);
}
++count;
});
};
f(maxlevel);
constexpr unsigned int expected = (2<<maxlevel) - 1;
constexpr unsigned int joining_threads = size * 2;
std::atomic<unsigned int> ok_count(0);
{
mt::thread_pool joining_tpool(joining_threads);
for (unsigned int i = 0; i < joining_threads; ++i) {
joining_tpool.submit([&tpool,&count,&ok_count]() {
tpool.join();
if (count.load() == expected) ++ok_count;
});
}
}
return ok_count.load() == joining_threads;
}
/* test tasks with exceptions */
bool t14() {
mt::thread_pool tpool(2);
std::future<void> results[3];
results[0] = tpool.submit([]() { throw 8; });
results[1] = tpool.submit([](int val) { throw 2 * val; }, 11);
results[2] = tpool.submit([](int val1, int val2) { throw val1 * val2; }, 3, 4);
int sum = 0;
for (auto& result: results) {
try {
result.get();
} catch (int& res) {
sum += res;
}
}
return sum == 42;
}
/* test that tasks submitted after terminate
deliver broken promises */
bool t15() {
mt::thread_pool tpool(2);
std::promise<void> p1; auto f1 = p1.get_future();
std::promise<void> p2; auto f2 = p2.get_future();
auto r1 = tpool.submit([&p1]() {
p1.set_value();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return 20;
});
auto r2 = tpool.submit([&p2]() {
p2.set_value();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return 22;
});
/* make sure that both initial tasks were started
before we kill the thread pool */
f1.get(); f2.get();
tpool.terminate();
auto r3 = tpool.submit([]() { return 42; });
bool ok;
try {
r3.get();
ok = false;
} catch (std::future_error& e) {
ok = e.code() == std::future_errc::broken_promise;
}
return ok && r1.get() + r2.get() == 42;
}
struct statistics {
statistics() : passed(0), failed(0), exceptions(0) {
}
unsigned int passed;
unsigned int failed;
unsigned int exceptions;
};
template<typename F>
void t(const std::string& name, F&& f, statistics& stats) {
std::cout << name << ": ";
try {
if (f()) {
++stats.passed;
std::cout << "ok";
} else {
++stats.failed;
std::cout << "failed";
}
} catch (std::exception& e) {
++stats.exceptions; ++stats.failed;
std::cout << "failed due to " << e.what();
}
std::cout << std::endl;
}
int main() {
statistics stats;
t(" t1", t1, stats);
t(" t2", t2, stats);
t(" t3", t3, stats);
t(" t4", t4, stats);
t(" t5", t5, stats);
t(" t6", t6, stats);
t(" t7", t7, stats);
t(" t8", t8, stats);
t(" t9", t9, stats);
t("t10", t10, stats);
t("t11", t11, stats);
t("t12", t12, stats);
t("t13", t13, stats);
t("t14", t14, stats);
t("t15", t15, stats);
unsigned int tests = stats.passed + stats.failed;
if (tests == stats.passed) {
std::cout << "all tests passed" << std::endl;
} else {
std::cout << stats.passed << " tests passed, " <<
stats.failed << " tests failed (" <<
(double) stats.failed / tests * 100.0 << "%)" <<
std::endl;
}
}