template <class B = T>
typename std::enable_if<std::is_void<B>::value, void>::type
setValue() {
- set(Try<T>());
+ setTry(Try<T>());
}
/// Sugar to fulfill this SharedPromise<Unit>
template <class B = T>
typename std::enable_if<std::is_same<Unit, B>::value, void>::type
setValue() {
- set(Try<T>(T()));
+ setTry(Try<T>(T()));
}
/** Set the value (use perfect forwarding for both move and copy) */
#pragma once
+#include <folly/futures/SharedPromise.h>
#include <folly/wangle/channel/Handler.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/EventBaseManager.h>
DCHECK(isLoopCallbackScheduled());
sends_->prependChain(std::move(buf));
}
- Promise<void> p;
- auto f = p.getFuture();
- promises_.push_back(std::move(p));
- return f;
+ return sharedPromise_.getFuture();
}
}
void runLoopCallback() noexcept override {
- MoveWrapper<std::vector<Promise<void>>> promises(std::move(promises_));
+ MoveWrapper<SharedPromise<void>> sharedPromise;
+ std::swap(*sharedPromise, sharedPromise_);
getContext()->fireWrite(std::move(sends_))
- .then([promises](Try<void> t) mutable {
- for (auto& p : *promises) {
- p.setTry(Try<void>(t));
- }
+ .then([sharedPromise](Try<void> t) mutable {
+ sharedPromise->setTry(std::move(t));
});
}
}
// If there are sends queued, cancel them
- for (auto& promise : promises_) {
- promise.setException(
- folly::make_exception_wrapper<std::runtime_error>(
- "close() called while sends still pending"));
- }
+ sharedPromise_.setException(
+ folly::make_exception_wrapper<std::runtime_error>(
+ "close() called while sends still pending"));
sends_.reset();
- promises_.clear();
+ sharedPromise_ = SharedPromise<void>();
return ctx->fireClose();
}
- std::vector<Promise<void>> promises_;
+ SharedPromise<void> sharedPromise_;
std::unique_ptr<IOBuf> sends_{nullptr};
bool queueSends_{true};
};
EXPECT_TRUE(f1.isReady());
EXPECT_TRUE(f2.isReady());
EXPECT_CALL(mockHandler, detachPipeline(_));
+
+ // Make sure the SharedPromise resets correctly
+ auto f = pipeline.write(IOBuf::copyBuffer("foo"));
+ EXPECT_FALSE(f.isReady());
+ EXPECT_CALL(mockHandler, write_(_, IOBufContains("foo")));
+ eb.loopOnce();
+ EXPECT_TRUE(f.isReady());
}