2 * Copyright 2017 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>
20 using namespace folly;
21 using std::unique_ptr;
24 typedef FutureException eggs_t;
25 static eggs_t eggs("eggs");
27 TEST(Promise, special) {
28 EXPECT_FALSE(std::is_copy_constructible<Promise<int>>::value);
29 EXPECT_FALSE(std::is_copy_assignable<Promise<int>>::value);
30 EXPECT_TRUE(std::is_move_constructible<Promise<int>>::value);
31 EXPECT_TRUE(std::is_move_assignable<Promise<int>>::value);
34 TEST(Promise, getFuture) {
36 Future<int> f = p.getFuture();
37 EXPECT_FALSE(f.isReady());
40 TEST(Promise, setValueUnit) {
45 TEST(Promise, setValue) {
47 auto ffund = fund.getFuture();
49 EXPECT_EQ(42, ffund.value());
57 auto fpod = pod.getFuture();
58 Foo f = {"the answer", 42};
60 Foo f2 = fpod.value();
61 EXPECT_EQ(f.name, f2.name);
62 EXPECT_EQ(f.value, f2.value);
65 fpod = pod.getFuture();
66 pod.setValue(std::move(f2));
67 Foo f3 = fpod.value();
68 EXPECT_EQ(f.name, f3.name);
69 EXPECT_EQ(f.value, f3.value);
71 Promise<unique_ptr<int>> mov;
72 auto fmov = mov.getFuture();
73 mov.setValue(unique_ptr<int>(new int(42)));
74 unique_ptr<int> ptr = std::move(fmov.value());
78 auto fv = v.getFuture();
80 EXPECT_TRUE(fv.isReady());
83 TEST(Promise, setException) {
86 auto f = p.getFuture();
88 EXPECT_THROW(f.value(), eggs_t);
92 auto f = p.getFuture();
95 } catch (const std::exception& e) {
96 p.setException(exception_wrapper(std::current_exception(), e));
98 p.setException(exception_wrapper(std::current_exception()));
100 EXPECT_THROW(f.value(), eggs_t);
104 TEST(Promise, setWith) {
107 auto f = p.getFuture();
108 p.setWith([] { return 42; });
109 EXPECT_EQ(42, f.value());
113 auto f = p.getFuture();
114 p.setWith([]() -> int { throw eggs; });
115 EXPECT_THROW(f.value(), eggs_t);
119 TEST(Promise, isFulfilled) {
122 EXPECT_FALSE(p.isFulfilled());
124 EXPECT_TRUE(p.isFulfilled());
127 TEST(Promise, isFulfilledWithFuture) {
129 auto f = p.getFuture(); // so core_ will become null
131 EXPECT_FALSE(p.isFulfilled());
132 p.setValue(42); // after here
133 EXPECT_TRUE(p.isFulfilled());
136 TEST(Promise, brokenOnDelete) {
137 auto p = std::make_unique<Promise<int>>();
138 auto f = p->getFuture();
140 EXPECT_FALSE(f.isReady());
144 EXPECT_TRUE(f.isReady());
148 EXPECT_TRUE(t.hasException<BrokenPromise>());
151 TEST(Promise, brokenPromiseHasTypeInfo) {
152 auto pInt = std::make_unique<Promise<int>>();
153 auto fInt = pInt->getFuture();
155 auto pFloat = std::make_unique<Promise<float>>();
156 auto fFloat = pFloat->getFuture();
161 auto whatInt = fInt.getTry().exception().what();
162 auto whatFloat = fFloat.getTry().exception().what();
164 EXPECT_NE(whatInt, whatFloat);