本文整理汇总了C++中Future::discard方法的典型用法代码示例。如果您正苦于以下问题:C++ Future::discard方法的具体用法?C++ Future::discard怎么用?C++ Future::discard使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Future
的用法示例。
在下文中一共展示了Future::discard方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: executed
// Checks that the 'after' callback gets executed if the future is not
// completed.
TEST(FutureTest, After1)
{
Clock::pause();
std::atomic_bool executed(false);
Future<Nothing> future = Future<Nothing>()
.after(Hours(42), lambda::bind(&after, &executed, lambda::_1));
// A pending future should stay pending until 'after' is executed.
EXPECT_TRUE(future.isPending());
// Only advanced halfway, future should remain pending.
Clock::advance(Hours(21));
EXPECT_TRUE(future.isPending());
// Even doing a discard on the future should keep it pending.
future.discard();
EXPECT_TRUE(future.isPending());
// After advancing all the way the future should now fail because
// the 'after' callback gets executed.
Clock::advance(Hours(21));
AWAIT_FAILED(future);
EXPECT_TRUE(executed.load());
Clock::resume();
}
示例2: close
TEST(IO, Read)
{
ASSERT_TRUE(GTEST_IS_THREADSAFE);
int pipes[2];
char data[3];
// Create a blocking pipe.
ASSERT_NE(-1, ::pipe(pipes));
// Test on a blocking file descriptor.
AWAIT_EXPECT_FAILED(io::read(pipes[0], data, 3));
close(pipes[0]);
close(pipes[1]);
// Test on a closed file descriptor.
AWAIT_EXPECT_FAILED(io::read(pipes[0], data, 3));
// Create a nonblocking pipe.
ASSERT_NE(-1, ::pipe(pipes));
ASSERT_SOME(os::nonblock(pipes[0]));
ASSERT_SOME(os::nonblock(pipes[1]));
// Test reading nothing.
AWAIT_EXPECT_FAILED(io::read(pipes[0], data, 0));
// Test successful read.
Future<size_t> future = io::read(pipes[0], data, 3);
ASSERT_FALSE(future.isReady());
ASSERT_EQ(2, write(pipes[1], "hi", 2));
AWAIT_ASSERT_EQ(2u, future);
EXPECT_EQ('h', data[0]);
EXPECT_EQ('i', data[1]);
// Test cancellation.
future = io::read(pipes[0], data, 1);
ASSERT_FALSE(future.isReady());
future.discard();
ASSERT_EQ(3, write(pipes[1], "omg", 3));
AWAIT_ASSERT_EQ(3u, io::read(pipes[0], data, 3));
EXPECT_EQ('o', data[0]);
EXPECT_EQ('m', data[1]);
EXPECT_EQ('g', data[2]);
// Test read EOF.
future = io::read(pipes[0], data, 3);
ASSERT_FALSE(future.isReady());
close(pipes[1]);
AWAIT_ASSERT_EQ(0u, future);
close(pipes[0]);
}
示例3: 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());
}
示例4:
TEST(BasicMasterContenderDetectorTest, Detector)
{
PID<Master> master;
master.node.ip = 10000000;
master.node.port = 10000;
StandaloneMasterDetector detector;
Future<Option<MasterInfo> > detected = detector.detect();
// No one has appointed the leader so we are pending.
EXPECT_TRUE(detected.isPending());
// Ensure that the future can be discarded.
detected.discard();
AWAIT_DISCARDED(detected);
detected = detector.detect();
// Still no leader appointed yet.
EXPECT_TRUE(detected.isPending());
detector.appoint(master);
AWAIT_READY(detected);
}
示例5: select
TEST(FutureTest, Select)
{
Promise<int> promise1;
Promise<int> promise2;
Promise<int> promise3;
Promise<int> promise4;
std::set<Future<int>> futures = {
promise1.future(),
promise2.future(),
promise3.future(),
promise4.future()
};
promise1.set(42);
Future<Future<int>> future = select(futures);
AWAIT_READY(future);
AWAIT_READY(future.get());
EXPECT_EQ(42, future->get());
futures.erase(promise1.future());
future = select(futures);
EXPECT_TRUE(future.isPending());
future.discard();
AWAIT_DISCARDED(future);
}
示例6: Seconds
TEST_F(DockerTest, ROOT_DOCKER_CancelPull)
{
// Delete the test image if it exists.
Try<Subprocess> s = process::subprocess(
tests::flags.docker + " rmi lingmann/1gb",
Subprocess::PATH("/dev/null"),
Subprocess::PATH("/dev/null"),
Subprocess::PATH("/dev/null"));
ASSERT_SOME(s);
AWAIT_READY_FOR(s.get().status(), Seconds(30));
Owned<Docker> docker = Docker::create(
tests::flags.docker,
tests::flags.docker_socket,
false).get();
Try<string> directory = environment->mkdtemp();
CHECK_SOME(directory) << "Failed to create temporary directory";
// Assume that pulling the very large image 'lingmann/1gb' will take
// sufficiently long that we can start it and discard (i.e., cancel
// it) right away and the future will indeed get discarded.
Future<Docker::Image> future =
docker->pull(directory.get(), "lingmann/1gb");
future.discard();
AWAIT_DISCARDED(future);
}
示例7: Group
// A single contender gets elected automatically.
TEST_F(ZooKeeperMasterContenderDetectorTest, MasterContender)
{
Try<zookeeper::URL> url = zookeeper::URL::parse(
"zk://" + server->connectString() + "/mesos");
ASSERT_SOME(url);
Owned<zookeeper::Group> group(
new Group(url.get(), MASTER_CONTENDER_ZK_SESSION_TIMEOUT));
ZooKeeperMasterContender* contender = new ZooKeeperMasterContender(group);
PID<Master> pid;
pid.node.ip = 10000000;
pid.node.port = 10000;
MasterInfo master = internal::protobuf::createMasterInfo(pid);
contender->initialize(master);
Future<Future<Nothing> > contended = contender->contend();
AWAIT_READY(contended);
ZooKeeperMasterDetector detector(url.get());
Future<Option<MasterInfo> > leader = detector.detect();
AWAIT_READY(leader);
EXPECT_SOME_EQ(master, leader.get());
leader = detector.detect(leader.get());
// No change to leadership.
ASSERT_TRUE(leader.isPending());
// Ensure we can discard the future.
leader.discard();
AWAIT_DISCARDED(leader);
// After the discard, we can re-detect correctly.
leader = detector.detect(None());
AWAIT_READY(leader);
EXPECT_SOME_EQ(master, leader.get());
// Now test that a session expiration causes candidacy to be lost
// and the future to become ready.
Future<Nothing> lostCandidacy = contended.get();
leader = detector.detect(leader.get());
Future<Option<int64_t> > sessionId = group.get()->session();
AWAIT_READY(sessionId);
server->expireSession(sessionId.get().get());
AWAIT_READY(lostCandidacy);
AWAIT_READY(leader);
EXPECT_NONE(leader.get());
}
示例8:
// Tests that we don't propagate a discard through a `recover()` but a
// discard can still be called and propagate later.
TEST(FutureTest, RecoverDiscard)
{
Promise<int> promise1;
Promise<string> promise2;
Promise<string> promise3;
Promise<string> promise4;
Future<string> future = promise1.future()
.then([]() -> string {
return "hello";
})
.recover([&](const Future<string>&) {
return promise2.future()
.then([&]() {
return promise3.future()
.then([&]() {
return promise4.future();
});
});
});
future.discard();
promise1.discard();
EXPECT_FALSE(promise2.future().hasDiscard());
promise2.set(string("not world"));
EXPECT_FALSE(promise3.future().hasDiscard());
promise3.set(string("also not world"));
EXPECT_FALSE(promise4.future().hasDiscard());
future.discard();
EXPECT_TRUE(promise4.future().hasDiscard());
promise4.set(string("world"));
AWAIT_EQ("world", future);
}
示例9: return
/*
* Class: org_apache_mesos_state_AbstractState
* Method: __expunge_cancel
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_org_apache_mesos_state_AbstractState__1_1expunge_1cancel
(JNIEnv* env, jobject thiz, jlong jfuture)
{
Future<bool>* future = (Future<bool>*) jfuture;
// We'll initiate a discard but we won't consider it cancelled since
// we don't know if/when the future will get discarded.
future->discard();
return (jboolean) false;
}
示例10: return
/*
* Class: org_apache_mesos_state_ZooKeeperState
* Method: __names_cancel
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_org_apache_mesos_state_ZooKeeperState__1_1names_1cancel
(JNIEnv* env, jobject thiz, jlong jfuture)
{
Future<vector<string> >* future = (Future<vector<string> >*) jfuture;
if (!future->isDiscarded()) {
future->discard();
return (jboolean) future->isDiscarded();
}
return (jboolean) true;
}
示例11: timedout
void timedout()
{
// Need to discard all of the futures so any of their associated
// resources can get properly cleaned up.
typename std::list<Future<T> >::const_iterator iterator;
for (iterator = futures.begin(); iterator != futures.end(); ++iterator) {
Future<T> future = *iterator; // Need a non-const copy to discard.
future.discard();
}
promise->fail("Collect failed: timed out");
terminate(this);
}
示例12: 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);
}
示例13:
TEST(CollectTest, DiscardPropagation)
{
Future<int> future1;
Future<bool> future2;
future1
.onDiscard([=](){ process::internal::discarded(future1); });
future2
.onDiscard([=](){ process::internal::discarded(future2); });
Future<std::tuple<int, bool>> collect = process::collect(future1, future2);
collect.discard();
AWAIT_DISCARDED(future1);
AWAIT_DISCARDED(future2);
}
示例14: 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());
}
示例15:
TEST(AwaitTest, DiscardPropagation)
{
Promise<int> promise1;
Promise<bool> promise2;
promise1.future()
.onDiscard([&](){ promise1.discard(); });
promise2.future()
.onDiscard([&](){ promise2.discard(); });
Future<std::tuple<Future<int>, Future<bool>>> await = process::await(
promise1.future(),
promise2.future());
await.discard();
AWAIT_DISCARDED(await);
AWAIT_DISCARDED(promise1.future());
AWAIT_DISCARDED(promise2.future());
}