Commit df20b646 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

Implement blockingWait for coro::Task

Reviewed By: lewissbaker

Differential Revision: D14002394

fbshipit-source-id: 8f0422d66c6b6859ec42a6ee5ea58c97e9a15de9
parent 3f488e75
......@@ -16,7 +16,10 @@
#pragma once
#include <folly/Try.h>
#include <folly/executors/ManualExecutor.h>
#include <folly/experimental/coro/Task.h>
#include <folly/experimental/coro/Traits.h>
#include <folly/experimental/coro/ViaIfAsync.h>
#include <folly/fibers/Baton.h>
#include <folly/synchronization/Baton.h>
......@@ -317,5 +320,22 @@ auto blockingWait(Awaitable&& awaitable)
.get());
}
template <typename T>
T blockingWait(Task<T>&& task) {
folly::ManualExecutor executor;
Try<T> ret;
bool done{false};
std::move(task).scheduleOn(&executor).start([&](Try<T>&& result) {
ret = std::move(result);
done = true;
});
while (!done) {
executor.drive();
}
return std::move(ret).value();
}
} // namespace coro
} // namespace folly
......@@ -287,9 +287,7 @@ coro::Task<void> taskTimedWait() {
}
TEST(Coro, TimedWait) {
ManualExecutor executor;
taskTimedWait().scheduleOn(&executor).start().via(&executor).getVia(
&executor);
coro::blockingWait(taskTimedWait());
}
template <int value>
......@@ -318,14 +316,7 @@ coro::Task<int> taskAwaitableWithOperator() {
}
TEST(Coro, AwaitableWithOperator) {
ManualExecutor executor;
EXPECT_EQ(
42,
taskAwaitableWithOperator()
.scheduleOn(&executor)
.start()
.via(&executor)
.getVia(&executor));
EXPECT_EQ(42, coro::blockingWait(taskAwaitableWithOperator()));
}
struct AwaitableWithMemberOperator {
......@@ -343,14 +334,7 @@ coro::Task<int> taskAwaitableWithMemberOperator() {
}
TEST(Coro, AwaitableWithMemberOperator) {
ManualExecutor executor;
EXPECT_EQ(
42,
taskAwaitableWithMemberOperator()
.scheduleOn(&executor)
.start()
.via(&executor)
.getVia(&executor));
EXPECT_EQ(42, coro::blockingWait(taskAwaitableWithMemberOperator()));
}
coro::Task<int> taskBaton(fibers::Baton& baton) {
......@@ -382,21 +366,11 @@ coro::Task<Type> taskFuture(Type value) {
}
TEST(Coro, FulfilledFuture) {
ManualExecutor executor;
auto value =
taskFuture(42).scheduleOn(&executor).start().via(&executor).getVia(
&executor);
EXPECT_EQ(42, value);
EXPECT_EQ(42, coro::blockingWait(taskFuture(42)));
}
TEST(Coro, MoveOnlyReturn) {
ManualExecutor executor;
auto value = taskFuture(std::make_unique<int>(42))
.scheduleOn(&executor)
.start()
.via(&executor)
.getVia(&executor);
EXPECT_EQ(42, *value);
EXPECT_EQ(42, *coro::blockingWait(taskFuture(std::make_unique<int>(42))));
}
TEST(Coro, co_invoke) {
......
......@@ -21,6 +21,7 @@
#include <folly/executors/InlineExecutor.h>
#include <folly/executors/ManualExecutor.h>
#include <folly/experimental/coro/Baton.h>
#include <folly/experimental/coro/BlockingWait.h>
#include <folly/experimental/coro/Mutex.h>
#include <folly/experimental/coro/Task.h>
#include <folly/experimental/coro/detail/InlineTask.h>
......@@ -298,17 +299,13 @@ TEST(Task, RequestContextSideEffectsArePreserved) {
}
TEST(Task, FutureTailCall) {
folly::ManualExecutor executor;
EXPECT_EQ(
42,
folly::coro::co_invoke([&]() -> folly::coro::Task<int> {
co_return co_await folly::makeSemiFuture().deferValue(
[](auto) { return folly::makeSemiFuture(42); });
})
.scheduleOn(&executor)
.start()
.via(&executor)
.getVia(&executor));
folly::coro::blockingWait(
folly::coro::co_invoke([&]() -> folly::coro::Task<int> {
co_return co_await folly::makeSemiFuture().deferValue(
[](auto) { return folly::makeSemiFuture(42); });
})));
}
#endif
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