2 * Copyright 2016 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 <gtest/gtest.h>
19 #include <folly/futures/Future.h>
21 using namespace folly;
22 using std::unique_ptr;
25 typedef FutureException eggs_t;
26 static eggs_t eggs("eggs");
28 TEST(Promise, special) {
29 EXPECT_FALSE(std::is_copy_constructible<Promise<int>>::value);
30 EXPECT_FALSE(std::is_copy_assignable<Promise<int>>::value);
31 EXPECT_TRUE(std::is_move_constructible<Promise<int>>::value);
32 EXPECT_TRUE(std::is_move_assignable<Promise<int>>::value);
35 TEST(Promise, getFuture) {
37 Future<int> f = p.getFuture();
38 EXPECT_FALSE(f.isReady());
41 TEST(Promise, setValueUnit) {
46 TEST(Promise, setValue) {
48 auto ffund = fund.getFuture();
50 EXPECT_EQ(42, ffund.value());
58 auto fpod = pod.getFuture();
59 Foo f = {"the answer", 42};
61 Foo f2 = fpod.value();
62 EXPECT_EQ(f.name, f2.name);
63 EXPECT_EQ(f.value, f2.value);
66 fpod = pod.getFuture();
67 pod.setValue(std::move(f2));
68 Foo f3 = fpod.value();
69 EXPECT_EQ(f.name, f3.name);
70 EXPECT_EQ(f.value, f3.value);
72 Promise<unique_ptr<int>> mov;
73 auto fmov = mov.getFuture();
74 mov.setValue(unique_ptr<int>(new int(42)));
75 unique_ptr<int> ptr = std::move(fmov.value());
79 auto fv = v.getFuture();
81 EXPECT_TRUE(fv.isReady());
84 TEST(Promise, setException) {
87 auto f = p.getFuture();
89 EXPECT_THROW(f.value(), eggs_t);
93 auto f = p.getFuture();
97 p.setException(exception_wrapper(std::current_exception()));
99 EXPECT_THROW(f.value(), eggs_t);
103 TEST(Promise, setWith) {
106 auto f = p.getFuture();
107 p.setWith([] { return 42; });
108 EXPECT_EQ(42, f.value());
112 auto f = p.getFuture();
113 p.setWith([]() -> int { throw eggs; });
114 EXPECT_THROW(f.value(), eggs_t);
118 TEST(Promise, isFulfilled) {
121 EXPECT_FALSE(p.isFulfilled());
123 EXPECT_TRUE(p.isFulfilled());
126 TEST(Promise, isFulfilledWithFuture) {
128 auto f = p.getFuture(); // so core_ will become null
130 EXPECT_FALSE(p.isFulfilled());
131 p.setValue(42); // after here
132 EXPECT_TRUE(p.isFulfilled());
135 TEST(Promise, brokenOnDelete) {
136 auto p = folly::make_unique<Promise<int>>();
137 auto f = p->getFuture();
139 EXPECT_FALSE(f.isReady());
143 EXPECT_TRUE(f.isReady());
147 EXPECT_TRUE(t.hasException<BrokenPromise>());
150 TEST(Promise, brokenPromiseHasTypeInfo) {
151 auto pInt = folly::make_unique<Promise<int>>();
152 auto fInt = pInt->getFuture();
154 auto pFloat = folly::make_unique<Promise<float>>();
155 auto fFloat = pFloat->getFuture();
160 auto whatInt = fInt.getTry().exception().what();
161 auto whatFloat = fFloat.getTry().exception().what();
163 EXPECT_NE(whatInt, whatFloat);