20e2924f305be95ae01f83330f0691a4b3e56849
[folly.git] / folly / futures / test / ReduceTest.cpp
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <gtest/gtest.h>
18
19 #include <folly/futures/Future.h>
20
21 using namespace folly;
22
23 TEST(Reduce, basic) {
24   auto makeFutures = [](int count) {
25     std::vector<Future<int>> fs;
26     for (int i = 1; i <= count; ++i) {
27       fs.emplace_back(makeFuture(i));
28     }
29     return fs;
30   };
31
32   // Empty (Try)
33   {
34     auto fs = makeFutures(0);
35
36     Future<double> f1 = reduce(fs, 1.2,
37       [](double a, Try<int>&& b){
38         return a + *b + 0.1;
39       });
40     EXPECT_EQ(1.2, f1.get());
41   }
42
43   // One (Try)
44   {
45     auto fs = makeFutures(1);
46
47     Future<double> f1 = reduce(fs, 0.0,
48       [](double a, Try<int>&& b){
49         return a + *b + 0.1;
50       });
51     EXPECT_EQ(1.1, f1.get());
52   }
53
54   // Returning values (Try)
55   {
56     auto fs = makeFutures(3);
57
58     Future<double> f1 = reduce(fs, 0.0,
59       [](double a, Try<int>&& b){
60         return a + *b + 0.1;
61       });
62     EXPECT_EQ(6.3, f1.get());
63   }
64
65   // Returning values
66   {
67     auto fs = makeFutures(3);
68
69     Future<double> f1 = reduce(fs, 0.0,
70       [](double a, int&& b){
71         return a + b + 0.1;
72       });
73     EXPECT_EQ(6.3, f1.get());
74   }
75
76   // Returning futures (Try)
77   {
78     auto fs = makeFutures(3);
79
80     Future<double> f2 = reduce(fs, 0.0,
81       [](double a, Try<int>&& b){
82         return makeFuture<double>(a + *b + 0.1);
83       });
84     EXPECT_EQ(6.3, f2.get());
85   }
86
87   // Returning futures
88   {
89     auto fs = makeFutures(3);
90
91     Future<double> f2 = reduce(fs, 0.0,
92       [](double a, int&& b){
93         return makeFuture<double>(a + b + 0.1);
94       });
95     EXPECT_EQ(6.3, f2.get());
96   }
97 }
98
99 TEST(Reduce, chain) {
100   auto makeFutures = [](int count) {
101     std::vector<Future<int>> fs;
102     for (int i = 1; i <= count; ++i) {
103       fs.emplace_back(makeFuture(i));
104     }
105     return fs;
106   };
107
108   {
109     auto f = collectAll(makeFutures(3)).reduce(0, [](int a, Try<int>&& b){
110       return a + *b;
111     });
112     EXPECT_EQ(6, f.get());
113   }
114   {
115     auto f = collect(makeFutures(3)).reduce(0, [](int a, int&& b){
116       return a + b;
117     });
118     EXPECT_EQ(6, f.get());
119   }
120 }
121
122 TEST(Reduce, unorderedReduce) {
123   {
124     std::vector<Future<int>> fs;
125     fs.push_back(makeFuture(1));
126     fs.push_back(makeFuture(2));
127     fs.push_back(makeFuture(3));
128
129     Future<double> f = unorderedReduce(fs.begin(), fs.end(), 0.0,
130       [](double a, int&& b){
131         return double(b);
132       });
133     EXPECT_EQ(3.0, f.get());
134   }
135   {
136     Promise<int> p1;
137     Promise<int> p2;
138     Promise<int> p3;
139
140     std::vector<Future<int>> fs;
141     fs.push_back(p1.getFuture());
142     fs.push_back(p2.getFuture());
143     fs.push_back(p3.getFuture());
144
145     Future<double> f = unorderedReduce(fs.begin(), fs.end(), 0.0,
146       [](double a, int&& b){
147         return double(b);
148       });
149     p3.setValue(3);
150     p2.setValue(2);
151     p1.setValue(1);
152     EXPECT_EQ(1.0, f.get());
153   }
154 }
155
156 TEST(Reduce, unorderedReduceException) {
157   Promise<int> p1;
158   Promise<int> p2;
159   Promise<int> p3;
160
161   std::vector<Future<int>> fs;
162   fs.push_back(p1.getFuture());
163   fs.push_back(p2.getFuture());
164   fs.push_back(p3.getFuture());
165
166   Future<double> f = unorderedReduce(fs.begin(), fs.end(), 0.0,
167     [](double a, int&& b){
168       return b + 0.0;
169     });
170   p3.setValue(3);
171   p2.setException(exception_wrapper(std::runtime_error("blah")));
172   p1.setValue(1);
173   EXPECT_THROW(f.get(), std::runtime_error);
174 }