Added futures helpers times, when, and whileDo
authorNoel Sardana <nsardana@fb.com>
Tue, 11 Aug 2015 18:13:29 +0000 (11:13 -0700)
committerfacebook-github-bot-9 <folly-bot@fb.com>
Tue, 11 Aug 2015 18:22:05 +0000 (11:22 -0700)
Summary: Implemented the given functions by porting similar functionality from the twitter Future utility.

Reviewed By: @​hannesr

Differential Revision: D2303701

folly/futures/Future-inl.h
folly/futures/Future.h
folly/futures/test/TimesTest.cpp [new file with mode: 0644]
folly/futures/test/WhenTest.cpp [new file with mode: 0644]
folly/futures/test/WhileDoTest.cpp [new file with mode: 0644]
folly/test/Makefile.am

index 995d94b7e087151c79d47833e4d1210db5b0f7ac..fecf1781e0d35c147ef183d8840ab730e5419011 100644 (file)
@@ -248,6 +248,7 @@ Future<T>::onError(F&& func) {
       "Return type of onError callback must be T or Future<T>");
 
   Promise<T> p;
+  p.core_->setInterruptHandlerNoLock(core_->getInterruptHandler());
   auto f = p.getFuture();
   auto pm = folly::makeMoveWrapper(std::move(p));
   auto funcm = folly::makeMoveWrapper(std::move(func));
@@ -1086,6 +1087,31 @@ auto Future<T>::thenMultiWithExecutor(Executor* x, Callback&& fn)
   return then(x, std::forward<Callback>(fn));
 }
 
