Commit b4067a71 authored by Noel Sardana's avatar Noel Sardana Committed by facebook-github-bot-9

Added futures helpers times, when, and whileDo

Summary: Implemented the given functions by porting similar functionality from the twitter Future utility.

Reviewed By: @​hannesr

Differential Revision: D2303701
parent f0852f33
......@@ -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) {
......
......@@ -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>
......
/*
* 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);
}
}
/*
* 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);
}
/*
* 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);
}
}
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment