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); }
TEST(FutureTest, RecoverDiscarded) { Promise<int> promise; Future<string> future = promise.future() .then([]() -> string { return "hello"; }) .recover([](const Future<string>&) -> string { return "world"; }); promise.discard(); AWAIT_EQ("world", future); }
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()); }
TEST(FutureTest, Stringify) { Future<bool> future; EXPECT_EQ("Abandoned", stringify(future)); { Owned<Promise<bool>> promise(new Promise<bool>()); future = promise->future(); promise.reset(); EXPECT_EQ("Abandoned", stringify(future)); } { Owned<Promise<bool>> promise(new Promise<bool>()); future = promise->future(); promise->future().discard(); promise.reset(); EXPECT_EQ("Abandoned (with discard)", stringify(future)); } { Promise<bool> promise; future = promise.future(); EXPECT_EQ("Pending", stringify(future)); promise.future().discard(); EXPECT_EQ("Pending (with discard)", stringify(future)); } { Promise<bool> promise; future = promise.future(); promise.set(true); EXPECT_EQ("Ready", stringify(future)); } { Promise<bool> promise; future = promise.future(); promise.future().discard(); promise.set(true); EXPECT_EQ("Ready (with discard)", stringify(future)); } { Promise<bool> promise; future = promise.future(); promise.fail("Failure"); EXPECT_EQ("Failed: Failure", stringify(future)); } { Promise<bool> promise; future = promise.future(); promise.future().discard(); promise.fail("Failure"); EXPECT_EQ("Failed (with discard): Failure", stringify(future)); } { Promise<bool> promise; future = promise.future(); promise.discard(); EXPECT_EQ("Discarded", stringify(future)); } { Promise<bool> promise; future = promise.future(); promise.future().discard(); promise.discard(); EXPECT_EQ("Discarded (with discard)", stringify(future)); } }