proxygen
SharedPromiseTest.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 
19 
20 using namespace folly;
21 
22 TEST(SharedPromise, setGetSemiFuture) {
24  p.setValue(1);
25  auto f1 = p.getSemiFuture();
26  auto f2 = p.getSemiFuture();
27  EXPECT_EQ(1, f1.value());
28  EXPECT_EQ(1, f2.value());
29 }
30 
31 TEST(SharedPromise, setGetMixed) {
33  p.setValue(1);
34  auto f1 = p.getSemiFuture();
35  auto f2 = p.getFuture();
36  EXPECT_EQ(1, f1.value());
37  EXPECT_EQ(1, f2.value());
38 }
39 
40 TEST(SharedPromise, setGet) {
42  p.setValue(1);
43  auto f1 = p.getFuture();
44  auto f2 = p.getFuture();
45  EXPECT_EQ(1, f1.value());
46  EXPECT_EQ(1, f2.value());
47 }
48 TEST(SharedPromise, getSet) {
50  auto f1 = p.getFuture();
51  auto f2 = p.getFuture();
52  p.setValue(1);
53  EXPECT_EQ(1, f1.value());
54  EXPECT_EQ(1, f2.value());
55 }
56 
57 TEST(SharedPromise, getSetGet) {
59  auto f1 = p.getFuture();
60  p.setValue(1);
61  auto f2 = p.getFuture();
62  EXPECT_EQ(1, f1.value());
63  EXPECT_EQ(1, f2.value());
64 }
65 
68 
69  auto f1 = p.getFuture();
70  p.setValue(1);
71  EXPECT_EQ(1, f1.value());
72 
73  p = SharedPromise<int>();
74  auto f2 = p.getFuture();
75  EXPECT_FALSE(f2.isReady());
76  p.setValue(2);
77  EXPECT_EQ(2, f2.value());
78 }
79 
80 TEST(SharedPromise, getMoveSet) {
82  auto f = p.getFuture();
83  auto p2 = std::move(p);
84  p2.setValue(1);
85  EXPECT_EQ(1, f.value());
86 }
87 
88 TEST(SharedPromise, setMoveGet) {
90  p.setValue(1);
91  auto p2 = std::move(p);
92  auto f = p2.getFuture();
93  EXPECT_EQ(1, f.value());
94 }
95 
96 TEST(SharedPromise, moveSetGet) {
98  auto p2 = std::move(p);
99  p2.setValue(1);
100  auto f = p2.getFuture();
101  EXPECT_EQ(1, f.value());
102 }
103 
104 TEST(SharedPromise, moveGetSet) {
106  auto p2 = std::move(p);
107  auto f = p2.getFuture();
108  p2.setValue(1);
109  EXPECT_EQ(1, f.value());
110 }
111 
112 TEST(SharedPromise, moveMove) {
114  auto f1 = p.getFuture();
115  auto f2 = p.getFuture();
116  auto p2 = std::move(p);
117  p = std::move(p2);
118  p.setValue(std::make_shared<int>(1));
119 }
120 
121 TEST(SharedPromise, setWith) {
123  p.setWith([] { return 1; });
124  EXPECT_EQ(1, p.getFuture().value());
125 }
126 
127 TEST(SharedPromise, isFulfilled) {
130  auto p2 = std::move(p);
131  EXPECT_FALSE(p2.isFulfilled());
132  p2.setValue(1);
133  EXPECT_TRUE(p2.isFulfilled());
134  p = std::move(p2);
135  EXPECT_TRUE(p.isFulfilled());
136 }
137 
138 TEST(SharedPromise, interruptHandler) {
140  bool flag = false;
141  p.setInterruptHandler([&](const exception_wrapper&) { flag = true; });
142  auto f = p.getFuture();
143  f.cancel();
144  EXPECT_TRUE(flag);
145 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void setInterruptHandler(std::function< void(exception_wrapper const &)>)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
static once_flag flag
Definition: Random.cpp:75
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Sugar to fulfill this SharedPromise<Unit>
Definition: SharedPromise.h:92
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST(SequencedExecutor, CPUThreadPoolExecutor)
SemiFuture< T > getSemiFuture()