-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy paththread_pool_tests.cpp
More file actions
1496 lines (1241 loc) · 42.9 KB
/
Copy paththread_pool_tests.cpp
File metadata and controls
1496 lines (1241 loc) · 42.9 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cassert>
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <functional>
#include <mutex>
#include <numeric>
#include <stdexcept>
#include <string>
#include <vector>
#include "universal_thread_pool/thread_pool.hpp"
using namespace universal_thread_pool;
using namespace std::chrono_literals;
namespace {
thread_pool_options base_options(std::size_t threads = 4) {
thread_pool_options options;
options.initial_threads = threads;
options.max_threads = std::max<std::size_t>(threads, 8);
return options;
}
void test_submit_and_future() {
assert(version_major >= 0);
assert(std::string(version_string).size() > 0);
thread_pool pool(base_options(2));
auto a = pool.submit([] { return 20; });
auto b = pool.submit([](int x) { return x + 2; }, 20);
assert(a.get() == 20);
assert(b.get() == 22);
assert(pool.wait_for_idle(2s));
const auto metrics = pool.metrics();
assert(metrics.submitted_tasks_total == 2);
assert(metrics.completed_tasks_total == 2);
assert(metrics.failed_tasks_total == 0);
}
void test_dispatch_inline_counts_submission() {
thread_pool pool(base_options(1));
auto dispatched = pool.submit([&pool] {
bool ran_inline = false;
const bool ok = pool.dispatch([&ran_inline] {
ran_inline = true;
});
assert(ran_inline);
return ok;
});
assert(dispatched.get());
assert(pool.wait_for_idle(2s));
const auto metrics = pool.metrics();
assert(metrics.submitted_tasks_total == 2);
assert(metrics.completed_tasks_total == 2);
assert(metrics.failed_tasks_total == 0);
}
void test_detach_and_wait_for_idle() {
thread_pool pool(base_options(4));
std::atomic<int> counter{0};
for (int i = 0; i < 1000; ++i) {
assert(pool.detach([&counter] {
counter.fetch_add(1, std::memory_order_relaxed);
}));
}
assert(pool.wait_for_idle(5s));
assert(counter.load(std::memory_order_relaxed) == 1000);
}
void test_exception_propagates_from_submit() {
thread_pool pool(base_options(2));
auto future = pool.submit([]() -> int {
throw std::runtime_error("boom");
});
bool threw = false;
try {
(void)future.get();
} catch (const std::runtime_error&) {
threw = true;
}
assert(threw);
assert(pool.wait_for_idle(2s));
const auto metrics = pool.metrics();
assert(metrics.completed_tasks_total == 1);
assert(metrics.failed_tasks_total == 0);
}
void test_detached_exception_is_counted() {
thread_pool pool(base_options(2));
assert(pool.detach([] {
throw std::runtime_error("detached boom");
}));
assert(pool.wait_for_idle(2s));
const auto metrics = pool.metrics();
assert(metrics.failed_tasks_total == 1);
}
void test_pause_and_resume() {
thread_pool_options options = base_options(2);
options.queue = queue_policy::bounded_reject;
options.max_queue_size = 8;
thread_pool pool(options);
pool.pause();
std::atomic<int> counter{0};
assert(pool.detach([&counter] {
counter.fetch_add(1, std::memory_order_relaxed);
}));
std::this_thread::sleep_for(100ms);
assert(counter.load(std::memory_order_relaxed) == 0);
pool.resume();
assert(pool.wait_for_idle(2s));
assert(counter.load(std::memory_order_relaxed) == 1);
}
void test_bounded_reject() {
thread_pool_options options = base_options(1);
options.queue = queue_policy::bounded_reject;
options.max_queue_size = 1;
thread_pool pool(options);
pool.pause();
assert(pool.detach([] {}));
assert(!pool.detach([] {}));
pool.resume();
assert(pool.wait_for_idle(2s));
}
void test_try_submit_and_try_detach() {
thread_pool_options options = base_options(1);
options.queue = queue_policy::bounded_block;
options.max_queue_size = 1;
thread_pool pool(options);
pool.pause();
auto accepted = pool.try_submit([] {
return 1;
});
assert(accepted);
auto rejected = pool.try_submit([] {
return 2;
});
assert(!rejected);
auto timed_rejected = pool.try_submit_for(1ms, [] {
return 5;
});
assert(!timed_rejected);
task_options named;
named.name = "try-submit-with-options";
auto rejected_with_options = pool.try_submit_with_options(named, [] {
return 3;
});
assert(!rejected_with_options);
auto timed_options = pool.try_submit_with_options_for(named, 1ms, [] {
return 6;
});
assert(!timed_options);
assert(!pool.try_detach([] {}));
assert(!pool.try_detach_for(1ms, [] {}));
pool.resume();
assert(accepted->get() == 1);
assert(pool.wait_for_idle(2s));
auto executor_future = pool.get_executor().try_submit_for(1s, [] {
return 4;
});
assert(executor_future);
assert(executor_future->get() == 4);
std::atomic<int> detached{0};
assert(pool.get_executor().try_detach_for(1s, [&detached] {
detached.fetch_add(1, std::memory_order_relaxed);
}));
assert(pool.wait_for_idle(2s));
assert(detached.load(std::memory_order_relaxed) == 1);
}
void test_executor_task_options() {
thread_pool_options options = base_options(2);
std::mutex names_mutex;
std::vector<std::string> names;
std::vector<task_priority> priorities;
options.on_task_start = [&](const task_metadata& metadata) {
if (!metadata.name.empty()) {
std::lock_guard<std::mutex> lock(names_mutex);
names.push_back(metadata.name);
priorities.push_back(metadata.priority);
}
};
thread_pool pool(options);
auto ex = pool.get_executor(task_priority::high);
task_options submit_options;
submit_options.name = "executor-submit";
submit_options.source = UNIVERSAL_THREAD_POOL_SOURCE_LOCATION;
auto value = ex.submit_with_options(submit_options, [] {
return 9;
});
task_options detach_options;
detach_options.name = "executor-detach";
detach_options.priority = task_priority::critical;
detach_options.source = UNIVERSAL_THREAD_POOL_SOURCE_LOCATION;
assert(ex.detach_with_options(detach_options, [] {}));
assert(value.get() == 9);
assert(pool.wait_for_idle(2s));
std::lock_guard<std::mutex> lock(names_mutex);
assert(std::find(names.begin(), names.end(), "executor-submit") != names.end());
assert(std::find(names.begin(), names.end(), "executor-detach") != names.end());
assert(std::find(priorities.begin(), priorities.end(), task_priority::high) != priorities.end());
assert(std::find(priorities.begin(), priorities.end(), task_priority::critical) != priorities.end());
}
void test_bulk_options_and_executor_bulk() {
thread_pool_options options = base_options(2);
std::mutex names_mutex;
std::vector<std::string> names;
std::vector<task_priority> priorities;
options.on_task_start = [&](const task_metadata& metadata) {
if (!metadata.name.empty()) {
std::lock_guard<std::mutex> lock(names_mutex);
names.push_back(metadata.name);
priorities.push_back(metadata.priority);
}
};
thread_pool pool(options);
task_options bulk_options;
bulk_options.name = "bulk-options";
bulk_options.priority = task_priority::high;
auto futures = pool.bulk_submit_with_options(bulk_options, 4, [](std::size_t index) {
return static_cast<int>(index * 3);
});
auto ex = pool.get_executor(task_priority::low);
task_options executor_bulk_options;
executor_bulk_options.name = "executor-bulk-options";
auto executor_futures = ex.bulk_submit_with_options(executor_bulk_options, 2, [](std::size_t index) {
return static_cast<int>(index + 10);
});
std::atomic<int> detached{0};
task_options detach_options;
detach_options.name = "bulk-detach-options";
detach_options.priority = task_priority::critical;
assert(pool.bulk_detach_with_options(detach_options, 3, [&detached](std::size_t) {
detached.fetch_add(1, std::memory_order_relaxed);
}) == 3);
task_options executor_detach_options;
executor_detach_options.name = "executor-bulk-detach-options";
assert(ex.bulk_detach_with_options(executor_detach_options, 2, [&detached](std::size_t) {
detached.fetch_add(1, std::memory_order_relaxed);
}) == 2);
assert(futures.back().get() == 9);
assert(executor_futures.back().get() == 11);
assert(pool.wait_for_idle(2s));
assert(detached.load(std::memory_order_relaxed) == 5);
std::lock_guard<std::mutex> lock(names_mutex);
assert(std::find(names.begin(), names.end(), "bulk-options") != names.end());
assert(std::find(names.begin(), names.end(), "executor-bulk-options") != names.end());
assert(std::find(names.begin(), names.end(), "bulk-detach-options") != names.end());
assert(std::find(priorities.begin(), priorities.end(), task_priority::high) != priorities.end());
assert(std::find(priorities.begin(), priorities.end(), task_priority::low) != priorities.end());
assert(std::find(priorities.begin(), priorities.end(), task_priority::critical) != priorities.end());
}
void test_caller_runs() {
thread_pool_options options = base_options(1);
options.queue = queue_policy::bounded_caller_runs;
options.max_queue_size = 1;
thread_pool pool(options);
pool.pause();
std::atomic<int> counter{0};
assert(pool.detach([&counter] {
counter.fetch_add(1, std::memory_order_relaxed);
}));
assert(pool.detach([&counter] {
counter.fetch_add(1, std::memory_order_relaxed);
}));
assert(counter.load(std::memory_order_relaxed) == 1);
pool.resume();
assert(pool.wait_for_idle(2s));
assert(counter.load(std::memory_order_relaxed) == 2);
const auto metrics = pool.metrics();
assert(metrics.caller_runs_total == 1);
}
void test_priority_order() {
thread_pool_options options = base_options(1);
options.scheduler = schedule_policy::priority;
options.enable_priority = true;
options.max_queue_size = 8;
thread_pool pool(options);
pool.pause();
std::vector<int> order;
std::mutex order_mutex;
assert(pool.detach_with_priority(task_priority::low, [&] {
std::lock_guard<std::mutex> lock(order_mutex);
order.push_back(1);
}));
assert(pool.detach_with_priority(task_priority::critical, [&] {
std::lock_guard<std::mutex> lock(order_mutex);
order.push_back(2);
}));
assert(pool.detach_with_priority(task_priority::normal, [&] {
std::lock_guard<std::mutex> lock(order_mutex);
order.push_back(3);
}));
pool.resume();
assert(pool.wait_for_idle(2s));
assert(order.size() == 3);
assert(order[0] == 2);
assert(order[1] == 3);
assert(order[2] == 1);
}
void test_priority_fairness_interval() {
thread_pool_options options = base_options(1);
options.scheduler = schedule_policy::priority;
options.priority_fairness_interval = 1;
options.max_queue_size = 8;
thread_pool pool(options);
pool.pause();
std::vector<int> order;
std::mutex order_mutex;
assert(pool.detach_with_priority(task_priority::low, [&] {
std::lock_guard<std::mutex> lock(order_mutex);
order.push_back(1);
}));
assert(pool.detach_with_priority(task_priority::critical, [&] {
std::lock_guard<std::mutex> lock(order_mutex);
order.push_back(2);
}));
assert(pool.detach_with_priority(task_priority::critical, [&] {
std::lock_guard<std::mutex> lock(order_mutex);
order.push_back(3);
}));
pool.resume();
assert(pool.wait_for_idle(2s));
assert(order.size() == 3);
assert(order[0] == 2);
assert(order[1] == 1);
assert(order[2] == 3);
assert(pool.metrics().priority_fairness_picks_total >= 1);
}
void test_resize_grow_and_shrink() {
thread_pool pool(base_options(1));
assert(pool.thread_count() == 1);
pool.resize(3);
for (int i = 0; i < 50 && pool.thread_count() < 3; ++i) {
std::this_thread::sleep_for(10ms);
}
assert(pool.thread_count() == 3);
pool.resize(1);
for (int i = 0; i < 100 && pool.thread_count() > 1; ++i) {
std::this_thread::sleep_for(10ms);
}
assert(pool.thread_count() == 1);
}
void test_resize_shrink_is_idempotent() {
thread_pool_options options = base_options(6);
options.initial_threads = 6;
options.min_threads = 1;
options.max_threads = 6;
options.worker_batch_size = 1;
thread_pool pool(options);
assert(pool.thread_count() == 6);
for (int i = 0; i < 8; ++i) {
pool.resize(2);
}
for (int i = 0; i < 100 && pool.thread_count() > 2; ++i) {
std::this_thread::sleep_for(10ms);
}
assert(pool.thread_count() == 2);
for (int i = 0; i < 8; ++i) {
pool.resize(2);
}
std::this_thread::sleep_for(50ms);
assert(pool.thread_count() == 2);
}
void test_resize_grow_cancels_pending_retirements() {
thread_pool_options options = base_options(6);
options.initial_threads = 6;
options.min_threads = 1;
options.max_threads = 6;
options.worker_batch_size = 1;
thread_pool pool(options);
std::atomic<bool> release{false};
std::atomic<int> started{0};
for (int i = 0; i < 6; ++i) {
assert(pool.detach([&] {
started.fetch_add(1, std::memory_order_relaxed);
while (!release.load(std::memory_order_acquire)) {
std::this_thread::sleep_for(1ms);
}
}));
}
for (int i = 0; i < 100 && started.load(std::memory_order_relaxed) < 6; ++i) {
std::this_thread::sleep_for(10ms);
}
assert(started.load(std::memory_order_relaxed) == 6);
pool.resize(2);
pool.resize(6);
release.store(true, std::memory_order_release);
assert(pool.wait_for_idle(2s));
for (int i = 0; i < 100 && pool.thread_count() < 6; ++i) {
std::this_thread::sleep_for(10ms);
}
assert(pool.thread_count() == 6);
}
void test_parallel_for() {
thread_pool pool(base_options(4));
std::vector<int> values(1000, 0);
loop_options options;
options.block_size = 17;
pool.parallel_for<std::size_t>(0, values.size(), [&](std::size_t i) {
values[i] = static_cast<int>(i);
}, options);
assert(values[0] == 0);
assert(values[999] == 999);
assert(std::accumulate(values.begin(), values.end(), 0) == 499500);
std::vector<int> shifted(10, 0);
pool.parallel_for<int>(5, 15, [&](int i) {
shifted[static_cast<std::size_t>(i - 5)] = i;
}, options);
assert(shifted.front() == 5);
assert(shifted.back() == 14);
}
void test_parallel_for_each_and_transform() {
thread_pool pool(base_options(4));
std::vector<int> values(100, 1);
loop_options options;
options.block_size = 5;
pool.parallel_for_each(values.begin(), values.end(), [](int& value) {
value += 1;
}, options);
std::vector<int> transformed(values.size(), 0);
pool.parallel_transform(values.begin(), values.end(), transformed.begin(), [](int value) {
return value * 3;
}, options);
assert(values.front() == 2);
assert(values.back() == 2);
assert(transformed.front() == 6);
assert(transformed.back() == 6);
}
void test_parallel_schedules_and_reduce() {
thread_pool pool(base_options(4));
std::vector<int> dynamic_values(1000, 0);
std::vector<int> guided_values(1000, 0);
loop_options dynamic;
dynamic.schedule = loop_schedule::dynamic_blocks;
dynamic.block_size = 13;
pool.parallel_for<std::size_t>(0, dynamic_values.size(), [&](std::size_t i) {
dynamic_values[i] = static_cast<int>(i);
}, dynamic);
loop_options guided;
guided.schedule = loop_schedule::guided_blocks;
guided.block_size = 7;
pool.parallel_for<std::size_t>(0, guided_values.size(), [&](std::size_t i) {
guided_values[i] = static_cast<int>(i);
}, guided);
assert(std::accumulate(dynamic_values.begin(), dynamic_values.end(), 0) == 499500);
assert(std::accumulate(guided_values.begin(), guided_values.end(), 0) == 499500);
const auto reduced = pool.parallel_reduce<int>(
0,
1000,
0,
[](int a, int b) {
return a + b;
},
guided);
assert(reduced == 499500);
}
void test_parallel_3d_and_nd() {
thread_pool pool(base_options(4));
std::vector<int> grid(4 * 3 * 2, 0);
pool.parallel_for_3d<int>(0, 4, 0, 3, 0, 2, [&](int x, int y, int z) {
grid[static_cast<std::size_t>((x * 3 + y) * 2 + z)] = x + y + z;
});
assert(grid.front() == 0);
assert(grid.back() == 3 + 2 + 1);
std::atomic<int> count{0};
pool.parallel_for_nd<int, 4>(
std::array<int, 4>{0, 0, 0, 0},
std::array<int, 4>{2, 2, 2, 2},
[&](int, int, int, int) {
count.fetch_add(1, std::memory_order_relaxed);
});
assert(count.load(std::memory_order_relaxed) == 16);
}
void test_cached_pool_growth_and_worker_context() {
auto options = make_cached_pool_options(0, 4);
options.queue_expand_threshold = 1;
options.idle_timeout = 50ms;
thread_pool pool(options);
assert(pool.thread_count() == 0);
std::vector<std::future<bool>> futures;
for (int i = 0; i < 4; ++i) {
futures.push_back(pool.submit([&pool] {
std::this_thread::sleep_for(20ms);
return pool.is_worker_thread() &&
thread_pool::current_worker_id() != static_cast<std::size_t>(-1);
}));
}
for (auto& future : futures) {
assert(future.get());
}
assert(pool.wait_for_idle(2s));
for (int i = 0; i < 50 && pool.thread_count() != 0; ++i) {
std::this_thread::sleep_for(10ms);
}
assert(pool.thread_count() == 0);
const auto metrics = pool.metrics();
assert(metrics.total_threads == 0);
assert(metrics.idle_threads == 0);
}
void test_task_group() {
thread_pool pool(base_options(4));
task_group group(pool.get_executor());
std::atomic<int> counter{0};
auto grouped_value = group.submit([] {
return 42;
});
auto maybe_grouped = group.try_submit_for(1s, [] {
return 7;
});
assert(maybe_grouped);
auto token_value = group.submit_with_token(
[](cancellation_token token, int value) {
return token.stop_requested() ? -1 : value + 1;
},
8);
auto maybe_token_value = group.try_submit_with_token_for(
1s,
[](cancellation_token token) {
return token.stop_requested() ? -1 : 10;
});
assert(maybe_token_value);
for (int i = 0; i < 100; ++i) {
group.run([&counter] {
counter.fetch_add(1, std::memory_order_relaxed);
});
}
group.run_with_token([&counter](cancellation_token token) {
if (!token.stop_requested()) {
counter.fetch_add(1, std::memory_order_relaxed);
}
});
group.wait();
assert(group.pending() == 0);
assert(counter.load(std::memory_order_relaxed) == 101);
assert(grouped_value.get() == 42);
assert(maybe_grouped->get() == 7);
assert(token_value.get() == 9);
assert(maybe_token_value->get() == 10);
task_group cleanup_group(pool.get_executor());
cleanup_group.run([] {});
assert(pool.wait_for_idle(2s));
assert(cleanup_group.clear_ready() == 1);
assert(cleanup_group.pending() == 0);
cleanup_group.cancel();
assert(cleanup_group.stop_requested());
assert(cleanup_group.token().stop_requested());
}
void test_scoped_task_group() {
thread_pool pool(base_options(2));
std::atomic<int> counter{0};
{
scoped_task_group scope(pool.get_executor());
scope.run([&counter] {
counter.fetch_add(1, std::memory_order_relaxed);
});
auto value = scope.submit_with_token([](cancellation_token token) {
return token.stop_requested() ? -1 : 11;
});
scope.wait();
assert(value.get() == 11);
}
assert(counter.load(std::memory_order_relaxed) == 1);
{
scoped_task_group scope(
pool.get_executor(),
scoped_task_group_policy::cancel_and_wait);
scope.cancel();
auto cancelled = scope.submit_with_token([](cancellation_token token) {
return token.stop_requested() ? -1 : 12;
});
bool wait_threw = false;
try {
scope.wait();
} catch (const task_cancelled&) {
wait_threw = true;
}
assert(wait_threw);
bool threw = false;
try {
(void)cancelled.get();
} catch (const task_cancelled&) {
threw = true;
}
assert(threw);
}
}
void test_submit_after() {
thread_pool pool(base_options(2));
const auto start = std::chrono::steady_clock::now();
auto future = pool.submit_after(80ms, [] {
return 77;
});
assert(future.get() == 77);
const auto elapsed = std::chrono::steady_clock::now() - start;
assert(elapsed >= 60ms);
const auto metrics = pool.metrics();
assert(metrics.scheduled_tasks_total == 1);
}
void test_schedule_submit_after_and_cancel() {
thread_pool pool(base_options(2));
auto scheduled = pool.schedule_submit_after(40ms, [] {
return 78;
});
assert(scheduled);
assert(scheduled.future.get() == 78);
auto cancelled = pool.schedule_submit_after(40ms, [] {
return 79;
});
assert(cancelled.cancel());
assert(cancelled.cancelled());
bool threw = false;
try {
(void)cancelled.future.get();
} catch (const task_cancelled&) {
threw = true;
}
assert(threw);
task_options options;
options.name = "scheduled-options";
options.priority = task_priority::high;
auto with_options = pool.schedule_submit_after_with_options(options, 1ms, [] {
return 80;
});
assert(with_options.future.get() == 80);
}
void test_detach_after_and_cancel() {
thread_pool pool(base_options(2));
std::atomic<int> counter{0};
auto handle = pool.detach_after(40ms, [&counter] {
counter.fetch_add(1, std::memory_order_relaxed);
});
assert(handle);
std::this_thread::sleep_for(120ms);
assert(pool.wait_for_idle(2s));
assert(counter.load(std::memory_order_relaxed) == 1);
auto cancelled = pool.detach_after(40ms, [&counter] {
counter.fetch_add(100, std::memory_order_relaxed);
});
assert(cancelled.cancel());
assert(cancelled.cancelled());
std::this_thread::sleep_for(120ms);
assert(pool.wait_for_idle(2s));
assert(counter.load(std::memory_order_relaxed) == 1);
const auto metrics = pool.metrics();
assert(metrics.scheduled_tasks_total == 2);
assert(metrics.scheduled_tasks_cancelled_total >= 1);
}
void test_cancellable_tasks() {
thread_pool pool(base_options(2));
pool.pause();
cancellation_source source;
std::atomic<int> counter{0};
auto future = pool.submit_cancellable(source.token(), [] {
return 42;
});
assert(pool.detach_cancellable(source.token(), [&counter] {
counter.fetch_add(1, std::memory_order_relaxed);
}));
source.request_stop();
pool.resume();
bool threw = false;
try {
(void)future.get();
} catch (const task_cancelled&) {
threw = true;
}
assert(threw);
assert(pool.wait_for_idle(2s));
assert(counter.load(std::memory_order_relaxed) == 0);
const auto metrics = pool.metrics();
assert(metrics.cancelled_tasks_total >= 2);
}
void test_cancel_pending_sets_task_cancelled() {
thread_pool_options options = base_options(1);
options.queue = queue_policy::bounded_reject;
options.max_queue_size = 4;
std::atomic<int> cancelled_hook_count{0};
options.on_task_cancel = [&cancelled_hook_count](const task_metadata&) {
cancelled_hook_count.fetch_add(1, std::memory_order_relaxed);
};
thread_pool pool(options);
pool.pause();
auto future = pool.submit([] {
return 42;
});
pool.shutdown(shutdown_policy::cancel_pending);
bool threw = false;
try {
(void)future.get();
} catch (const task_cancelled&) {
threw = true;
}
assert(threw);
assert(cancelled_hook_count.load(std::memory_order_relaxed) >= 1);
const auto metrics = pool.metrics();
assert(metrics.cancelled_tasks_total >= 1);
}
void test_drop_oldest_sets_task_cancelled() {
thread_pool_options options = base_options(1);
options.queue = queue_policy::bounded_drop_oldest;
options.max_queue_size = 1;
thread_pool pool(options);
pool.pause();
auto dropped = pool.submit([] {
return 1;
});
auto kept = pool.submit([] {
return 2;
});
bool threw = false;
try {
(void)dropped.get();
} catch (const task_cancelled&) {
threw = true;
}
assert(threw);
pool.resume();
assert(kept.get() == 2);
assert(pool.wait_for_idle(2s));
}
void test_deadline_tasks() {
thread_pool pool(base_options(2));
pool.pause();
std::atomic<int> counter{0};
auto future = pool.submit_with_timeout(10ms, [] {
return 42;
});
assert(pool.detach_with_timeout(10ms, [&counter] {
counter.fetch_add(1, std::memory_order_relaxed);
}));
std::this_thread::sleep_for(40ms);
pool.resume();
bool threw = false;
try {
(void)future.get();
} catch (const task_cancelled&) {
threw = true;
}
assert(threw);
assert(pool.wait_for_idle(2s));
assert(counter.load(std::memory_order_relaxed) == 0);
const auto metrics = pool.metrics();
assert(metrics.cancelled_tasks_total >= 2);
}
void test_submit_then() {
thread_pool pool(base_options(2));
auto first = pool.submit([] {
return 41;
});
auto second = pool.submit_then(std::move(first), [](int value) {
return value + 1;
});
assert(second.get() == 42);
auto void_first = pool.submit([] {});
auto after_void = pool.get_executor().submit_then(std::move(void_first), [] {
return 9;
});
assert(after_void.get() == 9);
}
void test_submit_then_does_not_occupy_worker_while_waiting() {
thread_pool pool(base_options(1));
std::promise<int> predecessor;
auto continued = pool.submit_then(predecessor.get_future(), [](int value) {
return value + 1;
});
assert(pool.detach([predecessor = std::move(predecessor)]() mutable {
predecessor.set_value(41);
}));
assert(continued.get() == 42);
assert(pool.wait_for_idle(2s));
}
void test_continue_with_and_dataflow() {
thread_pool pool(base_options(2));
auto first = pool.submit([] {
return 21;
});
auto continued = pool.continue_with(std::move(first), [](int value) {
return value * 2;
});
assert(continued.get() == 42);
auto void_first = pool.submit([] {});
auto after_void = pool.get_executor().continue_with(std::move(void_first), [] {
return 7;
});
assert(after_void.get() == 7);
std::vector<std::future<int>> inputs;
for (int i = 1; i <= 4; ++i) {
inputs.push_back(pool.submit([i] {
return i;
}));
}
auto summed = pool.dataflow(std::move(inputs), [](std::vector<int> values) {
return std::accumulate(values.begin(), values.end(), 0);
});
assert(summed.get() == 10);
}
void test_continuation_scheduler_and_numa_hooks() {
thread_pool_options options = base_options(4);
options.continuation_threads = 2;
options.numa_nodes = 2;
std::mutex mutex;
std::vector<std::size_t> affinity_nodes;
std::vector<std::size_t> task_nodes;
options.on_thread_affinity = [&](std::size_t, std::size_t numa_node) {
std::lock_guard<std::mutex> lock(mutex);
affinity_nodes.push_back(numa_node);
};
options.on_task_start = [&](const task_metadata& metadata) {
if (metadata.name == "numa-preferred") {
std::lock_guard<std::mutex> lock(mutex);
task_nodes.push_back(metadata.numa_node);
}
};
thread_pool pool(options);
auto base = pool.submit([] {
return 20;
});
auto continued = pool.continue_with(std::move(base), [](int value) {
return value + 2;
});
assert(continued.get() == 22);