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 <folly/futures/SharedPromise.h>
18 #include <folly/portability/GTest.h>
20 using namespace folly;
22 TEST(SharedPromise, setGet) {
25 auto f1 = p.getFuture();
26 auto f2 = p.getFuture();
27 EXPECT_EQ(1, f1.value());
28 EXPECT_EQ(1, f2.value());
30 TEST(SharedPromise, getSet) {
32 auto f1 = p.getFuture();
33 auto f2 = p.getFuture();
35 EXPECT_EQ(1, f1.value());
36 EXPECT_EQ(1, f2.value());
39 TEST(SharedPromise, getSetGet) {
41 auto f1 = p.getFuture();
43 auto f2 = p.getFuture();
44 EXPECT_EQ(1, f1.value());
45 EXPECT_EQ(1, f2.value());
48 TEST(SharedPromise, reset) {
51 auto f1 = p.getFuture();
53 EXPECT_EQ(1, f1.value());
55 p = SharedPromise<int>();
56 auto f2 = p.getFuture();
57 EXPECT_FALSE(f2.isReady());
59 EXPECT_EQ(2, f2.value());
62 TEST(SharedPromise, getMoveSet) {
64 auto f = p.getFuture();
65 auto p2 = std::move(p);
67 EXPECT_EQ(1, f.value());
70 TEST(SharedPromise, setMoveGet) {
73 auto p2 = std::move(p);
74 auto f = p2.getFuture();
75 EXPECT_EQ(1, f.value());
78 TEST(SharedPromise, moveSetGet) {
80 auto p2 = std::move(p);
82 auto f = p2.getFuture();
83 EXPECT_EQ(1, f.value());
86 TEST(SharedPromise, moveGetSet) {
88 auto p2 = std::move(p);
89 auto f = p2.getFuture();
91 EXPECT_EQ(1, f.value());
94 TEST(SharedPromise, moveMove) {
95 SharedPromise<std::shared_ptr<int>> p;
96 auto f1 = p.getFuture();
97 auto f2 = p.getFuture();
98 auto p2 = std::move(p);
100 p.setValue(std::make_shared<int>(1));
103 TEST(SharedPromise, setWith) {
104 SharedPromise<int> p;
105 p.setWith([]{ return 1; });
106 EXPECT_EQ(1, p.getFuture().value());
109 TEST(SharedPromise, isFulfilled) {
110 SharedPromise<int> p;
111 EXPECT_FALSE(p.isFulfilled());
112 auto p2 = std::move(p);
113 EXPECT_FALSE(p2.isFulfilled());
115 EXPECT_TRUE(p2.isFulfilled());
117 EXPECT_TRUE(p.isFulfilled());
120 TEST(SharedPromise, interruptHandler) {
121 SharedPromise<int> p;
123 p.setInterruptHandler([&](const exception_wrapper&) { flag = true; });
124 auto f = p.getFuture();
129 TEST(SharedPromise, splitFutureSuccess) {
131 SharedPromise<int> sp(p.getFuture());
132 auto f1 = sp.getFuture();
133 EXPECT_FALSE(f1.isReady());
135 EXPECT_TRUE(f1.isReady());
136 EXPECT_TRUE(f1.hasValue());
137 auto f2 = sp.getFuture();
138 EXPECT_TRUE(f2.isReady());
139 EXPECT_TRUE(f2.hasValue());
142 TEST(SharedPromise, splitFutureFailure) {
144 SharedPromise<int> sp(p.getFuture());
145 auto f1 = sp.getFuture();
146 EXPECT_FALSE(f1.isReady());
148 throw std::runtime_error("Oops");
150 p.setException(exception_wrapper(std::current_exception()));
152 EXPECT_TRUE(f1.isReady());
153 EXPECT_TRUE(f1.hasException());
154 auto f2 = sp.getFuture();
155 EXPECT_TRUE(f2.isReady());
156 EXPECT_TRUE(f2.hasException());