/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
#include <folly/Executor.h>
#include <folly/Memory.h>
#include <folly/Unit.h>
ASSERT_EQ(result, 42);
}
+TEST(SemiFuture, SimpleGet) {
+ EventBase e2;
+ Promise<int> p;
+ auto sf = p.getSemiFuture();
+ p.setValue(3);
+ auto v = std::move(sf).get();
+ ASSERT_EQ(v, 3);
+}
+
+TEST(SemiFuture, SimpleGetTry) {
+ EventBase e2;
+ Promise<int> p;
+ auto sf = p.getSemiFuture();
+ p.setValue(3);
+ auto v = std::move(sf).getTry();
+ ASSERT_EQ(v.value(), 3);
+}
+
+TEST(SemiFuture, SimpleTimedGet) {
+ EventBase e2;
+ Promise<folly::Unit> p;
+ auto sf = p.getSemiFuture();
+ EXPECT_THROW(std::move(sf).get(std::chrono::seconds(1)), TimedOut);
+}
+
+TEST(SemiFuture, SimpleTimedGetTry) {
+ EventBase e2;
+ Promise<folly::Unit> p;
+ auto sf = p.getSemiFuture();
+ EXPECT_THROW(std::move(sf).getTry(std::chrono::seconds(1)), TimedOut);
+}
+
+TEST(SemiFuture, SimpleValue) {
+ EventBase e2;
+ Promise<int> p;
+ auto sf = p.getSemiFuture();
+ p.setValue(3);
+ auto v = std::move(sf).value();
+ ASSERT_EQ(v, 3);
+}
+
+TEST(SemiFuture, SimpleValueThrow) {
+ EventBase e2;
+ Promise<folly::Unit> p;
+ auto sf = p.getSemiFuture();
+ EXPECT_THROW(std::move(sf).value(), FutureNotReady);
+}
+
+TEST(SemiFuture, SimpleResult) {
+ EventBase e2;
+ Promise<int> p;
+ auto sf = p.getSemiFuture();
+ p.setValue(3);
+ auto v = std::move(sf).result();
+ ASSERT_EQ(v.value(), 3);
+}
+
+TEST(SemiFuture, SimpleResultThrow) {
+ EventBase e2;
+ Promise<folly::Unit> p;
+ auto sf = p.getSemiFuture();
+ EXPECT_THROW(std::move(sf).result(), FutureNotReady);
+}
+
TEST(SemiFuture, SimpleDefer) {
std::atomic<int> innerResult{0};
Promise<folly::Unit> p;
ASSERT_EQ(innerResult, 17);
}
+TEST(SemiFuture, DeferWithGetVia) {
+ std::atomic<int> innerResult{0};
+ EventBase e2;
+ Promise<folly::Unit> p;
+ auto f = p.getFuture();
+ auto sf = std::move(f).semi().defer([&]() { innerResult = 17; });
+ p.setValue();
+ std::move(sf).getVia(&e2);
+ ASSERT_EQ(innerResult, 17);
+}
+
TEST(SemiFuture, DeferWithVia) {
std::atomic<int> innerResult{0};
EventBase e2;