Mark constructing an Unexpected as cold
[folly.git] / folly / futures / test / PromiseTest.cpp
index 743095b8e39b5cc007d14a14351695129dcb0b97..509aa7aa6edbb9524c672e8ad95d3916665e0a06 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#include <gtest/gtest.h>
-
 #include <folly/futures/Future.h>
+#include <folly/portability/GTest.h>
+
+#include <memory>
 
 using namespace folly;
 using std::unique_ptr;
@@ -25,6 +26,11 @@ using std::string;
 typedef FutureException eggs_t;
 static eggs_t eggs("eggs");
 
+TEST(Promise, makeEmpty) {
+  auto p = Promise<int>::makeEmpty();
+  EXPECT_TRUE(p.isFulfilled());
+}
+
 TEST(Promise, special) {
   EXPECT_FALSE(std::is_copy_constructible<Promise<int>>::value);
   EXPECT_FALSE(std::is_copy_assignable<Promise<int>>::value);
@@ -38,6 +44,11 @@ TEST(Promise, getFuture) {
   EXPECT_FALSE(f.isReady());
 }
 
+TEST(Promise, setValueUnit) {
+  Promise<Unit> p;
+  p.setValue();
+}
+
 TEST(Promise, setValue) {
   Promise<int> fund;
   auto ffund = fund.getFuture();
@@ -66,7 +77,7 @@ TEST(Promise, setValue) {
 
   Promise<unique_ptr<int>> mov;
   auto fmov = mov.getFuture();
-  mov.setValue(unique_ptr<int>(new int(42)));
+  mov.setValue(std::make_unique<int>(42));
   unique_ptr<int> ptr = std::move(fmov.value());
   EXPECT_EQ(42, *ptr);
 
@@ -86,11 +97,13 @@ TEST(Promise, setException) {
   {
     Promise<Unit> p;
     auto f = p.getFuture();
-    try {
-      throw eggs;
-    } catch (...) {
-      p.setException(exception_wrapper(std::current_exception()));
-    }
+    p.setException(std::make_exception_ptr(eggs));
+    EXPECT_THROW(f.value(), eggs_t);
+  }
+  {
+    Promise<Unit> p;
+    auto f = p.getFuture();
+    p.setException(exception_wrapper(eggs));
     EXPECT_THROW(f.value(), eggs_t);
   }
 }
@@ -128,7 +141,7 @@ TEST(Promise, isFulfilledWithFuture) {
 }
 
 TEST(Promise, brokenOnDelete) {
-  auto p = folly::make_unique<Promise<int>>();
+  auto p = std::make_unique<Promise<int>>();
   auto f = p->getFuture();
 
   EXPECT_FALSE(f.isReady());
@@ -143,10 +156,10 @@ TEST(Promise, brokenOnDelete) {
 }
 
 TEST(Promise, brokenPromiseHasTypeInfo) {
-  auto pInt = folly::make_unique<Promise<int>>();
+  auto pInt = std::make_unique<Promise<int>>();
   auto fInt = pInt->getFuture();
 
-  auto pFloat = folly::make_unique<Promise<float>>();
+  auto pFloat = std::make_unique<Promise<float>>();
   auto fFloat = pFloat->getFuture();
 
   pInt.reset();