From 56df65bec3cbea1b84da4a4a03772de40cf2e200 Mon Sep 17 00:00:00 2001 From: Hans Fugal Date: Wed, 23 Jul 2014 15:12:19 -0700 Subject: [PATCH] (wangle) set* should not invalidate getFuture Summary: @jcoens observed in D1451114 that it was unintuitive that you have to retrieve the `Future` before fulfilling the `Promise`. That seemed wrong to me too, but sure enough when I wrote the unit tests that doesn't work (throws "promise already fulfilled" when you call `getFuture`). I think this is just a simple mistake, but I'm going to carefully look at the output of contbuild test suites before committing. Test Plan: red-green careful dependency unit test inspection Reviewed By: jon.coens@fb.com Subscribers: net-systems@, fugalh, exa, jcoens FB internal diff: D1453780 --- folly/wangle/Promise-inl.h | 1 - folly/wangle/test/FutureTest.cpp | 12 ++++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/folly/wangle/Promise-inl.h b/folly/wangle/Promise-inl.h index ef6bdb41..e5027963 100644 --- a/folly/wangle/Promise-inl.h +++ b/folly/wangle/Promise-inl.h @@ -72,7 +72,6 @@ void Promise::detach() { template Future Promise::getFuture() { throwIfRetrieved(); - throwIfFulfilled(); retrieved_ = true; return Future(state_); } diff --git a/folly/wangle/test/FutureTest.cpp b/folly/wangle/test/FutureTest.cpp index df5341de..4f85c139 100644 --- a/folly/wangle/test/FutureTest.cpp +++ b/folly/wangle/test/FutureTest.cpp @@ -788,3 +788,15 @@ TEST(Future, viaIsCold) { EXPECT_EQ(1, x.run()); EXPECT_EQ(1, count); } + +TEST(Future, getFuture_after_setValue) { + Promise p; + p.setValue(42); + EXPECT_EQ(42, p.getFuture().value()); +} + +TEST(Future, getFuture_after_setException) { + Promise p; + p.fulfil([]() -> void { throw std::logic_error("foo"); }); + EXPECT_THROW(p.getFuture().value(), std::logic_error); +} -- 2.34.1