Skip to content

Commit

Permalink
Add test for when_all throwing on individual tasks.
Browse files Browse the repository at this point in the history
The original behavior for coro::when_all was to capture the T result or
an exception and then when the user iterates all of the completed tasks
the task.return_value() would either return the result or re-throw the
exception for that specific task.
  • Loading branch information
jbaldwin committed Nov 3, 2024
1 parent ee02dc8 commit 7b71600
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/test_when_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,38 @@ TEST_CASE("when_all use std::ranges::view", "[when_all]")
auto result = coro::sync_wait(make_runner_task());
REQUIRE(result == (1 + 2 + 3));
}

TEST_CASE("when_all each task throws", "[when_all]")
{
coro::thread_pool tp{};

auto make_task = [&](uint64_t i) -> coro::task<uint64_t>
{
co_await tp.schedule();
if (i % 2 == 0)
{
throw std::runtime_error{std::to_string(i)};
}
co_return i;
};

std::vector<coro::task<uint64_t>> tasks;
for (auto i = 1; i <= 4; ++i)
{
tasks.emplace_back(make_task(i));
}

auto output_tasks = coro::sync_wait(coro::when_all(std::move(tasks)));
for (auto i = 1; i <= 4; ++i)
{
auto& task = output_tasks.at(i - 1);
if (i % 2 == 0)
{
REQUIRE_THROWS(task.return_value());
}
else
{
REQUIRE((int)task.return_value() == i);
}
}
}

0 comments on commit 7b71600

Please sign in to comment.