#define BOOST_TEST_DYN_LINK #include "core/sophiar_obj.hpp" #include "utility/coro_unique_lock.hpp" #include "utility/debug_utility.hpp" #include #include #include #include #include #include #include #include using boost::asio::awaitable; using boost::asio::co_spawn; using boost::asio::detached; using boost::asio::use_awaitable; using namespace sophiar; using namespace std::chrono_literals; coro_unique_lock *lock; awaitable unlocked_worker(int id) { DEBUG_PRINT(fmt::format("Worker {} starts to work.", id)); co_await coro_sleep(1s); DEBUG_PRINT(fmt::format("Worker {} finished work.", id)); } awaitable locked_worker(int id) { auto lock_guard = coro_lock_guard{*lock}; co_await lock_guard.coro_require_lock(); DEBUG_PRINT(fmt::format("Worker {} starts to work.", id)); co_await coro_sleep(1s); DEBUG_PRINT(fmt::format("Worker {} finished work.", id)); } BOOST_AUTO_TEST_CASE(test_coro_unique_lock) { initialize({}); lock = new coro_unique_lock{}; for (int i = 0; i < 5; ++i) { co_spawn(*global_context, locked_worker(i), detached); } global_context->run(); }