/*
- * 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;
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);
EXPECT_FALSE(f.isReady());
}
+TEST(Promise, setValueUnit) {
+ Promise<Unit> p;
+ p.setValue();
+}
+
TEST(Promise, setValue) {
Promise<int> fund;
auto ffund = fund.getFuture();
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);
{
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);
}
}
p.setValue(42); // after here
EXPECT_TRUE(p.isFulfilled());
}
+
+TEST(Promise, brokenOnDelete) {
+ auto p = std::make_unique<Promise<int>>();
+ auto f = p->getFuture();
+
+ EXPECT_FALSE(f.isReady());
+
+ p.reset();
+
+ EXPECT_TRUE(f.isReady());
+
+ auto t = f.getTry();
+
+ EXPECT_TRUE(t.hasException<BrokenPromise>());
+}
+
+TEST(Promise, brokenPromiseHasTypeInfo) {
+ auto pInt = std::make_unique<Promise<int>>();
+ auto fInt = pInt->getFuture();
+
+ auto pFloat = std::make_unique<Promise<float>>();
+ auto fFloat = pFloat->getFuture();
+
+ pInt.reset();
+ pFloat.reset();
+
+ auto whatInt = fInt.getTry().exception().what();
+ auto whatFloat = fFloat.getTry().exception().what();
+
+ EXPECT_NE(whatInt, whatFloat);
+}