+template <class F>
+inline Future<Unit> when(bool p, F thunk) {
+  return p ? thunk().unit() : makeFuture();
+}
+
+template <class P, class F>
+Future<Unit> whileDo(P predicate, F thunk) {
+  if (predicate()) {
+    return thunk().then([=] {
+      return whileDo(predicate, thunk);
+    });
+  }
+  return makeFuture();
+}
+
+template <class F>
+Future<Unit> times(const int n, F thunk) {
+  auto count = folly::makeMoveWrapper(
+    std::unique_ptr<std::atomic<int>>(new std::atomic<int>(0))
+  );
+  return folly::whileDo([=]() mutable {
+      return (*count)->fetch_add(1) < n;
+    }, thunk);
+}
+
 namespace futures {
   template <class It, class F, class ItT, class Result>
   std::vector<Future<Result>> map(It first, It last, F func) {
index 0235c77a91e45399dfb43baf94afba541aeef82e..dd8627cc04c9de6219d140f146673833bf41bea3 100644 (file)
@@ -441,6 +441,29 @@ class Future {
   template <class T2>
   friend Future<T2> makeFuture(Try<T2>&&);
 
+  /// Repeat the given future (i.e., the computation it contains)
+  /// n times.
+  ///
+  /// thunk behaves like std::function<Future<T2>(void)>
+  template <class F>
+  friend Future<Unit> times(const int n, F thunk);
+
+  /// Carry out the computation contained in the given future if
+  /// the predicate holds.
+  ///
+  /// thunk behaves like std::function<Future<T2>(void)>
+  template <class F>
+  friend Future<Unit> when(bool p, F thunk);
+
+  /// Carry out the computation contained in the given future if
+  /// while the predicate continues to hold.
+  ///
+  /// thunk behaves like std::function<Future<T2>(void)>
+  ///
+  /// predicate behaves like std::function<bool(void)>
+  template <class P, class F>
+  friend Future<Unit> whileDo(P predicate, F thunk);
+
   // Variant: returns a value
   // e.g. f.then([](Try<T> t){ return t.value(); });
   template <typename F, typename R, bool isTry, typename... Args>
diff --git a/folly/futures/test/TimesTest.cpp b/folly/futures/test/TimesTest.cpp
new file mode 100644 (file)
index 0000000..a9c63a8
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2015 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <memory>
+#include <mutex>
+
+#include <gtest/gtest.h>
+#include <glog/logging.h>
+
+#include <folly/futures/Future.h>
+#include <folly/futures/Promise.h>
+
+using namespace folly;
+
+inline void popAndFulfillPromise(
+    std::queue<std::shared_ptr<Promise<Unit>>>& ps,
+    std::mutex& ps_mutex) {
+  ps_mutex.lock();
+  auto p = ps.front();
+  ps.pop();
+  ps_mutex.unlock();
+  p->setValue();
+}
+
+inline std::function<Future<Unit>(void)> makeThunk(
+    std::queue<std::shared_ptr<Promise<Unit>>>& ps,
+    int& interrupt,
+    std::mutex& ps_mutex) {
+  return [&]() mutable {
+    auto p = std::make_shared<Promise<Unit>>();
+    p->setInterruptHandler([&](exception_wrapper const& e) {
+      ++interrupt;
+    });
+    ps_mutex.lock();
+    ps.push(p);
+    ps_mutex.unlock();
+
+    return p->getFuture();
+  };
+}
+
+inline std::function<bool(void)> makePred(int& i) {
+  return [&]() {
+    bool res = i < 3;
+    ++i;
+    return res;
+  };
+}
+
+TEST(Times, success) {
+  std::queue<std::shared_ptr<Promise<Unit>>> ps;
+  std::mutex ps_mutex;
+  int interrupt = 0;
+  bool complete = false;
+  bool failure = false;
+
+  auto thunk = makeThunk(ps, interrupt, ps_mutex);
+  auto f = folly::times(3, thunk)
+    .then([&]() mutable { complete = true; })
+    .onError([&] (FutureException& e) { failure = true; });
+
+  popAndFulfillPromise(ps, ps_mutex);
+  EXPECT_FALSE(complete);
+  EXPECT_FALSE(failure);
+
+  popAndFulfillPromise(ps, ps_mutex);
+  EXPECT_FALSE(complete);
+  EXPECT_FALSE(failure);
+
+  popAndFulfillPromise(ps, ps_mutex);
+  EXPECT_TRUE(f.isReady());
+  EXPECT_TRUE(complete);
+  EXPECT_FALSE(failure);
+}
+
+TEST(Times, failure) {
+  std::queue<std::shared_ptr<Promise<Unit>>> ps;
+  std::mutex ps_mutex;
+  int interrupt = 0;
+  bool complete = false;
+  bool failure = false;
+
+  auto thunk = makeThunk(ps, interrupt, ps_mutex);
+  auto f = folly::times(3, thunk)
+    .then([&]() mutable { complete = true; })
+    .onError([&] (FutureException& e) { failure = true; });
+
+  popAndFulfillPromise(ps, ps_mutex);
+  EXPECT_FALSE(complete);
+  EXPECT_FALSE(failure);
+
+  ps_mutex.lock();
+  auto p2 = ps.front();
+  ps.pop();
+  ps_mutex.unlock();
+  FutureException eggs("eggs");
+  p2->setException(eggs);
+
+  EXPECT_TRUE(f.isReady());
+  EXPECT_FALSE(complete);
+  EXPECT_TRUE(failure);
+}
+
+TEST(Times, interrupt) {
+  std::queue<std::shared_ptr<Promise<Unit>>> ps;
+  std::mutex ps_mutex;
+  int interrupt = 0;
+  bool complete = false;
+  bool failure = false;
+
+  auto thunk = makeThunk(ps, interrupt, ps_mutex);
+  auto f = folly::times(3, thunk)
+    .then([&]() mutable { complete = true; })
+    .onError([&] (FutureException& e) { failure = true; });
+
+  EXPECT_EQ(0, interrupt);
+
+  FutureException eggs("eggs");
+  f.raise(eggs);
+
+  for (int i = 1; i <= 3; ++i) {
+    EXPECT_EQ(1, interrupt);
+    popAndFulfillPromise(ps, ps_mutex);
+  }
+}
diff --git a/folly/futures/test/WhenTest.cpp b/folly/futures/test/WhenTest.cpp
new file mode 100644 (file)
index 0000000..9f52f69
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2015 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <memory>
+#include <mutex>
+
+#include <gtest/gtest.h>
+#include <glog/logging.h>
+
+#include <folly/futures/Future.h>
+#include <folly/futures/Promise.h>
+
+using namespace folly;
+
+TEST(When, predicateFalse) {
+  int i = 0;
+  auto thunk = [&] {
+    return makeFuture().then([&] { i += 1; });
+  };
+
+  // false
+  auto f1 = folly::when(false, thunk);
+  f1.wait();
+  EXPECT_EQ(0, i);
+}
+
+TEST(When, predicateTrue) {
+  int i = 0;
+  auto thunk = [&] {
+    return makeFuture().then([&] { i += 1; });
+  };
+
+  // true
+  auto f2 = folly::when(true, thunk);
+  f2.wait();
+  EXPECT_EQ(1, i);
+}
diff --git a/folly/futures/test/WhileDoTest.cpp b/folly/futures/test/WhileDoTest.cpp
new file mode 100644 (file)
index 0000000..70acf84
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2015 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <memory>
+#include <mutex>
+
+#include <gtest/gtest.h>
+#include <glog/logging.h>
+
+#include <folly/futures/Future.h>
+#include <folly/futures/Promise.h>
+
+using namespace folly;
+
+inline void popAndFulfillPromise(
+    std::queue<std::shared_ptr<Promise<Unit>>>& ps,
+    std::mutex& ps_mutex) {
+  ps_mutex.lock();
+  auto p = ps.front();
+  ps.pop();
+  ps_mutex.unlock();
+  p->setValue();
+}
+
+inline std::function<Future<Unit>(void)> makeThunk(
+    std::queue<std::shared_ptr<Promise<Unit>>>& ps,
+    int& interrupt,
+    std::mutex& ps_mutex) {
+  return [&]() mutable {
+    auto p = std::make_shared<Promise<Unit>>();
+    p->setInterruptHandler([&](exception_wrapper const& e) {
+      ++interrupt;
+    });
+    ps_mutex.lock();
+    ps.push(p);
+    ps_mutex.unlock();
+
+    return p->getFuture();
+  };
+}
+
+inline std::function<bool(void)> makePred(int& i) {
+  return [&]() {
+    bool res = i < 3;
+    ++i;
+    return res;
+  };
+}
+
+TEST(WhileDo, success) {
+  std::queue<std::shared_ptr<Promise<Unit>>> ps;
+  std::mutex ps_mutex;
+  int i = 0;
+  int interrupt = 0;
+  bool complete = false;
+  bool failure = false;
+
+  auto pred = makePred(i);
+  auto thunk = makeThunk(ps, interrupt, ps_mutex);
+  auto f = folly::whileDo(pred, thunk)
+    .then([&]() mutable { complete = true; })
+    .onError([&] (FutureException& e) { failure = true; });
+
+  popAndFulfillPromise(ps, ps_mutex);
+  EXPECT_FALSE(complete);
+  EXPECT_FALSE(failure);
+
+  popAndFulfillPromise(ps, ps_mutex);
+  EXPECT_FALSE(complete);
+  EXPECT_FALSE(failure);
+
+  popAndFulfillPromise(ps, ps_mutex);
+  EXPECT_TRUE(f.isReady());
+  EXPECT_TRUE(complete);
+  EXPECT_FALSE(failure);
+}
+
+TEST(WhileDo, failure) {
+  std::queue<std::shared_ptr<Promise<Unit>>> ps;
+  std::mutex ps_mutex;
+  int i = 0;
+  int interrupt = 0;
+  bool complete = false;
+  bool failure = false;
+
+  auto pred = makePred(i);
+  auto thunk = makeThunk(ps, interrupt, ps_mutex);
+  auto f = folly::whileDo(pred, thunk)
+    .then([&]() mutable { complete = true; })
+    .onError([&] (FutureException& e) { failure = true; });
+
+  popAndFulfillPromise(ps, ps_mutex);
+  EXPECT_FALSE(complete);
+  EXPECT_FALSE(failure);
+
+  ps_mutex.lock();
+  auto p2 = ps.front();
+  ps.pop();
+  ps_mutex.unlock();
+  FutureException eggs("eggs");
+  p2->setException(eggs);
+
+  EXPECT_TRUE(f.isReady());
+  EXPECT_FALSE(complete);
+  EXPECT_TRUE(failure);
+}
+
+TEST(WhileDo, interrupt) {
+  std::queue<std::shared_ptr<Promise<Unit>>> ps;
+  std::mutex ps_mutex;
+  int i = 0;
+  int interrupt = 0;
+  bool complete = false;
+  bool failure = false;
+
+  auto pred = makePred(i);
+  auto thunk = makeThunk(ps, interrupt, ps_mutex);
+  auto f = folly::whileDo(pred, thunk)
+    .then([&]() mutable { complete = true; })
+    .onError([&] (FutureException& e) { failure = true; });
+
+  EXPECT_EQ(0, interrupt);
+
+  FutureException eggs("eggs");
+  f.raise(eggs);
+
+  for (int i = 1; i <= 3; ++i) {
+    EXPECT_EQ(1, interrupt);
+    popAndFulfillPromise(ps, ps_mutex);
+  }
+}
index 22ee324528ddaae0e8fb63c6d799e44330940566..f144a7a1a1089f4b8aed63d08e63d1b090693727 100644 (file)
@@ -203,13 +203,16 @@ futures_test_SOURCES = \
     ../futures/test/ThenCompileTest.cpp \
     ../futures/test/ThenTest.cpp \
     ../futures/test/TimekeeperTest.cpp \
+    ../futures/test/TimesTest.cpp \
     ../futures/test/TryTest.cpp \
     ../futures/test/UnitTest.cpp \
     ../futures/test/UnwrapTest.cpp \
     ../futures/test/ViaTest.cpp \
     ../futures/test/WaitTest.cpp \
     ../futures/test/WillEqualTest.cpp \
-    ../futures/test/WindowTest.cpp
+    ../futures/test/WindowTest.cpp \
+    ../futures/test/WhenTest.cpp \
+    ../futures/test/WhileDoTest.cpp
 
 futures_test_LDADD = libgtestmain.la $(top_builddir)/libfolly.la
 TESTS += futures_test