fix waitWithSemaphore return type
authorMatt Dordal <mnd@fb.com>
Mon, 19 May 2014 20:03:28 +0000 (13:03 -0700)
committerDave Watson <davejwatson@fb.com>
Tue, 20 May 2014 19:59:05 +0000 (12:59 -0700)
Summary:
waitWithSemaphore always tried to return a value, which is not what the
underlying implementation did. If the value_type was an object, it would
fail to compile.

Test Plan: unit tests (added one to compile all the variants)

Reviewed By: hans@fb.com

Subscribers: folly@lists, fugalh

FB internal diff: D1326916

folly/wangle/Future-inl.h
folly/wangle/Future.h
folly/wangle/test/FutureTest.cpp

index 10d844c..940d869 100644 (file)
@@ -420,14 +420,15 @@ whenN(InputIterator first, InputIterator last, size_t n) {
 }
 
 template <typename F>
-typename F::value_type waitWithSemaphore(F&& f) {
+typename std::add_lvalue_reference<typename F::value_type>::type
+waitWithSemaphore(F&& f) {
   LifoSem sem;
-  Try<typename F::value_type> done;
-  f.then([&](Try<typename F::value_type> &&t) {
-    done = std::move(t);
+  auto done = f.then([&](Try<typename F::value_type> &&t) {
     sem.post();
+    return t.value();
   });
   sem.wait();
+  while (!done.isReady()) {}
   return done.value();
 }
 
index a0030f8..20ecce1 100644 (file)
@@ -320,7 +320,8 @@ whenN(InputIterator first, InputIterator last, size_t n);
  * you call this, it will deadlock.
  */
 template <class F>
-typename F::value_type waitWithSemaphore(F&& f);
+typename std::add_lvalue_reference<typename F::value_type>::type
+waitWithSemaphore(F&& f);
 
 }} // folly::wangle
 
index d925644..0202470 100644 (file)
@@ -627,7 +627,18 @@ TEST(Future, throwIfFailed) {
 TEST(Future, waitWithSemaphoreImmediate) {
   waitWithSemaphore(makeFuture());
   auto done = waitWithSemaphore(makeFuture(42));
-  EXPECT_EQ(done, 42);
+  EXPECT_EQ(42, done);
+
+  vector<int> v{1,2,3};
+  auto done_v = waitWithSemaphore(makeFuture(v));
+  EXPECT_EQ(v.size(), done_v.size());
+  EXPECT_EQ(v, done_v);
+
+  vector<Future<void>> v_f;
+  v_f.push_back(makeFuture());
+  v_f.push_back(makeFuture());
+  auto done_v_f = waitWithSemaphore(whenAll(v_f.begin(), v_f.end()));
+  EXPECT_EQ(2, done_v_f.size());
 }
 
 TEST(Future, waitWithSemaphore) {