Commit 3bb51070 authored by Simon Martin's avatar Simon Martin Committed by Anton Likhtarov

Future::value() should throw when unset

Summary:
Added a test to call Future::value() before the Promise value is set, expecting an exception.
In a dbg build the test failed due on the assertion in Optional::value().
In a opt build the test failed due as no exception was thrown.
There are 2 points where we could throw our exception:
a) Optional::value() - replacing the assertion
b) Future::value()

I'm not sure which location makes the most sense.
With the assertion in Optional it seems that adding the throw here would not be unexpected but this is outside the wangle code.
So as a first pass I've added the throw in Future::value(), and made a new WangleException for this.

Test Plan:
$ fbconfig folly/wangle
$ fbmake runtests

Reviewed By: hans@fb.com

Subscribers: folly@lists, fugalh

FB internal diff: D1340886
parent 206c8f8f
......@@ -63,6 +63,12 @@ class PromiseAlreadySatisfied : public WangleException {
WangleException("Promise already satisfied") { }
};
class FutureNotReady : public WangleException {
public:
explicit FutureNotReady() :
WangleException("Future not ready") { }
};
class FutureAlreadyRetrieved : public WangleException {
public:
explicit FutureAlreadyRetrieved () :
......
......@@ -38,7 +38,7 @@ class FutureObject {
FutureObject& operator=(FutureObject const&) = delete;
// not movable (see comment in the implementation of Future::then)
FutureObject(FutureObject&&) = delete;
FutureObject(FutureObject&&) noexcept = delete;
FutureObject& operator=(FutureObject&&) = delete;
Try<T>& getTry() {
......@@ -85,7 +85,11 @@ class FutureObject {
}
typename std::add_lvalue_reference<T>::type value() {
return value_->value();
if (ready()) {
return value_->value();
} else {
throw FutureNotReady();
}
}
private:
......
......@@ -153,6 +153,12 @@ TEST(Future, isReady) {
EXPECT_TRUE(f.isReady());
}
TEST(Future, futureNotReady) {
Promise<int> p;
Future<int> f = p.getFuture();
EXPECT_THROW(f.value(), eggs_t);
}
TEST(Future, hasException) {
EXPECT_TRUE(makeFuture<int>(eggs).getTry().hasException());
EXPECT_FALSE(makeFuture(42).getTry().hasException());
......
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