/* * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "tc_catch2.h" #include "Common.h" #include "Coro/AllOf.h" #include "Coro/AllocatorImpl.h" #include "Coro/Awaitable.h" #include "Coro/AwaitableValue.h" #include "Coro/CurrentCoroutine.h" #include "Coro/MultiThreadedExecutor.h" #include "Coro/Sleep.h" #include "Coro/Start.h" #include "Coro/SwitchExecutor.h" #include "Duration.h" #include "IoContext.h" #include class CoroutineFixture { public: Trinity::unique_trackable_ptr executor = Trinity::make_unique_trackable(); std::variant testCoroutineStatus = false; Trinity::Asio::IoContext timerContext; boost::asio::executor_work_guard timerWorkGuard = boost::asio::make_work_guard(timerContext); std::thread timerThread; CoroutineFixture() { Trinity::Coro::InitializeTimerExecutor(&timerContext); timerThread = std::thread([this] { timerContext.run(); }); } ~CoroutineFixture() noexcept(false) { bool timedOut = !executor->WaitForReadyCoroutinesAndResume(30s); timerWorkGuard.reset(); timerThread.join(); Trinity::Coro::ShutdownTimerExecutor(); if (timedOut) FAIL("executor->WaitForReadyCoroutinesAndResume(30s) timed out"); if (std::holds_alternative(testCoroutineStatus)) std::rethrow_exception(std::get(testCoroutineStatus)); if (!std::get(testCoroutineStatus)) FAIL("Test coroutine not finished"); } CoroutineFixture(CoroutineFixture const& other) = delete; CoroutineFixture(CoroutineFixture&& other) noexcept = delete; CoroutineFixture& operator=(CoroutineFixture const& other) = delete; CoroutineFixture& operator=(CoroutineFixture&& other) noexcept = delete; }; template TestFixture> struct std::coroutine_traits { struct promise_type : Trinity::Coro::CoroutineExecutionContext { promise_type(TestFixture& fixture) : CoroutineExecutionContext(std::coroutine_handle::from_promise(*this)), _testFixture(fixture) { CurrentExecutor = fixture.executor->weak_from_this(); CurrentAllocator = &_allocator; Trinity::Coro::Impl::CurrentAllocator::Set(CurrentAllocator); } ~promise_type() { Trinity::Coro::Impl::CurrentAllocator::Set(nullptr); } void get_return_object() noexcept { } std::suspend_never initial_suspend() noexcept { return { }; } std::suspend_never final_suspend() noexcept { return { }; } void return_void() noexcept { _testFixture.testCoroutineStatus = true; _testFixture.executor->StopWaiting(); } void unhandled_exception() noexcept { _testFixture.testCoroutineStatus = std::current_exception(); _testFixture.executor->StopWaiting(); } TestFixture& _testFixture; Trinity::Coro::Impl::Allocator _allocator; promise_type(promise_type const& other) = delete; promise_type(promise_type&& other) noexcept = delete; promise_type& operator=(promise_type const& other) = delete; promise_type& operator=(promise_type&& other) noexcept = delete; }; }; Trinity::Coro::Awaitable TestCoro1() { co_return 1; } TEST_CASE_METHOD(CoroutineFixture, "Coro::Allocator is available to nested coroutines", "[Coroutine][Allocator]") { // test failure is a segfault REQUIRE(co_await TestCoro1() == 1); } TEST_CASE_METHOD(CoroutineFixture, "Coro::AwaitableValue resumes coroutine after obtaining value", "[Coroutine][AwaitableValue]") { Trinity::Coro::AwaitableValue asyncValue; std::thread thrd([&asyncValue] { std::this_thread::sleep_for(0.5s); asyncValue.set_value(1); }); int32 result = co_await asyncValue; thrd.join(); REQUIRE(result == 1); } TEST_CASE_METHOD(CoroutineFixture, "Coro::AwaitableValue doesn't suspend coroutine when value is ready before co_await", "[Coroutine][AwaitableValue]") { Trinity::Coro::AwaitableValue asyncValue; asyncValue.set_value(1); int32 result = co_await asyncValue; REQUIRE(result == 1); } TEST_CASE_METHOD(CoroutineFixture, "Coro::CurrentExecutor returns current executor associated with coroutine", "[Coroutine][AwaitableValue]") { Trinity::unique_weak_ptr result = co_await Trinity::Coro::CurrentExecutor; REQUIRE(result.lock().get() == executor.get()); } TEST_CASE("Coro::Start starts a new coroutine", "[Coroutine][Start]") { Trinity::unique_trackable_ptr executor = Trinity::make_unique_trackable(); bool executed = false; Trinity::Coro::Start(*executor, [&executor, &executed]() -> Trinity::Coro::Awaitable { executed = true; executor->StopWaiting(); co_return; }); REQUIRE(executor->WaitForReadyCoroutinesAndResume(30s)); REQUIRE(executed); } TEST_CASE_METHOD(CoroutineFixture, "Coro::SwitchToExecutor changes executor associated with coroutine", "[Coroutine][SwitchToExecutor]") { Trinity::unique_trackable_ptr otherExecutor = Trinity::make_unique_trackable(); std::thread otherExecutorThread([&otherExecutor] { otherExecutor->WaitForReadyCoroutinesAndResume(30s); }); co_await Trinity::Coro::SwitchToExecutor(*otherExecutor); auto current = co_await Trinity::Coro::CurrentExecutor; REQUIRE(current.lock().get() == otherExecutor.get()); co_await Trinity::Coro::SwitchToExecutor(*executor); current = co_await Trinity::Coro::CurrentExecutor; REQUIRE(current.lock().get() == executor.get()); otherExecutor->StopWaiting(); otherExecutorThread.join(); } TEST_CASE_METHOD(CoroutineFixture, "Coro::SleepFor delays coroutine", "[Coroutine][Sleep]") { Seconds sleepDuration = 1s; TimePoint start = TimePoint::clock::now(); co_await Trinity::Coro::SleepFor(sleepDuration); TimePoint now = TimePoint::clock::now(); REQUIRE(now >= start + sleepDuration); } TEST_CASE_METHOD(CoroutineFixture, "Coro::SleepUntil delays coroutine", "[Coroutine][Sleep]") { Seconds sleepDuration = 1s; TimePoint expiry = TimePoint::clock::now() + sleepDuration; co_await Trinity::Coro::SleepUntil(expiry); TimePoint now = TimePoint::clock::now(); REQUIRE(now >= expiry); } TEST_CASE_METHOD(CoroutineFixture, "Coro::AllOf starts all awaitables at the same time and finishes when all are done", "[Coroutine][AllOf]") { bool startedFlags[3] = { }; auto testCoro = [&](std::size_t flagIndex, std::type_identity) -> Trinity::Coro::Awaitable { startedFlags[flagIndex] = true; co_await Trinity::Coro::SleepFor(100ms); REQUIRE(advstd::ranges::contains(startedFlags, false) == false); if constexpr (std::is_same_v) co_return; else co_return static_cast(flagIndex); }; Trinity::Coro::Awaitable coro1 = testCoro(0, std::type_identity()); Trinity::Coro::Awaitable coro2 = testCoro(1, std::type_identity()); Trinity::Coro::Awaitable coro3 = testCoro(2, std::type_identity()); auto allOf = co_await Trinity::Coro::AllOf(coro1, std::move(coro2), coro3); using AllResultOfTuple = decltype(allOf); STATIC_REQUIRE(std::tuple_size_v == 2); STATIC_REQUIRE(std::is_lvalue_reference_v>); STATIC_REQUIRE(std::is_reference_v> == false); REQUIRE(std::get<0>(allOf) == 0); REQUIRE(std::get<1>(allOf) == 1); } TEST_CASE_METHOD(CoroutineFixture, "Coro::AllOf rethrows exception from one of its nested awaitables", "[Coroutine][AllOf]") { bool startedFlags[3] = { }; auto testCoro = [&](std::size_t flagIndex, std::type_identity, bool throws = false) -> Trinity::Coro::Awaitable { startedFlags[flagIndex] = true; co_await Trinity::Coro::SleepFor(100ms); REQUIRE(advstd::ranges::contains(startedFlags, false) == false); if (throws) throw std::runtime_error("Controlled failure"); if constexpr (std::is_same_v) co_return; else co_return flagIndex; }; Trinity::Coro::Awaitable coro1 = testCoro(0, std::type_identity()); Trinity::Coro::Awaitable coro2 = testCoro(1, std::type_identity()); Trinity::Coro::Awaitable coro3 = testCoro(2, std::type_identity(), true); REQUIRE_THROWS_WITH(co_await Trinity::Coro::AllOf(coro1, std::move(coro2), coro3), Catch::Matchers::ContainsSubstring("Controlled failure")); }