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>
22 using namespace folly;
23 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, getFuture) {
43 Future<int> f = p.getFuture();
44 EXPECT_FALSE(f.isReady());
47 TEST(Promise, setValueUnit) {
52 TEST(Promise, setValue) {
54 auto ffund = fund.getFuture();
56 EXPECT_EQ(42, ffund.value());
64 auto fpod = pod.getFuture();
65 Foo f = {"the answer", 42};
67 Foo f2 = fpod.value();
68 EXPECT_EQ(f.name, f2.name);
69 EXPECT_EQ(f.value, f2.value);
72 fpod = pod.getFuture();
73 pod.setValue(std::move(f2));
74 Foo f3 = fpod.value();
75 EXPECT_EQ(f.name, f3.name);
76 EXPECT_EQ(f.value, f3.value);
78 Promise<unique_ptr<int>> mov;
79 auto fmov = mov.getFuture();
80 mov.setValue(std::make_unique<int>(42));
81 unique_ptr<int> ptr = std::move(fmov.value());
85 auto fv = v.getFuture();
87 EXPECT_TRUE(fv.isReady());
90 TEST(Promise, setException) {
93 auto f = p.getFuture();
95 EXPECT_THROW(f.value(), eggs_t);
99 auto f = p.getFuture();
100 p.setException(std::make_exception_ptr(eggs));
101 EXPECT_THROW(f.value(), eggs_t);
105 auto f = p.getFuture();
106 p.setException(exception_wrapper(eggs));
107 EXPECT_THROW(f.value(), eggs_t);
111 TEST(Promise, setWith) {
114 auto f = p.getFuture();
115 p.setWith([] { return 42; });
116 EXPECT_EQ(42, f.value());
120 auto f = p.getFuture();
121 p.setWith([]() -> int { throw eggs; });
122 EXPECT_THROW(f.value(), eggs_t);
126 TEST(Promise, isFulfilled) {
129 EXPECT_FALSE(p.isFulfilled());
131 EXPECT_TRUE(p.isFulfilled());
134 TEST(Promise, isFulfilledWithFuture) {
136 auto f = p.getFuture(); // so core_ will become null
138 EXPECT_FALSE(p.isFulfilled());
139 p.setValue(42); // after here
140 EXPECT_TRUE(p.isFulfilled());
143 TEST(Promise, brokenOnDelete) {
144 auto p = std::make_unique<Promise<int>>();
145 auto f = p->getFuture();
147 EXPECT_FALSE(f.isReady());
151 EXPECT_TRUE(f.isReady());
155 EXPECT_TRUE(t.hasException<BrokenPromise>());
158 TEST(Promise, brokenPromiseHasTypeInfo) {
159 auto pInt = std::make_unique<Promise<int>>();
160 auto fInt = pInt->getFuture();
162 auto pFloat = std::make_unique<Promise<float>>();
163 auto fFloat = pFloat->getFuture();
168 auto whatInt = fInt.getTry().exception().what();
169 auto whatFloat = fFloat.getTry().exception().what();
171 EXPECT_NE(whatInt, whatFloat);