Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
petiaccja committed Mar 14, 2024
1 parent 966b240 commit dca2c3e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/asyncpp/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace impl_task {
constexpr bool await_ready() const noexcept { return false; }
void await_suspend(std::coroutine_handle<promise> handle) const noexcept {
auto& owner = handle.promise();
owner.m_event.set(owner.m_result);
owner.m_event.set(std::move(owner.m_result));
auto self = std::move(owner.m_self); // owner.m_self.reset() call method on owner after it's been deleted.
self.reset();
}
Expand Down
15 changes: 15 additions & 0 deletions test/test_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,19 @@ TEST_CASE("Generator: sequence of references", "[Generator]") {
values.push_back(item++);
}
REQUIRE(values == std::vector{ 0, 2, 4, 6 });
}


TEST_CASE("Generator: sequence of mvoeables", "[Generator]") {
static const auto coro = [](int count) -> generator<std::unique_ptr<int>> {
for (int i = 0; i < count; ++i) {
co_yield std::make_unique<int>(i);
}
};
const auto g = coro(4);
std::vector<int> values;
for (auto& item : g) {
values.push_back(*item);
}
REQUIRE(values == std::vector{ 0, 1, 2, 3 });
}
11 changes: 11 additions & 0 deletions test/test_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ TEST_CASE("Stream: data types", "[Stream]") {
}();
REQUIRE(r.get_counters().done);
}
SECTION("moveable") {
static const auto coro = []() -> stream<std::unique_ptr<int>> {
co_yield std::make_unique<int>(42);
};
auto r = []() -> monitor_task {
auto s = coro();
auto item = co_await s;
REQUIRE(**item == 42);
}();
REQUIRE(r.get_counters().done);
}
SECTION("exception") {
static const auto coro = []() -> stream<int> {
throw std::runtime_error("test");
Expand Down
27 changes: 26 additions & 1 deletion test/test_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ TEMPLATE_TEST_CASE("Task: co_await ref", "[Task]", task<int&>, shared_task<int&>


TEMPLATE_TEST_CASE("Task: co_await void", "[Task]", task<void>, shared_task<void>) {
static int value = 42;
static const auto coro = []() -> TestType {
co_return;
};
Expand All @@ -161,6 +160,32 @@ TEMPLATE_TEST_CASE("Task: co_await void", "[Task]", task<void>, shared_task<void
}


TEST_CASE("Task: co_await moveable -- task", "[Task]") {
using TaskType = task<std::unique_ptr<int>>;
static const auto coro = [](std::unique_ptr<int> value) -> TaskType {
co_return value;
};
static const auto enclosing = [](std::unique_ptr<int> value) -> TaskType {
co_return co_await coro(std::move(value));
};
auto tsk = enclosing(std::make_unique<int>(42));
REQUIRE((*join(tsk)) == 42);
}


TEST_CASE("Task: co_await moveable -- shared_task", "[Task]") {
using TaskType = task<std::unique_ptr<int>>;
static const auto coro = [](std::unique_ptr<int> value) -> TaskType {
co_return value;
};
static const auto enclosing = [](std::unique_ptr<int> value) -> TaskType {
co_return std::move(co_await coro(std::move(value)));
};
auto tsk = enclosing(std::make_unique<int>(42));
REQUIRE((*join(tsk)) == 42);
}


TEMPLATE_TEST_CASE("Task: co_await exception", "[Task]", task<void>, shared_task<void>) {
static int value = 42;
static const auto coro = []() -> TestType {
Expand Down

0 comments on commit dca2c3e

Please sign in to comment.