本文整理汇总了C++中Promise::getFuture方法的典型用法代码示例。如果您正苦于以下问题:C++ Promise::getFuture方法的具体用法?C++ Promise::getFuture怎么用?C++ Promise::getFuture使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Promise
的用法示例。
在下文中一共展示了Promise::getFuture方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
TEST(Future, unwrap) {
Promise<int> a;
Promise<int> b;
auto fa = a.getFuture();
auto fb = b.getFuture();
bool flag1 = false;
bool flag2 = false;
// do a, then do b, and get the result of a + b.
Future<int> f = fa.then([&](Try<int>&& ta) {
auto va = ta.value();
flag1 = true;
return fb.then([va, &flag2](Try<int>&& tb) {
flag2 = true;
return va + tb.value();
});
});
EXPECT_FALSE(flag1);
EXPECT_FALSE(flag2);
EXPECT_FALSE(f.isReady());
a.setValue(3);
EXPECT_TRUE(flag1);
EXPECT_FALSE(flag2);
EXPECT_FALSE(f.isReady());
b.setValue(4);
EXPECT_TRUE(flag1);
EXPECT_TRUE(flag2);
EXPECT_EQ(7, f.value());
}
示例2: test_promise
void test_promise() {
Promise<bool> p;
auto f = p.getFuture();
try {
p.getFuture();
ok(false, "should throw");
} catch (const std::logic_error& exc) {
ok(!strcmp(exc.what(), "Future already obtained"), "can't getFuture twice");
}
ok(!f.isReady(), "not yet ready");
p.setValue(true);
try {
p.setValue(false);
ok(false, "should throw");
} catch (const std::logic_error& exc) {
ok(!strcmp(exc.what(), "Promise already fulfilled"),
"can't setValue twice");
}
ok(f.isReady(), "now ready");
ok(f.get() == true, "got our true value");
Promise<std::string> s;
s.setException(std::make_exception_ptr(std::runtime_error("boo")));
auto f2 = s.getFuture();
ok(f2.result().hasError(), "holds an error");
try {
f2.get();
} catch (const std::runtime_error& exc) {
ok(!strcmp(exc.what(), "boo"), "has boo string");
}
}
示例3: collect
TEST(Collect, collectVariadicWithException) {
Promise<bool> pb;
Promise<int> pi;
Future<bool> fb = pb.getFuture();
Future<int> fi = pi.getFuture();
auto f = collect(std::move(fb), std::move(fi));
pb.setValue(true);
EXPECT_FALSE(f.isReady());
pi.setException(eggs);
EXPECT_TRUE(f.isReady());
EXPECT_TRUE(f.getTry().hasException());
EXPECT_THROW(f.get(), eggs_t);
}
示例4:
TEST(Promise, setWith) {
{
Promise<int> p;
auto f = p.getFuture();
p.setWith([] { return 42; });
EXPECT_EQ(42, f.value());
}
{
Promise<int> p;
auto f = p.getFuture();
p.setWith([]() -> int { throw eggs; });
EXPECT_THROW(f.value(), eggs_t);
}
}
示例5: sizeof
TEST(Future, finishBigLambda) {
auto x = std::make_shared<int>(0);
// bulk_data, to be captured in the lambda passed to Future::then.
// This is meant to force that the lambda can't be stored inside
// the Future object.
std::array<char, sizeof(detail::Core<int>)> bulk_data = {0};
// suppress gcc warning about bulk_data not being used
EXPECT_EQ(bulk_data[0], 0);
Promise<int> p;
auto f = p.getFuture().then([x, bulk_data](Try<int>&& t) { *x = t.value(); });
// The callback hasn't executed
EXPECT_EQ(0, *x);
// The callback has a reference to x
EXPECT_EQ(2, x.use_count());
p.setValue(42);
// the callback has executed
EXPECT_EQ(42, *x);
// the callback has been destructed
// and has released its reference to x
EXPECT_EQ(1, x.use_count());
}
示例6:
TEST(Interrupt, interruptThenHandle) {
Promise<int> p;
bool flag = false;
p.getFuture().cancel();
p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
EXPECT_TRUE(flag);
}
示例7: t
TEST(Wait, wait) {
Promise<int> p;
Future<int> f = p.getFuture();
std::atomic<bool> flag{false};
std::atomic<int> result{1};
std::atomic<std::thread::id> id;
std::thread t([&](Future<int>&& tf){
auto n = tf.then([&](Try<int> && t) {
id = std::this_thread::get_id();
return t.value();
});
flag = true;
result.store(n.wait().value());
},
std::move(f)
);
while(!flag){}
EXPECT_EQ(result.load(), 1);
p.setValue(42);
t.join();
// validate that the callback ended up executing in this thread, which
// is more to ensure that this test actually tests what it should
EXPECT_EQ(id, std::this_thread::get_id());
EXPECT_EQ(result.load(), 42);
}
示例8: makeFuture
TEST(Future, thenTry) {
bool flag = false;
makeFuture<int>(42).then([&](Try<int>&& t) {
flag = true;
EXPECT_EQ(42, t.value());
});
EXPECT_TRUE(flag); flag = false;
makeFuture<int>(42)
.then([](Try<int>&& t) { return t.value(); })
.then([&](Try<int>&& t) { flag = true; EXPECT_EQ(42, t.value()); });
EXPECT_TRUE(flag); flag = false;
makeFuture().then([&](Try<Unit>&& t) { flag = true; t.value(); });
EXPECT_TRUE(flag); flag = false;
Promise<Unit> p;
auto f = p.getFuture().then([&](Try<Unit>&& /* t */) { flag = true; });
EXPECT_FALSE(flag);
EXPECT_FALSE(f.isReady());
p.setValue();
EXPECT_TRUE(flag);
EXPECT_TRUE(f.isReady());
}
示例9: TestData
TEST(Context, basic) {
// Start a new context
folly::RequestContextScopeGuard rctx;
EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
// Set some test data
RequestContext::get()->setContextData(
"test",
std::unique_ptr<TestData>(new TestData(10)));
// Start a future
Promise<Unit> p;
auto future = p.getFuture().then([&]{
// Check that the context followed the future
EXPECT_TRUE(RequestContext::get() != nullptr);
auto a = dynamic_cast<TestData*>(
RequestContext::get()->getContextData("test"));
auto data = a->data_;
EXPECT_EQ(10, data);
});
// Clear the context
RequestContext::setContext(nullptr);
EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
// Fulfill the promise
p.setValue();
}
示例10: move
TEST(NonCopyableLambda, unique_ptr) {
Promise<Unit> promise;
auto int_ptr = std::make_unique<int>(1);
EXPECT_EQ(*int_ptr, 1);
auto future = promise.getFuture().thenValue(std::bind(
[](std::unique_ptr<int>& p, folly::Unit) mutable {
++*p;
return std::move(p);
},
std::move(int_ptr),
std::placeholders::_1));
// The previous statement can be simplified in C++14:
// auto future =
// promise.getFuture().thenValue([int_ptr = std::move(int_ptr)](
// auto&&) mutable {
// ++*int_ptr;
// return std::move(int_ptr);
// });
EXPECT_FALSE(future.isReady());
promise.setValue();
EXPECT_TRUE(future.isReady());
EXPECT_EQ(*std::move(future).get(), 2);
}
示例11:
TEST(Timekeeper, futureWithinThrows) {
Promise<int> p;
auto f = p.getFuture()
.within(one_ms)
.onError([](TimedOut&) { return -1; });
EXPECT_EQ(-1, f.get());
}
示例12: collect
TEST(Collect, collectVariadic) {
Promise<bool> pb;
Promise<int> pi;
Future<bool> fb = pb.getFuture();
Future<int> fi = pi.getFuture();
bool flag = false;
collect(std::move(fb), std::move(fi))
.then([&](std::tuple<bool, int> tup) {
flag = true;
EXPECT_EQ(std::get<0>(tup), true);
EXPECT_EQ(std::get<1>(tup), 42);
});
pb.setValue(true);
EXPECT_FALSE(flag);
pi.setValue(42);
EXPECT_TRUE(flag);
}
示例13:
TEST(SemiFuture, DeferWithGetTimedGet) {
std::atomic<int> innerResult{0};
Promise<folly::Unit> p;
auto f = p.getFuture();
auto sf = std::move(f).semi().defer([&]() { innerResult = 17; });
EXPECT_THROW(std::move(sf).get(std::chrono::milliseconds(100)), TimedOut);
ASSERT_EQ(innerResult, 0);
}
示例14: catch
TEST(Promise, setException) {
{
Promise<Unit> p;
auto f = p.getFuture();
p.setException(eggs);
EXPECT_THROW(f.value(), eggs_t);
}
{
Promise<Unit> p;
auto f = p.getFuture();
try {
throw eggs;
} catch (...) {
p.setException(exception_wrapper(std::current_exception()));
}
EXPECT_THROW(f.value(), eggs_t);
}
}
示例15:
TEST(Timekeeper, futureWithinHandlesNullTimekeeperSingleton) {
Singleton<ThreadWheelTimekeeper>::make_mock([] { return nullptr; });
SCOPE_EXIT {
Singleton<ThreadWheelTimekeeper>::make_mock();
};
Promise<int> p;
auto f = p.getFuture().within(one_ms);
EXPECT_THROW(f.get(), NoTimekeeper);
}