2 * Copyright 2015 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, setValue) {
43 auto ffund = fund.getFuture();
45 EXPECT_EQ(42, ffund.value());
53 auto fpod = pod.getFuture();
54 Foo f = {"the answer", 42};
56 Foo f2 = fpod.value();
57 EXPECT_EQ(f.name, f2.name);
58 EXPECT_EQ(f.value, f2.value);
61 fpod = pod.getFuture();
62 pod.setValue(std::move(f2));
63 Foo f3 = fpod.value();
64 EXPECT_EQ(f.name, f3.name);
65 EXPECT_EQ(f.value, f3.value);
67 Promise<unique_ptr<int>> mov;
68 auto fmov = mov.getFuture();
69 mov.setValue(unique_ptr<int>(new int(42)));
70 unique_ptr<int> ptr = std::move(fmov.value());
74 auto fv = v.getFuture();
76 EXPECT_TRUE(fv.isReady());
79 TEST(Promise, setException) {
82 auto f = p.getFuture();
84 EXPECT_THROW(f.value(), eggs_t);
88 auto f = p.getFuture();
92 p.setException(exception_wrapper(std::current_exception()));
94 EXPECT_THROW(f.value(), eggs_t);
98 TEST(Promise, setWith) {
101 auto f = p.getFuture();
102 p.setWith([] { return 42; });
103 EXPECT_EQ(42, f.value());
107 auto f = p.getFuture();
108 p.setWith([]() -> int { throw eggs; });
109 EXPECT_THROW(f.value(), eggs_t);
113 TEST(Promise, isFulfilled) {
116 EXPECT_FALSE(p.isFulfilled());
118 EXPECT_TRUE(p.isFulfilled());
121 TEST(Promise, isFulfilledWithFuture) {
123 auto f = p.getFuture(); // so core_ will become null
125 EXPECT_FALSE(p.isFulfilled());
126 p.setValue(42); // after here
127 EXPECT_TRUE(p.isFulfilled());