Future<Option<int64_t> > GroupProcess::session() { if (error.isSome()) { Promise<Option<int64_t> > promise; promise.fail(error.get()); return promise.future(); } else if (state != CONNECTED) { return None(); } return Option<int64_t>::some(zk->getSessionId()); }
TEST(FutureTest, RecoverFailed) { Promise<int> promise; Future<string> future = promise.future() .then([]() -> string { return "hello"; }) .recover([](const Future<string>&) -> string { return "world"; }); promise.fail("Failure"); AWAIT_EQ("world", future); }
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)); } }