proxygen
MapTest.cpp
Go to the documentation of this file.
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>
19 
20 using namespace folly;
21 
23  Promise<int> p1;
24  Promise<int> p2;
25  Promise<int> p3;
26 
27  std::vector<Future<int>> fs;
28  fs.push_back(p1.getFuture());
29  fs.push_back(p2.getFuture());
30  fs.push_back(p3.getFuture());
31 
32  int c = 0;
33  std::vector<Future<Unit>> fs2 = futures::map(fs, [&](int i) { c += i; });
34 
35  // Ensure we call the callbacks as the futures complete regardless of order
36  p2.setValue(1);
37  EXPECT_EQ(1, c);
38  p3.setValue(1);
39  EXPECT_EQ(2, c);
40  p1.setValue(1);
41  EXPECT_EQ(3, c);
42 
43  EXPECT_TRUE(collect(fs2).isReady());
44 }
45 
47  Promise<int> p1;
48  Promise<int> p2;
49  Promise<int> p3;
51 
52  std::vector<Future<int>> fs;
53  fs.push_back(p1.getFuture());
54  fs.push_back(p2.getFuture());
55  fs.push_back(p3.getFuture());
56 
57  int c = 0;
58  std::vector<Future<Unit>> fs2 =
59  futures::map(exec, fs, [&](int i) { c += i; });
60 
61  // Ensure we call the callbacks as the futures complete regardless of order
62  p2.setValue(1);
63  EXPECT_EQ(1, c);
64  p3.setValue(1);
65  EXPECT_EQ(2, c);
66  p1.setValue(1);
67  EXPECT_EQ(3, c);
68 
69  EXPECT_TRUE(collect(fs2).isReady());
70 }
71 
72 TEST(Map, semifuture) {
73  Promise<int> p1;
74  Promise<int> p2;
75  Promise<int> p3;
77 
78  std::vector<SemiFuture<int>> fs;
79  fs.push_back(p1.getSemiFuture());
80  fs.push_back(p2.getSemiFuture());
81  fs.push_back(p3.getSemiFuture());
82 
83  int c = 0;
84  std::vector<Future<Unit>> fs2 =
85  futures::map(exec, fs, [&](int i) { c += i; });
86 
87  // Ensure we call the callbacks as the futures complete regardless of order
88  p2.setValue(1);
89  EXPECT_EQ(1, c);
90  p3.setValue(1);
91  EXPECT_EQ(2, c);
92  p1.setValue(1);
93  EXPECT_EQ(3, c);
94 
95  EXPECT_TRUE(collect(fs2).isReady());
96 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static void basic()
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
std::unordered_map< int64_t, VecT > Map
std::vector< Future< Result > > map(It first, It last, F func)
Definition: Future-inl.h:2358
Future< T > getFuture()
Definition: Promise-inl.h:97
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
Future< std::vector< typename std::iterator_traits< InputIterator >::value_type::value_type > > collect(InputIterator first, InputIterator last)
Definition: Future-inl.h:1536
char c
TEST(SequencedExecutor, CPUThreadPoolExecutor)