Modernize use of std::make_unique
[folly.git] / folly / futures / test / PromiseTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/futures/Future.h>
18 #include <folly/portability/GTest.h>
19
20 #include <memory>
21
22 using namespace folly;
23 using std::unique_ptr;
24 using std::string;
25
26 typedef FutureException eggs_t;
27 static eggs_t eggs("eggs");
28
29 TEST(Promise, makeEmpty) {
30   auto p = Promise<int>::makeEmpty();
31   EXPECT_TRUE(p.isFulfilled());
32 }
33
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);
39 }
40
41 TEST(Promise, getFuture) {
42   Promise<int> p;
43   Future<int> f = p.getFuture();
44   EXPECT_FALSE(f.isReady());
45 }
46
47 TEST(Promise, setValueUnit) {
48   Promise<Unit> p;
49   p.setValue();
50 }
51
52 TEST(Promise, setValue) {
53   Promise<int> fund;
54   auto ffund = fund.getFuture();
55   fund.setValue(42);
56   EXPECT_EQ(42, ffund.value());
57
58   struct Foo {
59     string name;
60     int value;
61   };
62
63   Promise<Foo> pod;
64   auto fpod = pod.getFuture();
65   Foo f = {"the answer", 42};
66   pod.setValue(f);
67   Foo f2 = fpod.value();
68   EXPECT_EQ(f.name, f2.name);
69   EXPECT_EQ(f.value, f2.value);
70
71   pod = Promise<Foo>();
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);
77
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());
82   EXPECT_EQ(42, *ptr);
83
84   Promise<Unit> v;
85   auto fv = v.getFuture();
86   v.setValue();
87   EXPECT_TRUE(fv.isReady());
88 }
89
90 TEST(Promise, setException) {
91   {
92     Promise<Unit> p;
93     auto f = p.getFuture();
94     p.setException(eggs);
95     EXPECT_THROW(f.value(), eggs_t);
96   }
97   {
98     Promise<Unit> p;
99     auto f = p.getFuture();
100     p.setException(std::make_exception_ptr(eggs));
101     EXPECT_THROW(f.value(), eggs_t);
102   }
103   {
104     Promise<Unit> p;
105     auto f = p.getFuture();
106     p.setException(exception_wrapper(eggs));
107     EXPECT_THROW(f.value(), eggs_t);
108   }
109 }
110
111 TEST(Promise, setWith) {
112   {
113     Promise<int> p;
114     auto f = p.getFuture();
115     p.setWith([] { return 42; });
116     EXPECT_EQ(42, f.value());
117   }
118   {
119     Promise<int> p;
120     auto f = p.getFuture();
121     p.setWith([]() -> int { throw eggs; });
122     EXPECT_THROW(f.value(), eggs_t);
123   }
124 }
125
126 TEST(Promise, isFulfilled) {
127   Promise<int> p;
128
129   EXPECT_FALSE(p.isFulfilled());
130   p.setValue(42);
131   EXPECT_TRUE(p.isFulfilled());
132 }
133
134 TEST(Promise, isFulfilledWithFuture) {
135   Promise<int> p;
136   auto f = p.getFuture(); // so core_ will become null
137
138   EXPECT_FALSE(p.isFulfilled());
139   p.setValue(42); // after here
140   EXPECT_TRUE(p.isFulfilled());
141 }
142
143 TEST(Promise, brokenOnDelete) {
144   auto p = std::make_unique<Promise<int>>();
145   auto f = p->getFuture();
146
147   EXPECT_FALSE(f.isReady());
148
149   p.reset();
150
151   EXPECT_TRUE(f.isReady());
152
153   auto t = f.getTry();
154
155   EXPECT_TRUE(t.hasException<BrokenPromise>());
156 }
157
158 TEST(Promise, brokenPromiseHasTypeInfo) {
159   auto pInt = std::make_unique<Promise<int>>();
160   auto fInt = pInt->getFuture();
161
162   auto pFloat = std::make_unique<Promise<float>>();
163   auto fFloat = pFloat->getFuture();
164
165   pInt.reset();
166   pFloat.reset();
167
168   auto whatInt = fInt.getTry().exception().what();
169   auto whatFloat = fFloat.getTry().exception().what();
170
171   EXPECT_NE(whatInt, whatFloat);
172 }