-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathfuture_when_all_range_tests.cpp
631 lines (532 loc) · 22.3 KB
/
future_when_all_range_tests.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
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
/*
Copyright 2015 Adobe
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
/**************************************************************************************************/
#include <array>
#include <atomic>
#include <chrono> // IWYU pragma: keep
#include <cstddef>
#include <numeric>
#include <thread>
#include <utility>
#include <vector>
#include <boost/test/unit_test.hpp>
#include <stlab/concurrency/await.hpp>
#include <stlab/concurrency/default_executor.hpp>
#include <stlab/concurrency/future.hpp>
#include <stlab/concurrency/immediate_executor.hpp>
#include <stlab/concurrency/serial_queue.hpp>
#include <stlab/test/model.hpp>
#include <stlab/utility.hpp>
#include "future_test_helper.hpp"
using namespace stlab;
using namespace future_test_helper;
BOOST_FIXTURE_TEST_SUITE(future_when_all_range_void, test_fixture<void>)
BOOST_AUTO_TEST_CASE(future_when_all_void_void_empty_range) {
BOOST_TEST_MESSAGE("running future when_all void -> void with empty range");
bool check = {false};
std::vector<stlab::future<void>> emptyFutures;
sut = when_all(
make_executor<0>(), [&_check = check]() { _check = true; },
std::make_pair(emptyFutures.begin(), emptyFutures.end()));
check_valid_future(sut);
wait_until_future_completed(std::move(sut));
BOOST_REQUIRE(check);
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
}
BOOST_AUTO_TEST_CASE(future_when_all_void_empty_range) {
BOOST_TEST_MESSAGE("running future when_all void with empty range");
size_t p = 0;
std::vector<stlab::future<int>> emptyFutures;
sut = when_all(
make_executor<0>(), [&_p = p](const std::vector<int>& v) { _p = v.size(); },
std::make_pair(emptyFutures.begin(), emptyFutures.end()));
check_valid_future(sut);
wait_until_future_completed(std::move(sut));
BOOST_REQUIRE_EQUAL(size_t(0), p);
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
}
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_one_element) {
BOOST_TEST_MESSAGE("running future when_all void with range of one element");
size_t p = 0;
size_t r = 0;
std::vector<stlab::future<int>> futures;
futures.push_back(async(make_executor<0>(), [] { return 42; }));
sut = when_all(
make_executor<1>(),
[&_p = p, &_r = r](std::vector<int> v) {
_p = v.size();
_r = v[0];
},
std::make_pair(futures.begin(), futures.end()));
check_valid_future(sut);
wait_until_future_completed(std::move(sut));
BOOST_REQUIRE_EQUAL(size_t(1), p);
BOOST_REQUIRE_EQUAL(size_t(42), r);
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_all_delayed) {
using namespace std::chrono_literals;
stlab::serial_queue_t const seriel_queue_1{stlab::default_executor};
stlab::serial_queue_t const seriel_queue_2{stlab::default_executor};
stlab::serial_queue_t const seriel_queue_3{stlab::default_executor};
std::vector<stlab::future<void>> test_futures;
test_futures.emplace_back(stlab::async(seriel_queue_1.executor(), [] {
invoke_waiting([] { std::this_thread::sleep_for(0.1s); });
}));
test_futures.emplace_back(stlab::async(seriel_queue_2.executor(), [] {
invoke_waiting([] { std::this_thread::sleep_for(0.1s); });
}));
test_futures.emplace_back(stlab::async(seriel_queue_3.executor(), [] {
invoke_waiting([] { std::this_thread::sleep_for(0.1s); });
}));
bool done{false};
auto done_future = stlab::when_all(
stlab::default_executor, [&done] { done = true; },
std::make_pair(test_futures.begin(), test_futures.end()));
stlab::await(std::move(done_future));
BOOST_REQUIRE(done);
}
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_many_elements) {
BOOST_TEST_MESSAGE("running future when_all void with range with many elements");
size_t p = 0;
size_t r = 0;
std::vector<stlab::future<int>> futures;
futures.push_back(async(make_executor<0>(), [] { return 1; }));
futures.push_back(async(make_executor<0>(), [] { return 2; }));
futures.push_back(async(make_executor<0>(), [] { return 3; }));
futures.push_back(async(make_executor<0>(), [] { return 5; }));
sut = when_all(
make_executor<1>(),
[&_p = p, &_r = r](const std::vector<int>& v) {
_p = v.size();
for (auto i : v) {
_r += i;
}
},
std::make_pair(futures.begin(), futures.end()));
check_valid_future(sut);
wait_until_future_completed(std::move(sut));
BOOST_REQUIRE_EQUAL(size_t(4), p);
BOOST_REQUIRE_EQUAL(size_t(1 + 2 + 3 + 5), r);
BOOST_REQUIRE_LE(4, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_many_elements_and_immediate_continuation) {
BOOST_TEST_MESSAGE(
"running future when_all void with range with many elements and immediate continuation");
size_t p = 0;
size_t r = 0;
std::vector<stlab::future<int>> futures;
futures.reserve(1000);
for (auto i = 0; i < 1000; ++i) {
futures.push_back(async(make_executor<0>(), [] { return 1; }));
}
sut = when_all(
immediate_executor,
[&_p = p, &_r = r](const std::vector<int>& v) {
_p = v.size();
for (auto i : v) {
_r += i;
}
},
std::make_pair(futures.begin(), futures.end()));
check_valid_future(sut);
wait_until_future_completed(std::move(sut));
BOOST_REQUIRE_EQUAL(size_t(1000), p);
BOOST_REQUIRE_EQUAL(size_t(1000), r);
BOOST_REQUIRE_LE(1000, custom_scheduler<0>::usage_counter());
}
/*
/ F1 \
/ / F2 \ \
start sut
\ \ F3 / /
\ F4 /
*/
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_diamond_formation_elements) {
BOOST_TEST_MESSAGE("running future when_all void with range with diamond formation");
std::array v{0, 0, 0, 0};
int r = 0;
auto start = async(make_executor<0>(), [] { return 4711; });
std::vector<stlab::future<void>> futures(4);
futures[0] = start.then(make_executor<0>(), [&_p = v[0]](auto x) { _p = x + 1; });
futures[1] = start.then(make_executor<0>(), [&_p = v[1]](auto x) { _p = x + 2; });
futures[2] = start.then(make_executor<0>(), [&_p = v[2]](auto x) { _p = x + 3; });
futures[3] = start.then(make_executor<0>(), [&_p = v[3]](auto x) { _p = x + 5; });
sut = when_all(
make_executor<1>(),
[&_r = r, &v]() {
for (auto i : v) {
_r += i;
}
},
std::make_pair(futures.begin(), futures.end()));
check_valid_future(sut);
wait_until_future_completed(std::move(sut));
BOOST_REQUIRE_EQUAL(4711 + 1 + 4711 + 2 + 4711 + 3 + 4711 + 5, r);
BOOST_REQUIRE_LE(5, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_FIXTURE_TEST_SUITE(future_when_all_range_int, test_fixture<int>)
BOOST_AUTO_TEST_CASE(future_when_all_int_empty_range) {
BOOST_TEST_MESSAGE("running future when_all int with empty range");
std::vector<stlab::future<int>> emptyFutures;
sut = when_all(
make_executor<0>(), [](const std::vector<int>& v) { return static_cast<int>(v.size()); },
std::make_pair(emptyFutures.begin(), emptyFutures.end()));
check_valid_future(sut);
wait_until_future_completed(copy(sut));
BOOST_REQUIRE_EQUAL(0, *sut.get_try());
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
}
BOOST_AUTO_TEST_CASE(future_when_all_int_range_with_one_element) {
BOOST_TEST_MESSAGE("running future when_all int with range of one element");
size_t p = 0;
std::vector<stlab::future<int>> futures;
futures.push_back(async(make_executor<0>(), [] { return 42; }));
sut = when_all(
make_executor<1>(),
[&_p = p](std::vector<int> v) {
_p = v.size();
return v[0];
},
std::make_pair(futures.begin(), futures.end()));
check_valid_future(sut);
wait_until_future_completed(copy(sut));
BOOST_REQUIRE_EQUAL(size_t(1), p);
BOOST_REQUIRE_EQUAL(42, *sut.get_try());
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_CASE(future_when_all_int_range_with_many_elements) {
BOOST_TEST_MESSAGE("running future when_all int with range with many elements");
size_t p = 0;
std::vector<stlab::future<int>> futures;
futures.push_back(async(make_executor<0>(), [] { return 1; }));
futures.push_back(async(make_executor<0>(), [] { return 2; }));
futures.push_back(async(make_executor<0>(), [] { return 3; }));
futures.push_back(async(make_executor<0>(), [] { return 5; }));
sut = when_all(
make_executor<1>(),
[&_p = p](const std::vector<int>& v) {
_p = v.size();
auto r = 0;
for (auto i : v) {
r += i;
}
return r;
},
std::make_pair(futures.begin(), futures.end()));
check_valid_future(sut);
wait_until_future_completed(copy(sut));
BOOST_REQUIRE_EQUAL(size_t(4), p);
BOOST_REQUIRE_EQUAL(1 + 2 + 3 + 5, *sut.get_try());
BOOST_REQUIRE_LE(4, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
/*
/ F1 \
/ / F2 \ \
start sut
\ \ F3 / /
\ F4 /
*/
BOOST_AUTO_TEST_CASE(future_when_all_int_range_with_diamond_formation_elements) {
BOOST_TEST_MESSAGE("running future when_all int with range with diamond formation");
size_t p = 0;
auto start = async(make_executor<0>(), [] { return 4711; });
std::vector<stlab::future<int>> futures(4);
futures[0] = start.then(make_executor<0>(), [](auto x) { return x + 1; });
futures[1] = start.then(make_executor<0>(), [](auto x) { return x + 2; });
futures[2] = start.then(make_executor<0>(), [](auto x) { return x + 3; });
futures[3] = start.then(make_executor<0>(), [](auto x) { return x + 5; });
sut = when_all(
make_executor<1>(),
[&_p = p](const std::vector<int>& v) {
_p = v.size();
auto r = 0;
for (auto i : v) {
r += i;
}
return r;
},
std::make_pair(futures.begin(), futures.end()));
check_valid_future(sut);
wait_until_future_completed(copy(sut));
BOOST_REQUIRE_EQUAL(size_t(4), p);
BOOST_REQUIRE_EQUAL(4711 + 1 + 4711 + 2 + 4711 + 3 + 4711 + 5, *sut.get_try());
BOOST_REQUIRE_LE(4, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_FIXTURE_TEST_SUITE(future_when_all_range_move_only, test_fixture<stlab::move_only>)
BOOST_AUTO_TEST_CASE(future_when_all_move_range_with_many_elements) {
BOOST_TEST_MESSAGE("running future when_all move_only with range with many elements");
size_t p = 0;
std::vector<stlab::future<stlab::move_only>> futures;
futures.push_back(async(make_executor<0>(), [] { return stlab::move_only{1}; }));
futures.push_back(async(make_executor<0>(), [] { return stlab::move_only{2}; }));
futures.push_back(async(make_executor<0>(), [] { return stlab::move_only{3}; }));
futures.push_back(async(make_executor<0>(), [] { return stlab::move_only{5}; }));
sut = when_all(
make_executor<1>(),
[&_p = p](const std::vector<stlab::move_only>& v) {
_p = v.size();
auto r = 0;
for (const auto& i : v) {
r += i.member();
}
return stlab::move_only{r};
},
std::make_pair(futures.begin(), futures.end()));
check_valid_future(sut);
auto result = await(std::move(sut));
BOOST_REQUIRE_EQUAL(size_t(4), p);
BOOST_REQUIRE_EQUAL(1 + 2 + 3 + 5, result.member());
BOOST_REQUIRE_LE(4, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(future_when_all_range_with_mutable_task) {
BOOST_TEST_MESSAGE("future when all range with mutable task");
struct mutable_int {
int i = 0;
auto operator()() {
++i;
return i;
}
};
mutable_int func1;
mutable_int func2;
std::vector<future<mutable_int>> futures{
async(stlab::default_executor,
[func = func1]() mutable {
func();
return func;
}),
async(stlab::default_executor, [func = func2]() mutable {
func();
return func;
})};
auto sut = when_all(
stlab::default_executor,
[](auto result) {
return std::accumulate(result.begin(), result.end(), 0,
[](int sum, auto f) { return sum + f(); });
},
std::make_pair(futures.begin(), futures.end()));
BOOST_REQUIRE_EQUAL(4, stlab::await(std::move(sut)));
}
BOOST_AUTO_TEST_CASE(future_when_all_range_with_mutable_void_task) {
BOOST_TEST_MESSAGE("future when all range with mutable void task");
std::atomic_int check{0};
struct mutable_int {
std::atomic_int& _check;
int i = 0;
void operator()() {
++i;
++_check;
}
};
mutable_int func1{check};
mutable_int func2{check};
std::vector<future<void>> futures{
async(stlab::default_executor, [func = func1]() mutable { func(); }),
async(stlab::default_executor, [func = func2]() mutable { func(); })};
future<void> sut = when_all(
stlab::default_executor, [_func = mutable_int{check}]() mutable { _func(); },
std::make_pair(futures.begin(), futures.end()));
stlab::await(std::move(sut));
BOOST_REQUIRE_EQUAL(3, check);
}
// ----------------------------------------------------------------------------
// Error cases
// ----------------------------------------------------------------------------
BOOST_FIXTURE_TEST_SUITE(future_when_all_range_void_error, test_fixture<void>)
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_one_element) {
BOOST_TEST_MESSAGE("running future when_all void with range of one failing element");
size_t p = 0;
size_t r = 0;
std::vector<stlab::future<int>> futures;
futures.push_back(async(make_executor<0>(), []() -> int { throw test_exception("failure"); }));
sut = when_all(
make_executor<1>(),
[&_p = p, &_r = r](const std::vector<int>& v) {
_p = v.size();
_r = v[0];
},
std::make_pair(futures.begin(), futures.end()));
wait_until_future_fails<test_exception>(copy(sut));
check_failure<test_exception>(sut, "failure");
BOOST_REQUIRE_EQUAL(size_t(0), p);
BOOST_REQUIRE_EQUAL(size_t(0), r);
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_many_elements_one_failing) {
BOOST_TEST_MESSAGE(
"running future when_all void with range with many elements and one failing");
size_t p = 0;
size_t r = 0;
std::vector<stlab::future<int>> futures;
futures.push_back(async(make_executor<0>(), []() -> int { throw test_exception("failure"); }));
futures.push_back(async(make_executor<0>(), [] { return 2; }));
futures.push_back(async(make_executor<0>(), [] { return 3; }));
futures.push_back(async(make_executor<0>(), [] { return 5; }));
sut = when_all(
make_executor<1>(),
[&_p = p, &_r = r](const std::vector<int>& v) {
_p = v.size();
for (auto i : v) {
_r += i;
}
},
std::make_pair(futures.begin(), futures.end()));
wait_until_future_fails<test_exception>(copy(sut));
check_failure<test_exception>(sut, "failure");
BOOST_REQUIRE_EQUAL(size_t(0), p);
BOOST_REQUIRE_EQUAL(size_t(0), r);
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_many_elements_all_failing) {
BOOST_TEST_MESSAGE(
"running future when_all void with range with many elements and all failing");
size_t p = 0;
size_t r = 0;
std::vector<stlab::future<int>> futures;
futures.push_back(async(make_executor<0>(), []() -> int { throw test_exception("failure"); }));
futures.push_back(async(make_executor<0>(), []() -> int { throw test_exception("failure"); }));
futures.push_back(async(make_executor<0>(), []() -> int { throw test_exception("failure"); }));
futures.push_back(async(make_executor<0>(), []() -> int { throw test_exception("failure"); }));
sut = when_all(
make_executor<1>(),
[&_p = p, &_r = r](const std::vector<int>& v) {
_p = v.size();
for (auto i : v) {
_r += i;
}
},
std::make_pair(futures.begin(), futures.end()));
wait_until_future_fails<test_exception>(copy(sut));
check_failure<test_exception>(sut, "failure");
BOOST_REQUIRE_EQUAL(size_t(0), p);
BOOST_REQUIRE_EQUAL(size_t(0), r);
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
/*
/ F1 \
/ / F2 \ \
start sut
\ \ F3 / /
\ F4 /
*/
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_diamond_formation_elements_start_failing) {
BOOST_TEST_MESSAGE(
"running future when_all void with range with diamond formation and start failing");
std::array v{0, 0, 0, 0};
int r = 0;
auto start = async(make_executor<0>(), []() -> int { throw test_exception("failure"); });
std::vector<stlab::future<void>> futures(4);
futures[0] = start.then(make_executor<0>(), [&_p = v[0]](auto x) { _p = x + 1; });
futures[1] = start.then(make_executor<0>(), [&_p = v[1]](auto x) { _p = x + 2; });
futures[2] = start.then(make_executor<0>(), [&_p = v[2]](auto x) { _p = x + 3; });
futures[3] = start.then(make_executor<0>(), [&_p = v[3]](auto x) { _p = x + 5; });
sut = when_all(
make_executor<1>(),
[&_r = r, &v]() {
for (auto i : v) {
_r += i;
}
},
std::make_pair(futures.begin(), futures.end()));
wait_until_future_fails<test_exception>(copy(sut));
check_failure<test_exception>(sut, "failure");
BOOST_REQUIRE_EQUAL(0, r);
for (auto d : v) {
BOOST_REQUIRE_EQUAL(0, d);
}
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_CASE(
future_when_all_void_range_with_diamond_formation_elements_one_parallel_failing) {
BOOST_TEST_MESSAGE(
"running future when_all void with range with diamond formation and one of the parallel tasks is failing");
std::array v{0, 0, 0, 0};
int r = 0;
auto start = async(make_executor<0>(), []() -> int { return 42; });
std::vector<stlab::future<void>> futures(4);
futures[0] = start.then(make_executor<0>(), [&_p = v[0]](auto x) { _p = x + 1; });
futures[1] = start.then(make_executor<0>(), [](auto) { throw test_exception("failure"); });
futures[2] = start.then(make_executor<0>(), [&_p = v[2]](auto x) { _p = x + 3; });
futures[3] = start.then(make_executor<0>(), [&_p = v[3]](auto x) { _p = x + 5; });
sut = when_all(
make_executor<1>(),
[&_r = r, &v]() {
for (auto i : v) {
_r += i;
}
},
std::make_pair(futures.begin(), futures.end()));
wait_until_future_fails<test_exception>(copy(sut));
check_failure<test_exception>(sut, "failure");
BOOST_REQUIRE_EQUAL(0, r);
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_CASE(future_when_all_void_range_with_diamond_formation_elements_join_failing) {
BOOST_TEST_MESSAGE(
"running future when_all void with range with diamond formation and the joining tasks is failing");
std::array v{0, 0, 0, 0};
int const r = 0;
auto start = async(make_executor<0>(), []() -> int { return 42; });
std::vector<stlab::future<void>> futures(4);
futures[0] = start.then(make_executor<0>(), [&_p = v[0]](auto x) { _p = x + 1; });
futures[1] = start.then(make_executor<0>(), [&_p = v[1]](auto x) { _p = x + 2; });
futures[2] = start.then(make_executor<0>(), [&_p = v[2]](auto x) { _p = x + 3; });
futures[3] = start.then(make_executor<0>(), [&_p = v[3]](auto x) { _p = x + 5; });
sut = when_all(
make_executor<1>(), []() { throw test_exception("failure"); },
std::make_pair(futures.begin(), futures.end()));
wait_until_future_fails<test_exception>(copy(sut));
check_failure<test_exception>(sut, "failure");
BOOST_REQUIRE_EQUAL(0, r);
BOOST_REQUIRE_LE(1, custom_scheduler<0>::usage_counter());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}
BOOST_AUTO_TEST_CASE(
future_when_all_void_range_with_one_fails_ofmany_elements_and_immediate_continuation) {
BOOST_TEST_MESSAGE(
"running future when_all void with range with one fails one of manyelements and immediate continuation");
size_t p = 0;
size_t r = 0;
std::vector<stlab::future<int>> futures;
futures.reserve(500);
for (auto i = 0; i < 500; ++i) {
futures.push_back(async(make_executor<0>(), [] { return 1; }));
}
futures.push_back(async(make_executor<0>(), []() -> int { throw test_exception("failure"); }));
for (auto i = 0; i < 499; ++i) {
futures.push_back(async(make_executor<0>(), [] { return 1; }));
}
sut = when_all(
immediate_executor,
[&_p = p, &_r = r](const std::vector<int>& v) {
_p = v.size();
for (auto i : v) {
_r += i;
}
},
std::make_pair(futures.begin(), futures.end()));
wait_until_future_fails<test_exception>(copy(sut));
check_failure<test_exception>(sut, "failure");
BOOST_REQUIRE_EQUAL(0u, r);
}
BOOST_AUTO_TEST_SUITE_END()