本文整理汇总了C++中Promise::future方法的典型用法代码示例。如果您正苦于以下问题:C++ Promise::future方法的具体用法?C++ Promise::future怎么用?C++ Promise::future使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Promise
的用法示例。
在下文中一共展示了Promise::future方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
TEST(FutureTest, Future)
{
Promise<bool> promise;
promise.set(true);
ASSERT_TRUE(promise.future().isReady());
EXPECT_TRUE(promise.future().get());
}
示例2: await
TEST(AwaitTest, AwaitSingleDiscard)
{
Promise<int> promise;
auto bar = [&]() {
return promise.future();
};
auto foo = [&]() {
return await(bar())
.then([](const Future<int>& f) {
return f
.then([](int i) {
return stringify(i);
});
});
};
Future<string> future = foo();
future.discard();
AWAIT_DISCARDED(future);
EXPECT_TRUE(promise.future().hasDiscard());
}
示例3: futureCanDowncastToVoid
void futureCanDowncastToVoid()
{
Promise<int> promise;
Future<int> future1 = promise.future();
Future<void> future2 = promise.future();
Future<void> future3 = future1;
}
示例4: containerizer
// This test ensures that destroy can be called at the end of the
// launch loop. The composing containerizer still calls the
// underlying containerizer's destroy (because it's not sure
// if the containerizer can handle the type of container being
// launched). If the launch is not supported by any containerizers
// both the launch and destroy futures should be false.
TEST_F(ComposingContainerizerTest, DestroyAfterLaunchLoop)
{
vector<Containerizer*> containerizers;
MockContainerizer* mockContainerizer1 = new MockContainerizer();
containerizers.push_back(mockContainerizer1);
ComposingContainerizer containerizer(containerizers);
ContainerID containerId;
containerId.set_value("container");
TaskInfo taskInfo;
ExecutorInfo executorInfo;
SlaveID slaveId;
std::map<std::string, std::string> environment;
Promise<bool> launchPromise;
EXPECT_CALL(*mockContainerizer1, launch(_, _, _, _, _, _, _, _))
.WillOnce(Return(launchPromise.future()));
Future<Nothing> destroy;
Promise<bool> destroyPromise;
EXPECT_CALL(*mockContainerizer1, destroy(_))
.WillOnce(DoAll(FutureSatisfy(&destroy),
Return(destroyPromise.future())));
Future<bool> launched = containerizer.launch(
containerId,
taskInfo,
executorInfo,
"dir",
"user",
slaveId,
environment,
false);
Resources resources = Resources::parse("cpus:1;mem:256").get();
EXPECT_TRUE(launched.isPending());
Future<bool> destroyed = containerizer.destroy(containerId);
// We make sure the destroy is being called on the containerizer.
AWAIT_READY(destroy);
launchPromise.set(false);
destroyPromise.set(false);
// `launch` should return false and `destroyed` should return false
// because none of the containerizers support the launch.
AWAIT_EXPECT_EQ(false, launched);
AWAIT_EXPECT_EQ(false, destroyed);
}
示例5: detect
Future<Option<MasterInfo>> detect(
const Option<MasterInfo>& previous = None())
{
if (leader != previous) {
return leader;
}
Promise<Option<MasterInfo>>* promise = new Promise<Option<MasterInfo>>();
promise->future()
.onDiscard(defer(self(), &Self::discard, promise->future()));
promises.insert(promise);
return promise->future();
}
示例6: undiscardable
TEST(FutureTest, UndiscardableFuture)
{
Promise<int> promise;
Future<int> f = undiscardable(promise.future());
f.discard();
EXPECT_TRUE(f.hasDiscard());
EXPECT_FALSE(promise.future().hasDiscard());
promise.set(42);
AWAIT_ASSERT_EQ(42, f);
}
示例7: create
Future<int> create(
const string& path,
const string& data,
const ACL_vector& acl,
int flags,
string* result)
{
Promise<int>* promise = new Promise<int>();
Future<int> future = promise->future();
tuple<Promise<int>*, string*>* args =
new tuple<Promise<int>*, string*>(promise, result);
int ret = zoo_acreate(
zh,
path.c_str(),
data.data(),
data.size(),
&acl,
flags,
stringCompletion,
args);
if (ret != ZOK) {
delete promise;
delete args;
return ret;
}
return future;
}
示例8: set
Future<int> set(const string& path, const string& data, int version)
{
Promise<int>* promise = new Promise<int>();
Future<int> future = promise->future();
tuple<Promise<int>*, Stat*>* args =
new tuple<Promise<int>*, Stat*>(promise, nullptr);
int ret = zoo_aset(
zh,
path.c_str(),
data.data(),
data.size(),
version,
statCompletion,
args);
if (ret != ZOK) {
delete promise;
delete args;
return ret;
}
return future;
}
示例9:
FutureSync<void> TransportSocketCache::disconnect(MessageSocketPtr socket)
{
Promise<void> promiseSocketRemoved;
{
auto syncDisconnectInfos = _disconnectInfos.synchronize();
// TODO: Remove Promise<void>{} when get rid of VS2013.
syncDisconnectInfos->push_back(DisconnectInfo{socket, Promise<void>{}});
promiseSocketRemoved = syncDisconnectInfos->back().promiseSocketRemoved;
}
// We wait that the socket has been disconnected _and_ the `disconnected`
// signal has been received by the cache.
FutureBarrier<void> barrier;
barrier.addFuture(promiseSocketRemoved.future());
barrier.addFuture(socket->disconnect());
Promise<void> promise;
return barrier.future().then([=](const std::vector<Future<void>>& v) mutable {
const auto isInError = [](const Future<void>& f) {
return f.hasError();
};
if (std::any_of(begin(v), end(v), isInError))
{
promise.setError("disconnect error");
return;
}
promise.setValue(0);
});
}
示例10: readyFuture
TEST(FutureTest, Chain)
{
Future<string> s = readyFuture()
.then(lambda::bind(&second, lambda::_1))
.then(lambda::bind(&third, lambda::_1));
s.await();
ASSERT_TRUE(s.isReady());
EXPECT_EQ("true", s.get());
s = failedFuture()
.then(lambda::bind(&second, lambda::_1))
.then(lambda::bind(&third, lambda::_1));
s.await();
ASSERT_TRUE(s.isFailed());
Promise<bool> promise;
s = pendingFuture(promise.future())
.then(lambda::bind(&second, lambda::_1))
.then(lambda::bind(&third, lambda::_1));
ASSERT_TRUE(s.isPending());
promise.discard();
AWAIT_DISCARDED(s);
}
示例11: int
TEST(FutureTest, CallableOnce)
{
Promise<Nothing> promise;
promise.set(Nothing());
Future<int> future = promise.future()
.then(lambda::partial(
[](std::unique_ptr<int>&& o) {
return *o;
},
std::unique_ptr<int>(new int(42))));
ASSERT_TRUE(future.isReady());
EXPECT_EQ(42, future.get());
int n = 0;
future = promise.future()
.onReady(lambda::partial(
[&n](std::unique_ptr<int> o) {
n += *o;
},
std::unique_ptr<int>(new int(1))))
.onAny(lambda::partial(
[&n](std::unique_ptr<int>&& o) {
n += *o;
},
std::unique_ptr<int>(new int(10))))
.onFailed(lambda::partial(
[&n](const std::unique_ptr<int>& o) {
n += *o;
},
std::unique_ptr<int>(new int(100))))
.onDiscard(lambda::partial(
[&n](std::unique_ptr<int>&& o) {
n += *o;
},
std::unique_ptr<int>(new int(1000))))
.onDiscarded(lambda::partial(
[&n](std::unique_ptr<int>&& o) {
n += *o;
},
std::unique_ptr<int>(new int(10000))))
.then([&n]() { return n; });
ASSERT_TRUE(future.isReady());
EXPECT_EQ(11, future.get());
}
示例12: collect
inline Future<std::list<T> > collect(
std::list<Future<T> >& futures,
const Option<Timeout>& timeout)
{
Promise<std::list<T> >* promise = new Promise<std::list<T> >();
Future<std::list<T> > future = promise->future();
spawn(new internal::CollectProcess<T>(futures, timeout, promise), true);
return future;
}
示例13: containerizer
// This test checks if destroy is called while container is being
// launched, the composing containerizer still calls the underlying
// containerizer's destroy and skip calling the rest of the
// containerizers.
TEST_F(ComposingContainerizerTest, DestroyWhileLaunching)
{
vector<Containerizer*> containerizers;
MockContainerizer* mockContainerizer = new MockContainerizer();
MockContainerizer* mockContainerizer2 = new MockContainerizer();
containerizers.push_back(mockContainerizer);
containerizers.push_back(mockContainerizer2);
ComposingContainerizer containerizer(containerizers);
ContainerID containerId;
containerId.set_value("container");
TaskInfo taskInfo;
ExecutorInfo executorInfo;
SlaveID slaveId;
std::map<std::string, std::string> environment;
Promise<bool> launchPromise;
EXPECT_CALL(*mockContainerizer, launch(_, _, _, _, _, _, _, _))
.WillOnce(Return(launchPromise.future()));
Future<Nothing> destroy;
EXPECT_CALL(*mockContainerizer, destroy(_))
.WillOnce(FutureSatisfy(&destroy));
Future<bool> launch = containerizer.launch(
containerId,
taskInfo,
executorInfo,
"dir",
"user",
slaveId,
environment,
false);
Resources resources = Resources::parse("cpus:1;mem:256").get();
EXPECT_TRUE(launch.isPending());
containerizer.destroy(containerId);
EXPECT_CALL(*mockContainerizer2, launch(_, _, _, _, _, _, _, _))
.Times(0);
// We make sure the destroy is being called on the first containerizer.
// The second containerizer shouldn't be called as well since the
// container is already destroyed.
AWAIT_READY(destroy);
launchPromise.set(false);
AWAIT_FAILED(launch);
}
示例14:
TEST(Process, then)
{
Promise<int*> promise;
int i = 42;
promise.set(&i);
Future<std::string> future = promise.future()
.then(std::tr1::bind(&itoa1, std::tr1::placeholders::_1));
ASSERT_TRUE(future.isReady());
EXPECT_EQ("42", future.get());
future = promise.future()
.then(std::tr1::bind(&itoa2, std::tr1::placeholders::_1));
ASSERT_TRUE(future.isReady());
EXPECT_EQ("42", future.get());
}
示例15: Break
TEST(LoopTest, DiscardIterate)
{
Promise<int> promise;
promise.future().onDiscard([&]() { promise.discard(); });
Future<Nothing> future = loop(
[&]() {
return promise.future();
},
[&](int i) -> ControlFlow<Nothing> {
return Break();
});
EXPECT_TRUE(future.isPending());
future.discard();
AWAIT_DISCARDED(future);
EXPECT_TRUE(promise.future().hasDiscard());
}