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