#include <folly/wangle/Executor.h>
#include <folly/wangle/detail/FSM.h>
+#include <folly/io/async/Request.h>
+
namespace folly { namespace wangle { namespace detail {
// As of GCC 4.8.1, the std::function in libstdc++ optimizes only for pointers
throw std::logic_error("setCallback called twice");
}
+ context_ = RequestContext::saveContext();
callback_ = std::move(func);
};
// TODO(5306911) we should probably try/catch
calledBack_ = true;
Executor* x = executor_;
+
+ RequestContext::setContext(context_);
if (x) {
MoveWrapper<std::function<void(Try<T>&&)>> cb(std::move(callback_));
MoveWrapper<folly::Optional<Try<T>>> val(std::move(result_));
folly::Optional<Try<T>> result_;
std::function<void(Try<T>&&)> callback_;
+ std::shared_ptr<RequestContext> context_{nullptr};
std::atomic<bool> calledBack_ {false};
std::atomic<unsigned char> detached_ {0};
std::atomic<bool> active_ {true};
#include <folly/wangle/Future.h>
#include <folly/wangle/ManualExecutor.h>
+#include <folly/io/async/Request.h>
+
+using namespace folly;
using namespace folly::wangle;
using std::pair;
using std::string;
f.reset();
t1.join();
}
+
+class TestData : public RequestData {
+ public:
+ explicit TestData(int data) : data_(data) {}
+ virtual ~TestData() {}
+ int data_;
+};
+
+TEST(Future, context) {
+
+ // Start a new context
+ RequestContext::create();
+
+ EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
+
+ // Set some test data
+ RequestContext::get()->setContextData(
+ "test",
+ std::unique_ptr<TestData>(new TestData(10)));
+
+ // Start a future
+ Promise<void> p;
+ auto future = p.getFuture().then([&]{
+ // Check that the context followed the future
+ EXPECT_TRUE(RequestContext::get() != nullptr);
+ auto a = dynamic_cast<TestData*>(
+ RequestContext::get()->getContextData("test"));
+ auto data = a->data_;
+ EXPECT_EQ(10, data);
+ });
+
+ // Clear the context
+ RequestContext::setContext(nullptr);
+
+ EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
+
+ // Fulfil the promise
+ p.setValue();
+}