19 #include <boost/thread/barrier.hpp> 26 using namespace folly;
36 std::vector<Promise<int>> promises(10);
37 std::vector<Future<int>>
futures;
39 for (
auto& p : promises) {
40 futures.push_back(p.getFuture());
45 std::shuffle(promises.begin(), promises.end(),
rng);
46 for (
auto& p : promises) {
52 auto& results = allf.value();
53 for (
auto&
t : results) {
60 std::vector<Promise<int>> promises(4);
61 std::vector<Future<int>>
futures;
63 for (
auto& p : promises) {
64 futures.push_back(p.getFuture());
69 promises[0].setValue(42);
70 promises[1].setException(
eggs);
74 promises[2].setValue(42);
78 promises[3].setException(
eggs);
83 auto& results = allf.value();
92 std::vector<Promise<Unit>> promises(10);
93 std::vector<Future<Unit>>
futures;
95 for (
auto& p : promises) {
96 futures.push_back(p.getFuture());
101 for (
auto&
f : ts.value()) {
106 std::shuffle(promises.begin(), promises.end(),
rng);
107 for (
auto& p : promises) {
117 std::vector<Promise<int>> promises(10);
118 std::vector<Future<int>>
futures;
120 for (
auto& p : promises) {
121 futures.push_back(p.getFuture());
126 std::shuffle(promises.begin(), promises.end(),
rng);
127 for (
auto& p : promises) {
133 for (
auto i : allf.value()) {
140 std::vector<Promise<int>> promises(10);
141 std::vector<Future<int>>
futures;
143 for (
auto& p : promises) {
144 futures.push_back(p.getFuture());
149 std::shuffle(promises.begin(), promises.end(),
rng);
150 for (
int i = 0;
i < 10;
i++) {
154 promises[
i].setValue(42);
158 promises[
i].setException(
eggs);
163 promises[
i].setValue(42);
167 promises[
i].setException(
eggs);
176 std::vector<Promise<Unit>> promises(10);
177 std::vector<Future<Unit>>
futures;
179 for (
auto& p : promises) {
180 futures.push_back(p.getFuture());
185 std::shuffle(promises.begin(), promises.end(),
rng);
186 for (
auto& p : promises) {
196 std::vector<Promise<Unit>> promises(10);
197 std::vector<Future<Unit>>
futures;
199 for (
auto& p : promises) {
200 futures.push_back(p.getFuture());
205 std::shuffle(promises.begin(), promises.end(),
rng);
206 for (
int i = 0;
i < 10;
i++) {
210 promises[
i].setValue();
214 promises[
i].setException(
eggs);
219 promises[
i].setValue();
223 promises[
i].setException(
eggs);
232 std::vector<Promise<std::unique_ptr<int>>> promises(10);
233 std::vector<Future<std::unique_ptr<int>>>
futures;
235 for (
auto& p : promises) {
236 futures.push_back(p.getFuture());
251 TEST(Collect, collectNotDefaultConstructible) {
252 std::vector<Promise<NotDefaultConstructible>> promises(10);
253 std::vector<Future<NotDefaultConstructible>>
futures;
254 std::vector<int> indices(10);
255 std::iota(indices.begin(), indices.end(), 0);
256 std::shuffle(indices.begin(), indices.end(),
rng);
258 for (
auto& p : promises) {
259 futures.push_back(p.getFuture());
264 for (
auto i : indices) {
271 for (
auto val : allf.value()) {
279 std::vector<Promise<int>> promises(10);
280 std::vector<Future<int>>
futures;
282 for (
auto& p : promises) {
283 futures.push_back(p.getFuture());
286 for (
auto&
f : futures) {
295 promises[7].setValue(42);
297 auto& idx_fut = anyf.value();
299 auto i = idx_fut.first;
302 auto&
f = idx_fut.second;
308 std::vector<Promise<Unit>> promises(10);
309 std::vector<Future<Unit>>
futures;
311 for (
auto& p : promises) {
312 futures.push_back(p.getFuture());
315 for (
auto&
f : futures) {
323 promises[3].setException(
eggs);
330 std::vector<Promise<int>> promises(10);
331 std::vector<Future<int>>
futures;
333 for (
auto& p : promises) {
334 futures.push_back(p.getFuture());
340 promises[3].setValue(42);
349 std::vector<Promise<int>> promises(10);
350 std::vector<Future<int>>
futures;
352 for (
auto& p : promises) {
353 futures.push_back(p.getFuture());
361 promises[7].setValue(42);
363 auto& idx_fut = onef.value();
370 std::vector<Promise<int>> promises(10);
371 std::vector<Future<int>>
futures;
373 for (
auto& p : promises) {
374 futures.push_back(p.getFuture());
381 promises[3].setException(
eggs);
383 promises[4].setException(
eggs);
385 promises[0].setValue(99);
387 auto& idx_fut = onef.value();
394 std::vector<Promise<int>> promises(10);
395 std::vector<Future<int>>
futures;
397 for (
auto& p : promises) {
398 futures.push_back(p.getFuture());
404 for (
int i = 0;
i < 9; ++
i) {
405 promises[
i].setException(
eggs);
409 promises[9].setException(
eggs);
416 std::vector<Promise<int>> promises(10);
419 std::vector<SemiFuture<int>>
futures;
421 for (
auto& p : promises) {
423 p.getSemiFuture().deferValue([](
auto v) {
return v; }));
430 promises[7].setValue(42);
437 TEST(Collect, alreadyCompleted) {
439 std::vector<Future<Unit>> fs;
440 for (
int i = 0;
i < 10;
i++) {
448 std::vector<Future<int>> fs;
449 for (
int i = 0;
i < 10;
i++) {
460 std::vector<Promise<int>> ps(10);
461 std::vector<Future<int>> fs;
462 for (
size_t i = 0;
i < ps.size();
i++) {
463 fs.emplace_back(ps[
i].getFuture());
467 std::vector<std::thread> ts;
468 boost::barrier barrier(ps.size() + 1);
469 for (
size_t i = 0;
i < ps.size();
i++) {
470 ts.emplace_back([&ps, &barrier,
i]() {
478 for (
size_t i = 0;
i < ps.size();
i++) {
483 for (
size_t i = 0;
i < ps.size();
i++) {
488 TEST(Collect, parallelWithError) {
489 std::vector<Promise<int>> ps(10);
490 std::vector<Future<int>> fs;
491 for (
size_t i = 0;
i < ps.size();
i++) {
492 fs.emplace_back(ps[
i].getFuture());
496 std::vector<std::thread> ts;
497 boost::barrier barrier(ps.size() + 1);
498 for (
size_t i = 0;
i < ps.size();
i++) {
499 ts.emplace_back([&ps, &barrier,
i]() {
501 if (
i == (ps.size() / 2)) {
502 ps[
i].setException(
eggs);
511 for (
size_t i = 0;
i < ps.size();
i++) {
520 std::vector<Promise<int>> ps(10);
521 std::vector<Future<int>> fs;
522 for (
size_t i = 0;
i < ps.size();
i++) {
523 fs.emplace_back(ps[
i].getFuture());
527 std::vector<std::thread> ts;
528 boost::barrier barrier(ps.size() + 1);
529 for (
size_t i = 0;
i < ps.size();
i++) {
530 ts.emplace_back([&ps, &barrier,
i]() {
538 for (
size_t i = 0;
i < ps.size();
i++) {
543 for (
size_t i = 0;
i < ps.size();
i++) {
549 TEST(Collect, allParallelWithError) {
550 std::vector<Promise<int>> ps(10);
551 std::vector<Future<int>> fs;
552 for (
size_t i = 0;
i < ps.size();
i++) {
553 fs.emplace_back(ps[
i].getFuture());
557 std::vector<std::thread> ts;
558 boost::barrier barrier(ps.size() + 1);
559 for (
size_t i = 0;
i < ps.size();
i++) {
560 ts.emplace_back([&ps, &barrier,
i]() {
562 if (
i == (ps.size() / 2)) {
563 ps[
i].setException(
eggs);
572 for (
size_t i = 0;
i < ps.size();
i++) {
577 for (
size_t i = 0;
i < ps.size();
i++) {
578 if (
i == (ps.size() / 2)) {
588 std::vector<Promise<Unit>> promises(10);
589 std::vector<Future<Unit>>
futures;
591 for (
auto& p : promises) {
592 futures.push_back(p.getFuture());
599 .then([&](std::vector<std::pair<
size_t,
Try<Unit>>>
v) {
607 promises[0].setValue();
609 promises[1].setValue();
611 promises[2].setValue();
615 TEST(Collect, collectNParallel) {
616 std::vector<Promise<Unit>> ps(100);
617 std::vector<Future<Unit>>
futures;
620 futures.push_back(p.getFuture());
627 .then([&](std::vector<std::pair<
size_t,
Try<Unit>>>
v) {
635 std::vector<std::thread> ts;
636 boost::barrier barrier(ps.size() + 1);
637 for (
size_t i = 0;
i < ps.size();
i++) {
638 ts.emplace_back([&ps, &barrier,
i]() {
646 for (
size_t i = 0;
i < ps.size();
i++) {
657 "Futures should not be trivially copyable");
660 "Futures should not be trivially copyable");
665 for (
int i = 0;
i < 10;
i++) {
674 for (
int i = 0;
i < 10;
i++) {
682 TEST(Collect, collectAllVariadic) {
703 TEST(Collect, collectAllVariadicReferences) {
723 TEST(Collect, collectAllVariadicWithException) {
744 TEST(Collect, collectVariadic) {
761 TEST(Collect, collectVariadicWithException) {
775 TEST(Collect, collectAllNone) {
776 std::vector<Future<int>> fs;
781 TEST(Collect, noDefaultConstructor) {
783 explicit A(
size_t ) {}
#define EXPECT_THROW(statement, expected_exception)
#define EXPECT_EQ(val1, val2)
constexpr detail::Map< Move > move
void setException(exception_wrapper ew)
SemiFuture< std::tuple< Try< typename remove_cvref_t< Fs >::value_type >... > > collectAllSemiFuture(Fs &&...fs)
—— Concurrent Priority Queue Implementation ——
SemiFuture< std::vector< std::pair< size_t, Try< typename std::iterator_traits< InputIterator >::value_type::value_type > > > > collectN(InputIterator first, InputIterator last, size_t n)
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
SemiFuture< std::pair< size_t, typename std::iterator_traits< InputIterator >::value_type::value_type > > collectAnyWithoutException(InputIterator first, InputIterator last)
void push_back(value_type &&t)
Future< std::pair< size_t, Try< typename std::iterator_traits< InputIterator >::value_type::value_type > > > collectAny(InputIterator first, InputIterator last)
FOLLY_ATTR_VISIBILITY_HIDDEN static FOLLY_ALWAYS_INLINE InlineExecutor & instance() noexcept
Future< std::tuple< Try< typename remove_cvref_t< Fs >::value_type >... > > collectAll(Fs &&...fs)
Parallel parallel(Ops ops, size_t threads=0)
std::is_trivially_copyable< T > is_trivially_copyable
#define EXPECT_TRUE(condition)
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Future< std::vector< typename std::iterator_traits< InputIterator >::value_type::value_type > > collect(InputIterator first, InputIterator last)
static eggs_t eggs("eggs")
NotDefaultConstructible(int arg)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
#define EXPECT_FALSE(condition)
uint32_t randomNumberSeed()
TEST(SequencedExecutor, CPUThreadPoolExecutor)
Future< typename std::decay< T >::type > makeFuture(T &&t)