#define BOOST_TEST_DYN_LINK #define BOOST_TEST_MAIN // in only one cpp file #include "core/sophiar_obj.hpp" #include "utility/coro_signal2.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_signal2 *sig_a, *sig_b; awaitable coro_a() { auto watcher_b = sig_b->new_watcher(); FILE_LINE_TRACE for (int i = 0; i < 1000; ++i) { sig_a->try_notify_all(); co_await watcher_b.coro_wait(); } FILE_LINE_TRACE sig_a->try_notify_all(); co_return; } awaitable coro_b() { co_await coro_sleep(10ms); auto watcher_a = sig_a->new_watcher(); FILE_LINE_TRACE for (int i = 0; i < 1000; ++i) { sig_b->try_notify_all(); co_await watcher_a.coro_wait(); } FILE_LINE_TRACE sig_b->try_notify_all(); co_return; } awaitable coro_signal2_bench() { constexpr auto length = 100000; std::vector> sig_pool; std::vector watcher_pool; for (int i = 0; i < length; ++i) { auto new_sig = std::make_unique(); watcher_pool.push_back(new_sig->new_watcher()); sig_pool.push_back(std::move(new_sig)); } for (int i = 0; i < length - 1; ++i) { co_spawn(*global_context, [&]() -> awaitable { int index = i; co_await watcher_pool[index].coro_wait(); sig_pool[index + 1]->try_notify_all(); }, detached); } co_await coro_sleep(10ms); FILE_LINE_TRACE sig_pool[0]->try_notify_all(); co_await watcher_pool[length - 1].coro_wait(); FILE_LINE_TRACE co_return; } BOOST_AUTO_TEST_CASE(test_coro_signal2) { initialize({}); sig_a = new coro_signal2{}; sig_b = new coro_signal2{}; // co_spawn(*global_context, coro_a(), detached); // co_spawn(*global_context, coro_b(), detached); co_spawn(*global_context, coro_signal2_bench(), detached); global_context->run(); }