Commit 42ab2462 authored by Monica Lee's avatar Monica Lee Committed by Alecs King

Add willEqual to C++ Futures Code

Summary: Added willEqual function and wrote unit tests for it.

Test Plan: fbconfig --clang folly/futures && fbmake runtests

Reviewed By: hans@fb.com

Subscribers: hannesr, trunkagent, folly-diffs@, jsedgwick, yfeldblum

FB internal diff: D1859840

Tasks: 6166911

Signature: t1:1859840:1424967149:865ee96ab4d3f5dbf17eb371b2ac3ccb5066ac87
parent a0359850
...@@ -743,8 +743,18 @@ inline void Future<void>::getVia(DrivableExecutor* e) { ...@@ -743,8 +743,18 @@ inline void Future<void>::getVia(DrivableExecutor* e) {
waitVia(e).value(); waitVia(e).value();
} }
namespace futures { template <class T>
Future<bool> Future<T>::willEqual(Future<T>& f) {
return whenAll(*this, f).then([](const std::tuple<Try<T>, Try<T>>& t) {
if (std::get<0>(t).hasValue() && std::get<1>(t).hasValue()) {
return std::get<0>(t).value() == std::get<1>(t).value();
} else {
return false;
}
});
}
namespace futures {
namespace { namespace {
template <class Z> template <class Z>
Future<Z> chainHelper(Future<Z> f) { Future<Z> chainHelper(Future<Z> f) {
......
...@@ -455,7 +455,12 @@ class Future { ...@@ -455,7 +455,12 @@ class Future {
/// Overload of waitVia() for rvalue Futures /// Overload of waitVia() for rvalue Futures
Future<T>&& waitVia(DrivableExecutor* e) &&; Future<T>&& waitVia(DrivableExecutor* e) &&;
protected: /// If the value in this Future is equal to the given Future, when they have
/// both completed, the value of the resulting Future<bool> will be true. It
/// will be false otherwise (including when one or both Futures have an
/// exception)
Future<bool> willEqual(Future<T>&);
typedef detail::Core<T>* corePtr; typedef detail::Core<T>* corePtr;
// shared core state object // shared core state object
......
...@@ -1329,3 +1329,84 @@ TEST(Future, ensure) { ...@@ -1329,3 +1329,84 @@ TEST(Future, ensure) {
EXPECT_THROW(f.get(), std::runtime_error); EXPECT_THROW(f.get(), std::runtime_error);
EXPECT_EQ(2, count); EXPECT_EQ(2, count);
} }
TEST(Future, willEqual) {
//both p1 and p2 already fulfilled
{
Promise<int> p1;
Promise<int> p2;
p1.setValue(27);
p2.setValue(27);
auto f1 = p1.getFuture();
auto f2 = p2.getFuture();
EXPECT_TRUE(f1.willEqual(f2).get());
}{
Promise<int> p1;
Promise<int> p2;
p1.setValue(27);
p2.setValue(36);
auto f1 = p1.getFuture();
auto f2 = p2.getFuture();
EXPECT_FALSE(f1.willEqual(f2).get());
}
//both p1 and p2 not yet fulfilled
{
Promise<int> p1;
Promise<int> p2;
auto f1 = p1.getFuture();
auto f2 = p2.getFuture();
auto f3 = f1.willEqual(f2);
p1.setValue(27);
p2.setValue(27);
EXPECT_TRUE(f3.get());
}{
Promise<int> p1;
Promise<int> p2;
auto f1 = p1.getFuture();
auto f2 = p2.getFuture();
auto f3 = f1.willEqual(f2);
p1.setValue(27);
p2.setValue(36);
EXPECT_FALSE(f3.get());
}
//p1 already fulfilled, p2 not yet fulfilled
{
Promise<int> p1;
Promise<int> p2;
p1.setValue(27);
auto f1 = p1.getFuture();
auto f2 = p2.getFuture();
auto f3 = f1.willEqual(f2);
p2.setValue(27);
EXPECT_TRUE(f3.get());
}{
Promise<int> p1;
Promise<int> p2;
p1.setValue(27);
auto f1 = p1.getFuture();
auto f2 = p2.getFuture();
auto f3 = f1.willEqual(f2);
p2.setValue(36);
EXPECT_FALSE(f3.get());
}
//p2 already fulfilled, p1 not yet fulfilled
{
Promise<int> p1;
Promise<int> p2;
p2.setValue(27);
auto f1 = p1.getFuture();
auto f2 = p2.getFuture();
auto f3 = f1.willEqual(f2);
p1.setValue(27);
EXPECT_TRUE(f3.get());
}{
Promise<int> p1;
Promise<int> p2;
p2.setValue(36);
auto f1 = p1.getFuture();
auto f2 = p2.getFuture();
auto f3 = f1.willEqual(f2);
p1.setValue(27);
EXPECT_FALSE(f3.get());
}
}
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