Skip to content

Commit

Permalink
test: add enum for test status and type, make each test a variable, a…
Browse files Browse the repository at this point in the history
…dd coro tests
  • Loading branch information
Mishura4 committed Aug 15, 2023
1 parent 28a859b commit af392b8
Show file tree
Hide file tree
Showing 13 changed files with 1,362 additions and 753 deletions.
2 changes: 1 addition & 1 deletion buildtools/classes/Generator/CoroGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function generateCppDef(string $returnType, string $currentFunction, stri
{
if (substr($parameterNames, 0, 2) === ", ")
$parameterNames = substr($parameterNames, 2);
return "awaitable<confirmation_callback_t> cluster::co_${currentFunction}($noDefaults) {\n\treturn [=, this] (auto &&cc) { this->$currentFunction($parameterNames" . (empty($parameterNames) ? "": ", ") . "cc); };\n}\n\n";
return "awaitable<confirmation_callback_t> cluster::co_${currentFunction}($noDefaults) {\n\treturn awaitable{ [=, this] (auto &&cc) { this->$currentFunction($parameterNames" . (empty($parameterNames) ? "": ", ") . "cc); } };\n}\n\n";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class DPP_EXPORT cluster {
* @param seconds How long to wait for
* @return awaitable<timer> Object that can be co_await-ed to suspend the function for a certain time
*/
awaitable<timer> co_sleep(uint64_t seconds);
[[nodiscard]] awaitable<timer> co_sleep(uint64_t seconds);
#endif

/**
Expand Down
4 changes: 2 additions & 2 deletions include/dpp/coro/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class async {
#ifndef _DOXYGEN_
requires std::invocable<Fun, Obj, Args..., std::function<void(R)>>
#endif
async(Obj &&obj, Fun &&fun, Args&&... args) : api_callback{} {
explicit async(Obj &&obj, Fun &&fun, Args&&... args) : api_callback{} {
std::invoke(std::forward<Fun>(fun), std::forward<Obj>(obj), std::forward<Args>(args)..., api_callback);
}

Expand All @@ -230,7 +230,7 @@ class async {
#ifndef _DOXYGEN_
requires std::invocable<Fun, Args..., std::function<void(R)>>
#endif
async(Fun &&fun, Args&&... args) : api_callback{} {
explicit async(Fun &&fun, Args&&... args) : api_callback{} {
std::invoke(std::forward<Fun>(fun), std::forward<Args>(args)..., api_callback);
}

Expand Down
6 changes: 3 additions & 3 deletions include/dpp/coro/awaitable.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct awaitable {
*
* @warning This callback is to be executed <b>later</b>, on co_await. <a href="/lambdas-and-locals.html">Be mindful of reference captures</a>.
*/
awaitable(std::invocable<std::function<void(R)>> auto &&fun) : request{fun} {}
explicit awaitable(std::invocable<std::function<void(R)>> auto &&fun) : request{fun} {}

/**
* @brief Copy constructor.
Expand Down Expand Up @@ -112,8 +112,8 @@ struct awaitable {
*/
template <typename T>
void await_suspend(detail::std_coroutine::coroutine_handle<T> caller) noexcept(noexcept(std::invoke(fun, std::declval<std::function<void(R)>&&>()))) {
std::invoke(fun, [this, caller](auto &&api_result) {
result = api_result;
std::invoke(fun, [this, caller] <typename R_> (R_&& api_result) mutable {
result = std::forward<R_>(api_result);
caller.resume();
});
}
Expand Down
24 changes: 11 additions & 13 deletions include/dpp/coro/job.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@ struct job {};

namespace detail {

template <typename... Args>
inline constexpr bool coroutine_has_ref_params_v = (std::is_reference_v<Args> || ... || false);

template <typename T, typename... Args>
inline constexpr bool coroutine_has_ref_params_v<T, Args...> = (std::is_reference_v<Args> || ... || (std::is_reference_v<T> && !std::is_invocable_v<T, Args...>));

#ifdef DPP_CORO_TEST
struct job_promise_base{};
#endif

/**
* @brief Coroutine promise type for a job
*/
template <bool has_reference_params>
template <typename... Args>
struct job_promise {

#ifdef DPP_CORO_TEST
Expand Down Expand Up @@ -118,7 +124,7 @@ struct job_promise {
* If you must pass a reference, pass it as a pointer or with std::ref, but you must fully understand the reason behind this warning, and what to avoid.
* If you prefer a safer type, use `coroutine` for synchronous execution, or `task` for parallel tasks, and co_await them.
*/
static_assert(!has_reference_params, "co_await is disabled in dpp::job when taking parameters by reference. read comment above this line for more info");
static_assert(!coroutine_has_ref_params_v<Args...>, "co_await is disabled in dpp::job when taking parameters by reference. read comment above this line for more info");

return std::forward<T>(expr);
}
Expand All @@ -128,26 +134,18 @@ struct job_promise {

} // namespace dpp

template <>
struct dpp::detail::std_coroutine::coroutine_traits<dpp::job> {
/**
* @brief Promise type for this coroutine signature.
*/
using promise_type = dpp::detail::job_promise<false>;
};

/**
* @brief Specialization of std::coroutine_traits, helps the standard library figure out a promise type from a coroutine function.
*/
template<typename T, typename... Args>
struct dpp::detail::std_coroutine::coroutine_traits<dpp::job, T, Args...> {
template<typename... Args>
struct dpp::detail::std_coroutine::coroutine_traits<dpp::job, Args...> {
/**
* @brief Promise type for this coroutine signature.
*
* When the coroutine is created from a lambda, that lambda is passed as a first parameter.
* Not ideal but we'll allow any callable that takes the rest of the arguments passed
*/
using promise_type = dpp::detail::job_promise<(std::is_reference_v<Args> || ... || (std::is_reference_v<T> && !std::is_invocable_v<T, Args...>))>;
using promise_type = dpp::detail::job_promise<Args...>;
};

#endif /* DPP_CORO */
4 changes: 2 additions & 2 deletions include/dpp/coro/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ struct task_promise_base {
* Emulates the default behavior and sets is_sync to false if the awaited object is not ready.
*/
template <typename T>
T await_transform(T&& expr) {
decltype(auto) await_transform(T&& expr) {
if constexpr (requires { expr.operator co_await(); }) {
auto awaiter = expr.operator co_await();
if (!awaiter.await_ready())
Expand All @@ -277,7 +277,7 @@ struct task_promise_base {
else {
if (!expr.await_ready())
is_sync = false;
return (expr);
return static_cast<T&&>(expr);
}
}

Expand Down
49 changes: 28 additions & 21 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,27 +271,6 @@ target_compile_features(dpp PRIVATE cxx_thread_local)
target_compile_features(dpp PRIVATE cxx_variadic_templates)
target_compile_features(dpp PRIVATE cxx_attribute_deprecated)

if (DPP_BUILD_TEST)
enable_testing(${CMAKE_CURRENT_SOURCE_DIR}/..)
file(GLOB testnamelist "${CMAKE_CURRENT_SOURCE_DIR}/../src/*")
foreach (fulltestname ${testnamelist})
get_filename_component(testname ${fulltestname} NAME)
if (NOT "${testname}" STREQUAL "dpp")
message("-- Configuring test: ${Green}${testname}${ColourReset} with source: ${modules_dir}/${testname}/*.cpp")
set (testsrc "")
file(GLOB testsrc "${modules_dir}/${testname}/*.cpp")
add_executable(${testname} ${testsrc})
target_compile_features(${testname} PRIVATE cxx_std_17)
target_link_libraries(${testname} PUBLIC ${modname})
endif()
endforeach()
add_test(
NAME unittests
COMMAND library/unittest
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/library
)
endif()

if(HAVE_PRCTL)
target_compile_definitions(dpp PRIVATE HAVE_PRCTL)
endif()
Expand Down Expand Up @@ -351,6 +330,34 @@ if(DPP_CORO)
execute_process(WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.."
COMMAND php buildtools/make_struct.php "\\Dpp\\Generator\\CoroGenerator")
endif()

if (DPP_BUILD_TEST)
enable_testing(${CMAKE_CURRENT_SOURCE_DIR}/..)
file(GLOB testnamelist "${CMAKE_CURRENT_SOURCE_DIR}/../src/*")
foreach (fulltestname ${testnamelist})
get_filename_component(testname ${fulltestname} NAME)
if (NOT "${testname}" STREQUAL "dpp")
message("-- Configuring test: ${Green}${testname}${ColourReset} with source: ${modules_dir}/${testname}/*.cpp")
set (testsrc "")
file(GLOB testsrc "${modules_dir}/${testname}/*.cpp")
add_executable(${testname} ${testsrc})
if (DPP_CORO)
target_compile_features(${testname} PRIVATE cxx_std_20)
else()
target_compile_features(${testname} PRIVATE cxx_std_17)
endif()
if (MSVC)
target_compile_options(${testname} PRIVATE /utf-8)
endif()
target_link_libraries(${testname} PUBLIC ${modname})
endif()
endforeach()
add_test(
NAME unittests
COMMAND library/unittest
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/library
)
endif()

if(WIN32 AND NOT MINGW)
if (NOT WINDOWS_32_BIT)
Expand Down
4 changes: 2 additions & 2 deletions src/dpp/cluster/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ void cluster::tick_timers() {

#ifdef DPP_CORO
awaitable<timer> cluster::co_sleep(uint64_t seconds) {
return {[this, seconds] (auto &&cb) {
start_timer([this, cb](dpp::timer handle) {
return awaitable<timer>{[this, seconds] (auto &&cb) mutable {
start_timer([this, cb] (dpp::timer handle) {
cb(handle);
stop_timer(handle);
}, seconds);
Expand Down
Loading

0 comments on commit af392b8

Please sign in to comment.