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
WangleException("Promise already satisfied") { }
};
+class FutureNotReady : public WangleException {
+ public:
+ explicit FutureNotReady() :
+ WangleException("Future not ready") { }
+};
+
class FutureAlreadyRetrieved : public WangleException {
public:
explicit FutureAlreadyRetrieved () :
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() {
}
typename std::add_lvalue_reference<T>::type value() {
- return value_->value();
+ if (ready()) {
+ return value_->value();
+ } else {
+ throw FutureNotReady();
+ }
}
private:
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());