2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/futures/Future.h>
18 #include <folly/portability/GTest.h>
20 using namespace folly;
23 auto makeFutures = [](int count) {
24 std::vector<Future<int>> fs;
25 for (int i = 1; i <= count; ++i) {
26 fs.emplace_back(makeFuture(i));
33 auto fs = makeFutures(0);
35 Future<double> f1 = reduce(fs, 1.2,
36 [](double a, Try<int>&& b){
39 EXPECT_EQ(1.2, f1.get());
44 auto fs = makeFutures(1);
46 Future<double> f1 = reduce(fs, 0.0,
47 [](double a, Try<int>&& b){
50 EXPECT_EQ(1.1, f1.get());
53 // Returning values (Try)
55 auto fs = makeFutures(3);
57 Future<double> f1 = reduce(fs, 0.0,
58 [](double a, Try<int>&& b){
61 EXPECT_EQ(6.3, f1.get());
66 auto fs = makeFutures(3);
68 Future<double> f1 = reduce(fs, 0.0,
69 [](double a, int&& b){
72 EXPECT_EQ(6.3, f1.get());
75 // Returning futures (Try)
77 auto fs = makeFutures(3);
79 Future<double> f2 = reduce(fs, 0.0,
80 [](double a, Try<int>&& b){
81 return makeFuture<double>(a + *b + 0.1);
83 EXPECT_EQ(6.3, f2.get());
88 auto fs = makeFutures(3);
90 Future<double> f2 = reduce(fs, 0.0,
91 [](double a, int&& b){
92 return makeFuture<double>(a + b + 0.1);
94 EXPECT_EQ(6.3, f2.get());
99 auto makeFutures = [](int count) {
100 std::vector<Future<int>> fs;
101 for (int i = 1; i <= count; ++i) {
102 fs.emplace_back(makeFuture(i));
108 auto f = collectAll(makeFutures(3)).reduce(0, [](int a, Try<int>&& b){
111 EXPECT_EQ(6, f.get());
114 auto f = collect(makeFutures(3)).reduce(0, [](int a, int&& b){
117 EXPECT_EQ(6, f.get());
121 TEST(Reduce, unorderedReduce) {
123 std::vector<Future<int>> fs;
124 fs.push_back(makeFuture(1));
125 fs.push_back(makeFuture(2));
126 fs.push_back(makeFuture(3));
129 unorderedReduce(fs.begin(),
132 [](double /* a */, int&& b) { return double(b); });
133 EXPECT_EQ(3.0, f.get());
140 std::vector<Future<int>> fs;
141 fs.push_back(p1.getFuture());
142 fs.push_back(p2.getFuture());
143 fs.push_back(p3.getFuture());
146 unorderedReduce(fs.begin(),
149 [](double /* a */, int&& b) { return double(b); });
153 EXPECT_EQ(1.0, f.get());
157 TEST(Reduce, unorderedReduceException) {
162 std::vector<Future<int>> fs;
163 fs.push_back(p1.getFuture());
164 fs.push_back(p2.getFuture());
165 fs.push_back(p3.getFuture());
168 unorderedReduce(fs.begin(),
171 [](double /* a */, int&& b) { return b + 0.0; });
173 p2.setException(exception_wrapper(std::runtime_error("blah")));
175 EXPECT_THROW(f.get(), std::runtime_error);