(wangle) set* should not invalidate getFuture
authorHans Fugal <fugalh@fb.com>
Wed, 23 Jul 2014 22:12:19 +0000 (15:12 -0700)
committerChip Turner <chip@fb.com>
Fri, 25 Jul 2014 16:07:12 +0000 (09:07 -0700)
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
folly/wangle/test/FutureTest.cpp

index ef6bdb41c4c050261ddee868b5aaa524cb27e151..e502796346daa44304a65be4c9b5c1eb33efae1d 100644 (file)
@@ -72,7 +72,6 @@ void Promise<T>::detach() {
 template <class T>
 Future<T> Promise<T>::getFuture() {
   throwIfRetrieved();
-  throwIfFulfilled();
   retrieved_ = true;
   return Future<T>(state_);
 }
index df5341de38c1d1b0a982e956bb377168277a9f7f..4f85c139d06fb186ea38800e14479af8088a1fdd 100644 (file)
@@ -788,3 +788,15 @@ TEST(Future, viaIsCold) {
   EXPECT_EQ(1, x.run());
   EXPECT_EQ(1, count);
 }
+
+TEST(Future, getFuture_after_setValue) {
+  Promise<int> p;
+  p.setValue(42);
+  EXPECT_EQ(42, p.getFuture().value());
+}
+
+TEST(Future, getFuture_after_setException) {
+  Promise<void> p;
+  p.fulfil([]() -> void { throw std::logic_error("foo"); });
+  EXPECT_THROW(p.getFuture().value(), std::logic_error);
+}