2 * Copyright 2015-present 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>
22 using namespace folly;
25 using std::unique_ptr;
26 typedef FutureException eggs_t;
27 static eggs_t eggs("eggs");
29 TEST(Promise, makeEmpty) {
30 auto p = Promise<int>::makeEmpty();
31 EXPECT_TRUE(p.isFulfilled());
34 TEST(Promise, special) {
35 EXPECT_FALSE(std::is_copy_constructible<Promise<int>>::value);
36 EXPECT_FALSE(std::is_copy_assignable<Promise<int>>::value);
37 EXPECT_TRUE(std::is_move_constructible<Promise<int>>::value);
38 EXPECT_TRUE(std::is_move_assignable<Promise<int>>::value);
41 TEST(Promise, getSemiFuture) {
43 SemiFuture<int> f = p.getSemiFuture();
44 EXPECT_FALSE(f.isReady());
47 TEST(Promise, getFuture) {
49 Future<int> f = p.getFuture();
50 EXPECT_FALSE(f.isReady());
53 TEST(Promise, setValueUnit) {
58 TEST(Promise, setValueSemiFuture) {
60 auto ffund = fund.getSemiFuture();
62 EXPECT_EQ(42, ffund.value());
70 auto fpod = pod.getSemiFuture();
71 Foo f = {"the answer", 42};
73 Foo f2 = fpod.value();
74 EXPECT_EQ(f.name, f2.name);
75 EXPECT_EQ(f.value, f2.value);
78 fpod = pod.getSemiFuture();
79 pod.setValue(std::move(f2));
80 Foo f3 = fpod.value();
81 EXPECT_EQ(f.name, f3.name);
82 EXPECT_EQ(f.value, f3.value);
84 Promise<unique_ptr<int>> mov;
85 auto fmov = mov.getSemiFuture();
86 mov.setValue(std::make_unique<int>(42));
87 unique_ptr<int> ptr = std::move(fmov.value());
91 auto fv = v.getSemiFuture();
93 EXPECT_TRUE(fv.isReady());
96 TEST(Promise, setValue) {
98 auto ffund = fund.getFuture();
100 EXPECT_EQ(42, ffund.value());
108 auto fpod = pod.getFuture();
109 Foo f = {"the answer", 42};
111 Foo f2 = fpod.value();
112 EXPECT_EQ(f.name, f2.name);
113 EXPECT_EQ(f.value, f2.value);
115 pod = Promise<Foo>();
116 fpod = pod.getFuture();
117 pod.setValue(std::move(f2));
118 Foo f3 = fpod.value();
119 EXPECT_EQ(f.name, f3.name);
120 EXPECT_EQ(f.value, f3.value);
122 Promise<unique_ptr<int>> mov;
123 auto fmov = mov.getFuture();
124 mov.setValue(std::make_unique<int>(42));
125 unique_ptr<int> ptr = std::move(fmov.value());
129 auto fv = v.getFuture();
131 EXPECT_TRUE(fv.isReady());
134 TEST(Promise, setException) {
137 auto f = p.getFuture();
138 p.setException(eggs);
139 EXPECT_THROW(f.value(), eggs_t);
143 auto f = p.getFuture();
144 p.setException(std::make_exception_ptr(eggs));
145 EXPECT_THROW(f.value(), eggs_t);
149 auto f = p.getFuture();
150 p.setException(exception_wrapper(eggs));
151 EXPECT_THROW(f.value(), eggs_t);
155 TEST(Promise, setWith) {
158 auto f = p.getFuture();
159 p.setWith([] { return 42; });
160 EXPECT_EQ(42, f.value());
164 auto f = p.getFuture();
165 p.setWith([]() -> int { throw eggs; });
166 EXPECT_THROW(f.value(), eggs_t);
170 TEST(Promise, isFulfilled) {
173 EXPECT_FALSE(p.isFulfilled());
175 EXPECT_TRUE(p.isFulfilled());
178 TEST(Promise, isFulfilledWithFuture) {
180 auto f = p.getFuture(); // so core_ will become null
182 EXPECT_FALSE(p.isFulfilled());
183 p.setValue(42); // after here
184 EXPECT_TRUE(p.isFulfilled());
187 TEST(Promise, brokenOnDelete) {
188 auto p = std::make_unique<Promise<int>>();
189 auto f = p->getFuture();
191 EXPECT_FALSE(f.isReady());
195 EXPECT_TRUE(f.isReady());
199 EXPECT_TRUE(t.hasException<BrokenPromise>());
202 TEST(Promise, brokenPromiseHasTypeInfo) {
203 auto pInt = std::make_unique<Promise<int>>();
204 auto fInt = pInt->getFuture();
206 auto pFloat = std::make_unique<Promise<float>>();
207 auto fFloat = pFloat->getFuture();
212 auto whatInt = fInt.getTry().exception().what();
213 auto whatFloat = fFloat.getTry().exception().what();
215 EXPECT_NE(whatInt, whatFloat);