From df9f28a1a18fc84fc49ab969a72c4f2dd78685f6 Mon Sep 17 00:00:00 2001 From: Martin Chang Date: Sat, 3 Feb 2024 15:19:28 +0800 Subject: [PATCH] when_all can accept non-tasks --- lib/inc/drogon/utils/coroutine.h | 3 +-- lib/tests/unittests/CoroutineTest.cc | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/inc/drogon/utils/coroutine.h b/lib/inc/drogon/utils/coroutine.h index 1468eabf..484dc12f 100644 --- a/lib/inc/drogon/utils/coroutine.h +++ b/lib/inc/drogon/utils/coroutine.h @@ -928,7 +928,6 @@ inline Task<> when_all(std::vector tasks, trantor::EventLoop *loop = nullptr) { static_assert(is_awaitable_v); - static_assert(std::is_same_v, void>); std::exception_ptr eptr; std::atomic_size_t counter = tasks.size(); internal::WaitForNotify waiter; @@ -937,7 +936,7 @@ inline Task<> when_all(std::vector tasks, [](std::exception_ptr &eptr, std::atomic_size_t &counter, internal::WaitForNotify &waiter, - Task<> task) -> AsyncTask { + Awaiter task) -> AsyncTask { try { co_await task; diff --git a/lib/tests/unittests/CoroutineTest.cc b/lib/tests/unittests/CoroutineTest.cc index 6e20870f..1d75bb4f 100644 --- a/lib/tests/unittests/CoroutineTest.cc +++ b/lib/tests/unittests/CoroutineTest.cc @@ -281,4 +281,12 @@ DROGON_TEST(WhenAll) CHECK(results.size() == 2); CHECK(results[0] == 42); CHECK(results[1] == 42); + + // Check waiting on non-task works + auto sleep = sleepCoro(drogon::app().getLoop(), 0.001); + auto sleep2 = sleepCoro(drogon::app().getLoop(), 0.001); + std::vector tasks5; + tasks5.emplace_back(std::move(sleep)); + tasks5.emplace_back(std::move(sleep2)); + sync_wait(when_all(std::move(tasks5))); }